@tolki/types 1.7.0 → 1.8.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/package.json +1 -1
- package/src/arrays.d.ts +2 -2
- package/src/collections.d.ts +11 -0
- package/src/data.d.ts +3 -8
- package/src/objects.d.ts +179 -0
package/package.json
CHANGED
package/src/arrays.d.ts
CHANGED
|
@@ -126,10 +126,10 @@ export type TruthyArray<T extends readonly unknown[]> =
|
|
|
126
126
|
* - a `[key]` single-element tuple, identical to a bare key path, ascending
|
|
127
127
|
* - a `[key, direction]` tuple: `true`/`'asc'`/`"Ascending"` sorts ascending,
|
|
128
128
|
* everything else descending, mirroring `Collection::sortByMany`
|
|
129
|
-
* - a comparator returning a negative, zero, or positive number
|
|
129
|
+
* - a comparator returning a negative, zero, or positive number, or a bool, which sorts as PHP's `usort()` reads one
|
|
130
130
|
*/
|
|
131
131
|
export type SortSpec<TValue> =
|
|
132
132
|
| string
|
|
133
133
|
| readonly [string]
|
|
134
134
|
| readonly [string, boolean | "Ascending" | "Descending" | "asc" | "desc"]
|
|
135
|
-
| ((a: TValue, b: TValue) => number);
|
|
135
|
+
| ((a: TValue, b: TValue) => number | boolean);
|
package/src/collections.d.ts
CHANGED
|
@@ -1,2 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Any object a `Proxy` can wrap.
|
|
3
|
+
*
|
|
4
|
+
* @deprecated Nothing in @tolki reads it; the next major removes it.
|
|
5
|
+
*/
|
|
1
6
|
export type ProxyTarget = object;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A property key a `Proxy` trap receives.
|
|
10
|
+
*
|
|
11
|
+
* @deprecated Nothing in @tolki reads it; the next major removes it.
|
|
12
|
+
*/
|
|
2
13
|
export type PropertyName = string | symbol;
|
package/src/data.d.ts
CHANGED
|
@@ -8,14 +8,9 @@ export type DataItems<TValue, TKey extends PropertyKey = PropertyKey> =
|
|
|
8
8
|
| Record<TKey, TValue>;
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
* Data that may also
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* No `src/` file references it today; it is kept for `@tolki/collection`, whose constructor
|
|
16
|
-
* and `getArrayableItems` accept exactly this set, and it names the shape `@tolki/data`'s
|
|
17
|
-
* type tests measure their own widened rows against. Deleting it would be a breaking change
|
|
18
|
-
* to a published package for no gain, so it stays until that consumer lands.
|
|
11
|
+
* Data that may also come as one of the iterables standing in for a PHP `iterable`: a Map for keyed items, or any
|
|
12
|
+
* other iterable, such as a generator or a Set, for positional items. `@tolki/collection`'s constructor takes it in
|
|
13
|
+
* the row a subclass's own constructor forwards through.
|
|
19
14
|
*/
|
|
20
15
|
export type DataIterableItems<TValue, TKey extends PropertyKey = PropertyKey> =
|
|
21
16
|
| DataItems<TValue, TKey>
|
package/src/objects.d.ts
CHANGED
|
@@ -585,6 +585,185 @@ type FlatLeafValue<T, D extends number> = T extends readonly (infer E)[]
|
|
|
585
585
|
: ObjectFlatValue<ObjectValue<T>, ObjectDepth[D]>
|
|
586
586
|
: T;
|
|
587
587
|
|
|
588
|
+
/**
|
|
589
|
+
* Every value `flatten()` may push for `T` at some depth: `T` itself, or any value below it. At each level a
|
|
590
|
+
* Collection-like value is read through `all()` first, and a `Date`, `RegExp`, `Map`, `Set`, `Promise` or function is
|
|
591
|
+
* kept whole. `ObjectPathValue` cannot stand in: it drops `undefined` and never reads through `all()`.
|
|
592
|
+
*
|
|
593
|
+
* @example
|
|
594
|
+
* FlattenReach<{ a: [1, { b: "x" }] }> // { a: [1, { b: "x" }] } | [1, { b: "x" }] | 1 | { b: "x" } | "x"
|
|
595
|
+
*/
|
|
596
|
+
export type FlattenReach<T, D extends number = 5> = [D] extends [never]
|
|
597
|
+
? unknown
|
|
598
|
+
: T | FlattenItemReach<T, D>;
|
|
599
|
+
|
|
600
|
+
/**
|
|
601
|
+
* The values flattening the item `T` may push, at any depth: those a list or object holds at every level below it,
|
|
602
|
+
* read through `all()` first, or `T` itself when it is a scalar, `Date`, `RegExp`, `Map`, `Set`, `Promise` or function.
|
|
603
|
+
* TypeScript cannot tell a class instance from a plain object, so a class instance item is typed as walked while the
|
|
604
|
+
* runtime keeps it whole.
|
|
605
|
+
*
|
|
606
|
+
* @example
|
|
607
|
+
* FlattenItemReach<number[][]> // number[] | number
|
|
608
|
+
* FlattenItemReach<{ a: 1 }> // 1
|
|
609
|
+
*/
|
|
610
|
+
export type FlattenItemReach<T, D extends number = 5> = FlattenReachOf<
|
|
611
|
+
T extends { all: (...args: never[]) => infer R } ? R : T,
|
|
612
|
+
D
|
|
613
|
+
>;
|
|
614
|
+
|
|
615
|
+
/** What lies below a value `flatten()` has read through `all()`: a list's or object's values, each reached in turn. */
|
|
616
|
+
type FlattenReachOf<T, D extends number> = T extends readonly (infer E)[]
|
|
617
|
+
? FlattenReach<E, ObjectDepth[D]>
|
|
618
|
+
: T extends NonObjectItems | Date | RegExp | Promise<unknown>
|
|
619
|
+
? T
|
|
620
|
+
: T extends object
|
|
621
|
+
? [keyof T] extends [never]
|
|
622
|
+
? unknown
|
|
623
|
+
: FlattenReach<ObjectValue<T>, ObjectDepth[D]>
|
|
624
|
+
: T;
|
|
625
|
+
|
|
626
|
+
/** collapse reads a Collection-like item through all(), as Arr::collapse unwraps a Collection. */
|
|
627
|
+
type CollapseItem<V> = V extends { all: (...args: never[]) => infer R } ? R : V;
|
|
628
|
+
|
|
629
|
+
/**
|
|
630
|
+
* The items whose own entries collapse copies; a Map, Set, Date, RegExp, Promise or scalar is skipped. The runtime
|
|
631
|
+
* skips a class instance too, as PHP skips an object, but a type can't tell one from a plain object.
|
|
632
|
+
*/
|
|
633
|
+
type CollapseEntries<V> = Extract<
|
|
634
|
+
Exclude<V, NonObjectItems | Date | RegExp | Promise<unknown>>,
|
|
635
|
+
object
|
|
636
|
+
>;
|
|
637
|
+
|
|
638
|
+
/** An empty object fits Pick<I, K> only when K is optional in I; distributing checks each shape I may take. */
|
|
639
|
+
type CollapseRequired<I, K extends PropertyKey> = I extends unknown
|
|
640
|
+
? Record<never, never> extends Pick<I, K & keyof I>
|
|
641
|
+
? false
|
|
642
|
+
: true
|
|
643
|
+
: never;
|
|
644
|
+
|
|
645
|
+
/** A key is certain only when every shape I may take requires it; a Date, Map or scalar among them adds nothing. */
|
|
646
|
+
type CollapseAlwaysKeys<I> = [I] extends [CollapseEntries<I>]
|
|
647
|
+
? {
|
|
648
|
+
[K in keyof I]-?: false extends CollapseRequired<I, K> ? never : K;
|
|
649
|
+
}[keyof I]
|
|
650
|
+
: never;
|
|
651
|
+
|
|
652
|
+
/** Only an item under a declared, required key of T is sure to be merged; an index signature may hold none. */
|
|
653
|
+
type CollapseGuaranteed<T> = {
|
|
654
|
+
[P in keyof T]-?: string extends P
|
|
655
|
+
? never
|
|
656
|
+
: number extends P
|
|
657
|
+
? never
|
|
658
|
+
: Record<never, never> extends Pick<T, P>
|
|
659
|
+
? never
|
|
660
|
+
: CollapseAlwaysKeys<CollapseItem<T[P]>>;
|
|
661
|
+
}[keyof T];
|
|
662
|
+
|
|
663
|
+
/** Object.entries skips symbol keys, so collapse never copies one. */
|
|
664
|
+
type CollapseKeys<U> = U extends unknown ? Exclude<keyof U, symbol> : never;
|
|
665
|
+
|
|
666
|
+
/** The last item holding a key wins it, and a union has no order, so the key may hold any of their values. */
|
|
667
|
+
type CollapseValue<U, K extends PropertyKey> = U extends unknown
|
|
668
|
+
? K extends keyof U
|
|
669
|
+
? Required<U>[K]
|
|
670
|
+
: never
|
|
671
|
+
: never;
|
|
672
|
+
|
|
673
|
+
/** A string index signature swallows the literal keys beside it, so that result holds any item's value at any key. */
|
|
674
|
+
type CollapseMerge<U, G> =
|
|
675
|
+
string extends CollapseKeys<U>
|
|
676
|
+
? Record<string, CollapseAnyValue<U>>
|
|
677
|
+
: Simplify<
|
|
678
|
+
{ [K in Extract<CollapseKeys<U>, G>]: CollapseValue<U, K> } & {
|
|
679
|
+
[K in Exclude<CollapseKeys<U>, G>]?: CollapseValue<U, K>;
|
|
680
|
+
}
|
|
681
|
+
>;
|
|
682
|
+
|
|
683
|
+
/** Any value any item holds at any of its keys. */
|
|
684
|
+
type CollapseAnyValue<U> = U extends unknown
|
|
685
|
+
? Required<U>[CollapseKeys<U> & keyof U]
|
|
686
|
+
: never;
|
|
687
|
+
|
|
688
|
+
/**
|
|
689
|
+
* The object `collapse()` merges an object's (or a list's) items into, as `array_merge` does: every key an item may
|
|
690
|
+
* have, required only where an item under a required key must have it, and integer keys renumbered. A list among the
|
|
691
|
+
* items appends under integer keys, so that result holds any key and value.
|
|
692
|
+
*
|
|
693
|
+
* @example
|
|
694
|
+
* CollapsedObject<{ a: { x: 1 }; b: { y: "s" } }> // { x: 1; y: "s" }
|
|
695
|
+
* CollapsedObject<Record<number, { a: number }>> // { a?: number }
|
|
696
|
+
* CollapsedObject<{ a: number[]; b: { y: "s" } }> // Record<string | number, unknown>
|
|
697
|
+
*/
|
|
698
|
+
export type CollapsedObject<T> = [
|
|
699
|
+
Extract<CollapseItem<ObjectValue<T>>, readonly unknown[]>,
|
|
700
|
+
] extends [never]
|
|
701
|
+
? ReindexedObject<
|
|
702
|
+
CollapseMerge<
|
|
703
|
+
CollapseEntries<CollapseItem<ObjectValue<T>>>,
|
|
704
|
+
CollapseGuaranteed<T>
|
|
705
|
+
>
|
|
706
|
+
>
|
|
707
|
+
: Record<string | number, unknown>;
|
|
708
|
+
|
|
709
|
+
/**
|
|
710
|
+
* One row's items, as `mapSpread` reads it: a plain object or a Map stands for a PHP array, whose values spread, and
|
|
711
|
+
* a null row gives none; an unknown or keyless object row may hold any. A type cannot tell a class instance from a
|
|
712
|
+
* plain object, so an instance's fields spread and a plain object's `all` member unwraps, both unlike the runtime.
|
|
713
|
+
*/
|
|
714
|
+
type SpreadRowItems<V> = unknown extends V
|
|
715
|
+
? unknown[]
|
|
716
|
+
: V extends null
|
|
717
|
+
? []
|
|
718
|
+
: V extends readonly unknown[]
|
|
719
|
+
? V
|
|
720
|
+
: V extends (...args: never[]) => unknown
|
|
721
|
+
? [V]
|
|
722
|
+
: V extends { all: (...args: never[]) => infer R }
|
|
723
|
+
? SpreadRowItems<R>
|
|
724
|
+
: V extends ReadonlyMap<unknown, infer M>
|
|
725
|
+
? M[]
|
|
726
|
+
: V extends
|
|
727
|
+
| ReadonlySet<unknown>
|
|
728
|
+
| WeakMap<object, unknown>
|
|
729
|
+
| WeakSet<object>
|
|
730
|
+
| Date
|
|
731
|
+
| RegExp
|
|
732
|
+
| Promise<unknown>
|
|
733
|
+
? [V]
|
|
734
|
+
: V extends object
|
|
735
|
+
? [keyof V] extends [never]
|
|
736
|
+
? unknown[]
|
|
737
|
+
: ObjectValue<V>[]
|
|
738
|
+
: [V];
|
|
739
|
+
|
|
740
|
+
/** Rows of one fixed length zip into one tuple; rows of differing or open length give `false`. */
|
|
741
|
+
type SpreadZip<
|
|
742
|
+
S extends readonly unknown[],
|
|
743
|
+
A extends unknown[] = [],
|
|
744
|
+
> = S["length"] extends A["length"]
|
|
745
|
+
? A
|
|
746
|
+
: A["length"] extends S["length"]
|
|
747
|
+
? false
|
|
748
|
+
: SpreadZip<S, [...A, S[A["length"]]]>;
|
|
749
|
+
|
|
750
|
+
/**
|
|
751
|
+
* The arguments `mapSpread` and `eachSpread` hand their callback for one row: the row's items, then its key.
|
|
752
|
+
* Rows of one fixed length spread as a tuple, so a callback may leave off the key; otherwise the key's position
|
|
753
|
+
* varies, so every argument may be any item or the key.
|
|
754
|
+
*
|
|
755
|
+
* @example
|
|
756
|
+
* SpreadArgs<[number, string], "x"> // [number, string, "x"]
|
|
757
|
+
* SpreadArgs<number[], number> // number[]
|
|
758
|
+
* SpreadArgs<{ all(): string[] }, 0> // (string | 0)[]
|
|
759
|
+
*/
|
|
760
|
+
export type SpreadArgs<TRow, TKey> =
|
|
761
|
+
SpreadRowItems<TRow> extends infer S extends readonly unknown[]
|
|
762
|
+
? SpreadZip<S> extends infer Z extends unknown[]
|
|
763
|
+
? [...Z, TKey]
|
|
764
|
+
: (S[number] | TKey)[]
|
|
765
|
+
: never;
|
|
766
|
+
|
|
588
767
|
/**
|
|
589
768
|
* The object members of a resolved type, falling back to a loose record when
|
|
590
769
|
* it has none — `objectItem` throws for a non-object at runtime.
|