@asd14/m 9.1.0 → 11.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/escape/escape.js +6 -1
- package/dist/src/escape/html/escape-html.d.ts +17 -2
- package/dist/src/escape/html/escape-html.js +17 -2
- 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 +38 -26
- package/dist/src/flatten/flatten.js +94 -36
- 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 +3 -0
- package/dist/src/group/group-by.js +10 -1
- package/dist/src/index-by/index-by.d.ts +3 -0
- package/dist/src/index-by/index-by.js +10 -1
- package/dist/src/index.d.ts +0 -1
- package/dist/src/index.js +0 -1
- package/dist/src/intersect/by/intersect-by.d.ts +3 -0
- package/dist/src/intersect/by/intersect-by.js +3 -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-match/is-match.d.ts +4 -0
- package/dist/src/is-match/is-match.js +4 -0
- package/dist/src/is-uri/is-uri.js +12 -9
- package/dist/src/merge/merge.d.ts +5 -1
- package/dist/src/merge/merge.js +12 -6
- package/dist/src/pick/pick.js +7 -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/write/write.d.ts +3 -4
- package/dist/src/zip-to-object/zip-to-object.js +7 -0
- package/dist/types/array.d.ts +12 -0
- package/dist/types/object.d.ts +8 -4
- package/dist/types/object.matcher.d.ts +4 -2
- package/docs/SPEC.md +11 -11
- package/docs/how-to/jsdoc.md +33 -14
- package/docs/how-to/prototype-pollution-tests.md +156 -0
- package/docs/how-to/tape-runtime-tests.md +49 -23
- package/docs/how-to/tstyche-type-tests.md +49 -14
- package/docs/how-to/writing-functions.md +26 -0
- package/package.json +10 -7
- 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
|
@@ -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,11 +68,18 @@ 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"
|
|
72
76
|
|
|
73
|
-
|
|
77
|
+
import { partition } from "../partition.js"
|
|
78
|
+
|
|
79
|
+
test("partition - invalid", t => {
|
|
74
80
|
t.throws(
|
|
75
81
|
() => {
|
|
76
|
-
// @ts-expect-error
|
|
82
|
+
// @ts-expect-error No overload matches this call
|
|
77
83
|
partition(42, [1, 2])
|
|
78
84
|
},
|
|
79
85
|
/expected 'fn' to be 'Function' or 'Array', got 'Number'/,
|
|
@@ -82,7 +88,7 @@ test("partition - invalid input", t => {
|
|
|
82
88
|
|
|
83
89
|
t.throws(
|
|
84
90
|
() => {
|
|
85
|
-
// @ts-expect-error
|
|
91
|
+
// @ts-expect-error No overload matches this call
|
|
86
92
|
partition(isEven, "not-an-array")
|
|
87
93
|
},
|
|
88
94
|
/expected 'input' to be 'Array'/,
|
|
@@ -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,11 +166,28 @@ 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.
|
|
164
173
|
|
|
174
|
+
Follow it with the TypeScript error, in the form of the type tests: the types
|
|
175
|
+
and the count, `...` for the rest. The reader sees what TypeScript says before
|
|
176
|
+
the runtime guard throws:
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
// Good
|
|
180
|
+
// @ts-expect-error ... type 'number' is not assignable to ... type 'string'
|
|
181
|
+
escapeHTML(42)
|
|
182
|
+
|
|
183
|
+
// Bad — says why the line is there, not what TypeScript reports
|
|
184
|
+
// @ts-expect-error -- testing runtime validation
|
|
185
|
+
escapeHTML(42)
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
TSTyche checks the message in a `*.tst.ts` file only. In a tape test, `tsc`
|
|
189
|
+
checks that the error exists, not its text.
|
|
190
|
+
|
|
165
191
|
### Runtime error messages reference parameter names
|
|
166
192
|
|
|
167
193
|
Error messages use quoted parameter names so it's clear which argument is wrong.
|
|
@@ -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,63 @@ 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 whole message,
|
|
111
|
-
|
|
112
|
-
|
|
123
|
+
expected TypeScript error. TSTyche matches it against the whole message, with
|
|
124
|
+
every line under it, and `...` skips any part. Keep the types and the counts,
|
|
125
|
+
they say what went wrong. Skip the words around them, eg. `Argument of` and
|
|
126
|
+
`parameter of`. The test breaks if the error changes, and it tells the user what
|
|
127
|
+
error to expect when 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)
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
TypeScript 6 and 7 can word one error differently. Keep only the types both
|
|
142
|
+
name, and write both messages, and what the reader should do, in a comment
|
|
143
|
+
above:
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
// A level flattens an array. To flatten an object, pass a separator,
|
|
147
|
+
// eg. flatten("/")(user). TypeScript names only the types:
|
|
148
|
+
// TS 6: Argument of type '{ a: number; }' is not assignable to
|
|
149
|
+
// parameter of type 'readonly unknown[]'
|
|
150
|
+
// TS 7: Type '{ a: number; }' is missing the following properties
|
|
151
|
+
// from type 'readonly unknown[]': length, concat, join, slice,
|
|
152
|
+
// and 26 more
|
|
153
|
+
// @ts-expect-error ... '{ a: number; }' ... 'readonly unknown[]'
|
|
154
|
+
flatten(1)(user)
|
|
126
155
|
```
|
|
127
156
|
|
|
128
|
-
|
|
157
|
+
When a call matches no overload, TypeScript 7 reports the error of the last
|
|
158
|
+
overload that takes as many arguments as the call passes. Quote that one:
|
|
159
|
+
TypeScript 6 lists it too.
|
|
160
|
+
|
|
161
|
+
`toRaiseError()` is deprecated in TSTyche 7, use the directive instead. Prefer
|
|
162
|
+
it over `.not.toBeCallableWith()` too: that one passes for any reason the call
|
|
163
|
+
fails, eg. a missing argument.
|
|
129
164
|
|
|
130
165
|
### Tests mirror realistic usage
|
|
131
166
|
|
|
@@ -131,6 +131,32 @@ Mark each branch with a `// @signature` comment. These comments are the currying
|
|
|
131
131
|
contract — a reader scanning the file should immediately see all calling
|
|
132
132
|
conventions without parsing the logic.
|
|
133
133
|
|
|
134
|
+
### 2a. Types: one signature per arity
|
|
135
|
+
|
|
136
|
+
Give the `*Fn` type one signature per arity, eg. one uncurried and one curried.
|
|
137
|
+
Merge the overloads that differ only in one argument's type into a union:
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
// Bad: two 3-argument overloads compete. When neither fits, TypeScript 7
|
|
141
|
+
// reports the last one, so `write("name", "bob", 42)` blames the path
|
|
142
|
+
<I, K extends string, V>(path: K, value: V, input: I): DeepWrite<I, [K], V>
|
|
143
|
+
<I, const P extends WritePath, V>(path: P, value: V, input: I): DeepWrite<I, P, V>
|
|
144
|
+
|
|
145
|
+
// Good: one signature, and the error names the wrong argument on TS 6 and 7
|
|
146
|
+
<I, const P extends string | WritePath, V>(path: P, value: V, input: I): DeepWrite<I, ToArray<P>, V>
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
When the return type depends on an argument's type, pick it with a conditional
|
|
150
|
+
type, and end the cases with `never`, eg. `flatten`'s `FlattenInputFor`:
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
type FlattenInputFor<LevelOrSeparator> = LevelOrSeparator extends number
|
|
154
|
+
? readonly unknown[]
|
|
155
|
+
: LevelOrSeparator extends string
|
|
156
|
+
? PlainObject
|
|
157
|
+
: never
|
|
158
|
+
```
|
|
159
|
+
|
|
134
160
|
### 3. Exports
|
|
135
161
|
|
|
136
162
|
Always at the end of the file:
|
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": "11.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,7 +62,7 @@
|
|
|
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",
|
|
@@ -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
|
},
|
|
@@ -87,12 +88,14 @@
|
|
|
87
88
|
"@asd14/prettier-config": "^1.6.0",
|
|
88
89
|
"@asd14/semantic-release-config": "^2.0.0",
|
|
89
90
|
"@asd14/ts-config": "^2.1.0",
|
|
90
|
-
"@commitlint/cli": "^21.
|
|
91
|
-
"@commitlint/config-conventional": "^21.
|
|
91
|
+
"@commitlint/cli": "^21.3.0",
|
|
92
|
+
"@commitlint/config-conventional": "^21.3.0",
|
|
92
93
|
"@swc/cli": "^0.8.2",
|
|
93
94
|
"@swc/core": "^1.16.22",
|
|
95
|
+
"@types/lodash": "^4.17.28",
|
|
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",
|
|
@@ -106,7 +109,7 @@
|
|
|
106
109
|
"semantic-release": "^26.0.1",
|
|
107
110
|
"tape": "^5.11.0",
|
|
108
111
|
"tinybench": "^6.2.5",
|
|
109
|
-
"tstyche": "^
|
|
112
|
+
"tstyche": "^8.0.0-beta.3",
|
|
110
113
|
"tsx": "^4.23.15",
|
|
111
114
|
"typescript": "^6.0.3",
|
|
112
115
|
"underscore": "^1.13.8"
|
|
@@ -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 };
|