@rohal12/spindle 0.61.4 → 0.61.5
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/pkg/format.js +1 -1
- package/dist/pkg/headless.js +64 -33
- package/dist/pkg/story-variables.js +9 -4
- package/package.json +1 -1
- package/src/class-registry.ts +16 -8
- package/src/components/macros/VarDisplay.tsx +6 -4
- package/src/components/macros/arg-utils.ts +2 -9
- package/src/components/macros/macro-args.ts +1 -1
- package/src/define-macro.ts +2 -2
- package/src/format/format-passage.ts +12 -1
- package/src/saves/format.ts +6 -0
- package/src/structural.ts +57 -32
- package/src/utils/stable-key.ts +8 -5
- package/src/utils/value-kinds.ts +4 -0
package/src/structural.ts
CHANGED
|
@@ -14,6 +14,7 @@ import { hasOwn, setOwn } from './utils/namespace';
|
|
|
14
14
|
import { deleteByPath, getByPath, setByPath } from './utils/object-path';
|
|
15
15
|
import {
|
|
16
16
|
ERROR_HIDDEN_KEYS,
|
|
17
|
+
arrayIndices,
|
|
17
18
|
collectionSize,
|
|
18
19
|
extraKeys,
|
|
19
20
|
isAtomic,
|
|
@@ -182,9 +183,8 @@ export function deepClone<T>(value: T, options: DeepCloneOptions = {}): T {
|
|
|
182
183
|
if (Array.isArray(val)) {
|
|
183
184
|
// Holes stay holes
|
|
184
185
|
const arr = builtin(obj, new Array(val.length) as unknown[]);
|
|
185
|
-
for (
|
|
186
|
-
|
|
187
|
-
}
|
|
186
|
+
for (const key of arrayIndices(val))
|
|
187
|
+
arr[Number(key)] = clone(val[Number(key)]);
|
|
188
188
|
return arr;
|
|
189
189
|
}
|
|
190
190
|
|
|
@@ -360,9 +360,7 @@ export function shareEqual<T>(prev: unknown, curr: T): T {
|
|
|
360
360
|
}
|
|
361
361
|
if (Array.isArray(c) && Array.isArray(p)) {
|
|
362
362
|
const out = collection(new Array(c.length) as unknown[]);
|
|
363
|
-
for (
|
|
364
|
-
if (i in c) out[i] = share(p[i], c[i]);
|
|
365
|
-
}
|
|
363
|
+
for (const key of arrayIndices(c)) out[Number(key)] = shareKey(key);
|
|
366
364
|
return out;
|
|
367
365
|
}
|
|
368
366
|
if (mergesWith(p, c, false)) {
|
|
@@ -521,10 +519,10 @@ function equal(a: unknown, b: unknown, assumed: Pairs): boolean {
|
|
|
521
519
|
if (Array.isArray(a)) {
|
|
522
520
|
const bc = b as unknown[];
|
|
523
521
|
if (a.length !== bc.length) return false;
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
522
|
+
return (
|
|
523
|
+
equalKeys(a, b, assumed, (v) => arrayIndices(v as unknown[])) &&
|
|
524
|
+
equalExtraKeys(a, b, assumed)
|
|
525
|
+
);
|
|
528
526
|
}
|
|
529
527
|
|
|
530
528
|
return equalKeys(a, b, assumed);
|
|
@@ -626,16 +624,21 @@ export function keyChanges(
|
|
|
626
624
|
if (Array.isArray(after)) {
|
|
627
625
|
const b = before as unknown as unknown[];
|
|
628
626
|
const a = after as unknown[];
|
|
629
|
-
|
|
627
|
+
const indices = [...new Set([...arrayIndices(b), ...arrayIndices(a)])].sort(
|
|
628
|
+
(x, y) => Number(x) - Number(y),
|
|
629
|
+
);
|
|
630
|
+
for (const key of indices) {
|
|
631
|
+
const i = Number(key);
|
|
632
|
+
if (i >= Math.min(b.length, a.length)) continue;
|
|
630
633
|
// A hole is no element: not the same as an undefined one
|
|
631
|
-
const key = String(i);
|
|
632
634
|
if (i in b && i in a) compare(key, b[i], a[i]);
|
|
633
635
|
else if (i in b) changes.push({ key, kind: 'deleted', hole: true });
|
|
634
636
|
else if (i in a) changes.push({ key, kind: 'added', after: a[i] });
|
|
635
637
|
}
|
|
636
638
|
let next = b.length;
|
|
637
|
-
for (
|
|
638
|
-
|
|
639
|
+
for (const key of arrayIndices(a)) {
|
|
640
|
+
const i = Number(key);
|
|
641
|
+
if (i < b.length) continue;
|
|
639
642
|
const gap = i - next;
|
|
640
643
|
changes.push({
|
|
641
644
|
key: String(i),
|
|
@@ -645,9 +648,9 @@ export function keyChanges(
|
|
|
645
648
|
});
|
|
646
649
|
next = i + 1;
|
|
647
650
|
}
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
+
// One truncation also removes trailing holes, however large the gap.
|
|
652
|
+
if (a.length < b.length)
|
|
653
|
+
changes.push({ key: String(a.length), kind: 'deleted' });
|
|
651
654
|
// Holes at the end are in no index, only in the length
|
|
652
655
|
if (a.length > next) {
|
|
653
656
|
changes.push({ key: 'length', kind: 'holes', after: a.length - next });
|
|
@@ -824,11 +827,11 @@ export function underChange(
|
|
|
824
827
|
|
|
825
828
|
/**
|
|
826
829
|
* The segment standing for the entry `index` of a Map or Set in a path, or
|
|
827
|
-
* for the `cause` or `errors` of an Error ('c', 'e'), or for
|
|
828
|
-
* typed array or DataView views ('b').
|
|
830
|
+
* for the `message`, `cause` or `errors` of an Error ('m', 'c', 'e'), or for
|
|
831
|
+
* the buffer a typed array or DataView views ('b').
|
|
829
832
|
*/
|
|
830
833
|
interface EntrySegment {
|
|
831
|
-
kind: 'k' | 'v' | 's' | 'c' | 'e' | 'b';
|
|
834
|
+
kind: 'k' | 'v' | 's' | 'm' | 'c' | 'e' | 'b';
|
|
832
835
|
index: number;
|
|
833
836
|
}
|
|
834
837
|
|
|
@@ -847,8 +850,9 @@ export const isEntryKey = (
|
|
|
847
850
|
|
|
848
851
|
/**
|
|
849
852
|
* What a Map, Set, Error or view holds in place of properties (keys and
|
|
850
|
-
* values, members, `cause` and `errors`, the buffer), each with
|
|
851
|
-
* a path (see entryKey); undefined for any
|
|
853
|
+
* values, members, `message`, `cause` and `errors`, the buffer), each with
|
|
854
|
+
* the segment standing for it in a path (see entryKey); undefined for any
|
|
855
|
+
* other value.
|
|
852
856
|
*/
|
|
853
857
|
function entryChildren(value: object): [EntrySegment, unknown][] | undefined {
|
|
854
858
|
const children: [EntrySegment, unknown][] = [];
|
|
@@ -864,10 +868,10 @@ function entryChildren(value: object): [EntrySegment, unknown][] | undefined {
|
|
|
864
868
|
children.push([entryKey('s', index++), member]);
|
|
865
869
|
}
|
|
866
870
|
} else if (value instanceof Error) {
|
|
867
|
-
for (const key of
|
|
871
|
+
for (const key of ERROR_HIDDEN_KEYS) {
|
|
868
872
|
if (hasOwn(value, key)) {
|
|
869
873
|
children.push([
|
|
870
|
-
entryKey(key === 'cause' ? 'c' : 'e', 0),
|
|
874
|
+
entryKey(key === 'message' ? 'm' : key === 'cause' ? 'c' : 'e', 0),
|
|
871
875
|
(value as any)[key],
|
|
872
876
|
]);
|
|
873
877
|
}
|
|
@@ -922,11 +926,17 @@ function setEntryValue(
|
|
|
922
926
|
}
|
|
923
927
|
|
|
924
928
|
/**
|
|
925
|
-
* The own enumerable fields of
|
|
926
|
-
* registered subclass may hold some besides its built-in
|
|
929
|
+
* The own enumerable fields of an Error, Map, Set, Date or RegExp (an
|
|
930
|
+
* instance of a registered subclass may hold some besides its built-in
|
|
931
|
+
* content, #412). Hidden Error data is visited through entryChildren;
|
|
927
932
|
* none for any other value.
|
|
928
933
|
*/
|
|
929
934
|
function builtinFields(value: object): string[] {
|
|
935
|
+
if (value instanceof Error) {
|
|
936
|
+
return Object.keys(value).filter(
|
|
937
|
+
(key) => !ERROR_HIDDEN_KEYS.some((hidden) => hidden === key),
|
|
938
|
+
);
|
|
939
|
+
}
|
|
930
940
|
return value instanceof Map ||
|
|
931
941
|
value instanceof Set ||
|
|
932
942
|
value instanceof Date ||
|
|
@@ -945,16 +955,16 @@ function sameBuiltin(x: object, y: object): boolean {
|
|
|
945
955
|
|
|
946
956
|
/**
|
|
947
957
|
* Every path each object (or array, or other value) of `root` is at, the
|
|
948
|
-
* first appearance first, in depth-first order. Plain objects and arrays
|
|
949
|
-
* entered, where first met only (an
|
|
950
|
-
*
|
|
951
|
-
* of `skip` is left out.
|
|
958
|
+
* first appearance first, in depth-first order. Plain objects and arrays
|
|
959
|
+
* (including non-enumerable indices) are entered, where first met only (an
|
|
960
|
+
* object held in an array is one object, however it was reached); other
|
|
961
|
+
* values are leaves. A path at or below one of `skip` is left out.
|
|
952
962
|
*
|
|
953
963
|
* With `entries`, the objects held in a Map or Set (as key, value or member)
|
|
954
964
|
* are placed too, after everything else: one met nowhere else is at the path
|
|
955
965
|
* of its Map or Set and the entry's segment (see entryKey), which is no path
|
|
956
966
|
* a value can be read at (see getByEntryPath), so only to tell where an
|
|
957
|
-
* object is from; so are the `cause` and `errors` of an Error.
|
|
967
|
+
* object is from; so are the `message`, `cause` and `errors` of an Error.
|
|
958
968
|
*/
|
|
959
969
|
function objectPaths(
|
|
960
970
|
root: object,
|
|
@@ -980,7 +990,9 @@ function objectPaths(
|
|
|
980
990
|
function walk(
|
|
981
991
|
node: Record<string, unknown>,
|
|
982
992
|
path: ObjectPath,
|
|
983
|
-
keys: string[] =
|
|
993
|
+
keys: string[] = Array.isArray(node)
|
|
994
|
+
? [...arrayIndices(node), ...extraKeys(node)]
|
|
995
|
+
: Object.keys(node),
|
|
984
996
|
): void {
|
|
985
997
|
for (const key of keys) {
|
|
986
998
|
const value = node[key];
|
|
@@ -1013,6 +1025,19 @@ function firstPaths(
|
|
|
1013
1025
|
);
|
|
1014
1026
|
}
|
|
1015
1027
|
|
|
1028
|
+
/**
|
|
1029
|
+
* Refuse arrays whose logical length would make downstream slot scans
|
|
1030
|
+
* (including Immer's array freezing) excessive. Inspect populated keys and
|
|
1031
|
+
* collection entries only, sharing the graph walk used for structural diffs.
|
|
1032
|
+
*/
|
|
1033
|
+
export function assertArrayLengths(root: object, maxLength: number): void {
|
|
1034
|
+
for (const value of objectPaths({ root }, undefined, true).keys()) {
|
|
1035
|
+
if (Array.isArray(value) && value.length > maxLength) {
|
|
1036
|
+
throw new RangeError(`Save arrays cannot exceed ${maxLength} elements`);
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1016
1041
|
/**
|
|
1017
1042
|
* The objects `root` holds at more than one path (an object two variables
|
|
1018
1043
|
* refer to, or one that refers to itself), with every path they are at,
|
package/src/utils/stable-key.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { hasOwn } from './namespace';
|
|
|
2
2
|
import { registeredClassName } from '../class-registry';
|
|
3
3
|
import {
|
|
4
4
|
ERROR_HIDDEN_KEYS,
|
|
5
|
+
arrayIndices,
|
|
5
6
|
extraKeys,
|
|
6
7
|
isBoxed,
|
|
7
8
|
isTemporal,
|
|
@@ -42,9 +43,6 @@ export function stableKey(value: unknown): string {
|
|
|
42
43
|
}
|
|
43
44
|
}
|
|
44
45
|
|
|
45
|
-
/** The key of an array slot that holds no element. */
|
|
46
|
-
const HOLE = '<hole>';
|
|
47
|
-
|
|
48
46
|
/**
|
|
49
47
|
* The key of `val`. `seen` numbers the objects keyed so far, in the order
|
|
50
48
|
* they were first met.
|
|
@@ -106,9 +104,14 @@ function keyOf(val: unknown, seen: Map<object, number>): string {
|
|
|
106
104
|
// A hole is no element, not an undefined one (#497), as for deepEqual
|
|
107
105
|
// and a save; a plain `map` would skip it, giving `[,]` the key of `[]`
|
|
108
106
|
const items: string[] = [];
|
|
109
|
-
|
|
110
|
-
|
|
107
|
+
let next = 0;
|
|
108
|
+
for (const index of arrayIndices(val)) {
|
|
109
|
+
const i = Number(index);
|
|
110
|
+
if (i > next) items.push(`<holes ${i - next}>`);
|
|
111
|
+
items.push(keyOf(val[i], seen));
|
|
112
|
+
next = i + 1;
|
|
111
113
|
}
|
|
114
|
+
if (val.length > next) items.push(`<holes ${val.length - next}>`);
|
|
112
115
|
return withFields(`[${items.join(',')}]`);
|
|
113
116
|
}
|
|
114
117
|
const atomic = atomicKey(obj, seen);
|
package/src/utils/value-kinds.ts
CHANGED
|
@@ -53,6 +53,10 @@ export const ERROR_HIDDEN_KEYS = ['message', 'cause', 'errors'] as const;
|
|
|
53
53
|
export const isIndexKey = (key: string): boolean =>
|
|
54
54
|
key !== '4294967295' && String(Number(key) >>> 0) === key;
|
|
55
55
|
|
|
56
|
+
/** Populated own array indices, in order, without visiting holes. */
|
|
57
|
+
export const arrayIndices = (value: unknown[]): string[] =>
|
|
58
|
+
Object.getOwnPropertyNames(value).filter(isIndexKey);
|
|
59
|
+
|
|
56
60
|
/**
|
|
57
61
|
* The own enumerable string keys of a collection (an Array, Map or Set) that
|
|
58
62
|
* are not its elements: what an instance of a subclass may hold besides
|