@strictly/define 0.0.25 → 0.0.26
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/.out/index.d.ts +1 -0
- package/.out/index.js +1 -0
- package/.out/transformers/equals.d.ts +3 -0
- package/.out/transformers/equals.js +68 -0
- package/.out/tsconfig.tsbuildinfo +1 -1
- package/.turbo/turbo-build.log +8 -8
- package/.turbo/turbo-check-types.log +1 -1
- package/dist/index.cjs +84 -18
- package/dist/index.d.cts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +73 -6
- package/index.ts +1 -0
- package/package.json +1 -1
- package/transformers/equals.ts +101 -0
package/.out/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * from './transformers/copies/copy';
|
|
2
2
|
export * from './transformers/copies/mobx_copy';
|
|
3
|
+
export * from './transformers/equals';
|
|
3
4
|
export * from './transformers/flatteners/flatten_accessors_of_type';
|
|
4
5
|
export * from './transformers/flatteners/flatten_json_value_to_type_paths_of';
|
|
5
6
|
export * from './transformers/flatteners/flatten_types_of_type';
|
package/.out/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * from './transformers/copies/copy';
|
|
2
2
|
export * from './transformers/copies/mobx_copy';
|
|
3
|
+
export * from './transformers/equals';
|
|
3
4
|
export * from './transformers/flatteners/flatten_accessors_of_type';
|
|
4
5
|
export * from './transformers/flatteners/flatten_json_value_to_type_paths_of';
|
|
5
6
|
export * from './transformers/flatteners/flatten_types_of_type';
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { UnreachableError, } from '@strictly/base';
|
|
2
|
+
import { TypeDefType, } from 'types/definitions';
|
|
3
|
+
export function equals({ definition }, o1, o2) {
|
|
4
|
+
return internalEquals(definition, o1, o2);
|
|
5
|
+
}
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
7
|
+
function internalEquals(typeDef, o1, o2) {
|
|
8
|
+
// get rid of optional values
|
|
9
|
+
if (o1 === o2) {
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
if (o1 == null && o2 != null || o1 != null && o2 == null) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
switch (typeDef.type) {
|
|
16
|
+
case TypeDefType.Literal:
|
|
17
|
+
return o1 === o2;
|
|
18
|
+
case TypeDefType.List:
|
|
19
|
+
return internalListEquals(typeDef, o1, o2);
|
|
20
|
+
case TypeDefType.Record:
|
|
21
|
+
return internalRecordEquals(typeDef, o1, o2);
|
|
22
|
+
case TypeDefType.Object:
|
|
23
|
+
return internalObjectEquals(typeDef, o1, o2);
|
|
24
|
+
case TypeDefType.Union:
|
|
25
|
+
return internalUnionEquals(typeDef, o1, o2);
|
|
26
|
+
default:
|
|
27
|
+
throw new UnreachableError(typeDef);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
31
|
+
function internalListEquals({ elements }, o1, o2) {
|
|
32
|
+
return o1.length === o2.length && o1.every((v, i) => internalEquals(elements, v, o2[i]));
|
|
33
|
+
}
|
|
34
|
+
function internalRecordEquals({ valueTypeDef, },
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
36
|
+
o1,
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
38
|
+
o2) {
|
|
39
|
+
const k1s = Object.keys(o1).sort();
|
|
40
|
+
const k2s = Object.keys(o2).sort();
|
|
41
|
+
return k1s.length === k2s.length && k1s.every((k1, i) => {
|
|
42
|
+
const k2 = k2s[i];
|
|
43
|
+
return k1 === k2 && internalEquals(valueTypeDef, o1[k1], o2[k2]);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
function internalObjectEquals({ fields, },
|
|
47
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
48
|
+
o1,
|
|
49
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
50
|
+
o2) {
|
|
51
|
+
return Object.entries(fields).every(([key, typeDef,]) => {
|
|
52
|
+
return internalEquals(typeDef, o1[key], o2[key]);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
function internalUnionEquals({ discriminator, unions, },
|
|
56
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
57
|
+
o1,
|
|
58
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
59
|
+
o2) {
|
|
60
|
+
if (discriminator != null) {
|
|
61
|
+
return o1[discriminator] === o2[discriminator] && internalEquals(unions[o1[discriminator]], o1, o2);
|
|
62
|
+
}
|
|
63
|
+
const allTypeDefs = Object.values(unions);
|
|
64
|
+
const variableTypeDefs = allTypeDefs.filter(function (typeDef) {
|
|
65
|
+
return typeDef.type !== TypeDefType.Literal || typeDef.valuePrototype == null;
|
|
66
|
+
});
|
|
67
|
+
return o1 === o2 || variableTypeDefs.length === 1 && internalEquals(variableTypeDefs[0], o1, o2);
|
|
68
|
+
}
|