TS-Scribe - v0.6.2
    Preparing search index...

    Function maskObject

    • Masks sensitive properties in an object or array recursively. Traverses deeply nested structures and masks properties based on keys, predicates, or custom logic.

      This function creates a deep copy of the input object/array while masking sensitive values. It handles circular references, special object types (Date, RegExp, Error, etc.), and deeply nested structures. Non-sensitive values are preserved as-is.

      Type Parameters

      • T extends any[] | Record<string, any>

        The type of the object or array being masked.

      Parameters

      • object: T

        The object or array to mask.

      • Optionaloptions: MaskObjectOptions

        Configuration options for masking behavior.

      Returns T extends readonly any[] ? readonly any[] : T

      A new object with masked properties (returns original if no masking needed).

      // Mask by key list
      const user = {
      name: 'John',
      email: 'john@example.com',
      ssn: '123-45-6789'
      };
      const masked = maskObject(user, { keys: ['ssn', 'email'] });
      console.log(masked);
      // Output: {
      // name: 'John',
      // email: 'j***@example.com',
      // ssn: '***'
      // }
      // Mask with predicate function
      const userData = {
      username: 'john_doe',
      password: 'secret123',
      apiToken: 'sk_live_xyz'
      };
      const masked = maskObject(userData, {
      isSensitive: (key, value, path) =>
      key.toLowerCase().includes('password') ||
      key.toLowerCase().includes('token')
      });
      console.log(masked);
      // Output: {
      // username: 'john_doe',
      // password: '*****',
      // apiToken: '******'
      // }
      // Skip descending into specific objects (e.g., GeoJSON features)
      const data = {
      apiKey: 'secret123',
      geometry: {
      type: 'Feature',
      coordinates: [10, 20],
      properties: { secret: 'should-not-mask' }
      }
      };
      const masked = maskObject(data, {
      keys: ['apiKey'],
      shouldSkip: (value) => value?.type === 'Feature'
      });
      console.log(masked);
      // Output: apiKey is masked, but geometry object is preserved as-is
      // Mask entire GeoJSON geometries without descending
      const isGeoJSON = (v: any) =>
      v?.type && v?.coordinates &&
      ['Point', 'LineString', 'Polygon'].includes(v.type);

      const geoData = {
      name: 'Location',
      geometry: {
      type: 'Point',
      coordinates: [125.6, 10.1]
      }
      };
      const masked = maskObject(geoData, {
      isSensitive: (key, value) => key === 'geometry' && isGeoJSON(value)
      });
      console.log(masked);
      // Output: { name: 'Location', geometry: '***' }
      // Custom masking function
      const data = {
      ssn: '123-45-6789',
      email: 'john@example.com'
      };
      const masked = maskObject(data, {
      keys: ['ssn', 'email'],
      maskFn: (value, key) => {
      if (key === 'ssn') return '***-**-' + String(value).slice(-4);
      if (key === 'email') return String(value).replace(/(.{1}).*@/, '$1***@');
      return '***';
      }
      });
      console.log(masked);
      // Output: { ssn: '***-**-6789', email: 'j***@example.com' }

      Performance Considerations:

      • Uses WeakMap for circular reference tracking (O(1) lookups)
      • For large objects marked as sensitive, serialization is used for masking
      • Consider using shouldSkip for known large structures that don't need masking
      • Special object types (Date, RegExp, Error, Map, Set, etc.) are preserved without traversal

      Type Preservation:

      • Date, RegExp, Error, Map, Set, WeakMap, WeakSet, and TypedArrays are preserved as-is
      • Functions are preserved without modification
      • Circular references are maintained in the result structure

      Depth Control:

      • Default maxDepth is 100 to prevent stack overflow on deeply nested objects
      • Beyond maxDepth, objects are returned unchanged without further processing