pepka 1.2.1 → 1.3.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.
package/src/common.ts CHANGED
@@ -1,18 +1,54 @@
1
1
  import { curry2 } from "./curry"
2
- import { to, isNull } from "./utils"
2
+ import { BasicType } from "./types"
3
+ import { to, isNull, isStr, isUndef } from "./utils"
3
4
 
4
5
  // It's faster that toUpperCase() !
5
- const caseMap = {
6
- u: 'U', b: 'B', n: 'N', s: 'S', f: 'F'
7
- }
6
+ const caseMap = { u: 'U', b: 'B', n: 'N', s: 'S', f: 'F' }
8
7
 
9
8
  export const symbol = Symbol()
10
9
  export const toLower = (s: string) => s.toLowerCase()
11
10
  export const toUpper = (s: string) => s.toUpperCase()
12
- export const type = (s: any) => {
11
+ export const type = (s: any): string => {
13
12
  const t = to(s)
14
13
  return t==='object'
15
14
  ? isNull(s) ? 'Null' : s.constructor.name
16
15
  : caseMap[t[0]] + t.slice(1)
17
16
  }
18
- export const typeIs = curry2((t: string, s: any) => type(s)===t)
17
+ export const typeIs = curry2((t: string, s: any) => type(s)===t)
18
+ export const length = (s: any[] | string) => s.length
19
+ export const isNil = (s: any) => isNull(s) || isUndef(s)
20
+ export const eq = curry2((a: any, b: any) => a===b)
21
+ export const equals = curry2((a: any, b: any) => {
22
+ const typea = type(a)
23
+ if(eq(typea, type(b)) && (eq(typea, 'Object') || eq(typea, 'Array'))) {
24
+ if(isNull(a) || isNull(b)) return eq(a, b)
25
+ if(eq(a, b)) return true
26
+ for(const v of [a, b])
27
+ for(const k in v)
28
+ if(
29
+ !((eq(v, b)) && (k in a)) &&
30
+ !((eq(v, a)) && (k in b) && equals(a[k], b[k]))
31
+ ) return false
32
+ return true
33
+ }
34
+ return eq(a, b)
35
+ })
36
+ export const includes = curry2(
37
+ <T>(s: T, ss: T[]) => {
38
+ if(isStr(ss)) return ss.includes(s)
39
+ else {
40
+ for(const a of ss) if(equals(a, s)) return true
41
+ return false
42
+ }
43
+ }
44
+ )
45
+ /** @param start string | any[] @param s string | any[] */
46
+ export const qstartsWithWith = (comparator: (x: any, y: any)=>boolean) => curry2(
47
+ (start: any[] | string, s: any[] | string) => {
48
+ const len_start = length(start)
49
+ const len_s = length(s)
50
+ if(len_start>len_s) return false
51
+ for(let i=0; i<len_start; i++) if(!comparator(s[i], start[i])) return false
52
+ return true
53
+ }
54
+ )
package/src/index.ts CHANGED
@@ -5,4 +5,5 @@ export * from './common'
5
5
  export * from './safe'
6
6
  export * from './quick'
7
7
  export * from './strings'
8
+ export * from './async'
8
9
  export * from './types'
package/src/quick.ts CHANGED
@@ -1,9 +1,8 @@
1
1
  import { curry2, curry3 } from "./curry"
2
- import { type } from "./common"
2
+ import { includes, isNil, type, length, eq, qstartsWithWith } from "./common"
3
3
  import { AnyObject, Reducer, AnyFunc } from "./types"
4
4
  import { isFunc, isArray, isObj } from "./utils"
5
- import { includes, isNil } from "./safe"
6
- // TODO: qoverProp, qover array ?
5
+ // TODO: qflat, qoverProp, qover array ?
7
6
 
8
7
  export const qappend = curry2((s: any, xs: any[]) => {xs.push(s); return xs})
9
8
  export const qassoc = curry3((prop: string, v: any, obj: AnyObject) => { obj[prop] = v; return obj })
@@ -91,7 +90,7 @@ export const qfilter = curry2(
91
90
  }
92
91
  )
93
92
  export const qempty = (o: AnyObject|any[]) => {
94
- if(isArray(o)) o.splice(0)
93
+ if(isArray(o)) o.splice(0)
95
94
  else for(const i in o) delete o[i]
96
95
  return o
97
96
  }
@@ -119,4 +118,6 @@ export const qomit = curry2(
119
118
  (_: any, k: string) => !includes(k, props),
120
119
  o
121
120
  )
122
- )
121
+ )
122
+ /** @param start string | any[] @param s string | any[] */
123
+ export const qstartsWith = qstartsWithWith(eq)
package/src/safe.ts CHANGED
@@ -1,28 +1,12 @@
1
1
  import { __, curry, curry2, curry3 } from './curry'
2
- import { isNum, isUndef, undef, isNull, isArray, isFunc, isStr, isObj, inf } from './utils'
2
+ import { isNum, undef, isNull, isArray, isFunc, isObj, inf } from './utils'
3
3
  import { qmergeDeep, qreduce, qappend, qmapKeys, qmergeDeepX, qmergeDeepAdd, qfilter, qfreeze, qfreezeShallow, qmapObj } from './quick'
4
4
  import { AnyFunc, Cond, AnyObject, Reducer } from './types'
5
- import { symbol, type } from './common'
5
+ import { symbol, type, length, equals, includes, isNil, qstartsWithWith } from './common'
6
6
  // over, lensProp
7
7
 
8
8
  export const take = (argN: number) => (...args: any[]) => args[argN]
9
- export const eq = curry2((a: any, b: any) => a===b)
10
9
  export const weakEq = curry2((a: any, b: any) => a==b)
11
- export const equals = curry2((a: any, b: any) => {
12
- const typea = type(a)
13
- if(eq(typea, type(b)) && (eq(typea, 'Object') || eq(typea, 'Array'))) {
14
- if(isNull(a) || isNull(b)) return eq(a, b)
15
- if(eq(a, b)) return true
16
- for(const v of [a, b])
17
- for(const k in v)
18
- if(
19
- !((eq(v, b)) && (k in a)) &&
20
- !((eq(v, a)) && (k in b) && equals(a[k], b[k]))
21
- ) return false
22
- return true
23
- }
24
- return eq(a, b)
25
- })
26
10
  export const ifElse = curry(
27
11
  (
28
12
  cond: (s: any) => boolean,
@@ -58,15 +42,6 @@ export const bind = curry2<AnyFunc>(
58
42
  (fn: AnyFunc, context: any) => fn.bind(context)
59
43
  )
60
44
  export const nth = curry2(<T=any>(i: number, data: T[] | string) => data[i])
61
- export const includes = curry2(
62
- <T>(s: T, ss: T[]) => {
63
- if(isStr(ss)) return ss.includes(s)
64
- else {
65
- for(const a of ss) if(equals(a, s)) return true
66
- return false
67
- }
68
- }
69
- )
70
45
  export const slice = curry3(
71
46
  (from: number, to: number, o: any[] | string) =>
72
47
  o.slice(from, (isNum(to)?to:inf) as number)
@@ -74,26 +49,35 @@ export const slice = curry3(
74
49
  export const flip = <T extends AnyFunc>(fn: T) => curry2(
75
50
  (b: Parameters<T>[1], a: Parameters<T>[0]) => fn(a, b)
76
51
  )
52
+ /** @returns first element of an array. */
77
53
  export const head = nth(0) as <T = any>(xs: T[] | string) => T
54
+ /** @returns last element of an array. */
78
55
  export const tail = slice(1, inf)
79
- export const add = curry2((n: number, m: number) => n+m)
80
- export const subtract = curry2((n: number, m: number) => m-n)
81
- export const multiply = curry2((n: number, m: number) => n*m)
82
- export const gt = curry2( (a: number, b: number) => a>b )
83
- export const lt = curry2( (a: number, b: number) => a<b )
84
- export const gte = curry2( (a: number, b: number) => b>=a )
85
- export const lte = curry2( (a: number, b: number) => b<=a )
56
+ /** @param a @param b @returns a+b */
57
+ export const add = curry2((a: number, b: number) => a+b)
58
+ /** @param a @param b @returns b-a */
59
+ export const subtract = curry2((a: number, b: number) => b-a)
60
+ /**@param a @param b @returns a*b */
61
+ export const multiply = curry2((a: number, b: number) => a*b)
62
+ /** @param a @param b @returns a<b */
63
+ export const gt = curry2( (a: number, b: number) => a<b )
64
+ /** @param a @param b @returns a>b */
65
+ export const lt = curry2( (a: number, b: number) => a>b )
66
+ /** @param a @param b @returns a<=b */
67
+ export const gte = curry2( (a: number, b: number) => a<=b )
68
+ /** @param a @param b @returns a>=b */
69
+ export const lte = curry2( (a: number, b: number) => a>=b )
86
70
  export const sort = curry2((sortFn: any, xs: any[]) => xs.sort(sortFn))
87
71
  export const find = curry2((fn: Cond, s: any[]) => s.find(fn))
88
72
  export const findIndex = curry2((fn: Cond, s: any[]) => s.findIndex(fn))
89
73
  export const indexOf = curry2((x: any, xs: any[]) => findIndex(equals(x), xs))
90
74
  export const divide = curry2((n: number, m: number) => n/m)
91
- export const isNil = (s: any) => isNull(s) || isUndef(s)
92
- export const length = (s: any[] | string) => s.length
93
75
  export const always = <T=any>(s: T) => () => s
94
76
  export const identity = (s: any) => s
95
77
  export const trim = (s: string) => s.trim()
96
78
  export const last = (s: any[] | string) => s[length(s)-1]
79
+ /** @param start string | any[] @param s string | any[] */
80
+ export const startsWith = qstartsWithWith((x: any, y: any) => equals(x, y))
97
81
  type NotOverload = {
98
82
  (x: true): false
99
83
  (x: false): true
@@ -139,13 +123,11 @@ export const sizeof = (s: any[] | string | AnyObject) => {
139
123
  return len
140
124
  } else return length(s as any[])
141
125
  }
142
- export const range = curry2((from: number, to: number) =>
143
- genBy(add(from), to-from)
144
- )
145
- // TODO: make it using equals for deep stuff !
126
+ export const range = curry2((from: number, to: number) => genBy(add(from), to-from))
127
+ /** @param xs any[] @returns xs without duplicates. */
146
128
  export const uniq = (xs: any[]) => qreduce(
147
129
  <T>(accum: any, x: T) =>
148
- includes(x, accum) ? accum : qappend(x, accum),
130
+ find(equals(x), accum) ? accum : qappend(x, accum),
149
131
  [], xs)
150
132
  export const intersection = curry2(
151
133
  (xs1: any[], xs2: any[]) => xs1.filter(flip(includes)(xs2))
@@ -177,11 +159,7 @@ export const explore = (caption: string, level = 'log') => tap(
177
159
  )
178
160
  export const cond = curry2(
179
161
  (pairs: [Cond, Function][], s: any) => {
180
- for(const [cond, fn] of pairs) {
181
- if(cond(s)) {
182
- return fn(s)
183
- }
184
- }
162
+ for(const [cond, fn] of pairs) if(cond(s)) return fn(s)
185
163
  }
186
164
  )
187
165
  /** Assigns a prop to an object.
@@ -190,10 +168,7 @@ export const cond = curry2(
190
168
  * @param object AnyObject
191
169
  */
192
170
  export const assoc = curry3(
193
- (prop: string, v: any, obj: AnyObject) => ({
194
- ...obj,
195
- [prop]: v
196
- })
171
+ (prop: string, v: any, obj: AnyObject) => ({...obj, [prop]: v})
197
172
  )
198
173
  export const assocPath = curry3(
199
174
  (_path: string[], v: any, o: AnyObject) => compose(
@@ -209,16 +184,15 @@ export const assocPath = curry3(
209
184
  )
210
185
  export const all = curry2((pred: Cond, xs: any[]) => xs.every(pred))
211
186
  export const any = curry2((pred: Cond, xs: any[]) => xs.some(pred))
212
- export const allPass = curry2(
213
- (preds: Cond[], x: any) => preds.every((pred) => pred(x))
214
- )
215
- export const anyPass = curry2(
216
- (preds: Cond[], x: any) => preds.some((pred) => pred(x))
217
- )
218
- export const prop = curry2( (key: string, o: AnyObject) => o[key] )
187
+ export const allPass = curry2((preds: Cond[], x: any) => preds.every((pred) => pred(x)))
188
+ export const anyPass = curry2((preds: Cond[], x: any) => preds.some((pred) => pred(x)))
189
+ /** @param key string @param o AnyObject @returns o[key] */
190
+ export const prop = curry2((key: string, o: AnyObject) => o[key])
191
+ /** @param key string @param value any @param o AnyObject @returns o[key] equals value */
219
192
  export const propEq = curry3(
220
193
  (key: string, value: any, o: AnyObject) => equals(o[key], value)
221
194
  )
195
+ /** @param key string @param o1 AnyObject @param o2 AnyObject @returns o₁[key] equals o₂[key] */
222
196
  export const propsEq = curry3(
223
197
  (key: string, o1: any, o2: AnyObject) => equals(o1[key], o2[key])
224
198
  )
@@ -295,21 +269,15 @@ type Concat = ((a: string, b: string) => string)
295
269
  export const concat = curry2(
296
270
  ((a: any, b: string | any[]) => b.concat(a)) as Concat
297
271
  )
298
- export const join = curry2(
299
- (delimeter: string, arr: string[]) => arr.join(delimeter)
300
- )
301
272
  export const map = curry2(
302
273
  (pipe: (s: any, i?: number, list?: any[]) => any, arr: any[]) => arr.map(pipe)
303
274
  )
304
275
  export const mapObj = curry2(
305
276
  (pipe: (s: any, i?: string, list?: any[]) => any, o: AnyObject) => qmapObj(pipe, cloneShallow(o))
306
277
  )
307
- export const forEach = curry2(
308
- (pipe: (s: any) => any, arr: any[]) => arr.forEach(pipe)
309
- )
310
- export const both = curry3(
311
- (cond1: Cond, cond2: Cond, s: any) => cond2(s) && cond1(s)
312
- )
278
+ export const join = curry2((delimeter: string, arr: string[]) => arr.join(delimeter))
279
+ export const forEach = curry2((pipe: (s: any) => any, arr: any[]) => arr.forEach(pipe))
280
+ export const both = curry3((cond1: Cond, cond2: Cond, s: any) => cond2(s) && cond1(s))
313
281
  export const isEmpty = (s: any) => {
314
282
  switch(type(s)) {
315
283
  case 'String': case 'Array': return length(s)==0
@@ -392,40 +360,8 @@ export const zipWith = curry3(
392
360
  map((s: T1, i: number) => pipe(s, b[i]), a)
393
361
  )
394
362
 
395
- // ASYNCS
396
-
397
- /** One promise waits for another. */
398
- export const forEachSerial = (() => {
399
- const pipe = async (fn: AnyFunc, items: any[], i: number) => {
400
- if(i<items.length) {
401
- await fn(items[i])
402
- await pipe(fn, items, ++i)
403
- }
404
- }
405
- return curry2(
406
- (fn: AnyFunc, items: any[]) => pipe(fn, items, 0)
407
- )
408
- })()
409
- /** Promise.all wrapper for functional pipelining. */
410
- export const waitAll = (promises: Promise<any>[]) => Promise.all(promises)
411
- /** Waits for a Promise that been generated by the first arg, then returns an untoched value. Types T.
412
- * @param {AnyFunc<Promise>} fn - function to wait.
413
- * @param {T} s - any value to tap and return back
414
- * @returns {T}
415
- */
416
- export const waitTap = curry2(async (fn: AnyFunc, s: any) => { await fn(s); return s })
417
- /** Waits for all promises mapped by the fn. */
418
- export const forEachAsync = curry2(
419
- (fn: (item: any) => Promise<any>, items: any[]) =>
420
- Promise.all(items.map(fn))
421
- )
422
- /** The same as compose, but waits for promises in chains and returns a Promise. */
423
- export const composeAsync = (() => {
424
- const pipe = async (fns: AnyFunc[], input: any[], i: number): Promise<any> =>
425
- ~i ? await pipe(fns, [await fns[i](...input)], --i) : head(input)
426
- return <T = any>(...fns: AnyFunc[]) =>
427
- (...input: any[]) => pipe(fns, input, fns.length-1) as Promise<T>
428
- })()
363
+ // Reexport safe stuff that is ready to use externally.
364
+ export { toLower, toUpper, type, typeIs, length, isNil, eq, equals, includes } from './common'
429
365
 
430
366
  // ALIASES
431
367
  export const mirror = identity
package/src/strings.ts CHANGED
@@ -5,8 +5,8 @@ type StrTmpl = ((data: AnyObject) => string)
5
5
  const ecran = '\\'
6
6
 
7
7
  // TODO: make it splicy, not accumulatie by symbols.
8
- // Supports ecrans: '\{"json": {yes} \}'
9
- // get_tmpl(one{meme}two)({meme: 42}) -> one42two
8
+ /** Supports ecrans: '\\{"json": {yes} \\}'
9
+ @returns get_tmpl(one{meme}two)({meme: 42}) -> one42two */
10
10
  export const getTmpl = (tmpl: string): StrTmpl => {
11
11
  const parts: string[] = []
12
12
  const keymap: string[] = []
package/src/types.ts CHANGED
@@ -7,4 +7,5 @@ export type TupleFn<ARG1=any, ARG2=any, Out=any> = (a: ARG1, b: ARG2) => Out
7
7
  export type Curried<
8
8
  Args extends AnyArgs = AnyArgs,
9
9
  ReturnT = any
10
- > = (arg: Args[number]) => Curried<Args> | ReturnT
10
+ > = (arg: Args[number]) => Curried<Args> | ReturnT
11
+ export type BasicType = 'String'|'Object'|'Number'|'Symbol'|'Array'|'Null'|'Undefined'