@hackylabs/deep-redact 3.0.5 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/README.md +221 -106
  2. package/dist/adapters/console/index.cjs +74 -0
  3. package/dist/adapters/console/index.d.cts +22 -0
  4. package/dist/adapters/console/index.d.ts +22 -0
  5. package/dist/adapters/console/index.js +73 -0
  6. package/dist/index.cjs +2743 -0
  7. package/dist/index.d.cts +7 -0
  8. package/dist/index.d.ts +7 -0
  9. package/dist/index.js +2741 -55
  10. package/dist/node-console-sink-BnRUkAAr.cjs +19 -0
  11. package/dist/node-console-sink-DaQleNZ8.js +14 -0
  12. package/dist/public-Da0aARA9.d.cts +127 -0
  13. package/dist/public-Dw4ycNzO.d.ts +127 -0
  14. package/package.json +66 -106
  15. package/dist/index.mjs +0 -40
  16. package/dist/types/index.d.ts +0 -29
  17. package/dist/types/types.d.ts +0 -224
  18. package/dist/types/utils/TransformerRegistry.d.ts +0 -52
  19. package/dist/types/utils/index.d.ts +0 -131
  20. package/dist/types/utils/standardTransformers/bigint.d.ts +0 -2
  21. package/dist/types/utils/standardTransformers/date.d.ts +0 -2
  22. package/dist/types/utils/standardTransformers/error.d.ts +0 -2
  23. package/dist/types/utils/standardTransformers/index.d.ts +0 -9
  24. package/dist/types/utils/standardTransformers/map.d.ts +0 -2
  25. package/dist/types/utils/standardTransformers/regex.d.ts +0 -2
  26. package/dist/types/utils/standardTransformers/set.d.ts +0 -2
  27. package/dist/types/utils/standardTransformers/url.d.ts +0 -2
  28. package/dist/types.js +0 -2
  29. package/dist/types.mjs +0 -1
  30. package/dist/utils/TransformerRegistry.js +0 -100
  31. package/dist/utils/TransformerRegistry.mjs +0 -94
  32. package/dist/utils/index.js +0 -471
  33. package/dist/utils/index.mjs +0 -467
  34. package/dist/utils/standardTransformers/bigint.js +0 -10
  35. package/dist/utils/standardTransformers/bigint.mjs +0 -6
  36. package/dist/utils/standardTransformers/date.js +0 -9
  37. package/dist/utils/standardTransformers/date.mjs +0 -5
  38. package/dist/utils/standardTransformers/error.js +0 -16
  39. package/dist/utils/standardTransformers/error.mjs +0 -12
  40. package/dist/utils/standardTransformers/index.js +0 -38
  41. package/dist/utils/standardTransformers/index.mjs +0 -35
  42. package/dist/utils/standardTransformers/map.js +0 -9
  43. package/dist/utils/standardTransformers/map.mjs +0 -5
  44. package/dist/utils/standardTransformers/regex.js +0 -15
  45. package/dist/utils/standardTransformers/regex.mjs +0 -11
  46. package/dist/utils/standardTransformers/set.js +0 -9
  47. package/dist/utils/standardTransformers/set.mjs +0 -5
  48. package/dist/utils/standardTransformers/url.js +0 -9
  49. package/dist/utils/standardTransformers/url.mjs +0 -5
@@ -1,224 +0,0 @@
1
- export type Types = 'string' | 'number' | 'bigint' | 'boolean' | 'object' | 'function' | 'symbol' | 'undefined';
2
- export type Transformer = (value: unknown, key?: string, reference?: WeakMap<object, unknown>) => unknown;
3
- /**
4
- * Configuration for organised transformers by type and constructor
5
- */
6
- export interface OrganisedTransformers {
7
- /**
8
- * Transformers for primitive types (based on typeof result)
9
- */
10
- byType?: {
11
- bigint?: Transformer[];
12
- string?: Transformer[];
13
- number?: Transformer[];
14
- boolean?: Transformer[];
15
- symbol?: Transformer[];
16
- function?: Transformer[];
17
- object?: Transformer[];
18
- undefined?: Transformer[];
19
- };
20
- /**
21
- * Transformers for specific constructors (based on instanceof checks)
22
- */
23
- byConstructor?: {
24
- Date?: Transformer[];
25
- Error?: Transformer[];
26
- Map?: Transformer[];
27
- Set?: Transformer[];
28
- RegExp?: Transformer[];
29
- URL?: Transformer[];
30
- [key: string]: Transformer[] | undefined;
31
- };
32
- /**
33
- * Transformers that run on all values (like the current system)
34
- */
35
- fallback?: Transformer[];
36
- }
37
- /**
38
- * Transformer configuration - supports both old array format and new organised format
39
- */
40
- export type TransformerConfig = Transformer[] | OrganisedTransformers;
41
- export interface BlacklistKeyConfig {
42
- /**
43
- * Perform a fuzzy match on the key. This will match any key that contains the string, rather than a case-sensitive match.
44
- * @default false
45
- * @example true // match any key that contains the string 'address', such as 'homeAddress', 'workAddress', 'addressLine1', etc.
46
- * @example false // match only keys that contain 'address' from start to end.
47
- */
48
- fuzzyKeyMatch?: boolean;
49
- /**
50
- * Perform a case-sensitive match on the key
51
- * @default true
52
- * @example false // match any key that contains the string 'address' regardless of upper, lower, snake, camel or any other case.
53
- * @example true // match only keys that are exactly 'address' in the same case.
54
- */
55
- caseSensitiveKeyMatch?: boolean;
56
- /**
57
- * Retain the structure of the object, but redact the values.
58
- * @default false
59
- * @example true // retain the structure of the object, but redact the values. { a: '1' } => becomes { a: '[REDACTED]' }
60
- * @example false // redact the entire object. { a: '1' } => becomes '[REDACTED]'
61
- */
62
- retainStructure?: boolean;
63
- /**
64
- * Remove the redacted data instead of replacing it with the `replacement` value.
65
- * @default false // replace the redacted data with the `replacement` value.
66
- * @example true // remove the redacted data.
67
- */
68
- remove?: boolean;
69
- /**
70
- * The replacement value for redacted data. Can be a string, or a function that takes the original value and returns any value.
71
- * @default '[REDACTED]'
72
- * @example '*' // if `replacement` equals `*` then `joe.bloggs@example.com` becomes `**********************`
73
- * @example (value) => `REDACTED: ${typeof value}` // redact the value with a prefix of 'REDACTED: ' and the type of the value.
74
- * @example (value) => return typeof value === 'string' ? '*'.repeat(value.length) : '[REDACTED]' // redact the value with a string of the same length.
75
- * @param value The original value that is being redacted.
76
- * @returns The redacted value or undefined to remove the value.
77
- */
78
- replacement?: string | ((value: unknown) => unknown);
79
- /**
80
- * Replace string values with a redacted string of the same length, using the `replacement` option. Ignored if `remove` is true, `replacement` is a function, or the value is not a string.
81
- * @default false
82
- * @example true // if `replacement` equals `*` then `joe.bloggs@example.com` becomes `**********************`
83
- * @example false // if `replacement` equals `*` then `joe.bloggs@example.com` becomes `*`
84
- */
85
- replaceStringByLength?: boolean;
86
- /**
87
- * The key to redact. Can be a string or a RegExp.
88
- * @example 'address' // redact any key that is 'address'.
89
- * @example /^address$/ // redact any key that is exactly 'address'.
90
- */
91
- key: string | RegExp;
92
- }
93
- export interface ComplexStringTest {
94
- pattern: RegExp;
95
- replacer: (value: string, pattern: RegExp) => string;
96
- }
97
- export interface BaseDeepRedactConfig {
98
- /**
99
- * Keys that should be redacted. Can be a string, or an object with additional configuration options.
100
- * @default []
101
- * @example ['password', 'ssn'] // redact any key that is 'password' or 'ssn'.
102
- * @example [{ key: 'address', fuzzyKeyMatch: true, caseSensitiveKeyMatch: false }] // redact any key that contains 'address' regardless of case.
103
- */
104
- blacklistedKeys?: Array<string | RegExp | BlacklistKeyConfig>;
105
- /**
106
- * Redact a string value that matches a test pattern.
107
- * @default []
108
- * @example [
109
- * /^[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}$/, // redact any string that looks like an IP address.
110
- * ]
111
- */
112
- stringTests?: Array<RegExp | ComplexStringTest>;
113
- /**
114
- * Perform a fuzzy match on the key. This will match any key that contains the string, rather than a case-sensitive match.
115
- * @default false
116
- * @example true // match any key that contains the string 'address', such as 'homeAddress', 'workAddress', 'addressLine1', etc.
117
- * @example false // match only keys that contain 'address' from start to end.
118
- */
119
- fuzzyKeyMatch?: boolean;
120
- /**
121
- * Perform a case-sensitive match on the key
122
- * @default true
123
- * @example false // match any key that contains the string 'address' regardless of upper, lower, snake, camel or any other case.
124
- * @example true // match only keys that are exactly 'address' in the same case.
125
- */
126
- caseSensitiveKeyMatch?: boolean;
127
- /**
128
- * Retain the structure of the object, but redact the values.
129
- * @default false
130
- * @example true // retain the structure of the object, but redact the values. { a: '1' } => becomes { a: '[REDACTED]' }
131
- * @example false // redact the entire object. { a: '1' } => becomes '[REDACTED]'
132
- */
133
- retainStructure?: boolean;
134
- /**
135
- * Replace string values with a redacted string of the same length, using the `replacement` option. Ignored if `remove` is true, `replacement` is a function, or the value is not a string.
136
- * @default false
137
- * @example true // if `replacement` equals `*` then `joe.bloggs@example.com` becomes `**********************`
138
- * @example false // if `replacement` equals `*` then `joe.bloggs@example.com` becomes `*`
139
- */
140
- replaceStringByLength?: boolean;
141
- /**
142
- * The replacement value for redacted data. Can be a string, or a function that takes the original value and returns any value.
143
- * @default '[REDACTED]'
144
- * @example (value) => `REDACTED: ${typeof value}` // redact the value with a prefix of 'REDACTED: ' and the type of the value.
145
- * @example (value) => return typeof value === 'string' ? '*'.repeat(value.length) : '[REDACTED]' // redact the value with a string of the same length.
146
- * @param value The original value that is being redacted.
147
- * @returns The redacted value or undefined to remove the value.
148
- */
149
- replacement?: string | Transformer;
150
- /**
151
- * Remove the redacted data instead of replacing it with the `replacement` value.
152
- */
153
- remove?: boolean;
154
- /**
155
- * The types of values that should be redacted. If the value is not one of these types, it will not be redacted.
156
- * @default ['string']
157
- * @example ['string', 'number'] // redact only strings and numbers, leave other types unchanged.
158
- */
159
- types?: Types[];
160
- /**
161
- * Serialise the redacted data. If true, the redacted data will be returned as a JSON string. If false, it will be returned as an object.
162
- * @default true
163
- * @example true // return the redacted data as a JSON string.
164
- * @example false // return the redacted data as the same type as the original data.
165
- */
166
- serialise?: boolean;
167
- /**
168
- * Alias of `serialise` for International-English users.
169
- */
170
- serialize?: boolean;
171
- /**
172
- * Configuration for transformers to apply when transforming unsupported values.
173
- * Supports both legacy array format and new organised format for better performance.
174
- *
175
- * Legacy format: Array of transformers that run on all values in order
176
- * New format: Object with transformers organised by type and constructor
177
- *
178
- * @default []
179
- * @example
180
- * // Legacy format (still supported)
181
- * [
182
- * (value: unknown) => {
183
- * if (typeof value !== 'bigint') return value
184
- * return value.toString(10)
185
- * },
186
- * (value: unknown) => {
187
- * if (!(value instanceof Date)) return value
188
- * return value.toISOString()
189
- * }
190
- * ]
191
- *
192
- * @example
193
- * {
194
- * byType: {
195
- * bigint: [(value: unknown) => (value as bigint).toString(10)]
196
- * },
197
- * byConstructor: {
198
- * Date: [(value: unknown) => (value as Date).toISOString()]
199
- * },
200
- * fallback: [
201
- * // transformers that run on all values
202
- * ]
203
- * }
204
- */
205
- transformers?: TransformerConfig;
206
- }
207
- export type DeepRedactConfig = Partial<Omit<BaseDeepRedactConfig, '_blacklistedKeysTransformed' | 'blacklistedKeys' | 'stringTests'>> & ({
208
- blacklistedKeys: BaseDeepRedactConfig['blacklistedKeys'];
209
- stringTests: BaseDeepRedactConfig['stringTests'];
210
- } | {
211
- blacklistedKeys: BaseDeepRedactConfig['blacklistedKeys'];
212
- } | {
213
- stringTests: BaseDeepRedactConfig['stringTests'];
214
- });
215
- export type RedactorUtilsConfig = Omit<BaseDeepRedactConfig, 'serialise' | 'serialize'>;
216
- export type StackReference = WeakMap<object, unknown>;
217
- export type Stack = Array<{
218
- parent: any;
219
- key: string;
220
- value: unknown;
221
- path: Array<string | number>;
222
- redactingParent: boolean;
223
- keyConfig: BlacklistKeyConfig | undefined;
224
- }>;
@@ -1,52 +0,0 @@
1
- import type { Transformer } from '../types';
2
- /**
3
- * Registry for organizing transformers by type and constructor for efficient lookup
4
- */
5
- export declare class TransformerRegistry {
6
- private typeTransformers;
7
- private constructorTransformers;
8
- private fallbackTransformers;
9
- /**
10
- * Add a transformer for a specific typeof result
11
- * @param type - The typeof result (e.g., 'bigint', 'string', etc.)
12
- * @param transformer - The transformer function
13
- */
14
- addTypeTransformer(type: string, transformer: Transformer): void;
15
- /**
16
- * Add a transformer for a specific constructor
17
- * @param constructor - The constructor function (e.g., Date, Error, etc.)
18
- * @param transformer - The transformer function
19
- */
20
- addConstructorTransformer(constructor: Function, transformer: Transformer): void;
21
- /**
22
- * Add a fallback transformer that runs on all values
23
- * @param transformer - The transformer function
24
- */
25
- addFallbackTransformer(transformer: Transformer): void;
26
- /**
27
- * Get relevant transformers for a value
28
- * @param value - The value to get transformers for
29
- * @returns Array of relevant transformers
30
- */
31
- getTransformersForValue(value: unknown): Transformer[];
32
- /**
33
- * Apply transformers to a value
34
- * @param value - The value to transform
35
- * @param key - The key (optional)
36
- * @param referenceMap - Reference map for circular references (optional)
37
- * @returns The transformed value
38
- */
39
- applyTransformers(value: unknown, key?: string, referenceMap?: WeakMap<object, string>): unknown;
40
- /**
41
- * Clear all transformers
42
- */
43
- clear(): void;
44
- /**
45
- * Get all registered transformers for debugging
46
- */
47
- getRegisteredTransformers(): {
48
- types: Map<string, Transformer[]>;
49
- constructors: Map<Function, Transformer[]>;
50
- fallback: Transformer[];
51
- };
52
- }
@@ -1,131 +0,0 @@
1
- import type { RedactorUtilsConfig } from '../types.js';
2
- declare class RedactorUtils {
3
- /**
4
- * The configuration for the redaction.
5
- * @private
6
- */
7
- private readonly config;
8
- /**
9
- * The transformed blacklist keys of flat regex patterns and complex config objects
10
- * @private
11
- */
12
- private readonly blacklistedKeysTransformed;
13
- /**
14
- * The transformer registry for efficient transformer lookup
15
- * @private
16
- */
17
- private readonly transformerRegistry;
18
- constructor(customConfig: RedactorUtilsConfig);
19
- /**
20
- * Sets up the transformer registry based on the configuration
21
- * @param transformers - The transformer configuration
22
- * @private
23
- */
24
- private setupTransformerRegistry;
25
- private createTransformedBlacklistedKey;
26
- /**
27
- * Applies transformers to a value
28
- * @param value - The value to transform
29
- * @param key - The key to check
30
- * @returns The transformed value
31
- * @private
32
- */
33
- private applyTransformers;
34
- /**
35
- * Checks if a key should be redacted
36
- * @param key - The key to check
37
- * @returns Whether the key should be redacted
38
- * @private
39
- */
40
- private shouldRedactKey;
41
- /**
42
- * Checks if a value should be redacted
43
- * @param value - The value to check
44
- * @param key - The key to check
45
- * @returns Whether the value should be redacted
46
- * @private
47
- */
48
- private shouldRedactValue;
49
- /**
50
- * Redacts a value based on the key-specific config
51
- * @param value - The value to redact
52
- * @param key - The key to check
53
- * @param redactingParent - Whether the parent is being redacted
54
- * @returns The redacted value
55
- * @private
56
- */
57
- private redactValue;
58
- /**
59
- * Applies string transformations
60
- * @param value - The value to transform
61
- * @param key - The key to check
62
- * @returns The transformed value
63
- * @private
64
- */
65
- private applyStringTransformations;
66
- /**
67
- * Handles primitive values
68
- * @param value - The value to handle
69
- * @param key - The key to check
70
- * @param redactingParent - Whether the parent is being redacted
71
- * @param keyConfig - The key config
72
- * @returns The transformed value
73
- * @private
74
- */
75
- private handlePrimitiveValue;
76
- /**
77
- * Handles object values
78
- * @param value - The value to handle
79
- * @param key - The key to check
80
- * @param path - The path to the value
81
- * @param redactingParent - Whether the parent is being redacted
82
- * @param referenceMap - The reference map
83
- * @returns The transformed value and stack
84
- * @private
85
- */
86
- private handleObjectValue;
87
- /**
88
- * Handles object values
89
- * @param value - The value to handle
90
- * @param path - The path to the value
91
- * @param redactingParent - Whether the parent is being redacted
92
- * @returns The transformed value and stack
93
- * @private
94
- */
95
- private handleRetainStructure;
96
- /**
97
- * Finds the matching key config
98
- * @param key - The key to find
99
- * @returns The matching key config
100
- * @private
101
- */
102
- private findMatchingKeyConfig;
103
- /**
104
- * Initialises the traversal
105
- * @param raw - The raw value to traverse
106
- * @returns The output and stack
107
- * @private
108
- */
109
- private initialiseTraversal;
110
- /**
111
- * Pre-processes the input to replace circular references with transformer objects
112
- * @param raw - The raw value to process
113
- * @returns The processed value with circular references replaced
114
- * @private
115
- */
116
- private replaceCircularReferences;
117
- /**
118
- * Checks if a non-traversable value requires transformers
119
- * @param value - The value to check
120
- * @returns Whether the value requires transformers
121
- * @private
122
- */
123
- private requiresTransformers;
124
- /**
125
- * Traverses the raw value
126
- * @param raw - The raw value to traverse
127
- * @returns The transformed value
128
- */
129
- traverse: (raw: unknown) => unknown;
130
- }
131
- export default RedactorUtils;
@@ -1,2 +0,0 @@
1
- import type { Transformer } from '../../types.js';
2
- export declare const _bigint: Transformer;
@@ -1,2 +0,0 @@
1
- import type { Transformer } from "../../types.js";
2
- export declare const _date: Transformer;
@@ -1,2 +0,0 @@
1
- import type { Transformer } from "../../types.js";
2
- export declare const _error: Transformer;
@@ -1,9 +0,0 @@
1
- import type { Transformer, OrganisedTransformers } from "../../types.js";
2
- /**
3
- * Standard transformers in array for legacy support
4
- */
5
- export declare const standardTransformers: Transformer[];
6
- /**
7
- * Standard transformers organised by type and constructor for performance reasons
8
- */
9
- export declare const organisedStandardTransformers: OrganisedTransformers;
@@ -1,2 +0,0 @@
1
- import type { Transformer } from "../../types.js";
2
- export declare const _map: Transformer;
@@ -1,2 +0,0 @@
1
- import type { Transformer } from "../../types.js";
2
- export declare const _regex: Transformer;
@@ -1,2 +0,0 @@
1
- import type { Transformer } from "../../types.js";
2
- export declare const _set: Transformer;
@@ -1,2 +0,0 @@
1
- import type { Transformer } from "../../types.js";
2
- export declare const _url: Transformer;
package/dist/types.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
package/dist/types.mjs DELETED
@@ -1 +0,0 @@
1
- export {};
@@ -1,100 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TransformerRegistry = void 0;
4
- /**
5
- * Registry for organizing transformers by type and constructor for efficient lookup
6
- */
7
- class TransformerRegistry {
8
- constructor() {
9
- this.typeTransformers = new Map();
10
- this.constructorTransformers = new Map();
11
- this.fallbackTransformers = [];
12
- }
13
- /**
14
- * Add a transformer for a specific typeof result
15
- * @param type - The typeof result (e.g., 'bigint', 'string', etc.)
16
- * @param transformer - The transformer function
17
- */
18
- addTypeTransformer(type, transformer) {
19
- if (!this.typeTransformers.has(type)) {
20
- this.typeTransformers.set(type, []);
21
- }
22
- this.typeTransformers.get(type).push(transformer);
23
- }
24
- /**
25
- * Add a transformer for a specific constructor
26
- * @param constructor - The constructor function (e.g., Date, Error, etc.)
27
- * @param transformer - The transformer function
28
- */
29
- addConstructorTransformer(constructor, transformer) {
30
- if (!this.constructorTransformers.has(constructor)) {
31
- this.constructorTransformers.set(constructor, []);
32
- }
33
- this.constructorTransformers.get(constructor).push(transformer);
34
- }
35
- /**
36
- * Add a fallback transformer that runs on all values
37
- * @param transformer - The transformer function
38
- */
39
- addFallbackTransformer(transformer) {
40
- this.fallbackTransformers.push(transformer);
41
- }
42
- /**
43
- * Get relevant transformers for a value
44
- * @param value - The value to get transformers for
45
- * @returns Array of relevant transformers
46
- */
47
- getTransformersForValue(value) {
48
- const transformers = [];
49
- const type = typeof value;
50
- if (this.typeTransformers.has(type)) {
51
- transformers.push(...this.typeTransformers.get(type));
52
- }
53
- if (typeof value === 'object' && value !== null) {
54
- const constructor = value.constructor;
55
- if (this.constructorTransformers.has(constructor)) {
56
- transformers.push(...this.constructorTransformers.get(constructor));
57
- }
58
- }
59
- transformers.push(...this.fallbackTransformers);
60
- return transformers;
61
- }
62
- /**
63
- * Apply transformers to a value
64
- * @param value - The value to transform
65
- * @param key - The key (optional)
66
- * @param referenceMap - Reference map for circular references (optional)
67
- * @returns The transformed value
68
- */
69
- applyTransformers(value, key, referenceMap) {
70
- if (typeof value === 'string')
71
- return value;
72
- const transformers = this.getTransformersForValue(value);
73
- for (const transformer of transformers) {
74
- const transformed = transformer(value, key, referenceMap);
75
- if (transformed !== value) {
76
- return transformed;
77
- }
78
- }
79
- return value;
80
- }
81
- /**
82
- * Clear all transformers
83
- */
84
- clear() {
85
- this.typeTransformers.clear();
86
- this.constructorTransformers.clear();
87
- this.fallbackTransformers = [];
88
- }
89
- /**
90
- * Get all registered transformers for debugging
91
- */
92
- getRegisteredTransformers() {
93
- return {
94
- types: new Map(this.typeTransformers),
95
- constructors: new Map(this.constructorTransformers),
96
- fallback: [...this.fallbackTransformers]
97
- };
98
- }
99
- }
100
- exports.TransformerRegistry = TransformerRegistry;
@@ -1,94 +0,0 @@
1
- /**
2
- * Registry for organizing transformers by type and constructor for efficient lookup
3
- */
4
- export class TransformerRegistry {
5
- typeTransformers = new Map();
6
- constructorTransformers = new Map();
7
- fallbackTransformers = [];
8
- /**
9
- * Add a transformer for a specific typeof result
10
- * @param type - The typeof result (e.g., 'bigint', 'string', etc.)
11
- * @param transformer - The transformer function
12
- */
13
- addTypeTransformer(type, transformer) {
14
- if (!this.typeTransformers.has(type)) {
15
- this.typeTransformers.set(type, []);
16
- }
17
- this.typeTransformers.get(type).push(transformer);
18
- }
19
- /**
20
- * Add a transformer for a specific constructor
21
- * @param constructor - The constructor function (e.g., Date, Error, etc.)
22
- * @param transformer - The transformer function
23
- */
24
- addConstructorTransformer(constructor, transformer) {
25
- if (!this.constructorTransformers.has(constructor)) {
26
- this.constructorTransformers.set(constructor, []);
27
- }
28
- this.constructorTransformers.get(constructor).push(transformer);
29
- }
30
- /**
31
- * Add a fallback transformer that runs on all values
32
- * @param transformer - The transformer function
33
- */
34
- addFallbackTransformer(transformer) {
35
- this.fallbackTransformers.push(transformer);
36
- }
37
- /**
38
- * Get relevant transformers for a value
39
- * @param value - The value to get transformers for
40
- * @returns Array of relevant transformers
41
- */
42
- getTransformersForValue(value) {
43
- const transformers = [];
44
- const type = typeof value;
45
- if (this.typeTransformers.has(type)) {
46
- transformers.push(...this.typeTransformers.get(type));
47
- }
48
- if (typeof value === 'object' && value !== null) {
49
- const constructor = value.constructor;
50
- if (this.constructorTransformers.has(constructor)) {
51
- transformers.push(...this.constructorTransformers.get(constructor));
52
- }
53
- }
54
- transformers.push(...this.fallbackTransformers);
55
- return transformers;
56
- }
57
- /**
58
- * Apply transformers to a value
59
- * @param value - The value to transform
60
- * @param key - The key (optional)
61
- * @param referenceMap - Reference map for circular references (optional)
62
- * @returns The transformed value
63
- */
64
- applyTransformers(value, key, referenceMap) {
65
- if (typeof value === 'string')
66
- return value;
67
- const transformers = this.getTransformersForValue(value);
68
- for (const transformer of transformers) {
69
- const transformed = transformer(value, key, referenceMap);
70
- if (transformed !== value) {
71
- return transformed;
72
- }
73
- }
74
- return value;
75
- }
76
- /**
77
- * Clear all transformers
78
- */
79
- clear() {
80
- this.typeTransformers.clear();
81
- this.constructorTransformers.clear();
82
- this.fallbackTransformers = [];
83
- }
84
- /**
85
- * Get all registered transformers for debugging
86
- */
87
- getRegisteredTransformers() {
88
- return {
89
- types: new Map(this.typeTransformers),
90
- constructors: new Map(this.constructorTransformers),
91
- fallback: [...this.fallbackTransformers]
92
- };
93
- }
94
- }