fnts 2.0.0-rc.1 → 2.0.1

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.
Files changed (44) hide show
  1. package/README.md +1 -1
  2. package/compose.d.ts +13 -4
  3. package/compose.js +14 -8
  4. package/curry.d.ts +2 -2
  5. package/either/either.d.ts +1 -1
  6. package/either/left.d.ts +1 -1
  7. package/either/operators/bifold/bifold.d.ts +5 -1
  8. package/either/operators/bifold/bifoldl.d.ts +6 -1
  9. package/either/operators/bifold/bifoldr.d.ts +6 -1
  10. package/either/operators/bifold-map/bifold-map.js +7 -6
  11. package/either/operators/bifold-map/bifoldl-map.d.ts +12 -0
  12. package/either/operators/bifold-map/bifoldl-map.js +5 -4
  13. package/either/operators/bifold-map/bifoldr-map.d.ts +12 -0
  14. package/either/operators/bifold-map/bifoldr-map.js +5 -4
  15. package/either/operators/bimap/bimap.js +3 -2
  16. package/either/operators/bimap/first.d.ts +2 -0
  17. package/either/operators/bimap/first.js +5 -4
  18. package/either/operators/bimap/second.d.ts +2 -0
  19. package/either/operators/bimap/second.js +4 -3
  20. package/either/operators/bindr.js +3 -2
  21. package/either/right.d.ts +1 -1
  22. package/get.d.ts +25 -0
  23. package/get.js +26 -0
  24. package/guard.d.ts +1 -1
  25. package/identity.d.ts +1 -1
  26. package/index.d.ts +1 -1
  27. package/index.js +3 -3
  28. package/maybe/just.d.ts +1 -1
  29. package/maybe/maybe.d.ts +1 -1
  30. package/maybe/nothing.d.ts +1 -1
  31. package/maybe/operators/bind.js +3 -2
  32. package/maybe/operators/fmap.d.ts +2 -2
  33. package/maybe/operators/fmap.js +3 -2
  34. package/maybe/operators/fold-map.d.ts +3 -3
  35. package/maybe/operators/fold-map.js +4 -3
  36. package/maybe/operators/fold.d.ts +5 -1
  37. package/package.json +2 -2
  38. package/permutation/permutation-2.d.ts +3 -3
  39. package/permutation/permutation-3.d.ts +2 -2
  40. package/pipe.d.ts +17 -4
  41. package/pipe.js +14 -8
  42. package/tsconfig.json +4 -2
  43. package/ternary.d.ts +0 -10
  44. package/ternary.js +0 -16
package/README.md CHANGED
@@ -21,7 +21,7 @@ TypeScript / JavaScript functional programming utilities inspired by the
21
21
 
22
22
  ## Installation
23
23
 
24
- **TypeScript 4.2.2 is required for this package's typings to work properly.**
24
+ **TypeScript 4.7 is required for this package's typings to work properly.**
25
25
 
26
26
  ```shell
27
27
  npm i fnts
package/compose.d.ts CHANGED
@@ -1,11 +1,20 @@
1
1
  /**
2
2
  * @module Composition
3
3
  */
4
+ import type { UnaryFunction } from './types/function';
4
5
  /**
5
- * Applies first onto second onto args.
6
+ * Makes a composition of functions from received arguments.
6
7
  */
7
- export default function compose<First, Argument, Return>(first: (arg: First) => Return, second: (arg: Argument) => First): (arg: Argument) => Return;
8
+ export type Compose<Arguments extends any[], Functions extends any[] = []> = Arguments['length'] extends 0 ? Functions : Arguments extends [infer A, infer B] ? [...Functions, (arg: A) => B] : Arguments extends [infer A, ...infer Rest, infer P, infer L] ? Compose<[A, ...Rest, P], [...Functions, (arg: P) => L]> : [];
8
9
  /**
9
- * Applies first onto second onto args.
10
+ * Destructures a composition of functions into arguments.
10
11
  */
11
- export default function compose<First, Arguments extends any[], Return>(first: (arg: First) => Return, second: (...args: Arguments) => First): (...args: Arguments) => Return;
12
+ export type Decompose<Functions extends UnaryFunction[], Arguments extends any[] = []> = Functions extends [(arg: infer Arg) => infer Return] ? [...Arguments, Arg, Return] : Functions extends [...infer Rest extends UnaryFunction[], (arg: infer Arg) => any] ? Decompose<Rest, [...Arguments, Arg]> : [];
13
+ /**
14
+ * (B -> C) . (A -> B) = A -> C
15
+ */
16
+ export default function compose<A extends any[], B, C>(f: (arg: B) => C, g: (...args: A) => B): (...args: A) => C;
17
+ /**
18
+ * (B -> C) . (A -> B) = A -> C
19
+ */
20
+ export default function compose<A extends any[], B, C>(f: (arg: B) => C, g: (...args: A) => B, ...args: A): C;
package/compose.js CHANGED
@@ -4,15 +4,21 @@
4
4
  */
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  /**
7
- * Applies first onto second onto args.
7
+ * (B -> C) . (A -> B) = A -> C
8
8
  */
9
- function compose(first, second) {
10
- return function () {
11
- var args = [];
12
- for (var _i = 0; _i < arguments.length; _i++) {
13
- args[_i] = arguments[_i];
9
+ function compose(f, g) {
10
+ var maybeArgs = [];
11
+ for (var _i = 2; _i < arguments.length; _i++) {
12
+ maybeArgs[_i - 2] = arguments[_i];
13
+ }
14
+ return maybeArgs.length === 0
15
+ ? function () {
16
+ var args = [];
17
+ for (var _i = 0; _i < arguments.length; _i++) {
18
+ args[_i] = arguments[_i];
19
+ }
20
+ return f(g.apply(void 0, args));
14
21
  }
15
- return first(second.apply(void 0, args));
16
- };
22
+ : f(g.apply(void 0, maybeArgs));
17
23
  }
18
24
  exports.default = compose;
package/curry.d.ts CHANGED
@@ -7,7 +7,7 @@ import type { VariadicFunction } from './types/function';
7
7
  /**
8
8
  * Creates a type for an auto-curried function.
9
9
  */
10
- export declare type Currying<Function extends VariadicFunction, Length extends number = Parameters<Function>['length']> = <Args extends Gradual<Parameters<Function>>>(...args: Args) => Args['length'] extends Length ? ReturnType<Function> : Currying<(...args: Slice<Parameters<Function>, Args['length']>) => ReturnType<Function>>;
10
+ export type Curry<Function extends VariadicFunction, Length extends number = Parameters<Function>['length']> = <Args extends Gradual<Parameters<Function>>>(...args: Args) => Args['length'] extends Length ? ReturnType<Function> : Curry<(...args: Slice<Parameters<Function>, Args['length']>) => ReturnType<Function>>;
11
11
  /**
12
12
  * Creates an auto-curried function from the one provided.
13
13
  * Until the curried function receives an expected number of arguments,
@@ -16,4 +16,4 @@ export declare type Currying<Function extends VariadicFunction, Length extends n
16
16
  * of arguments.
17
17
  * Any excess arguments will not be applied.
18
18
  */
19
- export default function curry<Function extends VariadicFunction, Length extends number = Parameters<Function>['length']>(fn: Function, length?: Length): Currying<Function, Length>;
19
+ export default function curry<Function extends VariadicFunction, Length extends number = Parameters<Function>['length']>(fn: Function, length?: Length): Curry<Function, Length>;
@@ -8,7 +8,7 @@ import { Right } from './right';
8
8
  * Can be either `Right` of the value provided,
9
9
  * or `Left` of the error thrown.
10
10
  */
11
- export declare type Either<LeftValue, RightValue> = Left<LeftValue> | Right<RightValue>;
11
+ export type Either<LeftValue, RightValue> = Left<LeftValue> | Right<RightValue>;
12
12
  /**
13
13
  * Type constructor for `Either`.
14
14
  * Creates either `Right` of the value provided,
package/either/left.d.ts CHANGED
@@ -10,7 +10,7 @@ export declare const lid: unique symbol;
10
10
  * Describes the special object with one property, represented as unique symbol
11
11
  * key, holding the provided value type.
12
12
  */
13
- export declare type Left<Value> = {
13
+ export type Left<Value> = {
14
14
  [lid]: Value;
15
15
  };
16
16
  /**
@@ -8,8 +8,12 @@ import type { Either } from '../../either';
8
8
  * A type to unwrap either the left or the right value type
9
9
  * of the provided `Monad`.
10
10
  */
11
- export declare type Bifold<Monad extends Either<any, any>> = Monad extends Left<infer LeftValue> ? LeftValue : Monad extends Right<infer RightValue> ? RightValue : Monad extends Either<infer LeftValue, infer RightValue> ? LeftValue | RightValue : never;
11
+ export type Bifold<Monad extends Either<any, any>> = Monad extends Left<infer LeftValue> ? LeftValue : Monad extends Right<infer RightValue> ? RightValue : Monad extends Either<infer LeftValue, infer RightValue> ? LeftValue | RightValue : never;
12
12
  /**
13
13
  * Unwraps either the left or the right value of the provided `monad`.
14
14
  */
15
15
  export default function bifold<LeftValue, RightValue>(monad: Either<LeftValue, RightValue>): LeftValue | RightValue;
16
+ /**
17
+ * Unwraps either the left or the right value of the provided `monad`.
18
+ */
19
+ export default function bifold<Monad extends Either<any, any>>(monad: Monad): Bifold<Monad>;
@@ -7,7 +7,12 @@ import type { Either } from '../../either';
7
7
  * A type to unwrap the left value type of the provided `Monad`.
8
8
  * If the `Monad` is `Right`, returns `undefined`.
9
9
  */
10
- export declare type Bifoldl<Monad extends Either<any, any>> = Monad extends Left<infer LeftValue> ? LeftValue : Monad extends Either<infer LeftValue, any> ? LeftValue | undefined : never;
10
+ export type Bifoldl<Monad extends Either<any, any>> = Monad extends Left<infer LeftValue> ? LeftValue : Monad extends Either<infer LeftValue, any> ? LeftValue | undefined : never;
11
+ /**
12
+ * Returns the left value of the provided `monad`.
13
+ * If the `monad` is `Right`, returns `undefined`.
14
+ */
15
+ export default function bifoldl<Monad extends Left<any>>(monad: Monad): Bifoldl<Monad>;
11
16
  /**
12
17
  * Returns the left value of the provided `monad`.
13
18
  * If the `monad` is `Right`, returns `undefined`.
@@ -7,7 +7,12 @@ import type { Either } from '../../either';
7
7
  * A type to unwrap the right value type of the provided `Monad`.
8
8
  * If the `Monad` is `Left`, returns `undefined`.
9
9
  */
10
- export declare type Bifoldr<Monad extends Either<any, any>> = Monad extends Right<infer RightValue> ? RightValue : Monad extends Either<any, infer RightValue> ? RightValue | undefined : never;
10
+ export type Bifoldr<Monad extends Either<any, any>> = Monad extends Right<infer RightValue> ? RightValue : Monad extends Either<any, infer RightValue> ? RightValue | undefined : never;
11
+ /**
12
+ * Returns the right value of the provided `monad`.
13
+ * If the `monad` is `Left`, returns `undefined`.
14
+ */
15
+ export default function bifoldr<Monad extends Right<any>>(monad: Monad): Bifoldr<Monad>;
11
16
  /**
12
17
  * Returns the right value of the provided `monad`.
13
18
  * If the `monad` is `Left`, returns `undefined`.
@@ -3,13 +3,12 @@
3
3
  * @module Either Operators
4
4
  */
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ var bifold_1 = require("../bifold");
6
7
  var compose_1 = require("../../../compose");
7
- var ternary_1 = require("../../../ternary");
8
+ var guards_1 = require("../guards");
8
9
  var permutation_3_1 = require("../../../permutation/permutation-3");
9
- var bifold_1 = require("../bifold");
10
10
  var bifoldl_map_1 = require("./bifoldl-map");
11
11
  var bifoldr_map_1 = require("./bifoldr-map");
12
- var guards_1 = require("../guards");
13
12
  function bifoldMap() {
14
13
  var args = [];
15
14
  for (var _i = 0; _i < arguments.length; _i++) {
@@ -17,14 +16,16 @@ function bifoldMap() {
17
16
  }
18
17
  if (args.length === 1) {
19
18
  return function (monad) {
20
- return (0, compose_1.default)(args[0], bifold_1.default)(monad);
19
+ return (0, compose_1.default)(args[0], bifold_1.default, monad);
21
20
  };
22
21
  }
23
22
  if (args.length === 2 && (0, guards_1.isEither)(args[0])) {
24
- return (0, compose_1.default)(args[1], bifold_1.default)(args[0]);
23
+ return (0, compose_1.default)(args[1], bifold_1.default, args[0]);
25
24
  }
26
25
  return (0, permutation_3_1.default)(function (monad, mapLeft, mapRight) {
27
- return (0, ternary_1.default)(guards_1.isLeft, (0, bifoldl_map_1.default)(mapLeft), (0, bifoldr_map_1.default)(mapRight))(monad);
26
+ return ((0, guards_1.isLeft)(monad)
27
+ ? (0, bifoldl_map_1.default)(monad, mapLeft)
28
+ : (0, bifoldr_map_1.default)(monad, mapRight));
28
29
  }).apply(void 0, args);
29
30
  }
30
31
  exports.default = bifoldMap;
@@ -1,8 +1,10 @@
1
1
  /**
2
2
  * @module Either Operators
3
3
  */
4
+ import type { Left } from '../../left';
4
5
  import type { Either } from '../../either';
5
6
  import type { Map } from '../../../types/map';
7
+ import { Bifoldl } from '../bifold/bifoldl';
6
8
  /**
7
9
  * Maps provided `monad`'s left value to a new value,
8
10
  * then returns it or `undefined`.
@@ -13,3 +15,13 @@ export default function bifoldlMap<LeftValue, Value>(map: Map<LeftValue, Value>)
13
15
  * then returns it or `undefined`.
14
16
  */
15
17
  export default function bifoldlMap<LeftValue, Value>(monad: Either<LeftValue, any>, map: Map<LeftValue, Value>): Value | undefined;
18
+ /**
19
+ * Maps provided `monad`'s left value to a new value,
20
+ * then returns it or `undefined`.
21
+ */
22
+ export default function bifoldlMap<Monad extends Left<any>, Value>(map: Map<Bifoldl<Monad>, Value>): (monad: Monad) => Value;
23
+ /**
24
+ * Maps provided `monad`'s left value to a new value,
25
+ * then returns it or `undefined`.
26
+ */
27
+ export default function bifoldlMap<Monad extends Left<any>, Value>(monad: Monad, map: Map<Bifoldl<Monad>, Value>): Value;
@@ -3,18 +3,19 @@
3
3
  * @module Either Operators
4
4
  */
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ var guards_1 = require("../guards");
6
7
  var compose_1 = require("../../../compose");
7
- var ternary_1 = require("../../../ternary");
8
+ var bifoldl_1 = require("../bifold/bifoldl");
8
9
  var permutation_2_1 = require("../../../permutation/permutation-2");
9
- var guards_1 = require("../guards");
10
- var bifold_1 = require("../bifold");
11
10
  function bifoldlMap() {
12
11
  var args = [];
13
12
  for (var _i = 0; _i < arguments.length; _i++) {
14
13
  args[_i] = arguments[_i];
15
14
  }
16
15
  return (0, permutation_2_1.default)(function (monad, map) {
17
- return (0, ternary_1.default)(guards_1.isLeft, (0, compose_1.default)(map, bifold_1.bifoldl), function () { return undefined; })(monad);
16
+ return (0, guards_1.isLeft)(monad)
17
+ ? (0, compose_1.default)(map, bifoldl_1.default, monad)
18
+ : undefined;
18
19
  }).apply(void 0, args);
19
20
  }
20
21
  exports.default = bifoldlMap;
@@ -1,8 +1,10 @@
1
1
  /**
2
2
  * @module Either Operators
3
3
  */
4
+ import { Right } from '../../right';
4
5
  import type { Either } from '../../either';
5
6
  import type { Map } from '../../../types/map';
7
+ import { Bifoldr } from '../bifold/bifoldr';
6
8
  /**
7
9
  * Maps provided `monad`'s right value to a new value,
8
10
  * then returns it or `undefined`.
@@ -13,3 +15,13 @@ export default function bifoldrMap<RightValue, Value>(map: Map<RightValue, Value
13
15
  * then returns it or `undefined`.
14
16
  */
15
17
  export default function bifoldrMap<RightValue, Value>(monad: Either<any, RightValue>, map: Map<RightValue, Value>): Value | undefined;
18
+ /**
19
+ * Maps provided `monad`'s right value to a new value,
20
+ * then returns it or `undefined`.
21
+ */
22
+ export default function bifoldrMap<Monad extends Right<any>, Value>(map: Map<Bifoldr<Monad>, Value>): (monad: Monad) => Value;
23
+ /**
24
+ * Maps provided `monad`'s right value to a new value,
25
+ * then returns it or `undefined`.
26
+ */
27
+ export default function bifoldrMap<Monad extends Right<any>, Value>(monad: Monad, map: Map<Bifoldr<Monad>, Value>): Value;
@@ -3,18 +3,19 @@
3
3
  * @module Either Operators
4
4
  */
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ var guards_1 = require("../guards");
6
7
  var compose_1 = require("../../../compose");
7
- var ternary_1 = require("../../../ternary");
8
+ var bifoldr_1 = require("../bifold/bifoldr");
8
9
  var permutation_2_1 = require("../../../permutation/permutation-2");
9
- var guards_1 = require("../guards");
10
- var bifold_1 = require("../bifold");
11
10
  function bifoldrMap() {
12
11
  var args = [];
13
12
  for (var _i = 0; _i < arguments.length; _i++) {
14
13
  args[_i] = arguments[_i];
15
14
  }
16
15
  return (0, permutation_2_1.default)(function (monad, map) {
17
- return (0, ternary_1.default)(guards_1.isRight, (0, compose_1.default)(map, bifold_1.bifoldr), function () { return undefined; })(monad);
16
+ return (0, guards_1.isRight)(monad)
17
+ ? (0, compose_1.default)(map, bifoldr_1.default, monad)
18
+ : undefined;
18
19
  }).apply(void 0, args);
19
20
  }
20
21
  exports.default = bifoldrMap;
@@ -4,7 +4,6 @@
4
4
  */
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  var guards_1 = require("../guards");
7
- var ternary_1 = require("../../../ternary");
8
7
  var permutation_3_1 = require("../../../permutation/permutation-3");
9
8
  var first_1 = require("./first");
10
9
  var second_1 = require("./second");
@@ -14,7 +13,9 @@ function bimap() {
14
13
  args[_i] = arguments[_i];
15
14
  }
16
15
  return (0, permutation_3_1.default)(function (monad, mapLeft, mapRight) {
17
- return (0, ternary_1.default)(guards_1.isLeft, (0, first_1.default)(mapLeft), (0, second_1.default)(mapRight))(monad);
16
+ return (0, guards_1.isLeft)(monad)
17
+ ? (0, first_1.default)(monad, mapLeft)
18
+ : (0, second_1.default)(monad, mapRight);
18
19
  }).apply(void 0, args);
19
20
  }
20
21
  exports.default = bimap;
@@ -1,8 +1,10 @@
1
1
  /**
2
2
  * @module Either Operators
3
3
  */
4
+ import { Left } from '../../left';
4
5
  import type { Either } from '../../either';
5
6
  import type { Map } from '../../../types/map';
7
+ export type First<Monad extends Either<any, any>> = Monad extends Left<infer LeftValue> ? LeftValue : Monad extends Either<infer LeftValue, any> ? LeftValue : never;
6
8
  /**
7
9
  * Maps the left value of the provided `monad` to a new `Either` monad
8
10
  * with the same right value.
@@ -3,11 +3,10 @@
3
3
  * @module Either Operators
4
4
  */
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- var left_1 = require("../../left");
7
6
  var guards_1 = require("../guards");
8
- var bifold_1 = require("../bifold");
9
7
  var compose_1 = require("../../../compose");
10
- var ternary_1 = require("../../../ternary");
8
+ var bifoldl_1 = require("../bifold/bifoldl");
9
+ var left_1 = require("../../left");
11
10
  var identity_1 = require("../../../identity");
12
11
  var permutation_2_1 = require("../../../permutation/permutation-2");
13
12
  function first() {
@@ -16,7 +15,9 @@ function first() {
16
15
  args[_i] = arguments[_i];
17
16
  }
18
17
  return (0, permutation_2_1.default)(function (monad, mapLeft) {
19
- return (0, ternary_1.default)(guards_1.isLeft, (0, compose_1.default)((0, compose_1.default)(left_1.default, mapLeft), bifold_1.bifoldl), identity_1.default)(monad);
18
+ return (0, guards_1.isLeft)(monad)
19
+ ? (0, compose_1.default)((0, compose_1.default)(left_1.default, mapLeft), bifoldl_1.default, monad)
20
+ : (0, identity_1.default)(monad);
20
21
  }).apply(void 0, args);
21
22
  }
22
23
  exports.default = first;
@@ -2,7 +2,9 @@
2
2
  * @module Either Operators
3
3
  */
4
4
  import type { Either } from '../../either';
5
+ import { Right } from '../../right';
5
6
  import type { Map } from '../../../types/map';
7
+ export type Second<Monad extends Either<any, any>> = Monad extends Right<infer RightValue> ? RightValue : Monad extends Either<any, infer RightValue> ? RightValue : never;
6
8
  /**
7
9
  * Maps the right value of the provided `monad` to a new `Either` monad
8
10
  * with the same left value.
@@ -3,12 +3,11 @@
3
3
  * @module Either Operators
4
4
  */
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- var right_1 = require("../../right");
7
6
  var bifold_1 = require("../bifold");
8
7
  var guards_1 = require("../guards");
9
8
  var compose_1 = require("../../../compose");
10
- var ternary_1 = require("../../../ternary");
11
9
  var identity_1 = require("../../../identity");
10
+ var right_1 = require("../../right");
12
11
  var permutation_2_1 = require("../../../permutation/permutation-2");
13
12
  function second() {
14
13
  var args = [];
@@ -16,7 +15,9 @@ function second() {
16
15
  args[_i] = arguments[_i];
17
16
  }
18
17
  return (0, permutation_2_1.default)(function (monad, mapRight) {
19
- return (0, ternary_1.default)(guards_1.isRight, (0, compose_1.default)((0, compose_1.default)(right_1.default, mapRight), bifold_1.bifoldr), identity_1.default)(monad);
18
+ return (0, guards_1.isRight)(monad)
19
+ ? (0, compose_1.default)((0, compose_1.default)(right_1.default, mapRight), bifold_1.bifoldr, monad)
20
+ : (0, identity_1.default)(monad);
20
21
  }).apply(void 0, args);
21
22
  }
22
23
  exports.default = second;
@@ -4,7 +4,6 @@
4
4
  */
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  var compose_1 = require("../../compose");
7
- var ternary_1 = require("../../ternary");
8
7
  var identity_1 = require("../../identity");
9
8
  var permutation_2_1 = require("../../permutation/permutation-2");
10
9
  var bifold_1 = require("./bifold");
@@ -15,7 +14,9 @@ function bindr() {
15
14
  args[_i] = arguments[_i];
16
15
  }
17
16
  return (0, permutation_2_1.default)(function (monad, mapRight) {
18
- return (0, ternary_1.default)(guards_1.isRight, (0, compose_1.default)(mapRight, bifold_1.bifoldr), identity_1.default)(monad);
17
+ return (0, guards_1.isRight)(monad)
18
+ ? (0, compose_1.default)(mapRight, bifold_1.bifoldr, monad)
19
+ : (0, identity_1.default)(monad);
19
20
  }).apply(void 0, args);
20
21
  }
21
22
  exports.default = bindr;
package/either/right.d.ts CHANGED
@@ -10,7 +10,7 @@ export declare const rid: unique symbol;
10
10
  * Describes the special object with one property, represented as unique symbol
11
11
  * key, holding the provided value type.
12
12
  */
13
- export declare type Right<Value> = {
13
+ export type Right<Value> = {
14
14
  [rid]: Value;
15
15
  };
16
16
  /**
package/get.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ /**
2
+ * @module Get
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 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;
10
+ });
11
+ /**
12
+ * Gets the value type inside a nested object type `Source` by provided `Path`
13
+ * written in dot-notation.
14
+ */
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;
16
+ /**
17
+ * Gets the value inside a nested `source` object by provided `path`
18
+ * written in dot-notation.
19
+ */
20
+ export default function get<Source extends Record<string, unknown> | any[], Path extends Flatten<Source>>(path: Path): (source: Source) => Get<Source, Path>;
21
+ /**
22
+ * Gets the value inside a nested `source` object by provided `path`
23
+ * written in dot-notation.
24
+ */
25
+ export default function get<Source extends Record<string, unknown> | any[], Path extends Flatten<Source>>(source: Source, path: Path): Get<Source, Path>;
package/get.js ADDED
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ /**
3
+ * @module Get
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ var permutation_2_1 = require("./permutation/permutation-2");
7
+ /**
8
+ * Gets the value inside a nested `source` object by provided `path`
9
+ * written in dot-notation.
10
+ */
11
+ function get() {
12
+ var args = [];
13
+ for (var _i = 0; _i < arguments.length; _i++) {
14
+ args[_i] = arguments[_i];
15
+ }
16
+ return (0, permutation_2_1.default)(function (source, path) {
17
+ var keys = path.split('.');
18
+ var length = keys.length;
19
+ var result = source;
20
+ for (var index = 0; index < length; index += 1) {
21
+ result = result[keys[index]];
22
+ }
23
+ return result;
24
+ }).apply(void 0, args);
25
+ }
26
+ exports.default = get;
package/guard.d.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  * @module Guard
3
3
  */
4
4
  import type { VariadicFunction } from './types/function';
5
- export declare type GuardQualifier<Function extends VariadicFunction> = [
5
+ export type GuardQualifier<Function extends VariadicFunction> = [
6
6
  validator: <Result extends boolean>(...args: Parameters<Function>) => Result,
7
7
  executor: Function
8
8
  ];
package/identity.d.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  /**
5
5
  * A type describing the `identity` function.
6
6
  */
7
- export declare type Identity<T> = (x: T) => T;
7
+ export type Identity<T> = (x: T) => T;
8
8
  /**
9
9
  * A function which returns the provided value.
10
10
  */
package/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export { default as get } from './get';
1
2
  export { default as tap } from './tap';
2
3
  export { default as pipe } from './pipe';
3
4
  export { default as curry } from './curry';
@@ -5,5 +6,4 @@ export { default as match } from './guard';
5
6
  export { default as inject } from './inject';
6
7
  export { default as compose } from './compose';
7
8
  export { default as rethrow } from './rethrow';
8
- export { default as ternary } from './ternary';
9
9
  export { default as identity } from './identity';
package/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.identity = exports.ternary = exports.rethrow = exports.compose = exports.inject = exports.match = exports.curry = exports.pipe = exports.tap = void 0;
3
+ exports.identity = exports.rethrow = exports.compose = exports.inject = exports.match = exports.curry = exports.pipe = exports.tap = exports.get = void 0;
4
+ var get_1 = require("./get");
5
+ Object.defineProperty(exports, "get", { enumerable: true, get: function () { return get_1.default; } });
4
6
  var tap_1 = require("./tap");
5
7
  Object.defineProperty(exports, "tap", { enumerable: true, get: function () { return tap_1.default; } });
6
8
  var pipe_1 = require("./pipe");
@@ -15,7 +17,5 @@ var compose_1 = require("./compose");
15
17
  Object.defineProperty(exports, "compose", { enumerable: true, get: function () { return compose_1.default; } });
16
18
  var rethrow_1 = require("./rethrow");
17
19
  Object.defineProperty(exports, "rethrow", { enumerable: true, get: function () { return rethrow_1.default; } });
18
- var ternary_1 = require("./ternary");
19
- Object.defineProperty(exports, "ternary", { enumerable: true, get: function () { return ternary_1.default; } });
20
20
  var identity_1 = require("./identity");
21
21
  Object.defineProperty(exports, "identity", { enumerable: true, get: function () { return identity_1.default; } });
package/maybe/just.d.ts CHANGED
@@ -10,7 +10,7 @@ export declare const jid: unique symbol;
10
10
  * Describes the special object with one property, represented as unique symbol
11
11
  * key, holding the provided value type.
12
12
  */
13
- export declare type Just<Value> = {
13
+ export type Just<Value> = {
14
14
  [jid]: Value;
15
15
  };
16
16
  /**
package/maybe/maybe.d.ts CHANGED
@@ -7,7 +7,7 @@ import { Nothing } from './nothing';
7
7
  * Type constructor for `Maybe`.
8
8
  * Can be either `Just` of a provided value type, or `Nothing`.
9
9
  */
10
- export declare type Maybe<Value> = Just<Value> | Nothing;
10
+ export type Maybe<Value> = Just<Value> | Nothing;
11
11
  /**
12
12
  * Constructor function for `Maybe`.
13
13
  * Creates either `just` of a provided value, or `nothing`.
@@ -10,7 +10,7 @@ export declare const nid: unique symbol;
10
10
  * Describes the special object with one property, represented as unique symbol
11
11
  * key, holding the `null` value type.
12
12
  */
13
- export declare type Nothing = {
13
+ export type Nothing = {
14
14
  [nid]: undefined;
15
15
  };
16
16
  /**
@@ -4,7 +4,6 @@
4
4
  */
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  var compose_1 = require("../../compose");
7
- var ternary_1 = require("../../ternary");
8
7
  var identity_1 = require("../../identity");
9
8
  var permutation_2_1 = require("../../permutation/permutation-2");
10
9
  var fold_1 = require("./fold");
@@ -15,7 +14,9 @@ function bind() {
15
14
  args[_i] = arguments[_i];
16
15
  }
17
16
  return (0, permutation_2_1.default)(function (monad, map) {
18
- return (0, ternary_1.default)(guards_1.isJust, (0, compose_1.default)(map, fold_1.default), identity_1.default)(monad);
17
+ return (0, guards_1.isJust)(monad)
18
+ ? (0, compose_1.default)(map, fold_1.default, monad)
19
+ : (0, identity_1.default)(monad);
19
20
  }).apply(void 0, args);
20
21
  }
21
22
  exports.default = bind;
@@ -7,9 +7,9 @@ import type { Map } from '../../types/map';
7
7
  * Maps the value of the provided `monad` through the `transition` function
8
8
  * and returns a new `Maybe` of the mapped value.
9
9
  */
10
- export default function fmap<Value, NextValue>(map: Map<Value, NextValue>): (monad: Maybe<Value>) => Maybe<NextValue>;
10
+ export default function fmap<Value, NextValue>(map: Map<Value, NextValue>): (monad: Maybe<Value>) => Maybe<NonNullable<NextValue>>;
11
11
  /**
12
12
  * Maps the value of the provided `monad` through the `transition` function
13
13
  * and returns a new `Maybe` of the mapped value.
14
14
  */
15
- export default function fmap<Value, NextValue>(monad: Maybe<Value>, map: Map<Value, NextValue>): Maybe<NextValue>;
15
+ export default function fmap<Value, NextValue>(monad: Maybe<Value>, map: Map<Value, NextValue>): Maybe<NonNullable<NextValue>>;
@@ -9,14 +9,15 @@ var maybe_1 = require("../maybe");
9
9
  var permutation_2_1 = require("../../permutation/permutation-2");
10
10
  var fold_map_1 = require("./fold-map");
11
11
  var guards_1 = require("./guards");
12
- var ternary_1 = require("../../ternary");
13
12
  function fmap() {
14
13
  var args = [];
15
14
  for (var _i = 0; _i < arguments.length; _i++) {
16
15
  args[_i] = arguments[_i];
17
16
  }
18
17
  return (0, permutation_2_1.default)(function (monad, map) {
19
- return (0, ternary_1.default)(guards_1.isJust, (0, compose_1.default)((maybe_1.default), (0, fold_map_1.default)(map)), identity_1.default)(monad);
18
+ return (0, guards_1.isJust)(monad)
19
+ ? (0, compose_1.default)((maybe_1.default), (0, fold_map_1.default)(map), monad)
20
+ : (0, identity_1.default)(monad);
20
21
  }).apply(void 0, args);
21
22
  }
22
23
  exports.default = fmap;
@@ -8,14 +8,14 @@ import type { Map } from '../../types/map';
8
8
  /**
9
9
  * A type to unwrap the provided `Monad` into the new value type.
10
10
  */
11
- export declare type FoldMap<Monad extends Maybe<any>, NextValue> = Monad extends Just<any> ? NextValue : Monad extends Nothing ? undefined : Monad extends Maybe<any> ? NextValue | undefined : never;
11
+ export type FoldMap<Monad extends Maybe<any>, NextValue> = Monad extends Just<any> ? NextValue : Monad extends Nothing ? undefined : Monad extends Maybe<any> ? NextValue | undefined : never;
12
12
  /**
13
13
  * Maps the value of the provided `monad` through the `transition` function
14
14
  * and returns the mapped value or `null`.
15
15
  */
16
- export default function foldMap<Value, NextValue = Value>(map: Map<Value, NextValue>): (monad: Maybe<Value>) => NextValue | undefined;
16
+ export default function foldMap<Value, NextValue>(map: Map<Value, NextValue>): (monad: Maybe<Value>) => NextValue | undefined;
17
17
  /**
18
18
  * Maps the value of the provided `monad` through the `transition` function
19
19
  * and returns the mapped value or `null`.
20
20
  */
21
- export default function foldMap<Value, NextValue = Value>(monad: Maybe<Value>, map: Map<Value, NextValue>): NextValue | undefined;
21
+ export default function foldMap<Value, NextValue>(monad: Maybe<Value>, map: Map<Value, NextValue>): NextValue | undefined;
@@ -4,17 +4,18 @@
4
4
  */
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  var compose_1 = require("../../compose");
7
- var ternary_1 = require("../../ternary");
8
7
  var permutation_2_1 = require("../../permutation/permutation-2");
9
- var fold_1 = require("./fold");
10
8
  var guards_1 = require("./guards");
9
+ var fold_1 = require("./fold");
11
10
  function foldMap() {
12
11
  var args = [];
13
12
  for (var _i = 0; _i < arguments.length; _i++) {
14
13
  args[_i] = arguments[_i];
15
14
  }
16
15
  return (0, permutation_2_1.default)(function (monad, map) {
17
- return (0, ternary_1.default)(guards_1.isJust, (0, compose_1.default)(map, fold_1.default), function () { return undefined; })(monad);
16
+ return (0, guards_1.isJust)(monad)
17
+ ? (0, compose_1.default)(map, fold_1.default, monad)
18
+ : undefined;
18
19
  }).apply(void 0, args);
19
20
  }
20
21
  exports.default = foldMap;
@@ -7,7 +7,11 @@ import { Nothing } from '../nothing';
7
7
  /**
8
8
  * A type to unwrap the value type of the provided `Monad`.
9
9
  */
10
- export declare type Fold<Monad extends Maybe<any>> = Monad extends Just<infer Value> ? Value : Monad extends Nothing ? undefined : Monad extends Maybe<infer Value> ? Value | undefined : never;
10
+ export type Fold<Monad extends Maybe<any>> = Monad extends Just<infer Value> ? Value : Monad extends Nothing ? undefined : Monad extends Maybe<infer Value> ? Value | undefined : never;
11
+ /**
12
+ * Returns the value of the provided `monad`.
13
+ */
14
+ export default function fold<Monad extends Just<any>>(monad: Monad): Fold<Monad>;
11
15
  /**
12
16
  * Returns the value of the provided `monad`.
13
17
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fnts",
3
- "version": "2.0.0-rc.1",
3
+ "version": "2.0.1",
4
4
  "license": "MIT",
5
5
  "author": "Dmytro Vasylkivskyi",
6
6
  "description": "TypeScript / JavaScript FP utilities",
@@ -15,7 +15,7 @@
15
15
  "homepage": "https://drizzer14.github.io/fnts",
16
16
  "repository": "https://github.com/drizzer14/fnts",
17
17
  "peerDependencies": {
18
- "typescript": ">=4.2.2"
18
+ "typescript": ">=4.7"
19
19
  },
20
20
  "peerDependenciesMeta": {
21
21
  "typescript": {
@@ -2,7 +2,7 @@
2
2
  * @module Permutation
3
3
  */
4
4
  import type { VariadicFunction } from './../types/function';
5
- export declare type BinaryFunction = (a: any, b: any) => any;
6
- export declare type FirstParameter<Function extends VariadicFunction> = Parameters<Function>[0];
7
- export declare type SecondParameter<Function extends VariadicFunction> = Parameters<Function>[1];
5
+ export type BinaryFunction = (a: any, b: any) => any;
6
+ export type FirstParameter<Function extends VariadicFunction> = Parameters<Function>[0];
7
+ export type SecondParameter<Function extends VariadicFunction> = Parameters<Function>[1];
8
8
  export default function permutation2<Function extends BinaryFunction>(fn: Function, shouldCurry?: (a: FirstParameter<Function> | SecondParameter<Function>, b: SecondParameter<Function> | undefined) => boolean): VariadicFunction;
@@ -3,6 +3,6 @@
3
3
  */
4
4
  import type { VariadicFunction } from './../types/function';
5
5
  import type { FirstParameter, SecondParameter } from './permutation-2';
6
- export declare type TernaryFunction = (a: any, b: any, c: any) => any;
7
- export declare type ThirdParameter<Function extends VariadicFunction> = Parameters<Function>[2];
6
+ export type TernaryFunction = (a: any, b: any, c: any) => any;
7
+ export type ThirdParameter<Function extends VariadicFunction> = Parameters<Function>[2];
8
8
  export default function permutation3<Function extends TernaryFunction>(fn: Function, shouldCurry?: (a: FirstParameter<Function> | SecondParameter<Function>, b: SecondParameter<Function> | ThirdParameter<Function>, c: ThirdParameter<Function> | undefined) => boolean): VariadicFunction;
package/pipe.d.ts CHANGED
@@ -1,11 +1,24 @@
1
1
  /**
2
2
  * @module Composition
3
3
  */
4
+ import type { UnaryFunction } from './types/function';
4
5
  /**
5
- * Applies second onto first onto args.
6
+ * Makes a pipeline of functions from received arguments.
6
7
  */
7
- export default function pipe<First, Argument, Return>(first: (arg: Argument) => First, second: (arg: First) => Return): (arg: Argument) => Return;
8
+ export type Pipe<Arguments extends any[], Functions extends any[] = []> = Arguments['length'] extends 0 ? Functions : Arguments extends [infer A, infer B] ? [(arg: A) => B, ...Functions] : Arguments extends [infer A, ...infer Rest, infer P, infer L] ? Pipe<[A, ...Rest, P], [(arg: P) => L, ...Functions]> : [];
8
9
  /**
9
- * Applies second onto first onto args.
10
+ * Destructures a pipeline of functions into arguments.
10
11
  */
11
- export default function pipe<First, Arguments extends any[], Return>(first: (...args: Arguments) => First, second: (arg: First) => Return): (...args: Arguments) => Return;
12
+ export type Depipe<Functions extends UnaryFunction[], Arguments extends any[] = []> = Functions extends [(arg: infer Arg) => infer Return] ? [...Arguments, Arg, Return] : Functions extends [(arg: infer Arg) => any, ...infer Rest extends UnaryFunction[]] ? Depipe<Rest, [...Arguments, Arg]> : [];
13
+ /**
14
+ * (A -> B) . (B -> C) = A -> C
15
+ */
16
+ export default function pipe<A extends any[], B, C>(first: (...args: A) => B, second: (arg: B) => C): (...args: A) => C;
17
+ /**
18
+ * (A -> B) . (B -> C) = A -> C
19
+ */
20
+ export default function pipe<A extends any[], B, C>(...args: [
21
+ ...args: A,
22
+ first: (...args: A) => B,
23
+ second: (arg: B) => C
24
+ ]): C;
package/pipe.js CHANGED
@@ -4,15 +4,21 @@
4
4
  */
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  /**
7
- * Applies second onto first onto args.
7
+ * (A -> B) . (B -> C) = A -> C
8
8
  */
9
- function pipe(first, second) {
10
- return function () {
11
- var args = [];
12
- for (var _i = 0; _i < arguments.length; _i++) {
13
- args[_i] = arguments[_i];
9
+ function pipe() {
10
+ var maybeArgs = [];
11
+ for (var _i = 0; _i < arguments.length; _i++) {
12
+ maybeArgs[_i] = arguments[_i];
13
+ }
14
+ return maybeArgs.length === 2
15
+ ? function () {
16
+ var args = [];
17
+ for (var _i = 0; _i < arguments.length; _i++) {
18
+ args[_i] = arguments[_i];
19
+ }
20
+ return maybeArgs[1](maybeArgs[0].apply(maybeArgs, args));
14
21
  }
15
- return second(first.apply(void 0, args));
16
- };
22
+ : maybeArgs[maybeArgs.length - 1](maybeArgs[maybeArgs.length - 2].apply(maybeArgs, maybeArgs.slice(0, -2)));
17
23
  }
18
24
  exports.default = pipe;
package/tsconfig.json CHANGED
@@ -11,10 +11,12 @@
11
11
  "alwaysStrict": true,
12
12
  "skipLibCheck": true,
13
13
  "noImplicitAny": true,
14
- "noUnusedLocals": true,
14
+ "noUnusedLocals": false,
15
+ "keyofStringsOnly": true,
15
16
  "strictNullChecks": true,
16
17
  "noImplicitReturns": true,
17
- "noUnusedParameters": true,
18
+ "noErrorTruncation": true,
19
+ "noUnusedParameters": false,
18
20
  "strictFunctionTypes": true,
19
21
  "strictBindCallApply": true,
20
22
  "noStrictGenericChecks": true,
package/ternary.d.ts DELETED
@@ -1,10 +0,0 @@
1
- /**
2
- * @module Ternary
3
- */
4
- import type { Map } from './types/map';
5
- /**
6
- * Functional alternative to the ternary operator.
7
- * Enforces the same return type for both branches,
8
- * the result is evaluated lazily.
9
- */
10
- export default function ternary<Value, Return>(predicate: Map<Value, boolean>, left: Map<Value, Return>, right: Map<Value, Return>): Map<Value, Return>;
package/ternary.js DELETED
@@ -1,16 +0,0 @@
1
- "use strict";
2
- /**
3
- * @module Ternary
4
- */
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- /**
7
- * Functional alternative to the ternary operator.
8
- * Enforces the same return type for both branches,
9
- * the result is evaluated lazily.
10
- */
11
- function ternary(predicate, left, right) {
12
- return function (value) {
13
- return predicate(value) ? left(value) : right(value);
14
- };
15
- }
16
- exports.default = ternary;