fnts 3.0.3 → 3.0.4

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/lens/get.d.ts CHANGED
@@ -1,24 +1,24 @@
1
1
  /**
2
2
  * @module Lens
3
3
  */
4
- import type { Flatten, Flattenable, NormalizeFlattenable } from '../types/flatten';
4
+ import type { Flatten, Flattenable } from '../types/flatten';
5
5
  /**
6
6
  * Gets the value type inside a nested object type `Source` by provided `Path`
7
7
  * written in dot-notation.
8
8
  */
9
- export type Get<Source extends Flattenable, Path extends Flatten<NormalizeFlattenable<Source>>> = Path extends keyof Source ? Source[Path] : (Path extends `${infer Left}.${infer Right}` ? (Left extends keyof NormalizeFlattenable<Source> ? (NormalizeFlattenable<Source>[Left] extends Flattenable ? Get<NormalizeFlattenable<Source>[Left], Right> : never) : never) : never);
9
+ export type Get<Source extends Flattenable, Path extends Flatten<Source>> = Source extends Record<string, unknown> ? (Path extends keyof Source ? Source[Path] : (Path extends `${infer Left}.${infer Right}` ? (Left extends keyof Source ? (Source[Left] extends Flattenable ? Get<Source[Left], Right> : never) : never) : never)) : Source extends any[] ? (Path extends `${number}` ? Source[number] : (Path extends `${infer Left}.${infer Right}` ? (Left extends `${number}` ? (Source[number] extends Flattenable ? Get<Source[number], Right> : never) : never) : never)) : never;
10
10
  /**
11
11
  * Gets the value inside a nested `source` object by provided `path`
12
12
  * written in dot-notation.
13
13
  */
14
- export default function get<Source extends Flattenable>(source: Source): <Path extends Flatten<NormalizeFlattenable<Source>>>(path: Path) => Get<Source, Path>;
14
+ export default function get<Source extends Flattenable>(source: Source): <Path extends Flatten<Source>>(path: Path) => Get<Source, Path>;
15
15
  /**
16
16
  * Gets the value inside a nested `source` object by provided `path`
17
17
  * written in dot-notation.
18
18
  */
19
- export default function get<Source extends Flattenable, Path extends Flatten<NormalizeFlattenable<Source>>>(path: Path): (source: Source) => Get<Source, Path>;
19
+ export default function get<Source extends Flattenable, Path extends Flatten<Source>>(path: Path): (source: Source) => Get<Source, Path>;
20
20
  /**
21
21
  * Gets the value inside a nested `source` object by provided `path`
22
22
  * written in dot-notation.
23
23
  */
24
- export default function get<Source extends Flattenable, Path extends Flatten<NormalizeFlattenable<Source>>>(source: Source, path: Path): Get<Source, Path>;
24
+ export default function get<Source extends Flattenable, Path extends Flatten<Source>>(source: Source, path: Path): Get<Source, Path>;
package/lens/lens.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @module Lens
3
3
  */
4
- import type { Flatten, Flattenable, NormalizeFlattenable } from '../types';
4
+ import type { Flatten, Flattenable } from '../types';
5
5
  import { Get } from './get.js';
6
6
  import { Set } from './set.js';
7
7
  /**
@@ -9,8 +9,8 @@ import { Set } from './set.js';
9
9
  * effectively creating a functional lens.
10
10
  */
11
11
  export interface Lens<Source extends Flattenable> {
12
- <Path extends Flatten<NormalizeFlattenable<Source>>>(path: Path): Get<Source, Path>;
13
- <Path extends Flatten<NormalizeFlattenable<Source>>, Value extends Get<Source, Path>>(path: Path, value: Value): Set<Source, Path, Value>;
12
+ <Path extends Flatten<Source>>(path: Path): Get<Source, Path>;
13
+ <Path extends Flatten<Source>, Value extends Get<Source, Path>>(path: Path, value: Value): Set<Source, Path, Value>;
14
14
  }
15
15
  /**
16
16
  * Creates a wrapper for a given `source` that provides access to both `get` and `set` functionalities for it,
package/lens/set.d.ts CHANGED
@@ -1,30 +1,29 @@
1
1
  /**
2
2
  * @module Lens
3
3
  */
4
- import type { Unshift } from '../types/unshift';
5
- import type { Flatten, Flattenable, NormalizeFlattenable } from '../types/flatten';
4
+ import type { Flatten, Flattenable } from '../types/flatten';
6
5
  import type { Get } from './get.js';
7
6
  /**
8
7
  * Sets the `Value` type inside a nested object type `Source` by provided `Path`
9
8
  * written in dot-notation.
10
9
  */
11
- export type Set<Source extends Flattenable, Path extends Flatten<NormalizeFlattenable<Source>>, Value extends Get<Source, Path>> = Source extends Record<string, unknown> ? (Path extends `${number}.${infer Right}` ? Set<Source, Right, Value> : (Path extends `${infer Left}.${infer Right}` ? {
12
- [Key in keyof Source]: Key extends Exclude<keyof Source, Left> ? Source[Key] : Set<Exclude<Source[Left], undefined>, Right, Value>;
13
- } : (Path extends keyof Source ? {
14
- [Key in keyof Source]: Key extends Exclude<keyof Source, Path> ? Source[Key] : Value;
15
- } : 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);
10
+ export type Set<Source extends Flattenable, Path extends Flatten<Source>, Value extends Get<Source, Path>> = Source extends Record<string, unknown> ? (Path extends keyof Source ? {
11
+ [Key in keyof Source]: Key extends Path ? Value : Source[Key];
12
+ } : (Path extends `${infer Left}.${infer Right}` ? (Left extends keyof Source ? (Source[Left] extends Flattenable ? {
13
+ [Key in keyof Source]: (Key extends Left ? Set<Source[Left], Right, Value> : Source[Key]);
14
+ } : never) : never) : never)) : Source extends any[] ? (Path extends `${number}` ? Value[] : (Path extends `${infer Left}.${infer Right}` ? (Left extends `${number}` ? (Source[number] extends Flattenable ? Set<Source[number], Right, Value>[] : never) : never) : never)) : never;
16
15
  /**
17
16
  * Sets the `value` inside a nested `source` object by provided `path`
18
17
  * written in dot-notation.
19
18
  */
20
- export default function set<Source extends Flattenable>(source: Source): <Path extends Flatten<NormalizeFlattenable<Source>>, Value extends Get<Source, Path>>(path: Path, value: Value) => Set<Source, Path, Value>;
19
+ export default function set<Source extends Flattenable>(source: Source): <Path extends Flatten<Source>, Value extends Get<Source, Path>>(path: Path, value: Value) => Set<Source, Path, Value>;
21
20
  /**
22
21
  * Sets the `value` inside a nested `source` object by provided `path`
23
22
  * written in dot-notation.
24
23
  */
25
- export default function set<Source extends Flattenable, Path extends Flatten<NormalizeFlattenable<Source>>, Value extends Get<Source, Path>>(path: Path, value: Value): (source: Source) => Set<Source, Path, Value>;
24
+ export default function set<Source extends Flattenable, Path extends Flatten<Source>, Value extends Get<Source, Path>>(path: Path, value: Value): (source: Source) => Set<Source, Path, Value>;
26
25
  /**
27
26
  * Sets the `value` inside a nested `source` object by provided `path`
28
27
  * written in dot-notation.
29
28
  */
30
- export default function set<Source extends Flattenable, Path extends Flatten<NormalizeFlattenable<Source>>, Value extends Get<Source, Path>>(source: Source, path: Path, value: Value): Set<Source, Path, Value>;
29
+ export default function set<Source extends Flattenable, Path extends Flatten<Source>, Value extends Get<Source, Path>>(source: Source, path: Path, value: Value): Set<Source, Path, Value>;
package/lens/set.js CHANGED
@@ -21,7 +21,6 @@ export default function set(...args) {
21
21
  }
22
22
  }
23
23
  function setter(source, path, value) {
24
- // @ts-ignore
25
24
  const keys = path.split('.');
26
25
  const length = keys.length;
27
26
  let result = structuredClone(source);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fnts",
3
- "version": "3.0.3",
3
+ "version": "3.0.4",
4
4
  "license": "MIT",
5
5
  "author": "Dmytro Vasylkivskyi",
6
6
  "description": "TypeScript / JavaScript FP utilities",
@@ -5,12 +5,7 @@
5
5
  /**
6
6
  * Type subjective to flattening.
7
7
  */
8
- export type Flattenable = Record<string, unknown> | Array<any>
9
-
10
- export type NormalizeFlattenable<Source extends Flattenable> =
11
- Source extends Array<infer Element>
12
- ? Record<`${number}`, Element>
13
- : Source
8
+ export type Flattenable = Record<string, unknown> | any[]
14
9
 
15
10
  /**
16
11
  * Recursively flattens object type to a union of its keys
@@ -18,15 +13,26 @@ export type NormalizeFlattenable<Source extends Flattenable> =
18
13
  */
19
14
  export type Flatten<
20
15
  Source extends Flattenable,
21
- > = | keyof NormalizeFlattenable<Source>
22
- | keyof {
16
+ > = (
17
+ Source extends Record<string, unknown>
18
+ ? (keyof {
23
19
  [
24
20
  Key in keyof Source as
25
21
  Key extends string
26
22
  ? (Source[Key] extends Flattenable
27
- // @ts-ignore
28
- ? `${Key}.${Flatten<NormalizeFlattenable<Source[Key]>>}`
29
- : Key)
23
+ ? Key | `${Key}.${Flatten<Source[Key]>}`
24
+ : (Source[Key] extends any[]
25
+ ? Key | `${Key}.${number}.${Flatten<Source[Key][number]>}`
26
+ : Key))
30
27
  : never
31
- ]: never
32
- }
28
+ ]: void
29
+ })
30
+ : (Source extends any[]
31
+ ? (Source[number] extends Flattenable
32
+ ? `${number}` | `${number}.${Flatten<Source[number]>}`
33
+ : `${number}`)
34
+ : never)
35
+ // So that Flatten is recognized to always return strings
36
+ ) extends infer Key extends string
37
+ ? Key
38
+ : never
@@ -8,9 +8,9 @@ import { Slice } from './slice'
8
8
  /**
9
9
  * Replaces `T` element on specified index `I` with `E`.
10
10
  */
11
- export type Unshift<T extends 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>]
11
+ export type Unshift<Array extends ReadonlyArray<any>, Index extends number, Element> =
12
+ Index extends 0
13
+ ? [Element, ...Slice<Array, 1>]
14
+ : Index extends Trim<Array, 1>['length']
15
+ ? [...Trim<Array, 1>, Element]
16
+ : [...Trim<Array, Slice<Array, Index>['length']>, Element, ...Slice<Slice<Array, Index>, 1>]