@asd14/m 9.0.0 → 10.0.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/dist/src/debounce/debounce.d.ts +13 -1
- package/dist/src/debounce/debounce.js +27 -20
- package/dist/src/distinct/by/distinct-by.d.ts +3 -0
- package/dist/src/distinct/by/distinct-by.js +3 -0
- package/dist/src/distinct/distinct.js +12 -3
- package/dist/src/drop/drop.d.ts +38 -0
- package/dist/src/drop/drop.js +56 -0
- package/dist/src/escape/escape.js +6 -1
- package/dist/src/escape/reg-exp/escape-reg-exp.d.ts +3 -2
- package/dist/src/escape/reg-exp/escape-reg-exp.js +11 -4
- package/dist/src/flatten/flatten.d.ts +7 -0
- package/dist/src/flatten/flatten.js +14 -1
- package/dist/src/fuzzy-match/fuzzy-match.d.ts +3 -0
- package/dist/src/fuzzy-match/fuzzy-match.js +91 -55
- package/dist/src/group/group-by.d.ts +19 -7
- package/dist/src/group/group-by.js +20 -2
- package/dist/src/index-by/index-by.d.ts +70 -0
- package/dist/src/index-by/index-by.js +82 -0
- package/dist/src/index.d.ts +12 -1
- package/dist/src/index.js +12 -9
- package/dist/src/intersect/by/intersect-by.d.ts +48 -0
- package/dist/src/intersect/by/intersect-by.js +75 -0
- package/dist/src/intersect/intersect.d.ts +42 -0
- package/dist/src/intersect/intersect.js +63 -0
- package/dist/src/is-deep-equal/is-deep-equal.d.ts +20 -5
- package/dist/src/is-deep-equal/is-deep-equal.js +44 -7
- package/dist/src/is-uri/is-uri.js +12 -9
- package/dist/src/max/by/max-by.d.ts +52 -0
- package/dist/src/max/by/max-by.js +69 -0
- package/dist/src/max/max.d.ts +34 -0
- package/dist/src/max/max.js +49 -0
- package/dist/src/merge/merge.d.ts +5 -1
- package/dist/src/merge/merge.js +12 -6
- package/dist/src/min/by/min-by.d.ts +52 -0
- package/dist/src/min/by/min-by.js +69 -0
- package/dist/src/min/min.d.ts +34 -0
- package/dist/src/min/min.js +49 -0
- package/dist/src/pick/pick.js +7 -0
- package/dist/src/read/read.d.ts +1 -0
- package/dist/src/read/read.js +1 -0
- package/dist/src/take/last/take-last.d.ts +38 -0
- package/dist/src/take/last/take-last.js +56 -0
- package/dist/src/take/take.d.ts +38 -0
- package/dist/src/take/take.js +56 -0
- package/dist/src/throttle/throttle.d.ts +11 -1
- package/dist/src/throttle/throttle.js +49 -43
- package/dist/src/unite/by/unite-by.d.ts +3 -0
- package/dist/src/unite/by/unite-by.js +3 -0
- package/dist/src/update/update.d.ts +2 -2
- package/dist/src/utils/build-patch.js +3 -1
- package/dist/src/utils/parse-timer-options.d.ts +22 -0
- package/dist/src/utils/parse-timer-options.js +44 -0
- package/dist/src/utils/set-own.d.ts +5 -4
- package/dist/src/utils/set-own.js +6 -5
- package/dist/src/values/values.d.ts +42 -0
- package/dist/src/values/values.js +46 -0
- package/dist/src/write/write.d.ts +53 -0
- package/dist/src/write/write.js +104 -0
- package/dist/src/zip-to-object/zip-to-object.js +7 -0
- package/dist/types/object.d.ts +53 -4
- package/docs/SPEC.md +8 -7
- package/docs/how-to/prototype-pollution-tests.md +156 -0
- package/docs/how-to/tape-runtime-tests.md +30 -21
- package/docs/how-to/tstyche-type-tests.md +29 -14
- package/package.json +8 -5
- package/dist/src/evolve/deep-by/evolve-deep-by.d.ts +0 -65
- package/dist/src/evolve/deep-by/evolve-deep-by.js +0 -90
package/dist/types/object.d.ts
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
import { Property } from "./basic.js";
|
|
2
|
+
/**
|
|
3
|
+
* Widen a literal type, as a `let` does: `"done"` to `string`, `0` to
|
|
4
|
+
* `number`, `true` to `boolean`. Any other type stays as it is.
|
|
5
|
+
*/
|
|
6
|
+
type Widen<V> = V extends string ? string : V extends number ? number : V extends boolean ? boolean : V;
|
|
7
|
+
/**
|
|
8
|
+
* The type a written value gives its field: the field's own type when the
|
|
9
|
+
* value fits it, else the value's widened type.
|
|
10
|
+
*/
|
|
11
|
+
type Fit<Current, V> = [V] extends [Current] ? Current : Widen<V>;
|
|
2
12
|
export type PlainObject = Record<string, unknown>;
|
|
3
13
|
/**
|
|
4
14
|
* Merge two types, with properties from U overriding properties from T.
|
|
@@ -56,15 +66,54 @@ export type DeepPartial<T> = T extends object ? {
|
|
|
56
66
|
*/
|
|
57
67
|
export type DeepValue<T, Path extends readonly Property[]> = Path extends readonly [infer First, ...infer Rest] ? First extends keyof T ? Rest extends readonly Property[] ? DeepValue<T[First], Rest> : T[First] : undefined : T;
|
|
58
68
|
/**
|
|
59
|
-
* Resolve the type of
|
|
69
|
+
* Resolve the type of T after writing V at a given path. A missing step
|
|
70
|
+
* becomes an array before an index segment, and an object before any other.
|
|
71
|
+
* An index on an array widens its items to the old item or the written one.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* DeepWrite<{ id: number; status: "todo" | "done" }, ["status"], "done">
|
|
75
|
+
* // => { id: number; status: "todo" | "done" }, the value fits the field
|
|
76
|
+
*
|
|
77
|
+
* DeepWrite<{ id: number; name: string }, ["name"], boolean>
|
|
78
|
+
* // => { id: number; name: boolean }, it does not
|
|
79
|
+
*
|
|
80
|
+
* DeepWrite<{ id: number }, ["tag"], "x">
|
|
81
|
+
* // => { id: number; tag: string }, a new key widens the literal
|
|
82
|
+
*
|
|
83
|
+
* DeepWrite<{ a: { b: number; c: string } }, ["a", "b"], string>
|
|
84
|
+
* // => { a: { b: string; c: string } }
|
|
85
|
+
*
|
|
86
|
+
* DeepWrite<{}, ["tags", 0], string>
|
|
87
|
+
* // => { tags: string[] }
|
|
88
|
+
*
|
|
89
|
+
* DeepWrite<number[], [0], string>
|
|
90
|
+
* // => (number | string)[]
|
|
91
|
+
*/
|
|
92
|
+
export type DeepWrite<T, Path extends readonly Property[], V> = Path extends readonly [
|
|
93
|
+
infer First extends Property,
|
|
94
|
+
...infer Rest extends readonly Property[]
|
|
95
|
+
] ? T extends readonly (infer Item)[] ? First extends number ? Rest extends readonly [] ? [V] extends [Item] ? Item[] : (Item | Widen<V>)[] : (Item | DeepWrite<Item, Rest, V>)[] : unknown : {
|
|
96
|
+
[K in keyof T as [K] extends [First] ? [First] extends [K] ? never : K : K]: T[K];
|
|
97
|
+
} & {
|
|
98
|
+
[K in First]: Rest extends readonly [] ? First extends keyof T ? Fit<T[First], V> : Widen<V> : DeepWrite<First extends keyof T ? Exclude<T[First], undefined> | (undefined extends T[First] ? Rest extends readonly [number, ...unknown[]] ? [] : object : never) : Rest extends readonly [number, ...unknown[]] ? [] : object, Rest, V>;
|
|
99
|
+
} extends infer O ? {
|
|
100
|
+
[K in keyof O]: O[K];
|
|
101
|
+
} : never : unknown;
|
|
102
|
+
/**
|
|
103
|
+
* Resolve the type of all leaf values of an object (union). An array is not a
|
|
104
|
+
* leaf: its items are. A recursive type, eg. a tree, stops after 10 levels,
|
|
105
|
+
* which loses no leaf type, since each level adds the same ones
|
|
60
106
|
*
|
|
61
107
|
* @example
|
|
62
108
|
* DeepValues<{ a: number, b: string }>
|
|
63
109
|
* // => number | string
|
|
64
110
|
*
|
|
65
111
|
* DeepValues<{ a: number, b: { c: string[] } }>
|
|
66
|
-
* // => number | string
|
|
112
|
+
* // => number | string
|
|
67
113
|
*/
|
|
68
|
-
export type DeepValues<T extends PlainObject> = {
|
|
69
|
-
[K in keyof T]: T[K] extends
|
|
114
|
+
export type DeepValues<T extends PlainObject, Depth extends unknown[] = []> = Depth["length"] extends 10 ? never : {
|
|
115
|
+
[K in keyof T]: T[K] extends infer Value ? Value extends readonly unknown[] ? DeepValues<{
|
|
116
|
+
item: Value[number];
|
|
117
|
+
}, [...Depth, unknown]> : Value extends PlainObject ? DeepValues<Value, [...Depth, unknown]> : Value : never;
|
|
70
118
|
}[keyof T];
|
|
119
|
+
export {};
|
package/docs/SPEC.md
CHANGED
|
@@ -106,12 +106,12 @@ update({ tags: () => [] }, todo)
|
|
|
106
106
|
|
|
107
107
|
Each concept follows the same rule in every function:
|
|
108
108
|
|
|
109
|
-
| Concept | Rule
|
|
110
|
-
| -------------------- |
|
|
111
|
-
| A predicate passes | when it returns `=== true`
|
|
112
|
-
| Two values are equal | SameValueZero, as `Array.prototype.includes`
|
|
113
|
-
| A key | an own key, read with `Object.hasOwn`
|
|
114
|
-
| An object | a plain object: its prototype is `Object.prototype` or null
|
|
109
|
+
| Concept | Rule |
|
|
110
|
+
| -------------------- | ----------------------------------------------------------------------------------- |
|
|
111
|
+
| A predicate passes | when it returns `=== true` |
|
|
112
|
+
| Two values are equal | SameValueZero, as `Array.prototype.includes` |
|
|
113
|
+
| A key | an own key, read with `Object.hasOwn`, `__proto__` too. The prototype never changes |
|
|
114
|
+
| An object | a plain object: its prototype is `Object.prototype` or null |
|
|
115
115
|
|
|
116
116
|
```ts
|
|
117
117
|
// Bad: remove compares with `===`, while distinct uses SameValueZero
|
|
@@ -182,7 +182,8 @@ isPlainObject([])
|
|
|
182
182
|
### An error names the function and the path
|
|
183
183
|
|
|
184
184
|
A `TypeError` names the public function, and the path to the bad part, eg. an
|
|
185
|
-
item of a pipe array or a key of a patcher.
|
|
185
|
+
item of a pipe array or a key of a patcher. A callback's result is named as its
|
|
186
|
+
call, eg. `'fn(input[0])'`.
|
|
186
187
|
|
|
187
188
|
```ts
|
|
188
189
|
// Bad: the engine's error, from inside the pipe
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# Prototype pollution tests
|
|
2
|
+
|
|
3
|
+
Tests are the primary way users learn how a function works. For a function that
|
|
4
|
+
writes keys from input, eg. `write`, `merge` or `groupBy`, they also show the
|
|
5
|
+
one thing a user cannot see from a call: what happens when the input comes from
|
|
6
|
+
an attacker. A request body can carry a key named `__proto__`, and an assignment
|
|
7
|
+
of that key replaces the prototype of the object it lands on.
|
|
8
|
+
|
|
9
|
+
m gives such a key no special meaning: `__proto__` and `constructor` are
|
|
10
|
+
ordinary own keys, and the prototype never changes. A test of a function that
|
|
11
|
+
writes keys shows that, for each payload below. The reader should walk away
|
|
12
|
+
knowing:
|
|
13
|
+
|
|
14
|
+
- **Which payloads reach a prototype** — and that the function stops each one
|
|
15
|
+
- **What the function returns instead** — an own key they can read back, or an
|
|
16
|
+
error that names the bad part
|
|
17
|
+
|
|
18
|
+
## The payloads
|
|
19
|
+
|
|
20
|
+
| Payload | Reaches the prototype through |
|
|
21
|
+
| :-------------------------------------- | :----------------------------------------------- |
|
|
22
|
+
| `JSON.parse('{"__proto__": {"x": 1}}')` | an own `__proto__` key, assigned to the result |
|
|
23
|
+
| `"__proto__.x"`, `["__proto__", "x"]` | a path segment |
|
|
24
|
+
| `["constructor", "prototype", "x"]` | the constructor's prototype |
|
|
25
|
+
| `[["__proto__"]]`, a key in an array | a key that is not a string, coerced at the write |
|
|
26
|
+
| `"constructor"`, prototype frozen | an assignment to a read-only inherited key |
|
|
27
|
+
|
|
28
|
+
Test each payload the function can receive: a path payload for `write` and
|
|
29
|
+
`unflatten`, a key payload for `zipToObject` and `pick`, a JSON payload for
|
|
30
|
+
every function that copies the keys of an object.
|
|
31
|
+
|
|
32
|
+
## Full example
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
// write.test.ts
|
|
36
|
+
import test from "tape"
|
|
37
|
+
|
|
38
|
+
import { write } from "./write.js"
|
|
39
|
+
import { read } from "../read/read.js"
|
|
40
|
+
|
|
41
|
+
test("write - prototype pollution", t => {
|
|
42
|
+
{
|
|
43
|
+
const result = write(["__proto__", "x"], 1, {})
|
|
44
|
+
|
|
45
|
+
t.deepEqual(
|
|
46
|
+
Object.keys(result),
|
|
47
|
+
["__proto__"],
|
|
48
|
+
"given [a __proto__ segment] should [write an own key]"
|
|
49
|
+
)
|
|
50
|
+
t.equal(
|
|
51
|
+
Object.getPrototypeOf(result),
|
|
52
|
+
Object.prototype,
|
|
53
|
+
"given [a __proto__ segment] should [keep the prototype]"
|
|
54
|
+
)
|
|
55
|
+
t.equal(
|
|
56
|
+
"x" in {},
|
|
57
|
+
false,
|
|
58
|
+
"given [a __proto__ segment] should [not pollute other objects]"
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
{
|
|
63
|
+
const result = write("constructor", 2, {})
|
|
64
|
+
|
|
65
|
+
t.equal(
|
|
66
|
+
result.constructor,
|
|
67
|
+
2,
|
|
68
|
+
"given [the key constructor] should [read back the written value with a dot]"
|
|
69
|
+
)
|
|
70
|
+
t.equal(
|
|
71
|
+
read("constructor")(result),
|
|
72
|
+
2,
|
|
73
|
+
"given [the key constructor] should [read back the written value with read]"
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
t.end()
|
|
78
|
+
})
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Rules
|
|
82
|
+
|
|
83
|
+
### Assert four facts per payload
|
|
84
|
+
|
|
85
|
+
| Fact | Assert |
|
|
86
|
+
| :---------------------- | :----------------------------------------------------- |
|
|
87
|
+
| the key is an own key | `Object.keys(result)`, or `Object.hasOwn(result, key)` |
|
|
88
|
+
| it reads back | with a dot, with brackets and with `read` |
|
|
89
|
+
| the prototype stays | `Object.getPrototypeOf(result) === Object.prototype` |
|
|
90
|
+
| no other object changed | a fresh `{}` lacks the payload's key, eg. `"x" in {}` |
|
|
91
|
+
|
|
92
|
+
One fact alone misses a case. A result with a replaced prototype still reads
|
|
93
|
+
`result.x` as `1`, so only the prototype check catches it.
|
|
94
|
+
|
|
95
|
+
### Expose before you fix
|
|
96
|
+
|
|
97
|
+
Run a new test against the code before the fix, and see it fail. A test that
|
|
98
|
+
passes on both sides proves nothing: an assignment to `constructor` already
|
|
99
|
+
creates an own key while `Object.prototype` is writable, so that test exposes a
|
|
100
|
+
bug only with a frozen prototype.
|
|
101
|
+
|
|
102
|
+
### A frozen prototype gets its own file
|
|
103
|
+
|
|
104
|
+
`Object.freeze(Object.prototype)` reaches every test that runs after it in the
|
|
105
|
+
same process. `node --test` runs each file in its own process, so put the freeze
|
|
106
|
+
in a file of its own, next to the function:
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
// set-own.frozen-prototype.test.ts
|
|
110
|
+
import test from "tape"
|
|
111
|
+
|
|
112
|
+
import { _setOwn } from "./set-own.js"
|
|
113
|
+
import type { PlainObject } from "../../types/object.js"
|
|
114
|
+
|
|
115
|
+
// An app can freeze Object.prototype to stop pollution. An assignment to a key
|
|
116
|
+
// the prototype has, eg. `constructor`, then throws. This file runs in its own
|
|
117
|
+
// process, so the freeze reaches no other test
|
|
118
|
+
Object.freeze(Object.prototype)
|
|
119
|
+
|
|
120
|
+
test("_setOwn - frozen Object.prototype", t => {
|
|
121
|
+
const result: PlainObject = {}
|
|
122
|
+
|
|
123
|
+
_setOwn(result, "constructor", "admin")
|
|
124
|
+
|
|
125
|
+
t.deepEqual(
|
|
126
|
+
result,
|
|
127
|
+
{ constructor: "admin" },
|
|
128
|
+
"given [a key of a frozen Object.prototype] should [set an own key, not throw]"
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
t.end()
|
|
132
|
+
})
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Write keys with `_setOwn`, never `Object.assign`
|
|
136
|
+
|
|
137
|
+
`Object.assign` and a `for…in` loop that assigns each key reach the `__proto__`
|
|
138
|
+
setter, so an own `__proto__` key from `JSON.parse` replaces the prototype of
|
|
139
|
+
the target. A spread, `{ ...object }`, and `Object.fromEntries` define keys
|
|
140
|
+
instead, and so does `_setOwn`, which every function of m writes through:
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
const body = JSON.parse('{"__proto__": {"isAdmin": true}}')
|
|
144
|
+
|
|
145
|
+
Object.assign({}, body).isAdmin
|
|
146
|
+
// => true: the copy's prototype is the attacker's object
|
|
147
|
+
|
|
148
|
+
merge({}, body).isAdmin
|
|
149
|
+
// => undefined: `merge` writes through _setOwn, so "__proto__" is an own key
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Build the result through the function, never by hand
|
|
153
|
+
|
|
154
|
+
Test the object the function returns. An object built with `Object.create` or a
|
|
155
|
+
cast, eg. `Object.create(frozen) as PlainObject`, is one m never writes into, so
|
|
156
|
+
its test proves nothing about m.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# Runtime tests (Tape)
|
|
2
2
|
|
|
3
3
|
Tests are the primary way users learn how a function works. Before reading JSDoc
|
|
4
|
-
or source code, they look at
|
|
4
|
+
or source code, they look at the files in `test/` folders to understand calling
|
|
5
5
|
conventions, edge cases, and how primitives compose together.
|
|
6
6
|
|
|
7
7
|
Write every test as if it's a usage example in a tutorial. The reader should
|
|
@@ -16,16 +16,18 @@ walk away knowing:
|
|
|
16
16
|
|
|
17
17
|
## Full example
|
|
18
18
|
|
|
19
|
+
A function's tests live in `src/partition/test/`, one file per group:
|
|
20
|
+
|
|
19
21
|
```ts
|
|
20
|
-
// partition
|
|
22
|
+
// src/partition/test/valid.ts
|
|
21
23
|
import test from "tape"
|
|
22
24
|
|
|
23
|
-
import {
|
|
24
|
-
import {
|
|
25
|
+
import { isSomething } from "../../is/is.js"
|
|
26
|
+
import { partition } from "../partition.js"
|
|
25
27
|
|
|
26
28
|
const isEven = (input: number): boolean => input % 2 === 0
|
|
27
29
|
|
|
28
|
-
test("partition -
|
|
30
|
+
test("partition - valid", t => {
|
|
29
31
|
t.deepEqual(
|
|
30
32
|
partition(isEven, [1, 2, 3, 4, 5]),
|
|
31
33
|
[
|
|
@@ -54,10 +56,7 @@ test("partition - uncurried", t => {
|
|
|
54
56
|
)
|
|
55
57
|
}
|
|
56
58
|
|
|
57
|
-
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
test("partition - curried", t => {
|
|
59
|
+
// Curried
|
|
61
60
|
t.deepEqual(
|
|
62
61
|
partition(isEven)([1, 2, 3, 4, 5]),
|
|
63
62
|
[
|
|
@@ -69,8 +68,15 @@ test("partition - curried", t => {
|
|
|
69
68
|
|
|
70
69
|
t.end()
|
|
71
70
|
})
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
// src/partition/test/invalid.ts
|
|
75
|
+
import test from "tape"
|
|
76
|
+
|
|
77
|
+
import { partition } from "../partition.js"
|
|
72
78
|
|
|
73
|
-
test("partition - invalid
|
|
79
|
+
test("partition - invalid", t => {
|
|
74
80
|
t.throws(
|
|
75
81
|
() => {
|
|
76
82
|
// @ts-expect-error -- testing runtime validation
|
|
@@ -97,17 +103,20 @@ test("partition - invalid input", t => {
|
|
|
97
103
|
|
|
98
104
|
### Grouping
|
|
99
105
|
|
|
100
|
-
|
|
106
|
+
A function's tests live in `test/valid.ts`, `test/edge.ts` and
|
|
107
|
+
`test/invalid.ts`, without `.test` in the name. Each file holds one top-level
|
|
108
|
+
`test("fn - group")`:
|
|
101
109
|
|
|
102
|
-
| Group
|
|
103
|
-
|
|
|
104
|
-
| `fn -
|
|
105
|
-
| `fn -
|
|
106
|
-
| `fn - invalid
|
|
107
|
-
| `fn - aliases`
|
|
110
|
+
| Group | When to include |
|
|
111
|
+
| -------------- | ---------------------------------------------------------------------------------------------------------------- |
|
|
112
|
+
| `fn - valid` | Always — every call with input of the right type: uncurried calls first, then curried ones and calls in a `pipe` |
|
|
113
|
+
| `fn - edge` | When the function has edge cases worth their own group |
|
|
114
|
+
| `fn - invalid` | Always — every call that throws, with its message. Covers both `fn` and `input` validation |
|
|
115
|
+
| `fn - aliases` | When the function has aliases (`zipToObj`, `size`, etc.) |
|
|
108
116
|
|
|
109
|
-
Order matters:
|
|
110
|
-
|
|
117
|
+
Order matters: valid first (simplest call first, then composition), edge second,
|
|
118
|
+
invalid last (error contract). Mark the part of a group that comes from another
|
|
119
|
+
form with a one-line comment, eg. `// Curried` or `// In a pipe`.
|
|
111
120
|
|
|
112
121
|
### Description format
|
|
113
122
|
|
|
@@ -125,7 +134,7 @@ block. This avoids polluting the outer `test()` scope and lets you reuse
|
|
|
125
134
|
variable names like `items` across blocks:
|
|
126
135
|
|
|
127
136
|
```ts
|
|
128
|
-
test("partition -
|
|
137
|
+
test("partition - valid", t => {
|
|
129
138
|
// simple assertions go directly — the reader sees the basic usage first
|
|
130
139
|
t.deepEqual(partition(isEven, [1, 2, 3]), [[2], [1, 3]], "...")
|
|
131
140
|
|
|
@@ -157,7 +166,7 @@ Do NOT use `{}` blocks for simple one-liner assertions.
|
|
|
157
166
|
|
|
158
167
|
### `@ts-expect-error` only for invalid input
|
|
159
168
|
|
|
160
|
-
Use `@ts-expect-error` exclusively in the
|
|
169
|
+
Use `@ts-expect-error` exclusively in the `invalid` group, where the test
|
|
161
170
|
intentionally passes wrong types to verify runtime validation. Never use it to
|
|
162
171
|
work around type inference issues — if a test needs a cast, the API needs
|
|
163
172
|
fixing.
|
|
@@ -51,10 +51,10 @@ describe("partition", () => {
|
|
|
51
51
|
})
|
|
52
52
|
|
|
53
53
|
test("partition - invalid input", () => {
|
|
54
|
-
// @ts-expect-error Type 'string' is not assignable to type 'number'
|
|
54
|
+
// @ts-expect-error Type 'string' is not assignable to type 'number'
|
|
55
55
|
partition(isEven, [1, "2", 3])
|
|
56
56
|
|
|
57
|
-
// @ts-expect-error Type 'string' is not assignable to type 'number'
|
|
57
|
+
// @ts-expect-error Type 'string' is not assignable to type 'number'
|
|
58
58
|
partition(isEven)([1, "2", 3])
|
|
59
59
|
})
|
|
60
60
|
})
|
|
@@ -104,28 +104,43 @@ The comment `// you only need to type the 1st function...` is intentional — it
|
|
|
104
104
|
teaches the user the minimal annotation strategy instead of letting them guess
|
|
105
105
|
or over-annotate.
|
|
106
106
|
|
|
107
|
+
### The type under test goes in `expect()`
|
|
108
|
+
|
|
109
|
+
Read an assertion left to right: expect this type to be, or to accept, that one.
|
|
110
|
+
Use `toBe` for an exact type, and `toBeAssignableFrom` for what a type accepts:
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
// Good — the test is about PlainObject, so it comes first
|
|
114
|
+
expect<PlainObject>().type.toBeAssignableFrom<{ name: string }>()
|
|
115
|
+
|
|
116
|
+
// Bad — the reader must turn the sentence around to find the subject
|
|
117
|
+
expect<{ name: string }>().type.toBeAssignableTo<PlainObject>()
|
|
118
|
+
```
|
|
119
|
+
|
|
107
120
|
### `@ts-expect-error` must include the error message
|
|
108
121
|
|
|
109
122
|
Put `// @ts-expect-error` on the line above the call that fails, followed by the
|
|
110
|
-
expected TypeScript error. TSTyche matches the
|
|
111
|
-
|
|
112
|
-
|
|
123
|
+
expected TypeScript error. TSTyche matches it from the start of the message, and
|
|
124
|
+
`...` skips any part. Keep the types and the counts, they say what went wrong.
|
|
125
|
+
Skip the words around them, eg. `Argument of` and `parameter of`. The test
|
|
126
|
+
breaks if the error changes, and it tells the user what error to expect when
|
|
127
|
+
they misuse the API:
|
|
113
128
|
|
|
114
129
|
```ts
|
|
115
|
-
// Good —
|
|
116
|
-
// @ts-expect-error
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
// @ts-expect-error Argument of type ... is not assignable to parameter of type 'number'.
|
|
121
|
-
inc("1")
|
|
130
|
+
// Good — the types and the count, nothing else
|
|
131
|
+
// @ts-expect-error ... type 'number' is not assignable to ... type 'object'
|
|
132
|
+
write("name", "bob", 42)
|
|
133
|
+
// @ts-expect-error Expected 2-3 arguments, but got 1
|
|
134
|
+
write("name")
|
|
122
135
|
|
|
123
136
|
// Bad — passes for any error, user learns nothing about what went wrong
|
|
124
137
|
// @ts-expect-error
|
|
125
|
-
|
|
138
|
+
write("name", "bob", 42)
|
|
126
139
|
```
|
|
127
140
|
|
|
128
|
-
`toRaiseError()` is deprecated in TSTyche 7, use the directive instead.
|
|
141
|
+
`toRaiseError()` is deprecated in TSTyche 7, use the directive instead. Prefer
|
|
142
|
+
it over `.not.toBeCallableWith()` too: that one passes for any reason the call
|
|
143
|
+
fails, eg. a missing argument.
|
|
129
144
|
|
|
130
145
|
### Tests mirror realistic usage
|
|
131
146
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
3
|
"name": "@asd14/m",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "10.0.0",
|
|
5
5
|
"description": "Point free style, functional Javascript library with focus on object arrays",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/asd-xiv/m",
|
|
@@ -62,11 +62,11 @@
|
|
|
62
62
|
"postupdate": "npm audit fix",
|
|
63
63
|
"format": "prettier --write --prose-wrap=always . && eslint --fix src",
|
|
64
64
|
"----BUNDLE": "",
|
|
65
|
-
"build:code": "swc src -d 'dist' --ignore '**/*.test.ts,**/*.tst.ts'",
|
|
65
|
+
"build:code": "swc src -d 'dist' --ignore '**/*.test.ts,**/*.tst.ts,**/test/**,**/bench/**'",
|
|
66
66
|
"build:types": "tsc --declaration --emitDeclarationOnly --project ./.tsconfig/code.json",
|
|
67
67
|
"build:docs": "documentation build dist/src/index.js --project-name '@asd14/m' -f html -o dist/docs -c documentation.yml --infer-private '^_'",
|
|
68
68
|
"prebuild": "rm -rf dist/* node_modules/.tmp/tsconfig.*.tsbuildinfo",
|
|
69
|
-
"build": "npm run build:code && npm run build:types
|
|
69
|
+
"build": "npm run build:code && npm run build:types",
|
|
70
70
|
"----LINT": "",
|
|
71
71
|
"lint": "eslint --quiet .",
|
|
72
72
|
"typecheck:code": "tsc --noEmit --project ./.tsconfig/code.json",
|
|
@@ -74,10 +74,11 @@
|
|
|
74
74
|
"typecheck:tests": "tsc --noEmit --project ./.tsconfig/tests.json",
|
|
75
75
|
"typecheck": "npm run typecheck:code && npm run typecheck:config && npm run typecheck:tests",
|
|
76
76
|
"----TEST": "",
|
|
77
|
-
"unit-test": "node --import tsx --test 'src/**/*.test.ts'",
|
|
77
|
+
"unit-test": "node --import tsx --test 'src/**/*.test.ts' 'src/**/test/*.ts'",
|
|
78
78
|
"type-test": "tstyche",
|
|
79
|
+
"bench": "for file in $(find src -path '*/bench/*.ts' ! -name '_*'); do echo \"# $file\"; node --import tsx \"$file\"; done",
|
|
79
80
|
"tdd": "nodemon --watch src --exec 'npm run unit-test && npm run type-test'",
|
|
80
|
-
"coverage": "c8 --100 --exclude='src/**/*.test.ts' npm run unit-test",
|
|
81
|
+
"coverage": "c8 --100 --exclude='src/**/*.test.ts' --exclude='src/**/test/**' npm run unit-test",
|
|
81
82
|
"----PUBLISH": "",
|
|
82
83
|
"release": "semantic-release"
|
|
83
84
|
},
|
|
@@ -91,8 +92,10 @@
|
|
|
91
92
|
"@commitlint/config-conventional": "^21.2.4",
|
|
92
93
|
"@swc/cli": "^0.8.2",
|
|
93
94
|
"@swc/core": "^1.16.22",
|
|
95
|
+
"@types/lodash": "^4.17.27",
|
|
94
96
|
"@types/ramda": "^0.32.0",
|
|
95
97
|
"@types/tape": "^5.8.1",
|
|
98
|
+
"@types/underscore": "^1.13.0",
|
|
96
99
|
"c8": "^12.0.0",
|
|
97
100
|
"chi-squared-test": "^1.1.0",
|
|
98
101
|
"documentation": "^14.0.3",
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import type { DeepValues, PlainObject } from "../../../types/object.js";
|
|
2
|
-
type EvolveDeepByFn = {
|
|
3
|
-
<T extends PlainObject>(fn: (key: string, value: DeepValues<T>, parents: string[]) => DeepValues<T>, input: T): T;
|
|
4
|
-
<T extends PlainObject>(fn: (key: string, value: DeepValues<T>, parents: string[]) => DeepValues<T>): (input: T) => T;
|
|
5
|
-
};
|
|
6
|
-
/**
|
|
7
|
-
* Recursively walk all keys of a plain object, calling `fn` only on leaf
|
|
8
|
-
* values (non-plain-object values). Structure and value types are always
|
|
9
|
-
* preserved — `fn` must return a value of the same type union as the input.
|
|
10
|
-
*
|
|
11
|
-
* Input is never mutated - always returns a new object.
|
|
12
|
-
*
|
|
13
|
-
* @param {Function} fn - Called with `(key, leafValue, parents)` for every leaf; return the new value.
|
|
14
|
-
* `parents` is the ancestor key chain above the current leaf,
|
|
15
|
-
* e.g. `["user", "address"]` when processing `city`
|
|
16
|
-
* @param {Object} input - Plain object to walk
|
|
17
|
-
*
|
|
18
|
-
* @returns {Object} New object with the same structure, leaf values transformed
|
|
19
|
-
*
|
|
20
|
-
* @throws {TypeError} If fn is not a Function or input is not a plain Object
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
* evolveDeepBy(
|
|
24
|
-
* (_key, value) => typeof value === "number" ? value * 2 : value,
|
|
25
|
-
* { a: 1, b: { c: 2 } }
|
|
26
|
-
* )
|
|
27
|
-
* // => { a: 2, b: { c: 4 } }
|
|
28
|
-
*
|
|
29
|
-
* evolveDeepBy((_key, value) => typeof value === "number" ? value * 2 : value)(
|
|
30
|
-
* { a: 1, b: { c: 2 } }
|
|
31
|
-
* )
|
|
32
|
-
* // => { a: 2, b: { c: 4 } }
|
|
33
|
-
*
|
|
34
|
-
* // Use parents to redact all values nested under a sensitive key
|
|
35
|
-
* const SENSITIVE = ["password", "credentials"]
|
|
36
|
-
* const input = {
|
|
37
|
-
* credentials: { user: "alice", pass: "s3cr3t" },
|
|
38
|
-
* name: "Bob"
|
|
39
|
-
* }
|
|
40
|
-
* evolveDeepBy(
|
|
41
|
-
* (key, value, parents) =>
|
|
42
|
-
* SENSITIVE.includes(key) || parents.some(p => SENSITIVE.includes(p))
|
|
43
|
-
* ? "[redacted]"
|
|
44
|
-
* : value,
|
|
45
|
-
* input
|
|
46
|
-
* )
|
|
47
|
-
* // => { credentials: { user: "[redacted]", pass: "[redacted]" }, name: "Bob" }
|
|
48
|
-
*
|
|
49
|
-
* // In a pipe
|
|
50
|
-
* pipe(
|
|
51
|
-
* find(isMatch({ id: 2 })),
|
|
52
|
-
* evolveDeepBy((_key, value) => typeof value === "number" ? value + 1 : value)
|
|
53
|
-
* )([{ id: 1, meta: { n: 1 } }, { id: 2, meta: { n: 5 } }])
|
|
54
|
-
* // => { id: 3, meta: { n: 6 } }
|
|
55
|
-
*
|
|
56
|
-
* @name evolveDeepBy
|
|
57
|
-
* @tag Object
|
|
58
|
-
*
|
|
59
|
-
* @see {@link evolve}
|
|
60
|
-
* @see {@link evolveWith}
|
|
61
|
-
* @see {@link evolveMany}
|
|
62
|
-
*/
|
|
63
|
-
declare const evolveDeepBy: EvolveDeepByFn;
|
|
64
|
-
export { evolveDeepBy };
|
|
65
|
-
export type { EvolveDeepByFn };
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { isPlainObject } from "../../is/is.js";
|
|
2
|
-
import { type } from "../../type/type.js";
|
|
3
|
-
import { _setOwn } from "../../utils/set-own.js";
|
|
4
|
-
const _evolveDeepBy = (fn, input, parents = [], seen = new WeakSet())=>{
|
|
5
|
-
if (!isPlainObject(input)) {
|
|
6
|
-
throw new TypeError(`@asd14/m/evolveDeepBy: expected 'input' to be 'Object', got '${type(input)}'`);
|
|
7
|
-
}
|
|
8
|
-
// `seen` holds the ancestors of `input` only: an object referenced twice is
|
|
9
|
-
// evolved twice, an object that contains itself is left as is
|
|
10
|
-
seen.add(input);
|
|
11
|
-
const result = {};
|
|
12
|
-
for (const [key, value] of Object.entries(input)){
|
|
13
|
-
const evolved = isPlainObject(value) ? seen.has(value) ? value : _evolveDeepBy(fn, value, [
|
|
14
|
-
...parents,
|
|
15
|
-
key
|
|
16
|
-
], seen) : fn(key, value, parents);
|
|
17
|
-
_setOwn(result, key, evolved);
|
|
18
|
-
}
|
|
19
|
-
seen.delete(input);
|
|
20
|
-
return result;
|
|
21
|
-
};
|
|
22
|
-
/**
|
|
23
|
-
* Recursively walk all keys of a plain object, calling `fn` only on leaf
|
|
24
|
-
* values (non-plain-object values). Structure and value types are always
|
|
25
|
-
* preserved — `fn` must return a value of the same type union as the input.
|
|
26
|
-
*
|
|
27
|
-
* Input is never mutated - always returns a new object.
|
|
28
|
-
*
|
|
29
|
-
* @param {Function} fn - Called with `(key, leafValue, parents)` for every leaf; return the new value.
|
|
30
|
-
* `parents` is the ancestor key chain above the current leaf,
|
|
31
|
-
* e.g. `["user", "address"]` when processing `city`
|
|
32
|
-
* @param {Object} input - Plain object to walk
|
|
33
|
-
*
|
|
34
|
-
* @returns {Object} New object with the same structure, leaf values transformed
|
|
35
|
-
*
|
|
36
|
-
* @throws {TypeError} If fn is not a Function or input is not a plain Object
|
|
37
|
-
*
|
|
38
|
-
* @example
|
|
39
|
-
* evolveDeepBy(
|
|
40
|
-
* (_key, value) => typeof value === "number" ? value * 2 : value,
|
|
41
|
-
* { a: 1, b: { c: 2 } }
|
|
42
|
-
* )
|
|
43
|
-
* // => { a: 2, b: { c: 4 } }
|
|
44
|
-
*
|
|
45
|
-
* evolveDeepBy((_key, value) => typeof value === "number" ? value * 2 : value)(
|
|
46
|
-
* { a: 1, b: { c: 2 } }
|
|
47
|
-
* )
|
|
48
|
-
* // => { a: 2, b: { c: 4 } }
|
|
49
|
-
*
|
|
50
|
-
* // Use parents to redact all values nested under a sensitive key
|
|
51
|
-
* const SENSITIVE = ["password", "credentials"]
|
|
52
|
-
* const input = {
|
|
53
|
-
* credentials: { user: "alice", pass: "s3cr3t" },
|
|
54
|
-
* name: "Bob"
|
|
55
|
-
* }
|
|
56
|
-
* evolveDeepBy(
|
|
57
|
-
* (key, value, parents) =>
|
|
58
|
-
* SENSITIVE.includes(key) || parents.some(p => SENSITIVE.includes(p))
|
|
59
|
-
* ? "[redacted]"
|
|
60
|
-
* : value,
|
|
61
|
-
* input
|
|
62
|
-
* )
|
|
63
|
-
* // => { credentials: { user: "[redacted]", pass: "[redacted]" }, name: "Bob" }
|
|
64
|
-
*
|
|
65
|
-
* // In a pipe
|
|
66
|
-
* pipe(
|
|
67
|
-
* find(isMatch({ id: 2 })),
|
|
68
|
-
* evolveDeepBy((_key, value) => typeof value === "number" ? value + 1 : value)
|
|
69
|
-
* )([{ id: 1, meta: { n: 1 } }, { id: 2, meta: { n: 5 } }])
|
|
70
|
-
* // => { id: 3, meta: { n: 6 } }
|
|
71
|
-
*
|
|
72
|
-
* @name evolveDeepBy
|
|
73
|
-
* @tag Object
|
|
74
|
-
*
|
|
75
|
-
* @see {@link evolve}
|
|
76
|
-
* @see {@link evolveWith}
|
|
77
|
-
* @see {@link evolveMany}
|
|
78
|
-
*/ const evolveDeepBy = (...params)=>{
|
|
79
|
-
const fn = params[0];
|
|
80
|
-
if (typeof fn !== "function") {
|
|
81
|
-
throw new TypeError(`@asd14/m/evolveDeepBy: expected 'fn' to be 'Function', got '${type(fn)}'`);
|
|
82
|
-
}
|
|
83
|
-
// @signature (fn) => (input)
|
|
84
|
-
if (params.length === 1) {
|
|
85
|
-
return (input)=>_evolveDeepBy(fn, input);
|
|
86
|
-
}
|
|
87
|
-
// @signature (fn, input)
|
|
88
|
-
return _evolveDeepBy(fn, params[1]);
|
|
89
|
-
};
|
|
90
|
-
export { evolveDeepBy };
|