@cronn/lib-file-snapshots 0.4.0 → 0.6.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,9 @@ declare class ValidationFileMatcher {
110
128
  private writeFileSnapshots;
111
129
  }
112
130
 
113
- export { JsonSerializer, type SnapshotSerializer, TextSerializer, ValidationFileMatcher };
131
+ declare function isString(value: unknown): value is string;
132
+ declare function isArray(value: unknown): value is unknown[];
133
+ type PlainObject = Record<PropertyKey, unknown>;
134
+ declare function isPlainObject(value: unknown): value is PlainObject;
135
+
136
+ export { type JsonNormalizer, type JsonNormalizerContext, JsonSerializer, type PlainObject, type SnapshotSerializer, type TextNormalizer, TextSerializer, ValidationFileMatcher, isArray, isPlainObject, isString };
package/dist/index.js CHANGED
@@ -1,4 +1,7 @@
1
1
  // src/utils/guards.ts
2
+ function isString(value) {
3
+ return typeof value === "string";
4
+ }
2
5
  function isArray(value) {
3
6
  return Array.isArray(value);
4
7
  }
@@ -16,56 +19,54 @@ function isPlainObject(value) {
16
19
  // src/serializers/json-serializer.ts
17
20
  var JsonSerializer = class {
18
21
  includeUndefinedObjectProperties;
22
+ normalizers;
19
23
  constructor(options = {}) {
20
24
  this.includeUndefinedObjectProperties = options.includeUndefinedObjectProperties ?? false;
25
+ this.normalizers = options.normalizers ?? [];
21
26
  }
22
27
  canSerialize(value) {
23
28
  return true;
24
29
  }
25
30
  serialize(value) {
26
- const jsonValue = isPlainObject(value) || isArray(value) ? this.normalizeValueRecursive(value) : this.normalizeValue(value);
31
+ const jsonValue = this.normalizeValueRecursive(value);
27
32
  const serializedValue = JSON.stringify(jsonValue, void 0, 2);
28
33
  return {
29
34
  serializedValue,
30
35
  fileExtension: "json"
31
36
  };
32
37
  }
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) {
38
+ normalizeValueRecursive(value, context) {
39
+ const customValue = this.applyCustomNormalizers(value, context);
40
+ if (customValue === void 0) {
47
41
  return this.serializedValue("undefined");
48
42
  }
49
- if (value === null || typeof value === "string" || typeof value === "boolean") {
50
- return this.serializedValue(typeof value, { value });
43
+ if (customValue === null || isString(customValue) || typeof customValue === "boolean") {
44
+ const isRoot = context === void 0;
45
+ return isRoot ? this.serializedValue(typeof customValue, {
46
+ value: customValue
47
+ }) : customValue;
51
48
  }
52
- if (typeof value === "number") {
53
- return this.normalizeNumber(value);
49
+ if (typeof customValue === "number") {
50
+ return this.normalizeNumber(customValue);
54
51
  }
55
- if (typeof value === "bigint") {
56
- return this.serializedValue("bigint", { value: value.toString() });
52
+ if (typeof customValue === "bigint") {
53
+ return this.serializedValue("bigint", {
54
+ value: customValue.toString()
55
+ });
57
56
  }
58
- if (typeof value === "symbol") {
59
- return this.serializedValue("symbol", { description: value.description });
57
+ if (typeof customValue === "symbol") {
58
+ return this.serializedValue("symbol", {
59
+ description: customValue.description
60
+ });
60
61
  }
61
- if (typeof value === "function") {
62
- return this.serializedValue("function", { name: value.name });
62
+ if (typeof customValue === "function") {
63
+ return this.serializedValue("function", { name: customValue.name });
63
64
  }
64
- if (typeof value === "object") {
65
- return this.normalizeObject(value);
65
+ if (typeof customValue === "object") {
66
+ return this.normalizeObject(customValue);
66
67
  }
67
68
  throw new Error(
68
- `Missing JSON normalization for value of type ${typeof value}.`
69
+ `Missing JSON normalization for value of type ${typeof customValue}.`
69
70
  );
70
71
  }
71
72
  normalizeNumber(value) {
@@ -98,9 +99,17 @@ var JsonSerializer = class {
98
99
  }
99
100
  }
100
101
  normalizeArray(value) {
101
- return value.map((item) => this.normalizeValueRecursive(item));
102
+ return value.map(
103
+ (item, index) => this.normalizeValueRecursive(item, { key: index.toString() })
104
+ );
102
105
  }
103
106
  normalizeObject(value) {
107
+ if (isArray(value)) {
108
+ return this.normalizeArray(value);
109
+ }
110
+ if (isPlainObject(value)) {
111
+ return this.normalizePlainObject(value);
112
+ }
104
113
  if (value instanceof Date) {
105
114
  return this.normalizeDate(value);
106
115
  }
@@ -135,7 +144,9 @@ var JsonSerializer = class {
135
144
  continue;
136
145
  }
137
146
  this.assertKeyType(key);
138
- normalizedObject[key] = this.normalizeValueRecursive(propertyValue);
147
+ normalizedObject[key] = this.normalizeValueRecursive(propertyValue, {
148
+ key
149
+ });
139
150
  }
140
151
  return normalizedObject;
141
152
  }
@@ -153,16 +164,27 @@ var JsonSerializer = class {
153
164
  return { $type: type, ...additionalProps };
154
165
  }
155
166
  assertKeyType(key) {
156
- if (!(typeof key === "string")) {
167
+ if (!isString(key)) {
157
168
  throw new Error(`Key of type ${typeof key} cannot be normalized.`);
158
169
  }
159
170
  }
171
+ applyCustomNormalizers(value, context = {}) {
172
+ let normalizedValue = value;
173
+ for (const normalizer of this.normalizers) {
174
+ normalizedValue = normalizer(normalizedValue, context);
175
+ }
176
+ return normalizedValue;
177
+ }
160
178
  };
161
179
 
162
180
  // src/serializers/text-serializer.ts
163
181
  var TextSerializer = class {
182
+ normalizers;
183
+ constructor(options = {}) {
184
+ this.normalizers = options.normalizers ?? [];
185
+ }
164
186
  canSerialize(value) {
165
- return typeof value === "string";
187
+ return isString(value);
166
188
  }
167
189
  serialize(value) {
168
190
  if (!this.canSerialize(value)) {
@@ -176,7 +198,11 @@ var TextSerializer = class {
176
198
  };
177
199
  }
178
200
  normalizeValue(value) {
179
- return value.trim();
201
+ let normalizedValue = value;
202
+ for (const normalizer of this.normalizers) {
203
+ normalizedValue = normalizer(normalizedValue);
204
+ }
205
+ return normalizedValue.trim();
180
206
  }
181
207
  };
182
208
 
@@ -269,5 +295,8 @@ does not match validation file '${validationFile}'`
269
295
  export {
270
296
  JsonSerializer,
271
297
  TextSerializer,
272
- ValidationFileMatcher
298
+ ValidationFileMatcher,
299
+ isArray,
300
+ isPlainObject,
301
+ isString
273
302
  };
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.6.0",
4
4
  "description": "The library agnostic core for testing file snapshots",
5
5
  "keywords": [
6
6
  "file snapshots"