fnts 2.1.1 → 2.2.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/get.d.ts CHANGED
@@ -1,25 +1,19 @@
1
1
  /**
2
2
  * @module Get
3
3
  */
4
- /**
5
- * Recursively flattens object type to a union of its keys
6
- * combined through dot-notation.
7
- */
8
- export type Flatten<Source extends Record<string, unknown> | any[]> = keyof Source extends keyof any[] ? `${number}` : (keyof Source | keyof {
9
- [Key in keyof Source as Key extends string ? Source[Key] extends Record<string, unknown> ? `${Key}.${Flatten<Source[Key]>}` : Source[Key] extends any[] ? Source[Key] extends Array<any[]> | Record<string, unknown>[] ? `${Key}.${number}` | `${Key}.${number}.${Flatten<Source[Key][number]>}` : `${Key}.${number}` : Key : never]: never;
10
- });
4
+ import type { Flatten, Flattenable } from './types/flatten';
11
5
  /**
12
6
  * Gets the value type inside a nested object type `Source` by provided `Path`
13
7
  * written in dot-notation.
14
8
  */
15
- export type Get<Source, Path extends string> = Source extends Record<string, unknown> ? Path extends `${infer Index extends number}.${infer Right}` ? Get<Source, Right> : Path extends `${infer Left extends keyof Source}.${infer Right}` ? Get<Exclude<Source[Left], undefined>, Right> | Extract<Source[Left], undefined> : Path extends keyof Source ? Source[Path] : never : Source extends any[] ? Path extends `${infer Left extends number}.${infer Right}` ? Get<Exclude<Source[Left], undefined>, Right> | Extract<Source[Left], undefined> : Path extends `${infer Index extends number}` ? Source[Index] : never : never;
9
+ export type Get<Source extends Flattenable, Path extends string> = Source extends Record<string, unknown> ? Path extends `${number}.${infer Right}` ? Get<Source, Right> : Path extends `${infer Left extends keyof Source}.${infer Right}` ? Get<Exclude<Source[Left], undefined>, Right> | Extract<Source[Left], undefined> : Path extends keyof Source ? Source[Path] : never : Source extends any[] ? Path extends `${infer Left extends number}.${infer Right}` ? Get<Exclude<Source[Left], undefined>, Right> | Extract<Source[Left], undefined> : Path extends `${infer Index extends number}` ? Source[Index] : never : never;
16
10
  /**
17
11
  * Gets the value inside a nested `source` object by provided `path`
18
12
  * written in dot-notation.
19
13
  */
20
- export default function get<Source extends Record<string, unknown> | any[], Path extends Flatten<Source>>(path: Path): (source: Source) => Get<Source, Path>;
14
+ export default function get<Source extends Flattenable, Path extends Flatten<Source>>(path: Path): (source: Source) => Get<Source, Path>;
21
15
  /**
22
16
  * Gets the value inside a nested `source` object by provided `path`
23
17
  * written in dot-notation.
24
18
  */
25
- export default function get<Source extends Record<string, unknown> | any[], Path extends Flatten<Source>>(source: Source, path: Path): Get<Source, Path>;
19
+ export default function get<Source extends Flattenable, Path extends Flatten<Source>>(source: Source, path: Path): Get<Source, Path>;
package/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { default as get } from './get';
2
+ export { default as set } from './set';
2
3
  export { default as tap } from './tap';
3
4
  export { default as pipe } from './pipe';
4
5
  export { default as apply } from './apply';
package/index.js CHANGED
@@ -1,8 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.identity = exports.rethrow = exports.compose = exports.inject = exports.guard = exports.curry = exports.apply = exports.pipe = exports.tap = exports.get = void 0;
3
+ exports.identity = exports.rethrow = exports.compose = exports.inject = exports.guard = exports.curry = exports.apply = exports.pipe = exports.tap = exports.set = exports.get = void 0;
4
4
  var get_1 = require("./get");
5
5
  Object.defineProperty(exports, "get", { enumerable: true, get: function () { return get_1.default; } });
6
+ var set_1 = require("./set");
7
+ Object.defineProperty(exports, "set", { enumerable: true, get: function () { return set_1.default; } });
6
8
  var tap_1 = require("./tap");
7
9
  Object.defineProperty(exports, "tap", { enumerable: true, get: function () { return tap_1.default; } });
8
10
  var pipe_1 = require("./pipe");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fnts",
3
- "version": "2.1.1",
3
+ "version": "2.2.0",
4
4
  "license": "MIT",
5
5
  "author": "Dmytro Vasylkivskyi",
6
6
  "description": "TypeScript / JavaScript FP utilities",
package/set.d.ts ADDED
@@ -0,0 +1,24 @@
1
+ /**
2
+ * @module Set
3
+ */
4
+ import type { Unshift } from './types/unshift';
5
+ import type { Flatten, Flattenable } from './types/flatten';
6
+ /**
7
+ * Sets the `Value` type inside a nested object type `Source` by provided `Path`
8
+ * written in dot-notation.
9
+ */
10
+ export type Set<Source extends Flattenable, Path extends string, Value> = Source extends Record<string, unknown> ? Path extends `${number}.${infer Right}` ? Set<Source, Right, Value> : Path extends `${infer Left extends keyof Source}.${infer Right}` ? {
11
+ [Key in keyof Source]: Key extends Exclude<keyof Source, Left> ? Source[Key] : Set<Exclude<Source[Left], undefined>, Right, Value>;
12
+ } : Path extends keyof Source ? {
13
+ [Key in keyof Source]: Key extends Exclude<keyof Source, Path> ? Source[Key] : Value;
14
+ } : never : Source extends any[] ? Path extends `${infer Index extends number}.${infer Right}` ? Unshift<Source, Index, Set<Exclude<Source[Index], undefined>, Right, Value>> : Path extends `${infer Index extends number}` ? Unshift<Source, Index, Value> : never : never;
15
+ /**
16
+ * Sets the `value` inside a nested `source` object by provided `path`
17
+ * written in dot-notation.
18
+ */
19
+ export default function set<Source extends Flattenable, Path extends Flatten<Source>, Value>(path: Path, value: Value): (source: Source) => Set<Source, Path, Value>;
20
+ /**
21
+ * Sets the `value` inside a nested `source` object by provided `path`
22
+ * written in dot-notation.
23
+ */
24
+ export default function set<Source extends Flattenable, Path extends Flatten<Source>, Value>(source: Source, path: Path, value: Value): Set<Source, Path, Value>;
package/set.js ADDED
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ /**
3
+ * @module Set
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ var permutation_3_1 = require("./permutation/permutation-3");
7
+ /**
8
+ * Sets the `value` inside a nested `source` object by provided `path`
9
+ * written in dot-notation.
10
+ */
11
+ function set() {
12
+ var args = [];
13
+ for (var _i = 0; _i < arguments.length; _i++) {
14
+ args[_i] = arguments[_i];
15
+ }
16
+ // @ts-ignore
17
+ return (0, permutation_3_1.default)(
18
+ // @ts-ignore
19
+ function (source, path, value
20
+ // @ts-ignore
21
+ ) {
22
+ // @ts-ignore
23
+ var keys = path.split('.');
24
+ var length = keys.length;
25
+ var result = structuredClone(source);
26
+ if (length === 0) {
27
+ result[path] = value;
28
+ }
29
+ var scope = result[keys[0]];
30
+ for (var index = 1; index < length; index += 1) {
31
+ if (index === length - 1) {
32
+ scope[keys === null || keys === void 0 ? void 0 : keys[index]] = value;
33
+ }
34
+ else {
35
+ scope = scope === null || scope === void 0 ? void 0 : scope[keys === null || keys === void 0 ? void 0 : keys[index]];
36
+ }
37
+ }
38
+ // @ts-ignore
39
+ return result;
40
+ }).apply(void 0, args);
41
+ }
42
+ exports.default = set;
package/tsconfig.json CHANGED
@@ -5,7 +5,8 @@
5
5
  "moduleResolution": "node",
6
6
  "lib": [
7
7
  "es2015.iterable",
8
- "es2019"
8
+ "es2019",
9
+ "dom"
9
10
  ],
10
11
  "strict": true,
11
12
  "alwaysStrict": true,
@@ -0,0 +1,34 @@
1
+ /**
2
+ * @module Types
3
+ */
4
+
5
+ /**
6
+ * Type subjective to flattening.
7
+ */
8
+ export type Flattenable = Record<string, unknown> | any[]
9
+
10
+ /**
11
+ * Recursively flattens object type to a union of its keys
12
+ * combined through dot-notation.
13
+ */
14
+ export type Flatten<
15
+ Source extends Record<string, unknown> | any[],
16
+ > = keyof Source extends keyof any[]
17
+ ? `${number}`
18
+ : (
19
+ | keyof Source
20
+ | keyof {
21
+ [
22
+ Key in keyof Source as
23
+ Key extends string
24
+ ? Source[Key] extends Record<string, unknown>
25
+ ? `${Key}.${Flatten<Source[Key]>}`
26
+ : Source[Key] extends any[]
27
+ ? Source[Key] extends Array<any[]> | Record<string, unknown>[]
28
+ ? `${Key}.${number}` | `${Key}.${number}.${Flatten<Source[Key][number]>}`
29
+ : `${Key}.${number}`
30
+ : Key
31
+ : never
32
+ ] : never
33
+ }
34
+ )
package/types/index.d.ts CHANGED
@@ -1,7 +1,10 @@
1
1
  export type { Map } from './map'
2
2
  export type { Last } from './last'
3
+ export type { Trim } from './trim'
3
4
  export type { Slice } from './slice'
4
5
  export type { Tuple } from './tuple'
5
6
  export type { Effect } from './effect'
6
7
  export type { Gradual } from './gradual'
8
+ export type { Unshift } from './unshift'
9
+ export type { Flatten } from './flatten'
7
10
  export type { UnaryFunction, VariadicFunction } from './function'
package/types/slice.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  import type { Tuple } from './tuple'
6
6
 
7
7
  /**
8
- * Slices the tuple by `C` amount of entries from the **end**.
8
+ * Removes `C` amount of `T` elements from the **start**.
9
9
  */
10
10
  export type Slice<
11
11
  T extends any[] | readonly any[],
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @module Types
3
+ */
4
+
5
+ import { Tuple } from './tuple'
6
+
7
+ /**
8
+ * Removes `C` amount of `T` elements from the **end**.
9
+ */
10
+ export type Trim<
11
+ T extends any[] | readonly any[],
12
+ C extends number
13
+ > = T['length'] extends C
14
+ ? T
15
+ : T extends readonly [...infer S, ...Tuple<C>]
16
+ ? S
17
+ : T extends [...infer S, ...Tuple<C>]
18
+ ? S
19
+ : T
@@ -0,0 +1,16 @@
1
+ /**
2
+ * @module Types
3
+ */
4
+
5
+ import { Trim } from './trim'
6
+ import { Slice } from './slice'
7
+
8
+ /**
9
+ * Replaces `T` element on specified index `I` with `E`.
10
+ */
11
+ export type Unshift<T extends any[] | readonly any[], I extends number, E> =
12
+ I extends 0
13
+ ? [E, ...Slice<T, 1>]
14
+ : I extends Trim<T, 1>['length']
15
+ ? [...Trim<T, 1>, E]
16
+ : [...Trim<T, Slice<T, I>['length']>, E, ...Slice<Slice<T, I>, 1>]