@strictly/define 0.0.24 → 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.
@@ -7,12 +7,12 @@ $ tsup
7
7
  CLI Target: es6
8
8
  CJS Build start
9
9
  ESM Build start
10
- CJS dist/index.cjs 33.26 KB
11
- CJS ⚡️ Build success in 98ms
12
- ESM dist/index.js 30.67 KB
13
- ESM ⚡️ Build success in 106ms
10
+ CJS dist/index.cjs 35.17 KB
11
+ CJS ⚡️ Build success in 96ms
12
+ ESM dist/index.js 32.58 KB
13
+ ESM ⚡️ Build success in 97ms
14
14
  DTS Build start
15
- DTS ⚡️ Build success in 13457ms
16
- DTS dist/index.d.cts 41.51 KB
17
- DTS dist/index.d.ts 41.51 KB
18
- Done in 14.99s.
15
+ DTS ⚡️ Build success in 13028ms
16
+ DTS dist/index.d.cts 41.63 KB
17
+ DTS dist/index.d.ts 41.63 KB
18
+ Done in 14.29s.
@@ -1,3 +1,3 @@
1
1
  yarn run v1.22.22
2
2
  $ tsc -b
3
- Done in 0.36s.
3
+ Done in 0.38s.
@@ -1,3 +1,3 @@
1
1
  yarn run v1.22.22
2
2
  $ json -f package.json -f package.exports.json --merge > package.release.json
3
- Done in 0.10s.
3
+ Done in 0.11s.
package/dist/index.cjs CHANGED
@@ -50,6 +50,7 @@ __export(index_exports, {
50
50
  annotations: () => annotations,
51
51
  booleanType: () => booleanType,
52
52
  copy: () => copy,
53
+ equals: () => equals,
53
54
  flattenAccessorsOfType: () => flattenAccessorsOfType,
54
55
  flattenJsonValueToTypePathsOf: () => flattenJsonValueToTypePathsOf,
55
56
  flattenTypesOfType: () => flattenTypesOfType,
@@ -427,6 +428,70 @@ function mobxCopy(t, proto) {
427
428
  return copyTo(t, proto, observeValue);
428
429
  }
429
430
 
431
+ // transformers/equals.ts
432
+ var import_base5 = require("@strictly/base");
433
+ function equals({ definition }, o1, o2) {
434
+ return internalEquals(definition, o1, o2);
435
+ }
436
+ function internalEquals(typeDef, o1, o2) {
437
+ if (o1 === o2) {
438
+ return true;
439
+ }
440
+ if (o1 == null && o2 != null || o1 != null && o2 == null) {
441
+ return false;
442
+ }
443
+ switch (typeDef.type) {
444
+ case 1 /* Literal */:
445
+ return o1 === o2;
446
+ case 2 /* List */:
447
+ return internalListEquals(typeDef, o1, o2);
448
+ case 3 /* Record */:
449
+ return internalRecordEquals(typeDef, o1, o2);
450
+ case 4 /* Object */:
451
+ return internalObjectEquals(typeDef, o1, o2);
452
+ case 5 /* Union */:
453
+ return internalUnionEquals(typeDef, o1, o2);
454
+ default:
455
+ throw new import_base5.UnreachableError(typeDef);
456
+ }
457
+ }
458
+ function internalListEquals({ elements }, o1, o2) {
459
+ return o1.length === o2.length && o1.every((v, i) => internalEquals(elements, v, o2[i]));
460
+ }
461
+ function internalRecordEquals({
462
+ valueTypeDef
463
+ }, o1, o2) {
464
+ const k1s = Object.keys(o1).sort();
465
+ const k2s = Object.keys(o2).sort();
466
+ return k1s.length === k2s.length && k1s.every((k1, i) => {
467
+ const k2 = k2s[i];
468
+ return k1 === k2 && internalEquals(valueTypeDef, o1[k1], o2[k2]);
469
+ });
470
+ }
471
+ function internalObjectEquals({
472
+ fields
473
+ }, o1, o2) {
474
+ return Object.entries(fields).every(([
475
+ key,
476
+ typeDef
477
+ ]) => {
478
+ return internalEquals(typeDef, o1[key], o2[key]);
479
+ });
480
+ }
481
+ function internalUnionEquals({
482
+ discriminator,
483
+ unions
484
+ }, o1, o2) {
485
+ if (discriminator != null) {
486
+ return o1[discriminator] === o2[discriminator] && internalEquals(unions[o1[discriminator]], o1, o2);
487
+ }
488
+ const allTypeDefs = Object.values(unions);
489
+ const variableTypeDefs = allTypeDefs.filter(function(typeDef) {
490
+ return typeDef.type !== 1 /* Literal */ || typeDef.valuePrototype == null;
491
+ });
492
+ return o1 === o2 || variableTypeDefs.length === 1 && internalEquals(variableTypeDefs[0], o1, o2);
493
+ }
494
+
430
495
  // transformers/flatteners/flatten_accessors_of_type.ts
431
496
  function mapAccessor(_t, value, set) {
432
497
  return {
@@ -458,10 +523,10 @@ function flattenJsonValueToTypePathsOf(t, value) {
458
523
  }
459
524
 
460
525
  // transformers/flatteners/flatten_type_to.ts
461
- var import_base5 = require("@strictly/base");
526
+ var import_base6 = require("@strictly/base");
462
527
  function flattenTypeTo({ definition }, mapper2) {
463
528
  const typeDefs = internalFlattenTypeDef("$", definition, {});
464
- return (0, import_base5.reduce)(
529
+ return (0, import_base6.reduce)(
465
530
  typeDefs,
466
531
  function(acc, key, typeDef) {
467
532
  acc[key] = mapper2(typeDef, key);
@@ -488,7 +553,7 @@ function internalFlattenTypeDefChildren(path, qualifier, t, r) {
488
553
  case 5 /* Union */:
489
554
  return internalFlattenUnionTypeDefChildren(path, qualifier, t, r);
490
555
  default:
491
- throw new import_base5.UnreachableError(t);
556
+ throw new import_base6.UnreachableError(t);
492
557
  }
493
558
  }
494
559
  function internalFlattenedListTypeDefChildren(path, { elements }, r) {
@@ -498,7 +563,7 @@ function internalFlattenRecordTypeDefChildren(path, { valueTypeDef }, r) {
498
563
  return internalFlattenTypeDef(jsonPath(path, "*"), valueTypeDef, r);
499
564
  }
500
565
  function internalFlattenObjectTypeDefChildren(path, qualifier, { fields }, r) {
501
- return (0, import_base5.reduce)(
566
+ return (0, import_base6.reduce)(
502
567
  fields,
503
568
  function(acc, fieldName, fieldTypeDef) {
504
569
  return internalFlattenTypeDef(
@@ -514,7 +579,7 @@ function internalFlattenUnionTypeDefChildren(path, qualifier, {
514
579
  discriminator,
515
580
  unions
516
581
  }, r) {
517
- return (0, import_base5.reduce)(
582
+ return (0, import_base6.reduce)(
518
583
  unions,
519
584
  function(acc, key, typeDef) {
520
585
  return internalFlattenTypeDefChildren(
@@ -574,7 +639,7 @@ function flattenValuesOfType(typeDef, value) {
574
639
  }
575
640
 
576
641
  // transformers/flatteners/value_path_to_type_path.ts
577
- var import_base6 = require("@strictly/base");
642
+ var import_base7 = require("@strictly/base");
578
643
  function valuePathToTypePath({ definition: typeDef }, valuePath, allowMissingPaths = false) {
579
644
  const valueSteps = valuePath.split(/\.|\[/g);
580
645
  const parts = valueSteps[0].split(":");
@@ -582,7 +647,7 @@ function valuePathToTypePath({ definition: typeDef }, valuePath, allowMissingPat
582
647
  first,
583
648
  ...qualifiers
584
649
  ] = parts;
585
- (0, import_base6.assertEqual)(first, "$");
650
+ (0, import_base7.assertEqual)(first, "$");
586
651
  const typeSteps = internalJsonValuePathToTypePath(
587
652
  typeDef,
588
653
  qualifiers,
@@ -612,7 +677,7 @@ function internalJsonValuePathToTypePath(typeDef, qualifiers, valueSteps, allowM
612
677
  if (allowMissingPaths) {
613
678
  return valueSteps;
614
679
  } else {
615
- throw new import_base6.PreconditionFailedError(
680
+ throw new import_base7.PreconditionFailedError(
616
681
  "literal should terminate path {} ({})",
617
682
  originalValuePath,
618
683
  nextValueStepAndQualifiersString
@@ -652,7 +717,7 @@ function internalJsonValuePathToTypePath(typeDef, qualifiers, valueSteps, allowM
652
717
  return valueSteps;
653
718
  }
654
719
  } else {
655
- (0, import_base6.assertExists)(typeDef.fields[valueStep], "missing field in {} ({})", originalValuePath, valueStep);
720
+ (0, import_base7.assertExists)(typeDef.fields[valueStep], "missing field in {} ({})", originalValuePath, valueStep);
656
721
  }
657
722
  return [
658
723
  nextValueStepAndQualifiersString,
@@ -667,7 +732,7 @@ function internalJsonValuePathToTypePath(typeDef, qualifiers, valueSteps, allowM
667
732
  case 5 /* Union */:
668
733
  if (typeDef.discriminator == null) {
669
734
  if (remainingValueSteps.length > 0) {
670
- const union2 = (0, import_base6.reduce)(
735
+ const union2 = (0, import_base7.reduce)(
671
736
  typeDef.unions,
672
737
  function(acc, _k, v) {
673
738
  if (v.type !== 1 /* Literal */ || v.type === 1 /* Literal */ && v.valuePrototype == null) {
@@ -677,7 +742,7 @@ function internalJsonValuePathToTypePath(typeDef, qualifiers, valueSteps, allowM
677
742
  },
678
743
  null
679
744
  );
680
- (0, import_base6.assertExists)(union2, "expected a complex union {}", originalValuePath);
745
+ (0, import_base7.assertExists)(union2, "expected a complex union {}", originalValuePath);
681
746
  return internalJsonValuePathToTypePath(
682
747
  union2,
683
748
  nextQualifiers,
@@ -693,7 +758,7 @@ function internalJsonValuePathToTypePath(typeDef, qualifiers, valueSteps, allowM
693
758
  if (allowMissingPaths) {
694
759
  return valueSteps;
695
760
  } else {
696
- throw new import_base6.PreconditionFailedError(
761
+ throw new import_base7.PreconditionFailedError(
697
762
  "mismatched qualifiers in {} (at {})",
698
763
  originalValuePath,
699
764
  valueStep
@@ -704,7 +769,7 @@ function internalJsonValuePathToTypePath(typeDef, qualifiers, valueSteps, allowM
704
769
  qualifier,
705
770
  ...remainingQualifiers
706
771
  ] = qualifiers;
707
- const union2 = (0, import_base6.assertExistsAndReturn)(typeDef.unions[qualifier], "missing union {}", qualifier);
772
+ const union2 = (0, import_base7.assertExistsAndReturn)(typeDef.unions[qualifier], "missing union {}", qualifier);
708
773
  return internalJsonValuePathToTypePath(
709
774
  union2,
710
775
  remainingQualifiers,
@@ -714,7 +779,7 @@ function internalJsonValuePathToTypePath(typeDef, qualifiers, valueSteps, allowM
714
779
  );
715
780
  }
716
781
  default:
717
- throw new import_base6.UnreachableError(typeDef);
782
+ throw new import_base7.UnreachableError(typeDef);
718
783
  }
719
784
  }
720
785
 
@@ -787,7 +852,7 @@ function mergeAnnotations(a1, a2) {
787
852
  }
788
853
 
789
854
  // types/type_of_type.ts
790
- var import_base7 = require("@strictly/base");
855
+ var import_base8 = require("@strictly/base");
791
856
  function typeOfType({ definition }) {
792
857
  return {
793
858
  definition: typeDefOfTypeDef(definition)
@@ -806,7 +871,7 @@ function typeDefOfTypeDef(t) {
806
871
  case 5 /* Union */:
807
872
  return typeDefOfUnionTypeDef(t);
808
873
  default:
809
- throw new import_base7.UnreachableError(t);
874
+ throw new import_base8.UnreachableError(t);
810
875
  }
811
876
  }
812
877
  function typeDefOfLiteralTypeDef({
@@ -844,7 +909,7 @@ function typeDefOfObjectTypeDef({
844
909
  }) {
845
910
  return {
846
911
  type,
847
- fields: (0, import_base7.map)(fields, function(_k, v) {
912
+ fields: (0, import_base8.map)(fields, function(_k, v) {
848
913
  return typeDefOfTypeDef(v);
849
914
  })
850
915
  };
@@ -857,7 +922,7 @@ function typeDefOfUnionTypeDef({
857
922
  return {
858
923
  type,
859
924
  discriminator,
860
- unions: (0, import_base7.map)(unions, function(_k, v) {
925
+ unions: (0, import_base8.map)(unions, function(_k, v) {
861
926
  return typeDefOfTypeDef(v);
862
927
  })
863
928
  };
@@ -1216,6 +1281,7 @@ var RegexpValidator = _RegexpValidator;
1216
1281
  annotations,
1217
1282
  booleanType,
1218
1283
  copy,
1284
+ equals,
1219
1285
  flattenAccessorsOfType,
1220
1286
  flattenJsonValueToTypePathsOf,
1221
1287
  flattenTypesOfType,
package/dist/index.d.cts CHANGED
@@ -147,6 +147,8 @@ type MobxValueOfType<T extends Type> = ValueOfType<T, MobxObservable>;
147
147
 
148
148
  declare function mobxCopy<T extends StrictType>(t: T, proto: ValueOfType<ReadonlyTypeOfType<T>>): MobxValueOfType<T>;
149
149
 
150
+ declare function equals<T extends Type>({ definition }: T, o1: ValueOfType<T>, o2: ValueOfType<T>): boolean;
151
+
150
152
  type StartingDepth = 8;
151
153
  type Depths = [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
152
154
 
@@ -614,4 +616,4 @@ type ValidatorsOfValues<FlattenedValues extends Readonly<Record<string, any>>, T
614
616
  readonly [K in keyof FlattenedValues]: Validator<FlattenedValues[K], any, TypePathsToValuePaths[K], Context>;
615
617
  };
616
618
 
617
- export { type Accessor, type AnnotatedValidator, type Annotations, type AnyValueType, DefinedValidator, type ErrorOfValidator, type FlattenedAccessorsOfType, type FlattenedTypesOfType, type FlattenedValuesOfType, type FunctionalValidator, type InternalJsonPathsOf, type IsStrictUnion, type ListTypeDef, type LiteralTypeDef, type Mapper, type MaximumStringLengthValidationError, MaximumStringLengthValidationErrorType, MaximumStringLengthValidator, type MinimumStringLengthValidationError, MinimumStringLengthValidationErrorType, MinimumStringLengthValidator, type MobxObservable, type MobxValueOfType, type NonMobxObservable, type ObjectFieldKey, type ObjectTypeDef, type ObjectTypeDefFields, OptionalValidatorProxy, type PathsOfType, type ReadonlyOfTypeDef, type ReadonlyTypeOfType, type RecordKeyType, type RecordTypeDef, type RegexpValidationError, RegexpValidationErrorType, RegexpValidator, type Setter, type StrictListTypeDef, type StrictLiteralTypeDef, type StrictObjectTypeDef, type StrictObjectTypeDefFields, type StrictRecordTypeDef, type StrictType, type StrictTypeDef, type StrictUnionTypeDef, type Type, type TypeDef, TypeDefType, type UnionKey, type UnionTypeDef, type ValidationError, type Validator, type ValidatorsOfValues, type ValueOfType, type ValueOfTypeDef, type ValueToTypePathsOfType, type ValueTypesOfDiscriminatedUnion, annotations, booleanType, copy, flattenAccessorsOfType, flattenJsonValueToTypePathsOf, flattenTypesOfType, flattenValidatorsOfValidatingType, flattenValueTo, flattenValuesOfType, getUnionTypeDef, isAnnotatedValidator, isFunctionalValidator, jsonPath, jsonPathPop, jsonPathPrefix, jsonPathUnprefix, list, literal, mergeAnnotations, mergeValidators, mobxCopy, nullType, nullable, numberType, object, record, stringType, union, validate, valuePathToTypePath };
619
+ export { type Accessor, type AnnotatedValidator, type Annotations, type AnyValueType, DefinedValidator, type ErrorOfValidator, type FlattenedAccessorsOfType, type FlattenedTypesOfType, type FlattenedValuesOfType, type FunctionalValidator, type InternalJsonPathsOf, type IsStrictUnion, type ListTypeDef, type LiteralTypeDef, type Mapper, type MaximumStringLengthValidationError, MaximumStringLengthValidationErrorType, MaximumStringLengthValidator, type MinimumStringLengthValidationError, MinimumStringLengthValidationErrorType, MinimumStringLengthValidator, type MobxObservable, type MobxValueOfType, type NonMobxObservable, type ObjectFieldKey, type ObjectTypeDef, type ObjectTypeDefFields, OptionalValidatorProxy, type PathsOfType, type ReadonlyOfTypeDef, type ReadonlyTypeOfType, type RecordKeyType, type RecordTypeDef, type RegexpValidationError, RegexpValidationErrorType, RegexpValidator, type Setter, type StrictListTypeDef, type StrictLiteralTypeDef, type StrictObjectTypeDef, type StrictObjectTypeDefFields, type StrictRecordTypeDef, type StrictType, type StrictTypeDef, type StrictUnionTypeDef, type Type, type TypeDef, TypeDefType, type UnionKey, type UnionTypeDef, type ValidationError, type Validator, type ValidatorsOfValues, type ValueOfType, type ValueOfTypeDef, type ValueToTypePathsOfType, type ValueTypesOfDiscriminatedUnion, annotations, booleanType, copy, equals, flattenAccessorsOfType, flattenJsonValueToTypePathsOf, flattenTypesOfType, flattenValidatorsOfValidatingType, flattenValueTo, flattenValuesOfType, getUnionTypeDef, isAnnotatedValidator, isFunctionalValidator, jsonPath, jsonPathPop, jsonPathPrefix, jsonPathUnprefix, list, literal, mergeAnnotations, mergeValidators, mobxCopy, nullType, nullable, numberType, object, record, stringType, union, validate, valuePathToTypePath };
package/dist/index.d.ts CHANGED
@@ -147,6 +147,8 @@ type MobxValueOfType<T extends Type> = ValueOfType<T, MobxObservable>;
147
147
 
148
148
  declare function mobxCopy<T extends StrictType>(t: T, proto: ValueOfType<ReadonlyTypeOfType<T>>): MobxValueOfType<T>;
149
149
 
150
+ declare function equals<T extends Type>({ definition }: T, o1: ValueOfType<T>, o2: ValueOfType<T>): boolean;
151
+
150
152
  type StartingDepth = 8;
151
153
  type Depths = [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
152
154
 
@@ -614,4 +616,4 @@ type ValidatorsOfValues<FlattenedValues extends Readonly<Record<string, any>>, T
614
616
  readonly [K in keyof FlattenedValues]: Validator<FlattenedValues[K], any, TypePathsToValuePaths[K], Context>;
615
617
  };
616
618
 
617
- export { type Accessor, type AnnotatedValidator, type Annotations, type AnyValueType, DefinedValidator, type ErrorOfValidator, type FlattenedAccessorsOfType, type FlattenedTypesOfType, type FlattenedValuesOfType, type FunctionalValidator, type InternalJsonPathsOf, type IsStrictUnion, type ListTypeDef, type LiteralTypeDef, type Mapper, type MaximumStringLengthValidationError, MaximumStringLengthValidationErrorType, MaximumStringLengthValidator, type MinimumStringLengthValidationError, MinimumStringLengthValidationErrorType, MinimumStringLengthValidator, type MobxObservable, type MobxValueOfType, type NonMobxObservable, type ObjectFieldKey, type ObjectTypeDef, type ObjectTypeDefFields, OptionalValidatorProxy, type PathsOfType, type ReadonlyOfTypeDef, type ReadonlyTypeOfType, type RecordKeyType, type RecordTypeDef, type RegexpValidationError, RegexpValidationErrorType, RegexpValidator, type Setter, type StrictListTypeDef, type StrictLiteralTypeDef, type StrictObjectTypeDef, type StrictObjectTypeDefFields, type StrictRecordTypeDef, type StrictType, type StrictTypeDef, type StrictUnionTypeDef, type Type, type TypeDef, TypeDefType, type UnionKey, type UnionTypeDef, type ValidationError, type Validator, type ValidatorsOfValues, type ValueOfType, type ValueOfTypeDef, type ValueToTypePathsOfType, type ValueTypesOfDiscriminatedUnion, annotations, booleanType, copy, flattenAccessorsOfType, flattenJsonValueToTypePathsOf, flattenTypesOfType, flattenValidatorsOfValidatingType, flattenValueTo, flattenValuesOfType, getUnionTypeDef, isAnnotatedValidator, isFunctionalValidator, jsonPath, jsonPathPop, jsonPathPrefix, jsonPathUnprefix, list, literal, mergeAnnotations, mergeValidators, mobxCopy, nullType, nullable, numberType, object, record, stringType, union, validate, valuePathToTypePath };
619
+ export { type Accessor, type AnnotatedValidator, type Annotations, type AnyValueType, DefinedValidator, type ErrorOfValidator, type FlattenedAccessorsOfType, type FlattenedTypesOfType, type FlattenedValuesOfType, type FunctionalValidator, type InternalJsonPathsOf, type IsStrictUnion, type ListTypeDef, type LiteralTypeDef, type Mapper, type MaximumStringLengthValidationError, MaximumStringLengthValidationErrorType, MaximumStringLengthValidator, type MinimumStringLengthValidationError, MinimumStringLengthValidationErrorType, MinimumStringLengthValidator, type MobxObservable, type MobxValueOfType, type NonMobxObservable, type ObjectFieldKey, type ObjectTypeDef, type ObjectTypeDefFields, OptionalValidatorProxy, type PathsOfType, type ReadonlyOfTypeDef, type ReadonlyTypeOfType, type RecordKeyType, type RecordTypeDef, type RegexpValidationError, RegexpValidationErrorType, RegexpValidator, type Setter, type StrictListTypeDef, type StrictLiteralTypeDef, type StrictObjectTypeDef, type StrictObjectTypeDefFields, type StrictRecordTypeDef, type StrictType, type StrictTypeDef, type StrictUnionTypeDef, type Type, type TypeDef, TypeDefType, type UnionKey, type UnionTypeDef, type ValidationError, type Validator, type ValidatorsOfValues, type ValueOfType, type ValueOfTypeDef, type ValueToTypePathsOfType, type ValueTypesOfDiscriminatedUnion, annotations, booleanType, copy, equals, flattenAccessorsOfType, flattenJsonValueToTypePathsOf, flattenTypesOfType, flattenValidatorsOfValidatingType, flattenValueTo, flattenValuesOfType, getUnionTypeDef, isAnnotatedValidator, isFunctionalValidator, jsonPath, jsonPathPop, jsonPathPrefix, jsonPathUnprefix, list, literal, mergeAnnotations, mergeValidators, mobxCopy, nullType, nullable, numberType, object, record, stringType, union, validate, valuePathToTypePath };
package/dist/index.js CHANGED
@@ -383,6 +383,72 @@ function mobxCopy(t, proto) {
383
383
  return copyTo(t, proto, observeValue);
384
384
  }
385
385
 
386
+ // transformers/equals.ts
387
+ import {
388
+ UnreachableError as UnreachableError4
389
+ } from "@strictly/base";
390
+ function equals({ definition }, o1, o2) {
391
+ return internalEquals(definition, o1, o2);
392
+ }
393
+ function internalEquals(typeDef, o1, o2) {
394
+ if (o1 === o2) {
395
+ return true;
396
+ }
397
+ if (o1 == null && o2 != null || o1 != null && o2 == null) {
398
+ return false;
399
+ }
400
+ switch (typeDef.type) {
401
+ case 1 /* Literal */:
402
+ return o1 === o2;
403
+ case 2 /* List */:
404
+ return internalListEquals(typeDef, o1, o2);
405
+ case 3 /* Record */:
406
+ return internalRecordEquals(typeDef, o1, o2);
407
+ case 4 /* Object */:
408
+ return internalObjectEquals(typeDef, o1, o2);
409
+ case 5 /* Union */:
410
+ return internalUnionEquals(typeDef, o1, o2);
411
+ default:
412
+ throw new UnreachableError4(typeDef);
413
+ }
414
+ }
415
+ function internalListEquals({ elements }, o1, o2) {
416
+ return o1.length === o2.length && o1.every((v, i) => internalEquals(elements, v, o2[i]));
417
+ }
418
+ function internalRecordEquals({
419
+ valueTypeDef
420
+ }, o1, o2) {
421
+ const k1s = Object.keys(o1).sort();
422
+ const k2s = Object.keys(o2).sort();
423
+ return k1s.length === k2s.length && k1s.every((k1, i) => {
424
+ const k2 = k2s[i];
425
+ return k1 === k2 && internalEquals(valueTypeDef, o1[k1], o2[k2]);
426
+ });
427
+ }
428
+ function internalObjectEquals({
429
+ fields
430
+ }, o1, o2) {
431
+ return Object.entries(fields).every(([
432
+ key,
433
+ typeDef
434
+ ]) => {
435
+ return internalEquals(typeDef, o1[key], o2[key]);
436
+ });
437
+ }
438
+ function internalUnionEquals({
439
+ discriminator,
440
+ unions
441
+ }, o1, o2) {
442
+ if (discriminator != null) {
443
+ return o1[discriminator] === o2[discriminator] && internalEquals(unions[o1[discriminator]], o1, o2);
444
+ }
445
+ const allTypeDefs = Object.values(unions);
446
+ const variableTypeDefs = allTypeDefs.filter(function(typeDef) {
447
+ return typeDef.type !== 1 /* Literal */ || typeDef.valuePrototype == null;
448
+ });
449
+ return o1 === o2 || variableTypeDefs.length === 1 && internalEquals(variableTypeDefs[0], o1, o2);
450
+ }
451
+
386
452
  // transformers/flatteners/flatten_accessors_of_type.ts
387
453
  function mapAccessor(_t, value, set) {
388
454
  return {
@@ -416,7 +482,7 @@ function flattenJsonValueToTypePathsOf(t, value) {
416
482
  // transformers/flatteners/flatten_type_to.ts
417
483
  import {
418
484
  reduce as reduce4,
419
- UnreachableError as UnreachableError4
485
+ UnreachableError as UnreachableError5
420
486
  } from "@strictly/base";
421
487
  function flattenTypeTo({ definition }, mapper2) {
422
488
  const typeDefs = internalFlattenTypeDef("$", definition, {});
@@ -447,7 +513,7 @@ function internalFlattenTypeDefChildren(path, qualifier, t, r) {
447
513
  case 5 /* Union */:
448
514
  return internalFlattenUnionTypeDefChildren(path, qualifier, t, r);
449
515
  default:
450
- throw new UnreachableError4(t);
516
+ throw new UnreachableError5(t);
451
517
  }
452
518
  }
453
519
  function internalFlattenedListTypeDefChildren(path, { elements }, r) {
@@ -539,7 +605,7 @@ import {
539
605
  assertExistsAndReturn,
540
606
  PreconditionFailedError,
541
607
  reduce as reduce5,
542
- UnreachableError as UnreachableError5
608
+ UnreachableError as UnreachableError6
543
609
  } from "@strictly/base";
544
610
  function valuePathToTypePath({ definition: typeDef }, valuePath, allowMissingPaths = false) {
545
611
  const valueSteps = valuePath.split(/\.|\[/g);
@@ -680,7 +746,7 @@ function internalJsonValuePathToTypePath(typeDef, qualifiers, valueSteps, allowM
680
746
  );
681
747
  }
682
748
  default:
683
- throw new UnreachableError5(typeDef);
749
+ throw new UnreachableError6(typeDef);
684
750
  }
685
751
  }
686
752
 
@@ -755,7 +821,7 @@ function mergeAnnotations(a1, a2) {
755
821
  // types/type_of_type.ts
756
822
  import {
757
823
  map as map2,
758
- UnreachableError as UnreachableError6
824
+ UnreachableError as UnreachableError7
759
825
  } from "@strictly/base";
760
826
  function typeOfType({ definition }) {
761
827
  return {
@@ -775,7 +841,7 @@ function typeDefOfTypeDef(t) {
775
841
  case 5 /* Union */:
776
842
  return typeDefOfUnionTypeDef(t);
777
843
  default:
778
- throw new UnreachableError6(t);
844
+ throw new UnreachableError7(t);
779
845
  }
780
846
  }
781
847
  function typeDefOfLiteralTypeDef({
@@ -1184,6 +1250,7 @@ export {
1184
1250
  annotations,
1185
1251
  booleanType,
1186
1252
  copy,
1253
+ equals,
1187
1254
  flattenAccessorsOfType,
1188
1255
  flattenJsonValueToTypePathsOf,
1189
1256
  flattenTypesOfType,
package/index.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/package.json CHANGED
@@ -38,7 +38,7 @@
38
38
  "test:watch": "vitest"
39
39
  },
40
40
  "type": "module",
41
- "version": "0.0.24",
41
+ "version": "0.0.26",
42
42
  "exports": {
43
43
  ".": {
44
44
  "import": {
@@ -0,0 +1,101 @@
1
+ import {
2
+ UnreachableError,
3
+ } from '@strictly/base'
4
+ import {
5
+ type ListTypeDef,
6
+ type ObjectTypeDef,
7
+ type RecordTypeDef,
8
+ type Type,
9
+ type TypeDef,
10
+ TypeDefType,
11
+ type UnionTypeDef,
12
+ } from 'types/definitions'
13
+ import { type ValueOfType } from 'types/value_of_type'
14
+
15
+ export function equals<T extends Type>({ definition }: T, o1: ValueOfType<T>, o2: ValueOfType<T>): boolean {
16
+ return internalEquals(definition, o1, o2)
17
+ }
18
+
19
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
+ function internalEquals(typeDef: TypeDef, o1: any, o2: any): boolean {
21
+ // get rid of optional values
22
+ if (o1 === o2) {
23
+ return true
24
+ }
25
+ if (o1 == null && o2 != null || o1 != null && o2 == null) {
26
+ return false
27
+ }
28
+ switch (typeDef.type) {
29
+ case TypeDefType.Literal:
30
+ return o1 === o2
31
+ case TypeDefType.List:
32
+ return internalListEquals(typeDef, o1, o2)
33
+ case TypeDefType.Record:
34
+ return internalRecordEquals(typeDef, o1, o2)
35
+ case TypeDefType.Object:
36
+ return internalObjectEquals(typeDef, o1, o2)
37
+ case TypeDefType.Union:
38
+ return internalUnionEquals(typeDef, o1, o2)
39
+ default:
40
+ throw new UnreachableError(typeDef)
41
+ }
42
+ }
43
+
44
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
45
+ function internalListEquals({ elements }: ListTypeDef, o1: any[], o2: any[]) {
46
+ return o1.length === o2.length && o1.every((v, i) => internalEquals(elements, v, o2[i]))
47
+ }
48
+
49
+ function internalRecordEquals(
50
+ {
51
+ valueTypeDef,
52
+ }: RecordTypeDef,
53
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
54
+ o1: Record<string, any>,
55
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
56
+ o2: Record<string, any>,
57
+ ) {
58
+ const k1s = Object.keys(o1).sort()
59
+ const k2s = Object.keys(o2).sort()
60
+ return k1s.length === k2s.length && k1s.every((k1, i) => {
61
+ const k2 = k2s[i]
62
+ return k1 === k2 && internalEquals(valueTypeDef, o1[k1], o2[k2])
63
+ })
64
+ }
65
+
66
+ function internalObjectEquals(
67
+ {
68
+ fields,
69
+ }: ObjectTypeDef,
70
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
71
+ o1: Record<string, any>,
72
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
73
+ o2: Record<string, any>,
74
+ ) {
75
+ return Object.entries(fields).every(([
76
+ key,
77
+ typeDef,
78
+ ]) => {
79
+ return internalEquals(typeDef, o1[key], o2[key])
80
+ })
81
+ }
82
+
83
+ function internalUnionEquals(
84
+ {
85
+ discriminator,
86
+ unions,
87
+ }: UnionTypeDef,
88
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
89
+ o1: any,
90
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
91
+ o2: any,
92
+ ) {
93
+ if (discriminator != null) {
94
+ return o1[discriminator] === o2[discriminator] && internalEquals(unions[o1[discriminator]], o1, o2)
95
+ }
96
+ const allTypeDefs = Object.values<TypeDef>(unions)
97
+ const variableTypeDefs = allTypeDefs.filter(function (typeDef: TypeDef) {
98
+ return typeDef.type !== TypeDefType.Literal || typeDef.valuePrototype == null
99
+ })
100
+ return o1 === o2 || variableTypeDefs.length === 1 && internalEquals(variableTypeDefs[0], o1, o2)
101
+ }