@augment-vir/core 31.43.1 → 31.43.3

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.
@@ -1,4 +1,5 @@
1
1
  import { diffLines, diffWords } from 'diff';
2
+ import { sortObject } from '../object/object-sort.js';
2
3
  import { stringify } from '../object/stringify.js';
3
4
  import { isRuntimeEnv, RuntimeEnv } from '../runtime-env.js';
4
5
  export function prettyDiff(actual, expected) {
@@ -7,22 +8,26 @@ export function prettyDiff(actual, expected) {
7
8
  const diffFunction = (useLines ? diffLines : diffWords);
8
9
  const expectedString = [
9
10
  bothStrings ? '' : '\n',
10
- stringify(expected, 4),
11
+ stringify(!!expected && typeof expected === 'object' && !Array.isArray(expected)
12
+ ? sortObject(expected)
13
+ : expected, 4),
11
14
  '\n',
12
15
  ].join('');
13
16
  const actualString = [
14
17
  bothStrings ? '' : '\n',
15
- stringify(actual, 4),
18
+ stringify(!!actual && typeof actual === 'object' && !Array.isArray(actual)
19
+ ? sortObject(actual)
20
+ : actual, 4),
16
21
  '\n',
17
22
  ].join('');
18
- const changes = addDiffColors(useLines, diffFunction(expectedString, actualString));
23
+ const changes = addDiffColors(useLines, diffFunction(actualString, expectedString));
19
24
  const useColor = isRuntimeEnv(RuntimeEnv.Node);
20
25
  /* node:coverage ignore next 7: currently only tested in node */
21
26
  const explanationLine = [
22
27
  useColor ? NodeColor.Green : '',
23
- ' +added',
28
+ ' +added (unexpected, added in actual)',
24
29
  useColor ? NodeColor.Red : '',
25
- ' -missing',
30
+ ' -missing (expected, missing from actual)',
26
31
  useColor ? NodeColor.Reset : '',
27
32
  ].join('');
28
33
  return [
@@ -0,0 +1,9 @@
1
+ import { type AnyObject } from './generic-object-type.js';
2
+ /**
3
+ * Creates as new sorted object copied from the the original given object.
4
+ *
5
+ * @category Object
6
+ * @category Package : @augment-vir/common
7
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
8
+ */
9
+ export declare function sortObject<const T extends AnyObject>(original: Readonly<T>): T;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Creates as new sorted object copied from the the original given object.
3
+ *
4
+ * @category Object
5
+ * @category Package : @augment-vir/common
6
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
7
+ */
8
+ export function sortObject(original) {
9
+ return Object.fromEntries(Object.entries(original).sort((a, b) => a[0].localeCompare(b[0])));
10
+ }
@@ -4,12 +4,18 @@
4
4
  * @category Internal
5
5
  */
6
6
  export type BrandedTypeTag = '$_brand_$';
7
+ /**
8
+ * Applies a branding to types for {@link Branded}.
9
+ *
10
+ * @category Internal
11
+ */
12
+ export type Brand<BrandKey extends PropertyKey> = Readonly<Record<BrandedTypeTag, Record<BrandKey, never>>>;
7
13
  /**
8
14
  * Brand any type so that it is no longer assignable to itself. For example, brand a database id
9
15
  * `string` type so that standard strings cannot be assigned to it.
10
16
  *
11
17
  * Largely inspired by the `Tagged` type from the `type-fest` package at
12
- * https://github.com/sindresorhus/type-fest/tree/687a89d94c4403d93ac5cb969ac7f492cee006cb/source
18
+ * https://github.com/sindresorhus/type-fest/blob/687a89d94c4403d93ac5cb969ac7f492cee006cb/source/tagged.d.ts
13
19
  *
14
20
  * @category Type
15
21
  * @example
@@ -22,19 +28,33 @@ export type BrandedTypeTag = '$_brand_$';
22
28
  */
23
29
  export type Branded<OriginalType,
24
30
  /** The key for this brand. Two branded types with the same key will be assignable to each other. */
25
- BrandKey extends PropertyKey> = OriginalType & Readonly<Record<BrandedTypeTag, Record<BrandKey, never>>>;
31
+ BrandKey extends PropertyKey> = OriginalType & Brand<BrandKey>;
26
32
  /**
27
33
  * Unwrap a type brand applied via {@link Branded}.
28
34
  *
29
35
  * Largely inspired by the `Tagged` type from the `type-fest` package at
30
- * https://github.com/sindresorhus/type-fest/tree/687a89d94c4403d93ac5cb969ac7f492cee006cb/source
36
+ * https://github.com/sindresorhus/type-fest/blob/687a89d94c4403d93ac5cb969ac7f492cee006cb/source/tagged.d.ts
31
37
  *
32
38
  * @category Type
33
39
  */
34
- export type UnwrapBrand<BrandedType extends Branded<any, any>> = BrandedType extends Branded<infer OriginalType, any> ? OriginalType : BrandedType;
40
+ export type UnwrapBrand<BrandedType extends Branded<any, any>> = RemoveAllBranding<BrandedType>;
41
+ /**
42
+ * Removes all branding for {@link UnwrapBrand}.
43
+ *
44
+ * Largely inspired by the `RemoveAllTags` type from the `type-fest` package at
45
+ * https://github.com/sindresorhus/type-fest/blob/687a89d94c4403d93ac5cb969ac7f492cee006cb/source/tagged.d.ts
46
+ *
47
+ * @category Internal
48
+ */
49
+ export type RemoveAllBranding<T> = T extends Brand<any> ? {
50
+ [ThisBrand in keyof T[BrandedTypeTag]]: T extends Branded<infer OriginalType, ThisBrand> ? RemoveAllBranding<OriginalType> : never;
51
+ }[keyof T[BrandedTypeTag]] : T;
35
52
  /**
36
53
  * Wrap a value in a brand that matches its original type.
37
54
  *
38
55
  * @category Type
39
56
  */
40
- export declare function applyBrand<Brand extends Branded<any, any>>(value: UnwrapBrand<Brand>): Brand;
57
+ export declare function applyBrand<const NewBrand extends Branded<any, any> = never>(value: string): NewBrand;
58
+ export declare function applyBrand<const NewBrand extends Branded<any, any> = never>(value: string | undefined): NewBrand | undefined;
59
+ export declare function applyBrand<const NewBrand extends Branded<any, any> = never>(value: string | null): NewBrand | null;
60
+ export declare function applyBrand<const NewBrand extends Branded<any, any> = never>(value: string | undefined | null): NewBrand | undefined | null;
@@ -1,8 +1,3 @@
1
- /**
2
- * Wrap a value in a brand that matches its original type.
3
- *
4
- * @category Type
5
- */
6
1
  export function applyBrand(value) {
7
2
  return value;
8
3
  }
package/dist/index.d.ts CHANGED
@@ -15,6 +15,7 @@ export * from './augments/min-max.js';
15
15
  export * from './augments/narrow-type.js';
16
16
  export * from './augments/object/generic-object-type.js';
17
17
  export * from './augments/object/object-keys.js';
18
+ export * from './augments/object/object-sort.js';
18
19
  export * from './augments/object/object-value-types.js';
19
20
  export * from './augments/object/required-keys.js';
20
21
  export * from './augments/object/stringify.js';
package/dist/index.js CHANGED
@@ -15,6 +15,7 @@ export * from './augments/min-max.js';
15
15
  export * from './augments/narrow-type.js';
16
16
  export * from './augments/object/generic-object-type.js';
17
17
  export * from './augments/object/object-keys.js';
18
+ export * from './augments/object/object-sort.js';
18
19
  export * from './augments/object/object-value-types.js';
19
20
  export * from './augments/object/required-keys.js';
20
21
  export * from './augments/object/stringify.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/core",
3
- "version": "31.43.1",
3
+ "version": "31.43.3",
4
4
  "description": "Core augment-vir augments. Use @augment-vir/common instead.",
5
5
  "homepage": "https://github.com/electrovir/augment-vir",
6
6
  "bugs": {