pepka 1.8.2 → 1.9.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/dist/bundle.cjs CHANGED
@@ -95,6 +95,25 @@ function isFunc(s) { return to(s) === 'function'; }
95
95
  const isStr = (s) => (to(s) === 'string');
96
96
  const isObj = (s) => (!isNull(s) && to(s) === 'object');
97
97
  const isNil = (s) => (isNull(s) || isUndef(s));
98
+ class QPromise extends Promise {
99
+ ff;
100
+ rj;
101
+ cancel(resolve = false) {
102
+ if (resolve)
103
+ this.ff();
104
+ else {
105
+ this.catch(noop);
106
+ this.rj('canceled');
107
+ }
108
+ }
109
+ constructor(fn) {
110
+ super((ff, rj) => {
111
+ this.ff = ff;
112
+ this.rj = rj;
113
+ return fn(ff, rj);
114
+ });
115
+ }
116
+ }
98
117
 
99
118
  // It's faster that toUpperCase() !
100
119
  const caseMap = { u: 'U', b: 'B', n: 'N', s: 'S', f: 'F', o: 'O' };
@@ -398,6 +417,7 @@ const noop = (() => { });
398
417
  * @param {string} fnName - property name of the function.
399
418
  * @param {AnyObject} o - the object with the function. */
400
419
  const callFrom = curry((args, fn, o) => o[fn](...args));
420
+ // FIXME: complement(flip(includes)) -> one arg fn!
401
421
  const complement = (fn) => (...args) => {
402
422
  const out = fn(...args);
403
423
  const f = isFunc(out);
@@ -656,7 +676,7 @@ const qwaitAll = async (xs) => new Promise((ff, rj) => {
656
676
  */
657
677
  const waitTap = curry2(async (fn, s) => { await fn(s); return s; });
658
678
  /** Waits for all promises mapped by the fn. */
659
- const forEachAsync = curry2((fn, items) => Promise.all(items.map(fn)));
679
+ const forEachParallel = curry2((fn, items) => Promise.all(items.map(fn)));
660
680
  /** The same as compose, but waits for promises in chains and returns a Promise. */
661
681
  const composeAsync = (() => {
662
682
  const pipe = async (fns, input, i) => ~i ? await pipe(fns, [await fns[i](...input)], --i) : head(input);
@@ -765,12 +785,13 @@ const throttle = (time, fn) => {
765
785
  return res;
766
786
  };
767
787
  };
768
- const wait = (time) => new Promise((ff) => setTimeout(ff, time));
788
+ const wait = (time) => new QPromise((ff) => setTimeout(ff, time));
769
789
 
770
790
  // TODO: possibly introduce a second argument limiting unfolding.
771
791
  const uncurry = (fn) => (...args) => qreduce(((fn, arg) => fn ? fn(arg) : fn), fn, args);
772
792
 
773
793
  exports.F = F;
794
+ exports.QPromise = QPromise;
774
795
  exports.T = T;
775
796
  exports.__ = __;
776
797
  exports.add = add;
@@ -812,7 +833,7 @@ exports.flatShallow = flatShallow;
812
833
  exports.flatTo = flatTo;
813
834
  exports.flip = flip;
814
835
  exports.forEach = forEach;
815
- exports.forEachAsync = forEachAsync;
836
+ exports.forEachParallel = forEachParallel;
816
837
  exports.forEachSerial = forEachSerial;
817
838
  exports.freeze = freeze;
818
839
  exports.freezeShallow = freezeShallow;
package/dist/bundle.d.ts CHANGED
@@ -63,7 +63,7 @@ export declare const waitTap: {
63
63
  (a: AnyFunc<Promise<any>>, b: any): Promise<any>;
64
64
  };
65
65
  /** Waits for all promises mapped by the fn. */
66
- export declare const forEachAsync: {
66
+ export declare const forEachParallel: {
67
67
  (a: Placeholder, b: any[]): (a: (item: any) => Promise<any>) => Promise<any[]>;
68
68
  (a: (item: any) => Promise<any>, b: Placeholder): (b: any[]) => Promise<any[]>;
69
69
  (a: (item: any) => Promise<any>): (b: any[]) => Promise<any[]>;
@@ -239,6 +239,15 @@ export declare const qpush: {
239
239
  (a: any, b: any[]): any[];
240
240
  };
241
241
  export declare const isNil: <T extends any>(s: T) => T extends (null | undefined) ? true : false;
242
+ export declare class QPromise<T> extends Promise<T> {
243
+ private ff;
244
+ private rj;
245
+ cancel(resolve?: boolean): void;
246
+ constructor(fn: AnyFunc<any, [
247
+ AnyFunc,
248
+ AnyFunc
249
+ ]>);
250
+ }
242
251
  export declare const take: (argN: number) => (...args: any[]) => any;
243
252
  export declare const ifElse: (...args: AnyArgs) => any;
244
253
  export declare const when: (...args: AnyArgs) => any;
@@ -774,7 +783,7 @@ type StrTmpl = ((data: AnyObject) => string);
774
783
  export declare const getTmpl: (tmpl: string) => StrTmpl;
775
784
  export declare const debounce: <T extends AnyFunc>(time: number, fn: T) => (...args: Parameters<T>) => Promise<ReturnType<T>>;
776
785
  export declare const throttle: <T extends AnyFunc>(time: number, fn: T) => (...args: Parameters<T>) => any;
777
- export declare const wait: (time: number) => Promise<unknown>;
786
+ export declare const wait: (time: number) => QPromise<any>;
778
787
  export declare const uncurry: <Args extends any[] = any[], ReturnT = any>(fn: Curried<Args>) => AnyFunc;
779
788
 
780
789
  export {
package/dist/bundle.mjs CHANGED
@@ -93,6 +93,25 @@ function isFunc(s) { return to(s) === 'function'; }
93
93
  const isStr = (s) => (to(s) === 'string');
94
94
  const isObj = (s) => (!isNull(s) && to(s) === 'object');
95
95
  const isNil = (s) => (isNull(s) || isUndef(s));
96
+ class QPromise extends Promise {
97
+ ff;
98
+ rj;
99
+ cancel(resolve = false) {
100
+ if (resolve)
101
+ this.ff();
102
+ else {
103
+ this.catch(noop);
104
+ this.rj('canceled');
105
+ }
106
+ }
107
+ constructor(fn) {
108
+ super((ff, rj) => {
109
+ this.ff = ff;
110
+ this.rj = rj;
111
+ return fn(ff, rj);
112
+ });
113
+ }
114
+ }
96
115
 
97
116
  // It's faster that toUpperCase() !
98
117
  const caseMap = { u: 'U', b: 'B', n: 'N', s: 'S', f: 'F', o: 'O' };
@@ -396,6 +415,7 @@ const noop = (() => { });
396
415
  * @param {string} fnName - property name of the function.
397
416
  * @param {AnyObject} o - the object with the function. */
398
417
  const callFrom = curry((args, fn, o) => o[fn](...args));
418
+ // FIXME: complement(flip(includes)) -> one arg fn!
399
419
  const complement = (fn) => (...args) => {
400
420
  const out = fn(...args);
401
421
  const f = isFunc(out);
@@ -654,7 +674,7 @@ const qwaitAll = async (xs) => new Promise((ff, rj) => {
654
674
  */
655
675
  const waitTap = curry2(async (fn, s) => { await fn(s); return s; });
656
676
  /** Waits for all promises mapped by the fn. */
657
- const forEachAsync = curry2((fn, items) => Promise.all(items.map(fn)));
677
+ const forEachParallel = curry2((fn, items) => Promise.all(items.map(fn)));
658
678
  /** The same as compose, but waits for promises in chains and returns a Promise. */
659
679
  const composeAsync = (() => {
660
680
  const pipe = async (fns, input, i) => ~i ? await pipe(fns, [await fns[i](...input)], --i) : head(input);
@@ -763,9 +783,9 @@ const throttle = (time, fn) => {
763
783
  return res;
764
784
  };
765
785
  };
766
- const wait = (time) => new Promise((ff) => setTimeout(ff, time));
786
+ const wait = (time) => new QPromise((ff) => setTimeout(ff, time));
767
787
 
768
788
  // TODO: possibly introduce a second argument limiting unfolding.
769
789
  const uncurry = (fn) => (...args) => qreduce(((fn, arg) => fn ? fn(arg) : fn), fn, args);
770
790
 
771
- export { F, T, __, add, all, allPass, always, any, anyPass, append, assoc, assocPath, bind, both, callFrom, callWith, clone, cloneShallow, complement, compose, composeAsync, concat, cond, curry, curry2, curry3, debounce, diff, divide, echo, empty, eq, equals, explore, filter, find, findIndex, flat, flatShallow, flatTo, flip, forEach, forEachAsync, forEachSerial, freeze, freezeShallow, fromPairs, genBy, getTmpl, gt, gte, head, identity, ifElse, includes, indexOf, intersection, isEmpty, isNil, join, keys, last, length, lt, lte, map, mapKeys, mapObj, memoize, mergeDeep, mergeDeepAdd, mergeDeepX, mergeShallow, mirror, multiply, noop, not, notf, nth, omit, once, overProp, path, pathEq, pathExists, pathOr, pathsEq, pick, pickBy, prepend, prop, propEq, propsEq, push, qappend, qassoc, qassocPath, qempty, qfilter, qfilterAsync, qfreeze, qfreezeShallow, qmap, qmapKeys, qmapObj, qmergeDeep, qmergeDeepAdd, qmergeDeepX, qmergeShallow, qomit, qoverProp, qpick, qprepend, qpush, qreduce, qreverse, qslice, qsort, quniq, qwaitAll, range, reduce, reflect, replace, reverse, sizeof, slice, some, sort, split, startsWith, startsWithShallow, subtract, symbol, tail, take, tap, test, throttle, toLower, toPairs, toUpper, trim, type, typeIs, uncurry, uniq, uniqBy, uniqWith, values, wait, waitAll, waitTap, weakEq, when, zip, zipObj, zipWith };
791
+ export { F, QPromise, T, __, add, all, allPass, always, any, anyPass, append, assoc, assocPath, bind, both, callFrom, callWith, clone, cloneShallow, complement, compose, composeAsync, concat, cond, curry, curry2, curry3, debounce, diff, divide, echo, empty, eq, equals, explore, filter, find, findIndex, flat, flatShallow, flatTo, flip, forEach, forEachParallel, forEachSerial, freeze, freezeShallow, fromPairs, genBy, getTmpl, gt, gte, head, identity, ifElse, includes, indexOf, intersection, isEmpty, isNil, join, keys, last, length, lt, lte, map, mapKeys, mapObj, memoize, mergeDeep, mergeDeepAdd, mergeDeepX, mergeShallow, mirror, multiply, noop, not, notf, nth, omit, once, overProp, path, pathEq, pathExists, pathOr, pathsEq, pick, pickBy, prepend, prop, propEq, propsEq, push, qappend, qassoc, qassocPath, qempty, qfilter, qfilterAsync, qfreeze, qfreezeShallow, qmap, qmapKeys, qmapObj, qmergeDeep, qmergeDeepAdd, qmergeDeepX, qmergeShallow, qomit, qoverProp, qpick, qprepend, qpush, qreduce, qreverse, qslice, qsort, quniq, qwaitAll, range, reduce, reflect, replace, reverse, sizeof, slice, some, sort, split, startsWith, startsWithShallow, subtract, symbol, tail, take, tap, test, throttle, toLower, toPairs, toUpper, trim, type, typeIs, uncurry, uniq, uniqBy, uniqWith, values, wait, waitAll, waitTap, weakEq, when, zip, zipObj, zipWith };
package/package.json CHANGED
@@ -41,7 +41,7 @@
41
41
  "prod": "npm run gentypes && npm run prod:es && npm run prod:cjs",
42
42
  "all": "npm run dev && npm run prod"
43
43
  },
44
- "version": "1.8.2",
44
+ "version": "1.9.1",
45
45
  "devDependencies": {
46
46
  "@rollup/plugin-commonjs": "^29.0.2",
47
47
  "@rollup/plugin-node-resolve": "^16.0.3",
package/src/async.ts CHANGED
@@ -28,7 +28,7 @@ export const qwaitAll = async <T>(xs: Promise<T>[]) => new Promise<T[]>((ff, rj)
28
28
  */
29
29
  export const waitTap = curry2(async (fn: AnyFunc<Promise<any>>, s: any) => { await fn(s); return s })
30
30
  /** Waits for all promises mapped by the fn. */
31
- export const forEachAsync = curry2(
31
+ export const forEachParallel = curry2(
32
32
  (fn: (item: any) => Promise<any>, items: any[]) => Promise.all(items.map(fn))
33
33
  )
34
34
  /** The same as compose, but waits for promises in chains and returns a Promise. */
package/src/index.ts CHANGED
@@ -8,3 +8,5 @@ export * from './strings'
8
8
  export * from './timers'
9
9
  export * from './types'
10
10
  export * from './uncurry'
11
+ export { QPromise } from './utils'
12
+
package/src/safe.ts CHANGED
@@ -152,6 +152,7 @@ export const callFrom = curry((args: any[], fn: string, o: AnyObject) => o[fn](.
152
152
  type T_complement<F extends AnyFunc> = {
153
153
  (...args: Parameters<F>): ReturnType<F> extends AnyFunc ? T_complement<ReturnType<F>> : boolean
154
154
  }
155
+ // FIXME: complement(flip(includes)) -> one arg fn!
155
156
  export const complement = <F extends AnyFunc>(fn: F): T_complement<F> => (...args: any[]): any => {
156
157
  const out = fn(...args)
157
158
  const f = isFunc(out)
package/src/timers.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { AnyFunc } from "./types"
2
+ import { QPromise } from "./utils"
2
3
 
3
4
  export const debounce = <T extends AnyFunc>(time: number, fn: T) => {
4
5
  let queue: AnyFunc[] = []
@@ -25,4 +26,4 @@ export const throttle = <T extends AnyFunc>(time: number, fn: T) => {
25
26
  return res
26
27
  }
27
28
  }
28
- export const wait = (time: number) => new Promise((ff) => setTimeout(ff, time))
29
+ export const wait = (time: number) => new QPromise<any>((ff) => setTimeout(ff, time))
package/src/utils.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { noop } from "./safe"
1
2
  import { AnyFunc, AnyObject } from "./types"
2
3
 
3
4
  export const undef = undefined
@@ -13,4 +14,22 @@ export function isFunc(value: any): false
13
14
  export function isFunc(s: any) { return to(s)==='function' }
14
15
  export const isStr = <T extends any>(s: T) => (to(s)==='string') as T extends string ? true : false
15
16
  export const isObj = <T extends any>(s: T) => (!isNull(s) && to(s)==='object') as T extends AnyObject ? true : false
16
- export const isNil = <T extends any>(s: T) => (isNull(s) || isUndef(s)) as T extends (null|undefined) ? true : false
17
+ export const isNil = <T extends any>(s: T) => (isNull(s) || isUndef(s)) as T extends (null|undefined) ? true : false
18
+
19
+ export class QPromise<T> extends Promise<T> {
20
+ private ff: AnyFunc
21
+ private rj: AnyFunc
22
+ public cancel(resolve = false) {
23
+ if(resolve) this.ff()
24
+ else {
25
+ this.catch(noop)
26
+ this.rj('canceled')
27
+ }
28
+ }
29
+ constructor(fn: AnyFunc<any, [AnyFunc, AnyFunc]>) {
30
+ super((ff, rj) => {
31
+ this.ff=ff; this.rj = rj
32
+ return fn(ff, rj)
33
+ })
34
+ }
35
+ }