pepka 0.12.3 → 0.13.0-b10
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 +10 -7
- package/dist/bundle.d.ts +474 -84
- package/dist/bundle.dev.js +114 -73
- package/dist/bundle.js +1 -1
- package/dist/es/curry.js +29 -1
- package/dist/es/index.js +1 -0
- package/dist/es/quick.js +8 -8
- package/dist/es/safe.js +70 -66
- package/dist/es/uncurry.js +3 -0
- package/dist/es/utils.js +1 -0
- package/dts-fix.js +11 -0
- package/package.json +15 -12
- package/rollup.config.js +2 -1
- package/src/curry.ts +55 -11
- package/src/index.ts +1 -0
- package/src/quick.ts +9 -9
- package/src/safe.ts +85 -96
- package/src/types.ts +10 -3
- package/src/uncurry.ts +11 -0
- package/src/utils.ts +1 -0
- package/tsconfig.json +2 -4
package/dist/es/safe.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { __, curry } from './curry';
|
|
2
|
-
import { isNum,
|
|
1
|
+
import { __, curry, curry2, curry3 } from './curry';
|
|
2
|
+
import { isNum, isUndef, undef, isNull, isArray, isFunc, isStr, isObj, inf } from './utils';
|
|
3
3
|
import { qmergeDeep, qreduce, qappend, qmapKeys, qmergeDeepX, qmergeDeepAdd } from './quick';
|
|
4
4
|
import { type } from './common';
|
|
5
5
|
// over, lensProp
|
|
6
|
-
export const equals =
|
|
6
|
+
export const equals = curry2((a, b) => {
|
|
7
7
|
const typea = type(a);
|
|
8
8
|
if (typea === type(b) && (typea === 'Object' || typea == 'Array')) {
|
|
9
9
|
if (isNull(a) || isNull(b)) {
|
|
@@ -25,16 +25,17 @@ export const equals = curry((a, b) => {
|
|
|
25
25
|
return a === b;
|
|
26
26
|
});
|
|
27
27
|
export const ifElse = curry((cond, pipeYes, pipeNo, s) => cond(s) ? pipeYes(s) : pipeNo(s));
|
|
28
|
-
export const when =
|
|
29
|
-
export const compose = ((...fns) => (s =
|
|
28
|
+
export const when = curry3((cond, pipe, s) => ifElse(cond, pipe, identity, s));
|
|
29
|
+
export const compose = ((...fns) => (s = Symbol()) => {
|
|
30
30
|
for (let i = length(fns) - 1; i > -1; i--) {
|
|
31
31
|
s = s === __ ? fns[i]() : fns[i](s);
|
|
32
32
|
}
|
|
33
33
|
return s;
|
|
34
|
-
});
|
|
35
|
-
export const bind =
|
|
36
|
-
|
|
37
|
-
export const
|
|
34
|
+
});
|
|
35
|
+
export const bind = curry2((fn, context) => fn.bind(context));
|
|
36
|
+
const _nth = (i, data) => data[i];
|
|
37
|
+
export const nth = curry2(_nth);
|
|
38
|
+
export const includes = curry2((s, ss) => {
|
|
38
39
|
if (isStr(ss)) {
|
|
39
40
|
return ss.includes(s);
|
|
40
41
|
}
|
|
@@ -47,11 +48,11 @@ export const includes = curry((s, ss) => {
|
|
|
47
48
|
return false;
|
|
48
49
|
}
|
|
49
50
|
});
|
|
50
|
-
export const slice =
|
|
51
|
+
export const slice = curry3((from, to, o) => o.slice(from, (isNum(to) ? to : inf)));
|
|
51
52
|
export const head = nth(0);
|
|
52
|
-
export const tail = slice(1,
|
|
53
|
-
export const add =
|
|
54
|
-
export const subtract =
|
|
53
|
+
export const tail = slice(1, inf); // typeshit.
|
|
54
|
+
export const add = curry2((n, m) => n + m);
|
|
55
|
+
export const subtract = curry2((n, m) => m - n);
|
|
55
56
|
export const flip = (fn) => curry((b, a) => fn(a, b));
|
|
56
57
|
export const isNil = (s) => isNull(s) || isUndef(s);
|
|
57
58
|
export const length = (s) => s.length;
|
|
@@ -67,10 +68,10 @@ export const complement = (fn) => (...args) => {
|
|
|
67
68
|
export const keys = (o) => Object.keys(o);
|
|
68
69
|
export const values = (o) => Object.values(o);
|
|
69
70
|
export const toPairs = (o) => Object.entries(o);
|
|
70
|
-
export const test =
|
|
71
|
-
export const tap =
|
|
72
|
-
export const append =
|
|
73
|
-
export const split =
|
|
71
|
+
export const test = curry2((re, s) => re.test(s));
|
|
72
|
+
export const tap = curry2((fn, s) => { fn(s); return s; });
|
|
73
|
+
export const append = curry2((s, xs) => [...xs, s]);
|
|
74
|
+
export const split = curry2((s, xs) => xs.split(s));
|
|
74
75
|
export const T = always(true);
|
|
75
76
|
export const F = always(false);
|
|
76
77
|
export const sizeof = (s) => {
|
|
@@ -83,10 +84,10 @@ export const sizeof = (s) => {
|
|
|
83
84
|
else
|
|
84
85
|
return length(s);
|
|
85
86
|
};
|
|
86
|
-
export const range =
|
|
87
|
+
export const range = curry2((from, to) => genBy(add(from), to - from));
|
|
87
88
|
export const uniq = (xs) => qreduce((accum, x) => includes(x, accum) ? accum : qappend(x, accum), [], xs);
|
|
88
|
-
export const intersection =
|
|
89
|
-
export const genBy =
|
|
89
|
+
export const intersection = curry2((xs1, xs2) => xs1.filter(flip(includes)(xs2)));
|
|
90
|
+
export const genBy = curry2((generator, length) => [...Array(length)].map((_, i) => generator(i)));
|
|
90
91
|
export const once = (fn) => {
|
|
91
92
|
let done = false, cache;
|
|
92
93
|
return (...args) => {
|
|
@@ -100,50 +101,51 @@ export const once = (fn) => {
|
|
|
100
101
|
};
|
|
101
102
|
};
|
|
102
103
|
export const reverse = (xs) => compose((ln) => reduce((nxs, _, i) => qappend(xs[ln - i], nxs), [], xs), add(-1), length)(xs);
|
|
103
|
-
export const gt =
|
|
104
|
-
export const lt =
|
|
105
|
-
export const gte =
|
|
106
|
-
export const lte =
|
|
107
|
-
|
|
108
|
-
export const
|
|
109
|
-
export const
|
|
110
|
-
export const
|
|
111
|
-
export const indexOf = curry((x, xs) => findIndex(equals(x), xs));
|
|
104
|
+
export const gt = curry2((a, b) => a > b);
|
|
105
|
+
export const lt = curry2((a, b) => a < b);
|
|
106
|
+
export const gte = curry2((a, b) => b >= a);
|
|
107
|
+
export const lte = curry2((a, b) => b <= a);
|
|
108
|
+
export const sort = curry2((sortFn, xs) => xs.sort(sortFn));
|
|
109
|
+
export const find = curry2((fn, s) => s.find(fn));
|
|
110
|
+
export const findIndex = curry2((fn, s) => s.findIndex(fn));
|
|
111
|
+
export const indexOf = curry2((x, xs) => findIndex(equals(x), xs));
|
|
112
112
|
export const explore = (caption, level = 'log') => tap((v) => console[level](caption, v));
|
|
113
|
-
export const cond =
|
|
113
|
+
export const cond = curry2((pairs, s) => {
|
|
114
114
|
for (const [cond, fn] of pairs) {
|
|
115
115
|
if (cond(s)) {
|
|
116
116
|
return fn(s);
|
|
117
117
|
}
|
|
118
118
|
}
|
|
119
119
|
});
|
|
120
|
-
export const assoc =
|
|
120
|
+
export const assoc = curry3((prop, v, obj) => ({
|
|
121
121
|
...obj,
|
|
122
122
|
[prop]: v
|
|
123
123
|
}));
|
|
124
|
-
export const assocPath =
|
|
124
|
+
export const assocPath = curry3((_path, v, o) => compose((first) => assoc(first, length(_path) < 2
|
|
125
125
|
? v
|
|
126
|
-
: assocPath(slice(1,
|
|
127
|
-
export const all =
|
|
128
|
-
export const any =
|
|
129
|
-
export const allPass =
|
|
130
|
-
export const anyPass =
|
|
131
|
-
export const prop =
|
|
132
|
-
export const propEq =
|
|
133
|
-
export const propsEq =
|
|
134
|
-
export const pathOr =
|
|
126
|
+
: assocPath(slice(1, inf, _path), v, isObj(o[first]) ? o[first] : {}), o), head)(_path));
|
|
127
|
+
export const all = curry2((pred, xs) => xs.every(pred));
|
|
128
|
+
export const any = curry2((pred, xs) => xs.some(pred));
|
|
129
|
+
export const allPass = curry2((preds, x) => preds.every((pred) => pred(x)));
|
|
130
|
+
export const anyPass = curry2((preds, x) => preds.some((pred) => pred(x)));
|
|
131
|
+
export const prop = curry2((key, o) => o[key]);
|
|
132
|
+
export const propEq = curry3((key, value, o) => equals(o[key], value));
|
|
133
|
+
export const propsEq = curry3((key, o1, o2) => equals(o1[key], o2[key]));
|
|
134
|
+
export const pathOr = curry3((_default, path, o) => ifElse(length, () => isNil(o)
|
|
135
135
|
? _default
|
|
136
|
-
: compose(ifElse(isNil, always(_default), (o) => pathOr(_default, slice(1,
|
|
136
|
+
: compose(ifElse(isNil, always(_default), (o) => pathOr(_default, slice(1, inf, path), o)), flip(prop)(o), head)(path), always(o), path));
|
|
137
137
|
export const path = pathOr(undef);
|
|
138
|
-
export const pathEq =
|
|
139
|
-
export const pathsEq =
|
|
138
|
+
export const pathEq = curry3((_path, value, o) => equals(path(_path, o), value));
|
|
139
|
+
export const pathsEq = curry3((_path, o1, o2) => equals(path(_path, o1), path(_path, o2)));
|
|
140
140
|
const typed_arr_re = /^(.*?)(8|16|32|64)(Clamped)?Array$/;
|
|
141
|
-
export const clone = (s) => {
|
|
141
|
+
export const clone = (s, shallow = false) => {
|
|
142
142
|
const t = type(s);
|
|
143
143
|
switch (t) {
|
|
144
144
|
case 'Null': return s;
|
|
145
|
-
case 'Array': return map(clone, s);
|
|
145
|
+
case 'Array': return shallow ? [...s] : map(clone, s);
|
|
146
146
|
case 'Object':
|
|
147
|
+
if (shallow)
|
|
148
|
+
return { ...s };
|
|
147
149
|
const out = {};
|
|
148
150
|
for (let k in s) {
|
|
149
151
|
out[k] = clone(s[k]);
|
|
@@ -155,12 +157,13 @@ export const clone = (s) => {
|
|
|
155
157
|
case 'Symbol':
|
|
156
158
|
return s;
|
|
157
159
|
default:
|
|
158
|
-
return typed_arr_re.test(t) ?
|
|
160
|
+
return typed_arr_re.test(t) ? s.constructor.from(s) : s;
|
|
159
161
|
}
|
|
160
162
|
};
|
|
161
|
-
export const
|
|
162
|
-
export const
|
|
163
|
-
export const
|
|
163
|
+
export const cloneShallow = (s) => clone(s, true);
|
|
164
|
+
export const reduce = curry3((fn, accum, arr) => qreduce(fn, clone(accum), arr));
|
|
165
|
+
export const pickBy = curry2((cond, o) => filter(cond, o));
|
|
166
|
+
export const pick = curry2((props, o) => {
|
|
164
167
|
const out = {};
|
|
165
168
|
for (const p of props) {
|
|
166
169
|
if (p in o) {
|
|
@@ -169,13 +172,13 @@ export const pick = curry((props, o) => {
|
|
|
169
172
|
}
|
|
170
173
|
return out;
|
|
171
174
|
});
|
|
172
|
-
export const omit =
|
|
175
|
+
export const omit = curry2((props, o) => filter((_, k) => !includes(k, props), o));
|
|
173
176
|
export const fromPairs = (pairs) => reduce((o, pair) => assoc(...pair, o), {}, pairs);
|
|
174
|
-
export const concat =
|
|
175
|
-
export const join =
|
|
176
|
-
export const map =
|
|
177
|
-
export const forEach =
|
|
178
|
-
export const both =
|
|
177
|
+
export const concat = curry2(((a, b) => a.concat(b)));
|
|
178
|
+
export const join = curry2((delimeter, arr) => arr.join(delimeter));
|
|
179
|
+
export const map = curry2((pipe, arr) => arr.map(pipe));
|
|
180
|
+
export const forEach = curry2((pipe, arr) => arr.forEach(pipe));
|
|
181
|
+
export const both = curry3((cond1, cond2, s) => cond2(s) && cond1(s));
|
|
179
182
|
export const isEmpty = (s) => {
|
|
180
183
|
switch (type(s)) {
|
|
181
184
|
case 'String':
|
|
@@ -195,8 +198,8 @@ export const empty = (s) => {
|
|
|
195
198
|
default: return undef;
|
|
196
199
|
}
|
|
197
200
|
};
|
|
198
|
-
export const replace =
|
|
199
|
-
export const filter =
|
|
201
|
+
export const replace = curry3((a, b, where) => where.replace(a, b));
|
|
202
|
+
export const filter = curry2((cond, data) => isArray(data)
|
|
200
203
|
? data.filter(cond)
|
|
201
204
|
: compose(fromPairs, filter(([k, v]) => cond(v, k)), toPairs)(data));
|
|
202
205
|
export const memoize = (fn) => {
|
|
@@ -204,13 +207,13 @@ export const memoize = (fn) => {
|
|
|
204
207
|
let cached = false;
|
|
205
208
|
return () => cached ? cache : (cached = true, cache = fn());
|
|
206
209
|
};
|
|
207
|
-
export const mergeShallow =
|
|
208
|
-
export const mergeDeep =
|
|
209
|
-
export const mergeDeepX =
|
|
210
|
-
export const mergeDeepAdd =
|
|
211
|
-
export const overProp =
|
|
210
|
+
export const mergeShallow = curry2((o1, o2) => Object.assign({}, o1, o2));
|
|
211
|
+
export const mergeDeep = curry2((a, b) => qmergeDeep(clone(a), clone(b)));
|
|
212
|
+
export const mergeDeepX = curry2((a, b) => qmergeDeepX(clone(a), clone(b)));
|
|
213
|
+
export const mergeDeepAdd = curry2((a, b) => qmergeDeepAdd(clone(a), clone(b)));
|
|
214
|
+
export const overProp = curry3((prop, pipe, data) => assoc(prop, pipe(data[prop]), data));
|
|
212
215
|
/** mapKeys({ a: 'b' }, { a: 44 }) -> { b: 44 } */
|
|
213
|
-
export const mapKeys =
|
|
216
|
+
export const mapKeys = curry2((keyMap, o) => qmapKeys(keyMap, Object.assign({}, o)));
|
|
214
217
|
// ASYNCS
|
|
215
218
|
/** One promise waits for another. */
|
|
216
219
|
export const forEachSerial = (() => {
|
|
@@ -220,12 +223,13 @@ export const forEachSerial = (() => {
|
|
|
220
223
|
await pipe(fn, items, ++i);
|
|
221
224
|
}
|
|
222
225
|
};
|
|
223
|
-
return
|
|
226
|
+
return curry2((fn, items) => pipe(fn, items, 0));
|
|
224
227
|
})();
|
|
225
228
|
/** Promise.all wrapper for functional pipelining. */
|
|
226
229
|
export const waitAll = (promises) => Promise.all(promises);
|
|
230
|
+
export const waitTap = curry2(async (fn, s) => { await fn(s); return s; });
|
|
227
231
|
/** Waits for all promises mapped by the fn. */
|
|
228
|
-
export const forEachAsync =
|
|
232
|
+
export const forEachAsync = curry2((fn, items) => Promise.all(items.map(fn)));
|
|
229
233
|
/** The same as compose, but waits for promises in chains and returns a Promise. */
|
|
230
234
|
export const composeAsync = (() => {
|
|
231
235
|
const pipe = async (fns, data, i) => ~i ? await pipe(fns, await fns[i](data), --i) : data;
|
package/dist/es/utils.js
CHANGED
package/dts-fix.js
ADDED
package/package.json
CHANGED
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"d.ts"
|
|
20
20
|
],
|
|
21
21
|
"license": "MIT",
|
|
22
|
+
"type": "module",
|
|
22
23
|
"main": "dist/bundle.js",
|
|
23
24
|
"module": "dist/es/index.js",
|
|
24
25
|
"name": "pepka",
|
|
@@ -31,22 +32,21 @@
|
|
|
31
32
|
"test": "npm run gentypes && npm run prod:cjs && ava",
|
|
32
33
|
"test:report": "nyc npm test && nyc report --reporter=text-lcov > coverage.lcov && codecov",
|
|
33
34
|
"test:lazy": "ava",
|
|
34
|
-
"
|
|
35
|
+
"dts-fix": "node dts-fix.js",
|
|
36
|
+
"gentypes": "dts-bundle-generator --no-check -o dist/bundle.d.ts src/index.ts && npm run dts-fix",
|
|
35
37
|
"dev": "cross-env NODE_ENV=development BUILD=es rollup -c",
|
|
36
38
|
"prod:cjs": "cross-env NODE_ENV=production BUILD=cjs rollup -c",
|
|
37
39
|
"prod:es": "cross-env NODE_ENV=production tsc",
|
|
38
40
|
"prod": "npm run gentypes && npm run prod:es && npm run prod:cjs",
|
|
39
41
|
"all": "npm run dev && npm run prod"
|
|
40
42
|
},
|
|
41
|
-
"version": "0.
|
|
43
|
+
"version": "0.13.0b10",
|
|
42
44
|
"ava": {
|
|
43
45
|
"files": [
|
|
44
46
|
"./test/specs/*.ts"
|
|
45
47
|
],
|
|
46
|
-
"serial": false,
|
|
47
48
|
"failFast": true,
|
|
48
49
|
"timeout": "2m",
|
|
49
|
-
"compileEnhancements": false,
|
|
50
50
|
"extensions": [
|
|
51
51
|
"ts"
|
|
52
52
|
],
|
|
@@ -54,24 +54,27 @@
|
|
|
54
54
|
"ts-node/register"
|
|
55
55
|
]
|
|
56
56
|
},
|
|
57
|
+
"dependencies": {
|
|
58
|
+
"ts-toolbelt": "^9.6.0"
|
|
59
|
+
},
|
|
57
60
|
"devDependencies": {
|
|
58
|
-
"@types/node": "^
|
|
59
|
-
"ava": "^
|
|
61
|
+
"@types/node": "^18.11.10",
|
|
62
|
+
"ava": "^5.1.0",
|
|
60
63
|
"codecov": "^3.8.2",
|
|
61
64
|
"cross-env": "^7.0.3",
|
|
62
|
-
"dts-bundle-generator": "^
|
|
65
|
+
"dts-bundle-generator": "^7.1.0",
|
|
63
66
|
"nyc": "^15.1.0",
|
|
64
|
-
"
|
|
67
|
+
"prepend": "^1.0.2",
|
|
68
|
+
"rollup": "^3.5.1",
|
|
65
69
|
"rollup-plugin-commonjs": "^10.1.0",
|
|
66
70
|
"rollup-plugin-node-resolve": "^5.2.0",
|
|
67
71
|
"rollup-plugin-replace": "^2.2.0",
|
|
68
72
|
"rollup-plugin-resolve-aliases": "^0.3.0",
|
|
69
73
|
"rollup-plugin-terser": "^7.0.2",
|
|
70
|
-
"rollup-plugin-typescript2": "^0.
|
|
71
|
-
"ts-node": "^10.
|
|
72
|
-
"ts-toolbelt": "^9.6.0",
|
|
74
|
+
"rollup-plugin-typescript2": "^0.34.1",
|
|
75
|
+
"ts-node": "^10.9.1",
|
|
73
76
|
"tslint": "^6.1.0",
|
|
74
|
-
"typescript": "^4.3
|
|
77
|
+
"typescript": "^4.9.3"
|
|
75
78
|
},
|
|
76
79
|
"types": "./dist/bundle.d.ts",
|
|
77
80
|
"sideEffects": false
|
package/rollup.config.js
CHANGED
|
@@ -3,6 +3,7 @@ import resolve from 'rollup-plugin-node-resolve'
|
|
|
3
3
|
import typescript from 'rollup-plugin-typescript2'
|
|
4
4
|
import { terser } from 'rollup-plugin-terser'
|
|
5
5
|
import replace from 'rollup-plugin-replace'
|
|
6
|
+
import tsc from 'typescript'
|
|
6
7
|
|
|
7
8
|
export default {
|
|
8
9
|
input: process.env.NODE_ENV=='development' ? 'test/in-browser.ts' : 'src/index.ts',
|
|
@@ -16,7 +17,7 @@ export default {
|
|
|
16
17
|
resolve(),
|
|
17
18
|
commonjs(),
|
|
18
19
|
typescript({
|
|
19
|
-
typescript:
|
|
20
|
+
typescript: tsc,
|
|
20
21
|
tsconfig: "./tsconfig.json",
|
|
21
22
|
tsconfigOverride: {
|
|
22
23
|
compilerOptions: {
|
package/src/curry.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import { F as FT } from 'ts-toolbelt'
|
|
2
|
+
import { AnyFunc, AnyArgs } from "./types"
|
|
1
3
|
|
|
2
|
-
type
|
|
4
|
+
type Placeholder = symbol
|
|
5
|
+
export const __: Placeholder = Symbol('Placeholder')
|
|
3
6
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const countArgs = (s: Args) => {
|
|
7
|
+
const countArgs = (s: AnyArgs) => {
|
|
7
8
|
let i = 0
|
|
8
9
|
for (const v of s) v!==__ && i++
|
|
9
10
|
return i
|
|
@@ -11,7 +12,7 @@ const countArgs = (s: Args) => {
|
|
|
11
12
|
|
|
12
13
|
// TODO: try to make it mutable.
|
|
13
14
|
// { 0: __, 1: 10 }, [ 11 ]
|
|
14
|
-
const addArgs = (args:
|
|
15
|
+
const addArgs = (args: AnyArgs, _args: AnyArgs) => {
|
|
15
16
|
const len = args.length
|
|
16
17
|
const new_args = args.slice()
|
|
17
18
|
const _args_len = _args.length
|
|
@@ -29,12 +30,12 @@ const addArgs = (args: Args, _args: Args) => {
|
|
|
29
30
|
return new_args
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
const _curry = (fn: Function, args:
|
|
33
|
+
const _curry = (fn: Function, args: AnyArgs, new_args: AnyArgs) => {
|
|
33
34
|
const args2add = fn.length - args.length - countArgs(new_args)
|
|
34
35
|
if(args2add < 1) {
|
|
35
36
|
return fn(...addArgs(args, new_args))
|
|
36
37
|
} else {
|
|
37
|
-
const curried = (...__args:
|
|
38
|
+
const curried = (...__args: AnyArgs) => _curry(
|
|
38
39
|
fn,
|
|
39
40
|
addArgs(args, new_args),
|
|
40
41
|
__args
|
|
@@ -45,9 +46,52 @@ const _curry = (fn: Function, args: Args, new_args: Args) => {
|
|
|
45
46
|
}
|
|
46
47
|
|
|
47
48
|
export const curry = (
|
|
48
|
-
(fn:
|
|
49
|
-
(...args:
|
|
49
|
+
<Func extends AnyFunc>(fn: AnyFunc) => (
|
|
50
|
+
(...args: AnyArgs) => fn.length>countArgs(args)
|
|
50
51
|
? _curry(fn, [], args)
|
|
51
52
|
: fn(...args)
|
|
52
|
-
)
|
|
53
|
-
)
|
|
53
|
+
) as FT.Curry<Func>
|
|
54
|
+
)
|
|
55
|
+
const endlessph = <Func extends FT.Function>(fn: Func) => {
|
|
56
|
+
type ReturnT = ReturnType<Func>
|
|
57
|
+
type p0 = Parameters<Func>[0]
|
|
58
|
+
function _endlessph(a: p0): ReturnT
|
|
59
|
+
function _endlessph(a: Placeholder): Func
|
|
60
|
+
function _endlessph(a: p0 | Placeholder) {
|
|
61
|
+
return a===__ ? fn : fn(a)
|
|
62
|
+
}
|
|
63
|
+
return _endlessph
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
type Func2 = (a: any, b: any) => any
|
|
67
|
+
export function curry2<Func extends Func2>(fn: Func) {
|
|
68
|
+
type p0 = Parameters<Func>[0]
|
|
69
|
+
type p1 = Parameters<Func>[1]
|
|
70
|
+
type ReturnT = ReturnType<Func>
|
|
71
|
+
function curried2( a: Placeholder, b: p1 ): (a: p0) => ReturnT
|
|
72
|
+
function curried2( a: p0, b: Placeholder ): (b: p1) => ReturnT
|
|
73
|
+
function curried2( a: p0 ): (b: p1) => ReturnT
|
|
74
|
+
function curried2( a: p0, b: p1 ): ReturnT
|
|
75
|
+
function curried2( a: p0 | Placeholder, b?: p1 ) {
|
|
76
|
+
const withPlaceholder1 = a===__
|
|
77
|
+
const aln = arguments.length
|
|
78
|
+
if(aln === 1 && withPlaceholder1)
|
|
79
|
+
throw new Error('Senseless placeholder usage.')
|
|
80
|
+
return arguments.length>1
|
|
81
|
+
? withPlaceholder1
|
|
82
|
+
? endlessph((a: p0) => fn(a, b))
|
|
83
|
+
: fn(a, b) as ReturnType<Func>
|
|
84
|
+
: (b: p1) => fn(a, b)
|
|
85
|
+
}
|
|
86
|
+
return curried2
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
type Func3 = (a: any, b: any, c: any) => any
|
|
90
|
+
export function curry3<Func extends Func3>(fn: Func) {
|
|
91
|
+
// type p0 = Parameters<Func>[0]
|
|
92
|
+
// type p1 = Parameters<Func>[1]
|
|
93
|
+
// type p2 = Parameters<Func>[2]
|
|
94
|
+
// type ReturnT = ReturnType<Func>
|
|
95
|
+
// TODO: optimize.
|
|
96
|
+
return curry(fn) as FT.Curry<Func>
|
|
97
|
+
}
|
package/src/index.ts
CHANGED
package/src/quick.ts
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { curry2, curry3 } from "./curry"
|
|
2
2
|
import { type } from "./common"
|
|
3
3
|
import { AnyObject, Reducer, AnyFunc } from "./types"
|
|
4
4
|
import { isFunc, isArray } from "./utils"
|
|
5
5
|
|
|
6
|
-
export const qappend =
|
|
7
|
-
export const qassoc =
|
|
6
|
+
export const qappend = curry2((s: any, xs: any[]) => {xs.push(s); return xs})
|
|
7
|
+
export const qassoc = curry3(
|
|
8
8
|
(prop: string, v: any, obj: AnyObject) => {
|
|
9
9
|
obj[prop] = v
|
|
10
10
|
return obj
|
|
11
11
|
}
|
|
12
12
|
)
|
|
13
|
-
export const qreduce =
|
|
14
|
-
(fn: Reducer, accum: any, arr:
|
|
13
|
+
export const qreduce = curry3(
|
|
14
|
+
<T>(fn: Reducer, accum: any, arr: T[]) =>
|
|
15
15
|
arr.reduce(fn, accum)
|
|
16
16
|
)
|
|
17
17
|
// strategy is for arrays: 1->clean, 2->merge, 3->push.
|
|
18
|
-
const mergeDeep =
|
|
18
|
+
const mergeDeep = curry3((strategy: 1|2|3, o1: AnyObject, o2: AnyObject): AnyObject => {
|
|
19
19
|
for(let k in o2) {
|
|
20
20
|
switch(type(o2[k])) {
|
|
21
21
|
case 'Array':
|
|
@@ -54,7 +54,7 @@ export const qmergeDeep = mergeDeep(1)
|
|
|
54
54
|
export const qmergeDeepX = mergeDeep(2)
|
|
55
55
|
export const qmergeDeepAdd = mergeDeep(3)
|
|
56
56
|
/** qmapKeys({ a: 'b' }, { a: 44 }) -> { b: 44 } */
|
|
57
|
-
export const qmapKeys =
|
|
57
|
+
export const qmapKeys = curry2(
|
|
58
58
|
(
|
|
59
59
|
keyMap: {[oldKey: string]: string},
|
|
60
60
|
o: AnyObject
|
|
@@ -74,7 +74,7 @@ export const qmapKeys = curry(
|
|
|
74
74
|
}
|
|
75
75
|
)
|
|
76
76
|
|
|
77
|
-
export const qfilter =
|
|
77
|
+
export const qfilter = curry2(
|
|
78
78
|
(
|
|
79
79
|
cond: (v: any, k: string | number) => boolean,
|
|
80
80
|
data: any[] | AnyObject
|
|
@@ -94,6 +94,6 @@ export const qfilter = curry(
|
|
|
94
94
|
}
|
|
95
95
|
)
|
|
96
96
|
/** @deprecated */
|
|
97
|
-
export const qindexOf =
|
|
97
|
+
export const qindexOf = curry2(
|
|
98
98
|
(x: any, xs: any[]) => xs.indexOf(x)
|
|
99
99
|
)
|