@asd14/m 9.0.0 → 9.1.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/drop/drop.d.ts +38 -0
- package/dist/src/drop/drop.js +56 -0
- package/dist/src/group/group-by.d.ts +16 -7
- package/dist/src/group/group-by.js +10 -1
- package/dist/src/index-by/index-by.d.ts +67 -0
- package/dist/src/index-by/index-by.js +73 -0
- package/dist/src/index.d.ts +12 -0
- package/dist/src/index.js +12 -8
- package/dist/src/intersect/by/intersect-by.d.ts +45 -0
- package/dist/src/intersect/by/intersect-by.js +72 -0
- package/dist/src/intersect/intersect.d.ts +42 -0
- package/dist/src/intersect/intersect.js +63 -0
- 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/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/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/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/types/object.d.ts +45 -0
- package/docs/SPEC.md +2 -1
- package/package.json +2 -2
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { isPlainObject } from "../is/is.js";
|
|
2
|
+
import { type } from "../type/type.js";
|
|
3
|
+
/**
|
|
4
|
+
* Get list of own enumerable property values.
|
|
5
|
+
*
|
|
6
|
+
* Returns the items of an array, the values of a plain object in key order.
|
|
7
|
+
* Any other object throws, eg. a `Map`: read its values with
|
|
8
|
+
* `[...map.values()]`.
|
|
9
|
+
*
|
|
10
|
+
* @param {Array|Object} input - Array or Object to extract values from
|
|
11
|
+
*
|
|
12
|
+
* @returns {Array} List of values
|
|
13
|
+
*
|
|
14
|
+
* @throws {TypeError} If input is not an Array or Object
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* values(["lorem", "ipsum"])
|
|
18
|
+
* // => ["lorem", "ipsum"]
|
|
19
|
+
*
|
|
20
|
+
* values({ foo: "bar", lorem: "ipsum" })
|
|
21
|
+
* // => ["bar", "ipsum"]
|
|
22
|
+
*
|
|
23
|
+
* values(new Map([["foo", "bar"]]))
|
|
24
|
+
* // throws "@asd14/m/values: expected 'input' to be 'Array' or 'Object', got 'Map'"
|
|
25
|
+
*
|
|
26
|
+
* // In a pipe - sum the values of an object
|
|
27
|
+
* pipe(
|
|
28
|
+
* values,
|
|
29
|
+
* sum
|
|
30
|
+
* )({ a: 1, b: 2, c: 3 })
|
|
31
|
+
* // => 6
|
|
32
|
+
*
|
|
33
|
+
* @name values
|
|
34
|
+
* @tag Object
|
|
35
|
+
*
|
|
36
|
+
* @see {@link keys}
|
|
37
|
+
*/ const values = (input)=>{
|
|
38
|
+
// A Map, a Set or a class instance throws: `[]` for a Map would look like an
|
|
39
|
+
// answer. Read a Map's values with `[...map.values()]`
|
|
40
|
+
if (!Array.isArray(input) && !isPlainObject(input)) {
|
|
41
|
+
throw new TypeError(`@asd14/m/values: expected 'input' to be 'Array' or 'Object', got '${type(input)}'`);
|
|
42
|
+
}
|
|
43
|
+
const result = Object.values(input);
|
|
44
|
+
return result;
|
|
45
|
+
};
|
|
46
|
+
export { values };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { DeepWrite } from "../../types/object.js";
|
|
2
|
+
type WritePath = readonly [string | number, ...(string | number)[]];
|
|
3
|
+
type WriteValue = string | number | boolean | bigint | symbol | object | null | undefined;
|
|
4
|
+
type WriteFn = {
|
|
5
|
+
<I extends object, const P extends WritePath, V extends WriteValue>(path: P, value: V, input: I): DeepWrite<I, P, V>;
|
|
6
|
+
<const P extends WritePath, V extends WriteValue>(path: P, value: V): <I extends object>(input: I) => DeepWrite<I, P, V>;
|
|
7
|
+
<I extends object, K extends string, V extends WriteValue>(path: K, value: V, input: I): DeepWrite<I, [K], V>;
|
|
8
|
+
<K extends string, V extends WriteValue>(path: K, value: V): <I extends object>(input: I) => DeepWrite<I, [K], V>;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Write a value at a key or a deep path. Creates each missing step: an array
|
|
12
|
+
* for an integer segment, a plain object for any other. Copies only the
|
|
13
|
+
* objects along the path, every other branch keeps its reference.
|
|
14
|
+
*
|
|
15
|
+
* Input is never mutated, always returns a new object or array.
|
|
16
|
+
*
|
|
17
|
+
* @param {string|Array} path - Key, or array of keys and indexes for deep access
|
|
18
|
+
* @param {unknown} value - Value to write, a function included
|
|
19
|
+
* @param {Object|Array} input - Object or array to write into
|
|
20
|
+
*
|
|
21
|
+
* @returns {Object|Array} A copy of `input` with `value` at `path`
|
|
22
|
+
*
|
|
23
|
+
* @throws {TypeError} When a value on the path is neither a plain object nor
|
|
24
|
+
* an array, eg. `expected 'input.a' to be 'Object' or 'Array', got 'Number'`
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* write("name", "bob", { id: 1, name: "alice" })
|
|
28
|
+
* // => { id: 1, name: "bob" }
|
|
29
|
+
*
|
|
30
|
+
* write("name", "bob")({ id: 1 })
|
|
31
|
+
* // => { id: 1, name: "bob" }
|
|
32
|
+
*
|
|
33
|
+
* write(["a", "b"], 2, { a: { b: 1, c: 3 } })
|
|
34
|
+
* // => { a: { b: 2, c: 3 } }
|
|
35
|
+
*
|
|
36
|
+
* write(["tags", 0], "x")({})
|
|
37
|
+
* // => { tags: ["x"] }
|
|
38
|
+
*
|
|
39
|
+
* pipe(
|
|
40
|
+
* write(["user", "name"], "bob"),
|
|
41
|
+
* read(["user", "name"])
|
|
42
|
+
* )({ user: { name: "alice" } })
|
|
43
|
+
* // => "bob"
|
|
44
|
+
*
|
|
45
|
+
* @name write
|
|
46
|
+
* @tag Object
|
|
47
|
+
*
|
|
48
|
+
* @see {@link read}
|
|
49
|
+
* @see {@link update}
|
|
50
|
+
*/
|
|
51
|
+
declare const write: WriteFn;
|
|
52
|
+
export { write };
|
|
53
|
+
export type { WriteFn };
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { isPlainObject } from "../is/is.js";
|
|
2
|
+
import { type } from "../type/type.js";
|
|
3
|
+
import { _setOwn } from "../utils/set-own.js";
|
|
4
|
+
const isIndex = (segment)=>typeof segment === "number" && Number.isInteger(segment) && segment >= 0;
|
|
5
|
+
const shallowCopy = (container)=>Array.isArray(container) ? [
|
|
6
|
+
...container
|
|
7
|
+
] : {
|
|
8
|
+
...container
|
|
9
|
+
};
|
|
10
|
+
const _write = (segments, value, input)=>{
|
|
11
|
+
if (!isPlainObject(input) && !Array.isArray(input)) {
|
|
12
|
+
throw new TypeError(`@asd14/m/write: expected 'input' to be 'Object' or 'Array', got '${type(input)}'`);
|
|
13
|
+
}
|
|
14
|
+
const result = shallowCopy(input);
|
|
15
|
+
const lastIndex = segments.length - 1;
|
|
16
|
+
let node = result;
|
|
17
|
+
let pathName = "input";
|
|
18
|
+
for(let i = 0; i < lastIndex; i++){
|
|
19
|
+
const segment = segments[i];
|
|
20
|
+
const key = String(segment);
|
|
21
|
+
const child = Object.hasOwn(node, key) ? node[key] : undefined;
|
|
22
|
+
let copy;
|
|
23
|
+
pathName += isIndex(segment) ? `[${key}]` : `.${key}`;
|
|
24
|
+
if (child === undefined) {
|
|
25
|
+
copy = isIndex(segments[i + 1]) ? [] : {};
|
|
26
|
+
} else if (isPlainObject(child) || Array.isArray(child)) {
|
|
27
|
+
copy = shallowCopy(child);
|
|
28
|
+
} else {
|
|
29
|
+
throw new TypeError(`@asd14/m/write: expected '${pathName}' to be 'Object' or 'Array', got '${type(child)}'`);
|
|
30
|
+
}
|
|
31
|
+
_setOwn(node, key, copy);
|
|
32
|
+
node = copy;
|
|
33
|
+
}
|
|
34
|
+
_setOwn(node, String(segments[lastIndex]), value);
|
|
35
|
+
return result;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Write a value at a key or a deep path. Creates each missing step: an array
|
|
39
|
+
* for an integer segment, a plain object for any other. Copies only the
|
|
40
|
+
* objects along the path, every other branch keeps its reference.
|
|
41
|
+
*
|
|
42
|
+
* Input is never mutated, always returns a new object or array.
|
|
43
|
+
*
|
|
44
|
+
* @param {string|Array} path - Key, or array of keys and indexes for deep access
|
|
45
|
+
* @param {unknown} value - Value to write, a function included
|
|
46
|
+
* @param {Object|Array} input - Object or array to write into
|
|
47
|
+
*
|
|
48
|
+
* @returns {Object|Array} A copy of `input` with `value` at `path`
|
|
49
|
+
*
|
|
50
|
+
* @throws {TypeError} When a value on the path is neither a plain object nor
|
|
51
|
+
* an array, eg. `expected 'input.a' to be 'Object' or 'Array', got 'Number'`
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* write("name", "bob", { id: 1, name: "alice" })
|
|
55
|
+
* // => { id: 1, name: "bob" }
|
|
56
|
+
*
|
|
57
|
+
* write("name", "bob")({ id: 1 })
|
|
58
|
+
* // => { id: 1, name: "bob" }
|
|
59
|
+
*
|
|
60
|
+
* write(["a", "b"], 2, { a: { b: 1, c: 3 } })
|
|
61
|
+
* // => { a: { b: 2, c: 3 } }
|
|
62
|
+
*
|
|
63
|
+
* write(["tags", 0], "x")({})
|
|
64
|
+
* // => { tags: ["x"] }
|
|
65
|
+
*
|
|
66
|
+
* pipe(
|
|
67
|
+
* write(["user", "name"], "bob"),
|
|
68
|
+
* read(["user", "name"])
|
|
69
|
+
* )({ user: { name: "alice" } })
|
|
70
|
+
* // => "bob"
|
|
71
|
+
*
|
|
72
|
+
* @name write
|
|
73
|
+
* @tag Object
|
|
74
|
+
*
|
|
75
|
+
* @see {@link read}
|
|
76
|
+
* @see {@link update}
|
|
77
|
+
*/ const write = (...params)=>{
|
|
78
|
+
const path = params[0];
|
|
79
|
+
if (typeof path !== "string" && !Array.isArray(path)) {
|
|
80
|
+
throw new TypeError(`@asd14/m/write: expected 'path' to be 'String' or 'Array', got '${type(path)}'`);
|
|
81
|
+
}
|
|
82
|
+
if (Array.isArray(path) && path.length === 0) {
|
|
83
|
+
throw new TypeError("@asd14/m/write: expected 'path' to be 'NonEmpty', got 'Empty'");
|
|
84
|
+
}
|
|
85
|
+
if (params.length === 1) {
|
|
86
|
+
throw new TypeError("@asd14/m/write: expected 'value' to be 'Any', got 'Missing'");
|
|
87
|
+
}
|
|
88
|
+
const segments = Array.isArray(path) ? path : [
|
|
89
|
+
path
|
|
90
|
+
];
|
|
91
|
+
for(let i = 0, length = segments.length; i < length; i++){
|
|
92
|
+
const segment = segments[i];
|
|
93
|
+
if (typeof segment !== "string" && typeof segment !== "number") {
|
|
94
|
+
throw new TypeError(`@asd14/m/write: expected 'path[${i}]' to be 'String' or 'Number', got '${type(segment)}'`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
// @signature (path, value) => (input)
|
|
98
|
+
if (params.length === 2) {
|
|
99
|
+
return (input)=>_write(segments, params[1], input);
|
|
100
|
+
}
|
|
101
|
+
// @signature (path, value, input)
|
|
102
|
+
return _write(segments, params[1], params[2]);
|
|
103
|
+
};
|
|
104
|
+
export { write };
|
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.
|
|
@@ -55,6 +65,40 @@ export type DeepPartial<T> = T extends object ? {
|
|
|
55
65
|
* // => undefined
|
|
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;
|
|
68
|
+
/**
|
|
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;
|
|
58
102
|
/**
|
|
59
103
|
* Resolve the type of all values of an object (union)
|
|
60
104
|
*
|
|
@@ -68,3 +112,4 @@ export type DeepValue<T, Path extends readonly Property[]> = Path extends readon
|
|
|
68
112
|
export type DeepValues<T extends PlainObject> = {
|
|
69
113
|
[K in keyof T]: T[K] extends PlainObject ? DeepValues<T[K]> : T[K];
|
|
70
114
|
}[keyof T];
|
|
115
|
+
export {};
|
package/docs/SPEC.md
CHANGED
|
@@ -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
|
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": "9.
|
|
4
|
+
"version": "9.1.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",
|
|
@@ -66,7 +66,7 @@
|
|
|
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",
|