@cronn/lib-file-snapshots 0.3.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
@@ -24,13 +24,6 @@ interface SnapshotSerializerResult {
24
24
  fileExtension: string;
25
25
  }
26
26
 
27
- declare class CompositeSerializer implements SnapshotSerializer {
28
- private readonly serializers;
29
- constructor(serializers: SnapshotSerializer[]);
30
- canSerialize(value: unknown): boolean;
31
- serialize(value: unknown): SnapshotSerializerResult;
32
- }
33
-
34
27
  interface JsonSerializerOptions {
35
28
  /**
36
29
  * Serializes `undefined` properties in objects. By default, they are omitted.
@@ -38,14 +31,22 @@ interface JsonSerializerOptions {
38
31
  * @default false
39
32
  */
40
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;
41
42
  }
42
43
  declare class JsonSerializer implements SnapshotSerializer {
43
44
  private readonly includeUndefinedObjectProperties;
45
+ private readonly normalizers;
44
46
  constructor(options?: JsonSerializerOptions);
45
47
  canSerialize(value: unknown): boolean;
46
48
  serialize(value: unknown): SnapshotSerializerResult;
47
49
  private normalizeValueRecursive;
48
- private normalizeValue;
49
50
  private normalizeNumber;
50
51
  private normalizeArray;
51
52
  private normalizeObject;
@@ -54,9 +55,19 @@ declare class JsonSerializer implements SnapshotSerializer {
54
55
  private normalizeMap;
55
56
  private serializedValue;
56
57
  private assertKeyType;
58
+ private applyCustomNormalizers;
57
59
  }
58
60
 
61
+ interface TextSerializerOptions {
62
+ /**
63
+ * Custom normalizers to apply before serialization
64
+ */
65
+ normalizers?: TextNormalizer[];
66
+ }
67
+ type TextNormalizer = (value: string) => string;
59
68
  declare class TextSerializer implements SnapshotSerializer {
69
+ private readonly normalizers;
70
+ constructor(options?: TextSerializerOptions);
60
71
  canSerialize(value: unknown): value is string;
61
72
  serialize(value: unknown): SnapshotSerializerResult;
62
73
  private normalizeValue;
@@ -117,4 +128,4 @@ declare class ValidationFileMatcher {
117
128
  private writeFileSnapshots;
118
129
  }
119
130
 
120
- export { CompositeSerializer, JsonSerializer, TextSerializer, ValidationFileMatcher };
131
+ export { type JsonNormalizer, type JsonNormalizerContext, JsonSerializer, type SnapshotSerializer, type TextNormalizer, TextSerializer, ValidationFileMatcher };
package/dist/index.js CHANGED
@@ -1,24 +1,3 @@
1
- // src/serializers/composite-serializer.ts
2
- var CompositeSerializer = class {
3
- serializers;
4
- constructor(serializers) {
5
- this.serializers = serializers;
6
- }
7
- canSerialize(value) {
8
- return this.serializers.some(
9
- (serializer) => serializer.canSerialize(value)
10
- );
11
- }
12
- serialize(value) {
13
- for (const serializer of this.serializers) {
14
- if (serializer.canSerialize(value)) {
15
- return serializer.serialize(value);
16
- }
17
- }
18
- throw new Error(`Missing serializer for value of type ${typeof value}.`);
19
- }
20
- };
21
-
22
1
  // src/utils/guards.ts
23
2
  function isArray(value) {
24
3
  return Array.isArray(value);
@@ -37,56 +16,54 @@ function isPlainObject(value) {
37
16
  // src/serializers/json-serializer.ts
38
17
  var JsonSerializer = class {
39
18
  includeUndefinedObjectProperties;
19
+ normalizers;
40
20
  constructor(options = {}) {
41
21
  this.includeUndefinedObjectProperties = options.includeUndefinedObjectProperties ?? false;
22
+ this.normalizers = options.normalizers ?? [];
42
23
  }
43
24
  canSerialize(value) {
44
25
  return true;
45
26
  }
46
27
  serialize(value) {
47
- const jsonValue = isPlainObject(value) || isArray(value) ? this.normalizeValueRecursive(value) : this.normalizeValue(value);
28
+ const jsonValue = this.normalizeValueRecursive(value);
48
29
  const serializedValue = JSON.stringify(jsonValue, void 0, 2);
49
30
  return {
50
31
  serializedValue,
51
32
  fileExtension: "json"
52
33
  };
53
34
  }
54
- normalizeValueRecursive(value) {
55
- if (isArray(value)) {
56
- return this.normalizeArray(value);
57
- }
58
- if (isPlainObject(value)) {
59
- return this.normalizePlainObject(value);
60
- }
61
- if (typeof value === "string" || typeof value === "boolean" || value === null) {
62
- return value;
63
- }
64
- return this.normalizeValue(value);
65
- }
66
- normalizeValue(value) {
67
- if (value === void 0) {
35
+ normalizeValueRecursive(value, context) {
36
+ const customValue = this.applyCustomNormalizers(value, context);
37
+ if (customValue === void 0) {
68
38
  return this.serializedValue("undefined");
69
39
  }
70
- if (value === null || typeof value === "string" || typeof value === "boolean") {
71
- 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;
72
45
  }
73
- if (typeof value === "number") {
74
- return this.normalizeNumber(value);
46
+ if (typeof customValue === "number") {
47
+ return this.normalizeNumber(customValue);
75
48
  }
76
- if (typeof value === "bigint") {
77
- return this.serializedValue("bigint", { value: value.toString() });
49
+ if (typeof customValue === "bigint") {
50
+ return this.serializedValue("bigint", {
51
+ value: customValue.toString()
52
+ });
78
53
  }
79
- if (typeof value === "symbol") {
80
- return this.serializedValue("symbol", { description: value.description });
54
+ if (typeof customValue === "symbol") {
55
+ return this.serializedValue("symbol", {
56
+ description: customValue.description
57
+ });
81
58
  }
82
- if (typeof value === "function") {
83
- return this.serializedValue("function", { name: value.name });
59
+ if (typeof customValue === "function") {
60
+ return this.serializedValue("function", { name: customValue.name });
84
61
  }
85
- if (typeof value === "object") {
86
- return this.normalizeObject(value);
62
+ if (typeof customValue === "object") {
63
+ return this.normalizeObject(customValue);
87
64
  }
88
65
  throw new Error(
89
- `Missing JSON normalization for value of type ${typeof value}.`
66
+ `Missing JSON normalization for value of type ${typeof customValue}.`
90
67
  );
91
68
  }
92
69
  normalizeNumber(value) {
@@ -119,9 +96,17 @@ var JsonSerializer = class {
119
96
  }
120
97
  }
121
98
  normalizeArray(value) {
122
- return value.map((item) => this.normalizeValueRecursive(item));
99
+ return value.map(
100
+ (item, index) => this.normalizeValueRecursive(item, { key: index.toString() })
101
+ );
123
102
  }
124
103
  normalizeObject(value) {
104
+ if (isArray(value)) {
105
+ return this.normalizeArray(value);
106
+ }
107
+ if (isPlainObject(value)) {
108
+ return this.normalizePlainObject(value);
109
+ }
125
110
  if (value instanceof Date) {
126
111
  return this.normalizeDate(value);
127
112
  }
@@ -156,7 +141,9 @@ var JsonSerializer = class {
156
141
  continue;
157
142
  }
158
143
  this.assertKeyType(key);
159
- normalizedObject[key] = this.normalizeValueRecursive(propertyValue);
144
+ normalizedObject[key] = this.normalizeValueRecursive(propertyValue, {
145
+ key
146
+ });
160
147
  }
161
148
  return normalizedObject;
162
149
  }
@@ -178,10 +165,21 @@ var JsonSerializer = class {
178
165
  throw new Error(`Key of type ${typeof key} cannot be normalized.`);
179
166
  }
180
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
+ }
181
175
  };
182
176
 
183
177
  // src/serializers/text-serializer.ts
184
178
  var TextSerializer = class {
179
+ normalizers;
180
+ constructor(options = {}) {
181
+ this.normalizers = options.normalizers ?? [];
182
+ }
185
183
  canSerialize(value) {
186
184
  return typeof value === "string";
187
185
  }
@@ -197,7 +195,11 @@ var TextSerializer = class {
197
195
  };
198
196
  }
199
197
  normalizeValue(value) {
200
- return value.trim();
198
+ let normalizedValue = value;
199
+ for (const normalizer of this.normalizers) {
200
+ normalizedValue = normalizer(normalizedValue);
201
+ }
202
+ return normalizedValue.trim();
201
203
  }
202
204
  };
203
205
 
@@ -288,7 +290,6 @@ does not match validation file '${validationFile}'`
288
290
  }
289
291
  };
290
292
  export {
291
- CompositeSerializer,
292
293
  JsonSerializer,
293
294
  TextSerializer,
294
295
  ValidationFileMatcher
package/package.json CHANGED
@@ -1,21 +1,21 @@
1
1
  {
2
2
  "name": "@cronn/lib-file-snapshots",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "The library agnostic core for testing file snapshots",
5
5
  "keywords": [
6
6
  "file snapshots"
7
7
  ],
8
8
  "bugs": {
9
- "url": "https://github.com/cronn/vitest-file-snapshots/issues"
9
+ "url": "https://github.com/cronn/file-snapshots/issues"
10
10
  },
11
11
  "author": "cronn",
12
12
  "license": "Apache-2.0",
13
13
  "repository": {
14
14
  "type": "git",
15
- "url": "https://github.com/cronn/vitest-file-snapshots.git",
15
+ "url": "https://github.com/cronn/file-snapshots.git",
16
16
  "directory": "packages/lib-file-snapshots"
17
17
  },
18
- "homepage": "https://github.com/cronn/vitest-file-snapshots",
18
+ "homepage": "https://github.com/cronn/file-snapshots",
19
19
  "engines": {
20
20
  "node": ">=22",
21
21
  "pnpm": ">=10"
@@ -34,10 +34,10 @@
34
34
  "devDependencies": {
35
35
  "@biomejs/biome": "1.9.4",
36
36
  "@types/node": "22.15.30",
37
- "@vitest/coverage-istanbul": "3.2.3",
37
+ "@vitest/coverage-istanbul": "3.2.4",
38
38
  "tsup": "8.5.0",
39
39
  "typescript": "5.8.3",
40
- "vitest": "3.2.3",
40
+ "vitest": "3.2.4",
41
41
  "@cronn/shared-configs": "0.0.0"
42
42
  },
43
43
  "scripts": {