type-plus 4.2.0 → 4.3.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/README.md CHANGED
@@ -326,6 +326,7 @@ nominalMatch(b1, b2) // false
326
326
  ## Functional Types
327
327
 
328
328
  - `ChainFn<T>: T`: chain function that returns the input type.
329
+ - `compose(...fns): F`: compose functions
329
330
 
330
331
  ## Type Utilities
331
332
 
@@ -0,0 +1,6 @@
1
+ import { Head, Last } from '../array';
2
+ import { AnyFunction } from '../function/AnyFunction';
3
+ /**
4
+ * compose functions
5
+ */
6
+ export declare function compose<FS extends AnyFunction[]>(...fns: FS): (...args: Parameters<Head<FS>>) => ReturnType<Last<FS>>;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.compose = void 0;
4
+ /**
5
+ * compose functions
6
+ */
7
+ function compose() {
8
+ var fns = [];
9
+ for (var _i = 0; _i < arguments.length; _i++) {
10
+ fns[_i] = arguments[_i];
11
+ }
12
+ return function () {
13
+ var args = [];
14
+ for (var _i = 0; _i < arguments.length; _i++) {
15
+ args[_i] = arguments[_i];
16
+ }
17
+ return fns.reduce(function (args, fn) { return [fn.apply(void 0, args)]; }, args)[0];
18
+ };
19
+ }
20
+ exports.compose = compose;
21
+ //# sourceMappingURL=compose.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compose.js","sourceRoot":"","sources":["../../ts/functional/compose.ts"],"names":[],"mappings":";;;AAGA;;GAEG;AACH,SAAgB,OAAO;IAA2B,aAAU;SAAV,UAAU,EAAV,qBAAU,EAAV,IAAU;QAAV,wBAAU;;IAC1D,OAAO;QAAC,cAAc;aAAd,UAAc,EAAd,qBAAc,EAAd,IAAc;YAAd,yBAAc;;QAAK,OAAA,GAAG,CAAC,MAAM,CAAC,UAAC,IAAI,EAAE,EAAE,IAAK,OAAA,CAAC,EAAE,eAAI,IAAI,EAAE,EAAb,CAAa,EAAE,IAAI,CAAC,CAAC,CAAC,CAAQ;IAAvD,CAAuD,CAAA;AACpF,CAAC;AAFD,0BAEC"}
@@ -1 +1,2 @@
1
1
  export type { ChainFn, EndoFn } from './ChainFn';
2
+ export * from './compose';
@@ -1,3 +1,18 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./compose"), exports);
3
18
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../ts/functional/index.ts"],"names":[],"mappings":""}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../ts/functional/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAEA,4CAAyB"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * compose functions
3
+ */
4
+ export function compose(...fns) {
5
+ return (...args) => fns.reduce((args, fn) => [fn(...args)], args)[0];
6
+ }
7
+ //# sourceMappingURL=compose.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compose.js","sourceRoot":"","sources":["../../ts/functional/compose.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,MAAM,UAAU,OAAO,CAA2B,GAAG,GAAO;IAC1D,OAAO,CAAC,GAAG,IAAW,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,CAAQ,CAAA;AACpF,CAAC"}
@@ -1,2 +1,2 @@
1
- export {};
1
+ export * from './compose';
2
2
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../ts/functional/index.ts"],"names":[],"mappings":""}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../ts/functional/index.ts"],"names":[],"mappings":"AAEA,cAAc,WAAW,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-plus",
3
- "version": "4.2.0",
3
+ "version": "4.3.0",
4
4
  "description": "Provides additional types for `typescript`.",
5
5
  "homepage": "https://github.com/unional/type-plus",
6
6
  "bugs": {
@@ -0,0 +1,32 @@
1
+ import { compose, isType } from '..'
2
+
3
+ test('works with endofunctors: +2 *3', () => {
4
+ const plus2 = (x: number) => x + 2
5
+ const multiply3 = (x: number) => x * 3
6
+
7
+ const a = compose(plus2, multiply3)
8
+
9
+ isType.equal<true, (a: number) => number, typeof a>()
10
+ expect(a(2)).toBe(12)
11
+ })
12
+
13
+ test('cross types', () => {
14
+ const n2s = (n: number) => '' + n
15
+ const len1 = (s: string) => s.length === 1
16
+
17
+ const isSingleDigit = compose(n2s, len1)
18
+
19
+ isType.equal<true, (n: number) => boolean, typeof isSingleDigit>()
20
+ expect(isSingleDigit(1)).toBe(true)
21
+ expect(isSingleDigit(10)).toBe(false)
22
+ })
23
+
24
+ test('multiple params first function', () => {
25
+ const add = (a: number, b: number) => a + b
26
+ const multiply3 = (x: number) => x * 3
27
+
28
+ const a = compose(add, multiply3)
29
+
30
+ isType.equal<true, (a: number, b: number) => number, typeof a>()
31
+ expect(a(2, 2)).toBe(12)
32
+ })
@@ -0,0 +1,9 @@
1
+ import { Head, Last } from '../array'
2
+ import { AnyFunction } from '../function/AnyFunction'
3
+
4
+ /**
5
+ * compose functions
6
+ */
7
+ export function compose<FS extends AnyFunction[]>(...fns: FS): (...args: Parameters<Head<FS>>) => ReturnType<Last<FS>> {
8
+ return (...args: any[]) => fns.reduce((args, fn) => [fn(...args)], args)[0] as any
9
+ }
@@ -1,2 +1,3 @@
1
1
  // export * from './Maybe'
2
2
  export type { ChainFn, EndoFn } from './ChainFn'
3
+ export * from './compose'