@cronn/lib-file-snapshots 0.4.0 → 0.5.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.
package/dist/index.d.ts CHANGED
@@ -31,14 +31,22 @@ interface JsonSerializerOptions {
31
31
  * @default false
32
32
  */
33
33
  includeUndefinedObjectProperties?: boolean;
34
+ /**
35
+ * Custom normalizers to apply before serialization
36
+ */
37
+ normalizers?: JsonNormalizer[];
38
+ }
39
+ type JsonNormalizer = (value: unknown, context: JsonNormalizerContext) => unknown;
40
+ interface JsonNormalizerContext {
41
+ key?: string;
34
42
  }
35
43
  declare class JsonSerializer implements SnapshotSerializer {
36
44
  private readonly includeUndefinedObjectProperties;
45
+ private readonly normalizers;
37
46
  constructor(options?: JsonSerializerOptions);
38
47
  canSerialize(value: unknown): boolean;
39
48
  serialize(value: unknown): SnapshotSerializerResult;
40
49
  private normalizeValueRecursive;
41
- private normalizeValue;
42
50
  private normalizeNumber;
43
51
  private normalizeArray;
44
52
  private normalizeObject;
@@ -47,9 +55,19 @@ declare class JsonSerializer implements SnapshotSerializer {
47
55
  private normalizeMap;
48
56
  private serializedValue;
49
57
  private assertKeyType;
58
+ private applyCustomNormalizers;
50
59
  }
51
60
 
61
+ interface TextSerializerOptions {
62
+ /**
63
+ * Custom normalizers to apply before serialization
64
+ */
65
+ normalizers?: TextNormalizer[];
66
+ }
67
+ type TextNormalizer = (value: string) => string;
52
68
  declare class TextSerializer implements SnapshotSerializer {
69
+ private readonly normalizers;
70
+ constructor(options?: TextSerializerOptions);
53
71
  canSerialize(value: unknown): value is string;
54
72
  serialize(value: unknown): SnapshotSerializerResult;
55
73
  private normalizeValue;
@@ -110,4 +128,4 @@ declare class ValidationFileMatcher {
110
128
  private writeFileSnapshots;
111
129
  }
112
130
 
113
- export { JsonSerializer, type SnapshotSerializer, TextSerializer, ValidationFileMatcher };
131
+ export { type JsonNormalizer, type JsonNormalizerContext, JsonSerializer, type SnapshotSerializer, type TextNormalizer, TextSerializer, ValidationFileMatcher };
package/dist/index.js CHANGED
@@ -16,56 +16,54 @@ function isPlainObject(value) {
16
16
  // src/serializers/json-serializer.ts
17
17
  var JsonSerializer = class {
18
18
  includeUndefinedObjectProperties;
19
+ normalizers;
19
20
  constructor(options = {}) {
20
21
  this.includeUndefinedObjectProperties = options.includeUndefinedObjectProperties ?? false;
22
+ this.normalizers = options.normalizers ?? [];
21
23
  }
22
24
  canSerialize(value) {
23
25
  return true;
24
26
  }
25
27
  serialize(value) {
26
- const jsonValue = isPlainObject(value) || isArray(value) ? this.normalizeValueRecursive(value) : this.normalizeValue(value);
28
+ const jsonValue = this.normalizeValueRecursive(value);
27
29
  const serializedValue = JSON.stringify(jsonValue, void 0, 2);
28
30
  return {
29
31
  serializedValue,
30
32
  fileExtension: "json"
31
33
  };
32
34
  }
33
- normalizeValueRecursive(value) {
34
- if (isArray(value)) {
35
- return this.normalizeArray(value);
36
- }
37
- if (isPlainObject(value)) {
38
- return this.normalizePlainObject(value);
39
- }
40
- if (typeof value === "string" || typeof value === "boolean" || value === null) {
41
- return value;
42
- }
43
- return this.normalizeValue(value);
44
- }
45
- normalizeValue(value) {
46
- if (value === void 0) {
35
+ normalizeValueRecursive(value, context) {
36
+ const customValue = this.applyCustomNormalizers(value, context);
37
+ if (customValue === void 0) {
47
38
  return this.serializedValue("undefined");
48
39
  }
49
- if (value === null || typeof value === "string" || typeof value === "boolean") {
50
- return this.serializedValue(typeof value, { value });
40
+ if (customValue === null || typeof customValue === "string" || typeof customValue === "boolean") {
41
+ const isRoot = context === void 0;
42
+ return isRoot ? this.serializedValue(typeof customValue, {
43
+ value: customValue
44
+ }) : customValue;
51
45
  }
52
- if (typeof value === "number") {
53
- return this.normalizeNumber(value);
46
+ if (typeof customValue === "number") {
47
+ return this.normalizeNumber(customValue);
54
48
  }
55
- if (typeof value === "bigint") {
56
- return this.serializedValue("bigint", { value: value.toString() });
49
+ if (typeof customValue === "bigint") {
50
+ return this.serializedValue("bigint", {
51
+ value: customValue.toString()
52
+ });
57
53
  }
58
- if (typeof value === "symbol") {
59
- return this.serializedValue("symbol", { description: value.description });
54
+ if (typeof customValue === "symbol") {
55
+ return this.serializedValue("symbol", {
56
+ description: customValue.description
57
+ });
60
58
  }
61
- if (typeof value === "function") {
62
- return this.serializedValue("function", { name: value.name });
59
+ if (typeof customValue === "function") {
60
+ return this.serializedValue("function", { name: customValue.name });
63
61
  }
64
- if (typeof value === "object") {
65
- return this.normalizeObject(value);
62
+ if (typeof customValue === "object") {
63
+ return this.normalizeObject(customValue);
66
64
  }
67
65
  throw new Error(
68
- `Missing JSON normalization for value of type ${typeof value}.`
66
+ `Missing JSON normalization for value of type ${typeof customValue}.`
69
67
  );
70
68
  }
71
69
  normalizeNumber(value) {
@@ -98,9 +96,17 @@ var JsonSerializer = class {
98
96
  }
99
97
  }
100
98
  normalizeArray(value) {
101
- return value.map((item) => this.normalizeValueRecursive(item));
99
+ return value.map(
100
+ (item, index) => this.normalizeValueRecursive(item, { key: index.toString() })
101
+ );
102
102
  }
103
103
  normalizeObject(value) {
104
+ if (isArray(value)) {
105
+ return this.normalizeArray(value);
106
+ }
107
+ if (isPlainObject(value)) {
108
+ return this.normalizePlainObject(value);
109
+ }
104
110
  if (value instanceof Date) {
105
111
  return this.normalizeDate(value);
106
112
  }
@@ -135,7 +141,9 @@ var JsonSerializer = class {
135
141
  continue;
136
142
  }
137
143
  this.assertKeyType(key);
138
- normalizedObject[key] = this.normalizeValueRecursive(propertyValue);
144
+ normalizedObject[key] = this.normalizeValueRecursive(propertyValue, {
145
+ key
146
+ });
139
147
  }
140
148
  return normalizedObject;
141
149
  }
@@ -157,10 +165,21 @@ var JsonSerializer = class {
157
165
  throw new Error(`Key of type ${typeof key} cannot be normalized.`);
158
166
  }
159
167
  }
168
+ applyCustomNormalizers(value, context = {}) {
169
+ let normalizedValue = value;
170
+ for (const normalizer of this.normalizers) {
171
+ normalizedValue = normalizer(normalizedValue, context);
172
+ }
173
+ return normalizedValue;
174
+ }
160
175
  };
161
176
 
162
177
  // src/serializers/text-serializer.ts
163
178
  var TextSerializer = class {
179
+ normalizers;
180
+ constructor(options = {}) {
181
+ this.normalizers = options.normalizers ?? [];
182
+ }
164
183
  canSerialize(value) {
165
184
  return typeof value === "string";
166
185
  }
@@ -176,7 +195,11 @@ var TextSerializer = class {
176
195
  };
177
196
  }
178
197
  normalizeValue(value) {
179
- return value.trim();
198
+ let normalizedValue = value;
199
+ for (const normalizer of this.normalizers) {
200
+ normalizedValue = normalizer(normalizedValue);
201
+ }
202
+ return normalizedValue.trim();
180
203
  }
181
204
  };
182
205
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cronn/lib-file-snapshots",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "The library agnostic core for testing file snapshots",
5
5
  "keywords": [
6
6
  "file snapshots"