pepka 0.12.3 → 0.13.0-b2

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/src/safe.ts CHANGED
@@ -1,11 +1,12 @@
1
- import { __, curry } from './curry'
2
- import { isNum, nul, isUndef, undef, isNull, isArray, isFunc, isStr, isObj } from './utils'
1
+ import { __, curry, curry2, curry3 } from './curry'
2
+ import { isNum, nul, isUndef, undef, isNull, isArray, isFunc, isStr, isObj, inf } from './utils'
3
3
  import { qmergeDeep, qreduce, qappend, qmapKeys, qmergeDeepX, qmergeDeepAdd } from './quick'
4
4
  import { AnyFunc, Cond, AnyObject, Reducer } from './types'
5
5
  import { type } from './common'
6
+ import { F as FT, U } from 'ts-toolbelt'
6
7
  // over, lensProp
7
8
 
8
- export const equals = curry((a: any, b: any) => {
9
+ export const equals = curry2((a: any, b: any) => {
9
10
  const typea = type(a)
10
11
  if(typea===type(b) && (typea==='Object' || typea=='Array')) {
11
12
  if(isNull(a) || isNull(b)) {
@@ -36,31 +37,32 @@ export const ifElse = curry(
36
37
  s: any
37
38
  ) => cond(s) ? pipeYes(s) : pipeNo(s)
38
39
  )
39
- export const when = curry(
40
+ export const when = curry3(
40
41
  (
41
42
  cond: (s: any) => boolean,
42
43
  pipe: (s: any) => any,
43
44
  s: any
44
45
  ) => ifElse(cond, pipe, identity, s)
45
46
  )
46
- export const compose = (
47
- (...fns: Function[]) =>
47
+ export const compose: FT.Compose = (
48
+ (...fns: AnyFunc[]) =>
48
49
  (s: any = __) => {
49
50
  for(let i = length(fns)-1; i>-1; i--) {
50
51
  s = s===__ ? fns[i]() : fns[i](s)
51
52
  }
52
53
  return s
53
54
  }
54
- )// as F.Compose
55
+ )
55
56
 
56
- export const bind = curry(
57
+ export const bind = curry2<AnyFunc>(
57
58
  (fn: AnyFunc, context: any) => fn.bind(context)
58
59
  )
59
- export const nth = curry(
60
- (i: number, data: any[]) => data[i]
61
- )
62
- export const includes = curry(
63
- (s: any, ss: any[]) => {
60
+
61
+ const _nth = <T=any>(i: number, data: T[]) => data[i]
62
+ export const nth = curry2(_nth)
63
+
64
+ export const includes = curry2(
65
+ <T>(s: T, ss: T[]) => {
64
66
  if(isStr(ss)) {
65
67
  return ss.includes(s)
66
68
  } else {
@@ -73,14 +75,14 @@ export const includes = curry(
73
75
  }
74
76
  }
75
77
  )
76
- export const slice = curry(
77
- (from: number, to: number|null, o: any[] | string) =>
78
- o.slice(from, (isNum(to)?to:Infinity) as number)
78
+ export const slice = curry3(
79
+ (from: number, to: number, o: any[] | string) =>
80
+ o.slice(from, (isNum(to)?to:inf) as number)
79
81
  )
80
82
  export const head = nth(0)
81
- export const tail = slice(1, nul)
82
- export const add = curry((n: number, m: number) => n+m)
83
- export const subtract = curry((n: number, m: number) => m-n)
83
+ export const tail = slice(1, inf) // typeshit.
84
+ export const add = curry2((n: number, m: number) => n+m)
85
+ export const subtract = curry2((n: number, m: number) => m-n)
84
86
  export const flip = (fn: Function) => curry((b: any, a: any) => fn(a, b))
85
87
  export const isNil = (s: any) => isNull(s) || isUndef(s)
86
88
  export const length = (s: any[] | string) => s.length
@@ -96,10 +98,10 @@ export const complement = (fn: AnyFunc) => (...args: any) => {
96
98
  export const keys = (o: AnyObject | any[]) => Object.keys(o)
97
99
  export const values = (o: AnyObject | any[]) => Object.values(o)
98
100
  export const toPairs = (o: AnyObject | any[]) => Object.entries(o)
99
- export const test = curry((re: RegExp, s: string) => re.test(s))
100
- export const tap = curry((fn: Function, s: any) => { fn(s); return s })
101
- export const append = curry((s: any, xs: any[]) => [...xs, s])
102
- export const split = curry((s: string, xs: string) => xs.split(s))
101
+ export const test = curry2((re: RegExp, s: string) => re.test(s))
102
+ export const tap = curry2((fn: Function, s: any) => { fn(s); return s })
103
+ export const append = curry2((s: any, xs: any[]) => [...xs, s])
104
+ export const split = curry2((s: string, xs: string) => xs.split(s))
103
105
  export const T = always<true>(true) as (...args: any[]) => true
104
106
  export const F = always<false>(false) as (...args: any[]) => false
105
107
  export const sizeof = (s: any[] | string | AnyObject) => {
@@ -109,17 +111,17 @@ export const sizeof = (s: any[] | string | AnyObject) => {
109
111
  return len
110
112
  } else return length(s as any[])
111
113
  }
112
- export const range = curry((from: number, to: number) =>
114
+ export const range = curry2((from: number, to: number) =>
113
115
  genBy(add(from), to-from)
114
116
  )
115
117
  export const uniq = (xs: any[]) => qreduce(
116
- (accum: any[], x: any) =>
118
+ <T>(accum: any, x: T) =>
117
119
  includes(x, accum) ? accum : qappend(x, accum),
118
120
  [], xs)
119
- export const intersection = curry(
121
+ export const intersection = curry2(
120
122
  (xs1: any[], xs2: any[]) => xs1.filter(flip(includes)(xs2))
121
123
  )
122
- export const genBy = curry(
124
+ export const genBy = curry2(
123
125
  (
124
126
  generator: (i: number) => any,
125
127
  length: number
@@ -137,40 +139,25 @@ export const once = <Func extends AnyFunc>(fn: Func) => {
137
139
  }
138
140
  }
139
141
  export const reverse = (xs: any[]) => compose(
140
- (ln: number) => reduce(
141
- (nxs: any[], _: any, i: number) => qappend(xs[ln-i], nxs),
142
+ <T>(ln: number) => reduce<any>(
143
+ (nxs: T[], _: any, i: number) => qappend(xs[ln-i], nxs),
142
144
  [], xs
143
145
  ),
144
146
  add(-1),
145
147
  length
146
148
  )(xs)
147
- export const gt = curry(
148
- (a: number, b: number) => a>b
149
- )
150
- export const lt = curry(
151
- (a: number, b: number) => a<b
152
- )
153
- export const gte = curry(
154
- (a: number, b: number) => b>=a
155
- )
156
- export const lte = curry(
157
- (a: number, b: number) => b<=a
158
- )
159
- // : <U=any>(sortFn: (v: U)=>-1|1, xs: U[]) => U[]
160
- export const sort = curry((sortFn: any, xs: any[]) => xs.sort(sortFn))
161
- export const find = curry(
162
- (fn: Cond, s: any[]) => s.find(fn)
163
- )
164
- export const findIndex = curry(
165
- (fn: Cond, s: any[]) => s.findIndex(fn)
166
- )
167
- export const indexOf = curry(
168
- (x: any, xs: any[]) => findIndex(equals(x), xs)
169
- )
149
+ export const gt = curry2( (a: number, b: number) => a>b )
150
+ export const lt = curry2( (a: number, b: number) => a<b )
151
+ export const gte = curry2( (a: number, b: number) => b>=a )
152
+ export const lte = curry2( (a: number, b: number) => b<=a )
153
+ export const sort = curry2((sortFn: any, xs: any[]) => xs.sort(sortFn))
154
+ export const find = curry2((fn: Cond, s: any[]) => s.find(fn))
155
+ export const findIndex = curry2((fn: Cond, s: any[]) => s.findIndex(fn))
156
+ export const indexOf = curry2((x: any, xs: any[]) => findIndex(equals(x), xs))
170
157
  export const explore = (caption: string, level = 'log') => tap(
171
158
  (v: any) => console[level](caption, v)
172
159
  )
173
- export const cond = curry(
160
+ export const cond = curry2(
174
161
  (pairs: [Cond, Function][], s: any) => {
175
162
  for(const [cond, fn] of pairs) {
176
163
  if(cond(s)) {
@@ -179,42 +166,40 @@ export const cond = curry(
179
166
  }
180
167
  }
181
168
  )
182
- export const assoc = curry(
169
+ export const assoc = curry3(
183
170
  (prop: string, v: any, obj: AnyObject) => ({
184
171
  ...obj,
185
172
  [prop]: v
186
173
  })
187
174
  )
188
- export const assocPath = curry(
175
+ export const assocPath = curry3(
189
176
  (_path: string[], v: any, o: AnyObject) => compose(
190
177
  (first: string) => assoc(
191
178
  first,
192
179
  length(_path)<2
193
180
  ? v
194
- : assocPath(slice(1, null, _path), v, isObj(o[first]) ? o[first] : {}),
181
+ : assocPath(slice(1, inf, _path), v, isObj(o[first]) ? o[first] : {}),
195
182
  o
196
183
  ),
197
184
  head
198
185
  )(_path)
199
186
  )
200
- export const all = curry((pred: Cond, xs: any[]) => xs.every(pred))
201
- export const any = curry((pred: Cond, xs: any[]) => xs.some(pred))
202
- export const allPass = curry(
187
+ export const all = curry2((pred: Cond, xs: any[]) => xs.every(pred))
188
+ export const any = curry2((pred: Cond, xs: any[]) => xs.some(pred))
189
+ export const allPass = curry2(
203
190
  (preds: Cond[], x: any) => preds.every((pred) => pred(x))
204
191
  )
205
- export const anyPass = curry(
192
+ export const anyPass = curry2(
206
193
  (preds: Cond[], x: any) => preds.some((pred) => pred(x))
207
194
  )
208
- export const prop = curry(
209
- (key: string, o: AnyObject) => o[key]
210
- )
211
- export const propEq = curry(
195
+ export const prop = curry2( (key: string, o: AnyObject) => o[key] )
196
+ export const propEq = curry3(
212
197
  (key: string, value: any, o: AnyObject) => equals(o[key], value)
213
198
  )
214
- export const propsEq = curry(
199
+ export const propsEq = curry3(
215
200
  (key: string, o1: any, o2: AnyObject) => equals(o1[key], o2[key])
216
201
  )
217
- export const pathOr = curry(
202
+ export const pathOr = curry3(
218
203
  (_default: any, path: string[], o: any) =>
219
204
  ifElse(length,
220
205
  () => isNil(o)
@@ -222,7 +207,7 @@ export const pathOr = curry(
222
207
  : compose(
223
208
  ifElse(isNil,
224
209
  always(_default),
225
- (o: any) => pathOr(_default, slice(1, nul, path), o)
210
+ (o: any) => pathOr(_default, slice(1, inf, path), o)
226
211
  ),
227
212
  flip(prop)(o),
228
213
  head
@@ -231,20 +216,21 @@ export const pathOr = curry(
231
216
  path)
232
217
  )
233
218
  export const path = pathOr(undef)
234
- export const pathEq = curry(
219
+ export const pathEq = curry3(
235
220
  (_path: string[], value: any, o: AnyObject) => equals(path(_path, o), value)
236
221
  )
237
- export const pathsEq = curry(
222
+ export const pathsEq = curry3(
238
223
  (_path: string[], o1: AnyObject, o2: AnyObject) =>
239
224
  equals(path(_path, o1), path(_path, o2))
240
225
  )
241
226
  const typed_arr_re = /^(.*?)(8|16|32|64)(Clamped)?Array$/
242
- export const clone = (s: any) => {
227
+ export const clone = (s: any, shallow = false) => {
243
228
  const t = type(s)
244
229
  switch(t) {
245
230
  case 'Null': return s
246
- case 'Array': return map(clone, s)
231
+ case 'Array': return shallow ? [...s] : map(clone, s)
247
232
  case 'Object':
233
+ if(shallow) return {...s}
248
234
  const out = {}
249
235
  for(let k in s) {
250
236
  out[k] = clone(s[k])
@@ -254,17 +240,19 @@ export const clone = (s: any) => {
254
240
  case 'Boolean': case 'Symbol':
255
241
  return s
256
242
  default:
257
- return typed_arr_re.test(t) ? map(clone, s) : s
243
+ return typed_arr_re.test(t) ? s.constructor.from(s) : s
258
244
  }
259
245
  }
260
- export const reduce = curry(
246
+ export const cloneShallow = (s: any) => clone(s, true)
247
+
248
+ export const reduce = curry3(
261
249
  (fn: Reducer, accum: any, arr: any[]) =>
262
250
  qreduce(fn, clone(accum), arr)
263
251
  )
264
- export const pickBy = curry(
252
+ export const pickBy = curry2(
265
253
  (cond: Cond, o: AnyObject) => filter(cond, o)
266
254
  )
267
- export const pick = curry(
255
+ export const pick = curry2(
268
256
  (props: string[], o: AnyObject) => {
269
257
  const out = {}
270
258
  for(const p of props) {
@@ -275,31 +263,31 @@ export const pick = curry(
275
263
  return out
276
264
  }
277
265
  )
278
- export const omit = curry(
266
+ export const omit = curry2(
279
267
  (props: string[], o: AnyObject) => filter(
280
268
  (_: any, k: string) => !includes(k, props),
281
269
  o
282
270
  )
283
271
  )
284
- export const fromPairs = (pairs: [string, any][]) => reduce(
272
+ export const fromPairs = (pairs: [string, any][]) => reduce<any>(
285
273
  (o: AnyObject, pair: [string, any]) => assoc(...pair, o),
286
274
  {}, pairs
287
275
  )
288
276
  type Concat = ((a: string, b: string) => string)
289
277
  | ((a: any[], b: any[]) => any[])
290
- export const concat = curry(
278
+ export const concat = curry2(
291
279
  ((a, b) => a.concat(b)) as Concat
292
280
  )
293
- export const join = curry(
281
+ export const join = curry2(
294
282
  (delimeter: string, arr: string[]) => arr.join(delimeter)
295
283
  )
296
- export const map = curry(
284
+ export const map = curry2(
297
285
  (pipe: (s: any) => any, arr: any[]) => arr.map(pipe)
298
286
  )
299
- export const forEach = curry(
287
+ export const forEach = curry2(
300
288
  (pipe: (s: any) => any, arr: any[]) => arr.forEach(pipe)
301
289
  )
302
- export const both = curry(
290
+ export const both = curry3(
303
291
  (cond1: Cond, cond2: Cond, s: any) => cond2(s) && cond1(s)
304
292
  )
305
293
  export const isEmpty = (s: any) => {
@@ -319,14 +307,14 @@ export const empty = (s: any) => {
319
307
  default: return undef
320
308
  }
321
309
  }
322
- export const replace = curry(
310
+ export const replace = curry3(
323
311
  (
324
312
  a: string | RegExp,
325
313
  b: string,
326
314
  where: string
327
315
  ) => where.replace(a, b)
328
316
  )
329
- export const filter = curry(
317
+ export const filter = curry2(
330
318
  (
331
319
  cond: (v: any, k: string | number) => boolean,
332
320
  data: any[] | AnyObject
@@ -343,25 +331,25 @@ export const memoize = (fn: Function) => {
343
331
  let cached = false
344
332
  return () => cached ? cache : (cached = true, cache = fn())
345
333
  }
346
- export const mergeShallow = curry(
334
+ export const mergeShallow = curry2(
347
335
  (o1: AnyObject, o2: AnyObject): AnyObject =>
348
336
  Object.assign({}, o1, o2)
349
337
  )
350
- export const mergeDeep = curry(
338
+ export const mergeDeep = curry2(
351
339
  (a: AnyObject, b: AnyObject) => qmergeDeep(clone(a), clone(b))
352
340
  )
353
- export const mergeDeepX = curry(
341
+ export const mergeDeepX = curry2(
354
342
  (a: AnyObject, b: AnyObject) => qmergeDeepX(clone(a), clone(b))
355
343
  )
356
- export const mergeDeepAdd = curry(
344
+ export const mergeDeepAdd = curry2(
357
345
  (a: AnyObject, b: AnyObject) => qmergeDeepAdd(clone(a), clone(b))
358
346
  )
359
- export const overProp = curry(
347
+ export const overProp = curry3(
360
348
  (prop: string, pipe: AnyFunc, data: any) =>
361
349
  assoc(prop, pipe(data[prop]), data)
362
350
  )
363
351
  /** mapKeys({ a: 'b' }, { a: 44 }) -> { b: 44 } */
364
- export const mapKeys = curry(
352
+ export const mapKeys = curry2(
365
353
  (
366
354
  keyMap: {[oldKey: string]: string},
367
355
  o: AnyObject
@@ -378,14 +366,15 @@ export const forEachSerial = (() => {
378
366
  await pipe(fn, items, ++i)
379
367
  }
380
368
  }
381
- return curry(
369
+ return curry2(
382
370
  (fn: AnyFunc, items: any[]) => pipe(fn, items, 0)
383
371
  )
384
372
  })()
385
373
  /** Promise.all wrapper for functional pipelining. */
386
374
  export const waitAll = (promises: Promise<any>[]) => Promise.all(promises)
375
+ export const waitTap = curry2(async (fn: Function, s: any) => { await fn(s); return s })
387
376
  /** Waits for all promises mapped by the fn. */
388
- export const forEachAsync = curry(
377
+ export const forEachAsync = curry2(
389
378
  (fn: (item: any) => Promise<any>, items: any[]) =>
390
379
  Promise.all(items.map(fn))
391
380
  )
@@ -395,10 +384,9 @@ export const composeAsync = (() => {
395
384
  ~i ? await pipe(fns, await fns[i](data), --i) : data
396
385
  return <T = any>(...fns: AnyFunc[]) =>
397
386
  (data?: any) => pipe(fns, data, fns.length-1) as Promise<T>
398
- })()
387
+ })() as FT.Compose<'async'>
399
388
 
400
389
  // ALIASES
401
-
402
390
  export const mirror = identity
403
391
  export const reflect = identity
404
- export const echo = identity
392
+ export const echo = identity
package/src/types.ts CHANGED
@@ -1,7 +1,14 @@
1
-
2
1
  export type Cond = (...xs: any[]) => boolean
3
2
  export interface AnyObject {
4
3
  [k: string]: any
5
4
  }
6
- export type Reducer = <T>(accum: T, cur: any, index: number) => T
7
- export type AnyFunc = (...args: any[]) => any
5
+ export type AnyArgs = any[]
6
+ export type Curried<
7
+ Args extends AnyArgs = AnyArgs,
8
+ ReturnT = any
9
+ > = (arg: Args[number]) => Curried<Args> | ReturnT
10
+ export type Reducer = <T=any>(accum: T, cur: any, index: number) => T
11
+ export type AnyFunc<
12
+ ReturnT = any,
13
+ Args extends AnyArgs = AnyArgs
14
+ > = (...args: Args) => ReturnT
package/src/uncurry.ts ADDED
@@ -0,0 +1,11 @@
1
+ import { qreduce } from "./quick"
2
+ import { AnyFunc, Curried } from "./types"
3
+
4
+ // TODO: possibly introduce a second argument limiting unfolding.
5
+ export const uncurry = <
6
+ Args extends any[] = any[],
7
+ ReturnT = any
8
+ >(fn: Curried<Args>): AnyFunc =>
9
+ (...args: Args) => qreduce<any>(
10
+ ((fn: Curried<Args>, arg: any) => fn ? fn(arg) : fn), fn, args
11
+ ) as ReturnT
package/src/utils.ts CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  export const undef = undefined
4
4
  export const nul = null
5
+ export const inf = Infinity
5
6
  export const to = (s: any) => typeof s
6
7
  export const isNull = (s: any) => s===nul
7
8
  export const isUndef = (s: any) => s===undef
package/tsconfig.json CHANGED
@@ -1,5 +1,6 @@
1
1
  {
2
2
  "compilerOptions": {
3
+ "moduleResolution": "Node",
3
4
  "lib": [ "esnext", "DOM" ],
4
5
  "strictNullChecks": true,
5
6
  "target": "esnext",
@@ -14,8 +15,5 @@
14
15
  "rootDir": "src",
15
16
  "baseUrl": "."
16
17
  },
17
- "include": [
18
- "src/**/*",
19
- "src/node_modules/ts-toolbelt/out/index.d.ts"
20
- ]
18
+ "include": [ "src/**/*" ]
21
19
  }