@telperion/js-utils 3.5.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/LICENSE +21 -0
- package/README.md +621 -0
- package/array/arrayize.cjs +22 -0
- package/array/arrayize.d.ts +17 -0
- package/array/arrayize.js +21 -0
- package/array/arrayize.js.map +1 -0
- package/array/async-map.cjs +40 -0
- package/array/async-map.d.ts +34 -0
- package/array/async-map.js +39 -0
- package/array/async-map.js.map +1 -0
- package/array/compact.cjs +34 -0
- package/array/compact.d.ts +28 -0
- package/array/compact.js +33 -0
- package/array/compact.js.map +1 -0
- package/array/difference.cjs +16 -0
- package/array/difference.d.ts +45 -0
- package/array/difference.js +15 -0
- package/array/difference.js.map +1 -0
- package/array/find-by-key.cjs +35 -0
- package/array/find-by-key.d.ts +30 -0
- package/array/find-by-key.js +34 -0
- package/array/find-by-key.js.map +1 -0
- package/array/index.cjs +14 -0
- package/array/index.d.ts +11 -0
- package/array/index.js +13 -0
- package/array/index.js.map +1 -0
- package/array/intersection.cjs +16 -0
- package/array/intersection.d.ts +45 -0
- package/array/intersection.js +15 -0
- package/array/intersection.js.map +1 -0
- package/array/limit.cjs +37 -0
- package/array/limit.d.ts +29 -0
- package/array/limit.js +36 -0
- package/array/limit.js.map +1 -0
- package/array/proto/async-map.cjs +6 -0
- package/array/proto/async-map.d.ts +25 -0
- package/array/proto/async-map.js +6 -0
- package/array/proto/async-map.js.map +1 -0
- package/array/proto/compact.cjs +6 -0
- package/array/proto/compact.d.ts +15 -0
- package/array/proto/compact.js +6 -0
- package/array/proto/compact.js.map +1 -0
- package/array/proto/difference.cjs +6 -0
- package/array/proto/difference.d.ts +27 -0
- package/array/proto/difference.js +6 -0
- package/array/proto/difference.js.map +1 -0
- package/array/proto/find-by-key.cjs +6 -0
- package/array/proto/find-by-key.d.ts +25 -0
- package/array/proto/find-by-key.js +6 -0
- package/array/proto/find-by-key.js.map +1 -0
- package/array/proto/intersection.cjs +6 -0
- package/array/proto/intersection.d.ts +27 -0
- package/array/proto/intersection.js +6 -0
- package/array/proto/intersection.js.map +1 -0
- package/array/proto/limit.cjs +6 -0
- package/array/proto/limit.d.ts +24 -0
- package/array/proto/limit.js +6 -0
- package/array/proto/limit.js.map +1 -0
- package/array/proto/remove.cjs +6 -0
- package/array/proto/remove.d.ts +32 -0
- package/array/proto/remove.js +6 -0
- package/array/proto/remove.js.map +1 -0
- package/array/proto/replace.cjs +6 -0
- package/array/proto/replace.d.ts +72 -0
- package/array/proto/replace.js +6 -0
- package/array/proto/replace.js.map +1 -0
- package/array/proto/uniquify-by-key.cjs +6 -0
- package/array/proto/uniquify-by-key.d.ts +24 -0
- package/array/proto/uniquify-by-key.js +6 -0
- package/array/proto/uniquify-by-key.js.map +1 -0
- package/array/proto/uniquify.cjs +6 -0
- package/array/proto/uniquify.d.ts +23 -0
- package/array/proto/uniquify.js +6 -0
- package/array/proto/uniquify.js.map +1 -0
- package/array/remove.cjs +46 -0
- package/array/remove.d.ts +33 -0
- package/array/remove.js +45 -0
- package/array/remove.js.map +1 -0
- package/array/replace.cjs +69 -0
- package/array/replace.d.ts +139 -0
- package/array/replace.js +68 -0
- package/array/replace.js.map +1 -0
- package/array/static/arrayize.cjs +4 -0
- package/array/static/arrayize.d.ts +6 -0
- package/array/static/arrayize.js +4 -0
- package/array/static/arrayize.js.map +1 -0
- package/array/uniquify-by-key.cjs +41 -0
- package/array/uniquify-by-key.d.ts +29 -0
- package/array/uniquify-by-key.js +40 -0
- package/array/uniquify-by-key.js.map +1 -0
- package/array/uniquify.cjs +32 -0
- package/array/uniquify.d.ts +27 -0
- package/array/uniquify.js +31 -0
- package/array/uniquify.js.map +1 -0
- package/boolean/static/is-falsy.cjs +4 -0
- package/boolean/static/is-falsy.d.ts +23 -0
- package/boolean/static/is-falsy.js +4 -0
- package/boolean/static/is-falsy.js.map +1 -0
- package/boolean/static/is-truthy.cjs +4 -0
- package/boolean/static/is-truthy.d.ts +23 -0
- package/boolean/static/is-truthy.js +4 -0
- package/boolean/static/is-truthy.js.map +1 -0
- package/class/index.cjs +4 -0
- package/class/index.d.ts +1 -0
- package/class/index.js +3 -0
- package/class/index.js.map +1 -0
- package/class/mixin.cjs +60 -0
- package/class/mixin.d.ts +50 -0
- package/class/mixin.js +59 -0
- package/class/mixin.js.map +1 -0
- package/compact.cjs +39 -0
- package/compact.d.ts +2 -0
- package/compact.js +38 -0
- package/compact.js.map +1 -0
- package/function/call.cjs +6 -0
- package/function/call.d.ts +1 -0
- package/function/call.js +5 -0
- package/function/call.js.map +1 -0
- package/function/debounce.cjs +85 -0
- package/function/debounce.d.ts +44 -0
- package/function/debounce.js +82 -0
- package/function/debounce.js.map +1 -0
- package/function/defer.cjs +42 -0
- package/function/defer.d.ts +26 -0
- package/function/defer.js +41 -0
- package/function/defer.js.map +1 -0
- package/function/index.cjs +8 -0
- package/function/index.d.ts +5 -0
- package/function/index.js +7 -0
- package/function/index.js.map +1 -0
- package/function/noop.cjs +23 -0
- package/function/noop.d.ts +20 -0
- package/function/noop.js +22 -0
- package/function/noop.js.map +1 -0
- package/function/of.cjs +44 -0
- package/function/of.d.ts +31 -0
- package/function/of.js +43 -0
- package/function/of.js.map +1 -0
- package/function/proto/debounce.cjs +6 -0
- package/function/proto/debounce.d.ts +30 -0
- package/function/proto/debounce.js +6 -0
- package/function/proto/debounce.js.map +1 -0
- package/function/static/call.cjs +4 -0
- package/function/static/call.d.ts +6 -0
- package/function/static/call.js +4 -0
- package/function/static/call.js.map +1 -0
- package/function/static/defer.cjs +4 -0
- package/function/static/defer.d.ts +23 -0
- package/function/static/defer.js +4 -0
- package/function/static/defer.js.map +1 -0
- package/function/static/noop.cjs +4 -0
- package/function/static/noop.d.ts +18 -0
- package/function/static/noop.js +4 -0
- package/function/static/noop.js.map +1 -0
- package/function/static/of.cjs +4 -0
- package/function/static/of.d.ts +26 -0
- package/function/static/of.js +4 -0
- package/function/static/of.js.map +1 -0
- package/index.cjs +31 -0
- package/index.d.ts +14 -0
- package/index.js +17 -0
- package/index.js.map +1 -0
- package/is-falsy.cjs +33 -0
- package/is-falsy.d.ts +28 -0
- package/is-falsy.js +32 -0
- package/is-falsy.js.map +1 -0
- package/is-truthy.cjs +33 -0
- package/is-truthy.d.ts +28 -0
- package/is-truthy.js +32 -0
- package/is-truthy.js.map +1 -0
- package/limit.cjs +34 -0
- package/limit.d.ts +2 -0
- package/limit.js +33 -0
- package/limit.js.map +1 -0
- package/map/index.cjs +4 -0
- package/map/index.d.ts +1 -0
- package/map/index.js +3 -0
- package/map/index.js.map +1 -0
- package/map/merge.cjs +33 -0
- package/map/merge.d.ts +24 -0
- package/map/merge.js +32 -0
- package/map/merge.js.map +1 -0
- package/math/index.cjs +4 -0
- package/math/index.d.ts +1 -0
- package/math/index.js +3 -0
- package/math/index.js.map +1 -0
- package/math/min-max.cjs +29 -0
- package/math/min-max.d.ts +24 -0
- package/math/min-max.js +28 -0
- package/math/min-max.js.map +1 -0
- package/math/static/min-max.cjs +4 -0
- package/math/static/min-max.d.ts +20 -0
- package/math/static/min-max.js +4 -0
- package/math/static/min-max.js.map +1 -0
- package/object/clone.cjs +157 -0
- package/object/clone.d.ts +161 -0
- package/object/clone.js +156 -0
- package/object/clone.js.map +1 -0
- package/object/compact.cjs +49 -0
- package/object/compact.d.ts +37 -0
- package/object/compact.js +48 -0
- package/object/compact.js.map +1 -0
- package/object/deep-freeze.cjs +16 -0
- package/object/deep-freeze.d.ts +1 -0
- package/object/deep-freeze.js +15 -0
- package/object/deep-freeze.js.map +1 -0
- package/object/deepest.cjs +55 -0
- package/object/deepest.d.ts +47 -0
- package/object/deepest.js +54 -0
- package/object/deepest.js.map +1 -0
- package/object/index.cjs +7 -0
- package/object/index.d.ts +4 -0
- package/object/index.js +6 -0
- package/object/index.js.map +1 -0
- package/object/static/clone.cjs +4 -0
- package/object/static/clone.d.ts +43 -0
- package/object/static/clone.js +4 -0
- package/object/static/clone.js.map +1 -0
- package/object/static/compact.cjs +4 -0
- package/object/static/compact.d.ts +26 -0
- package/object/static/compact.js +4 -0
- package/object/static/compact.js.map +1 -0
- package/object/static/deep-freeze.cjs +4 -0
- package/object/static/deep-freeze.d.ts +6 -0
- package/object/static/deep-freeze.js +4 -0
- package/object/static/deep-freeze.js.map +1 -0
- package/object/static/deepest.cjs +4 -0
- package/object/static/deepest.d.ts +32 -0
- package/object/static/deepest.js +4 -0
- package/object/static/deepest.js.map +1 -0
- package/open-promise.cjs +115 -0
- package/open-promise.d.ts +80 -0
- package/open-promise.js +113 -0
- package/open-promise.js.map +1 -0
- package/package.json +667 -0
- package/promise/chain.cjs +12 -0
- package/promise/chain.d.ts +7 -0
- package/promise/chain.js +11 -0
- package/promise/chain.js.map +1 -0
- package/promise/index.cjs +10 -0
- package/promise/index.d.ts +7 -0
- package/promise/index.js +9 -0
- package/promise/index.js.map +1 -0
- package/promise/never.cjs +41 -0
- package/promise/never.d.ts +34 -0
- package/promise/never.js +39 -0
- package/promise/never.js.map +1 -0
- package/promise/promisify.cjs +12 -0
- package/promise/promisify.d.ts +7 -0
- package/promise/promisify.js +11 -0
- package/promise/promisify.js.map +1 -0
- package/promise/proto/revert.cjs +6 -0
- package/promise/proto/revert.d.ts +29 -0
- package/promise/proto/revert.js +6 -0
- package/promise/proto/revert.js.map +1 -0
- package/promise/proto/try-catch.cjs +7 -0
- package/promise/proto/try-catch.d.ts +30 -0
- package/promise/proto/try-catch.js +7 -0
- package/promise/proto/try-catch.js.map +1 -0
- package/promise/revert.cjs +43 -0
- package/promise/revert.d.ts +38 -0
- package/promise/revert.js +42 -0
- package/promise/revert.js.map +1 -0
- package/promise/static/chain.cjs +4 -0
- package/promise/static/chain.d.ts +10 -0
- package/promise/static/chain.js +4 -0
- package/promise/static/chain.js.map +1 -0
- package/promise/static/never.cjs +5 -0
- package/promise/static/never.d.ts +39 -0
- package/promise/static/never.js +5 -0
- package/promise/static/never.js.map +1 -0
- package/promise/static/timeout.cjs +4 -0
- package/promise/static/timeout.d.ts +21 -0
- package/promise/static/timeout.js +4 -0
- package/promise/static/timeout.js.map +1 -0
- package/promise/static/try-one-by-one.cjs +4 -0
- package/promise/static/try-one-by-one.d.ts +27 -0
- package/promise/static/try-one-by-one.js +4 -0
- package/promise/static/try-one-by-one.js.map +1 -0
- package/promise/timeout.cjs +104 -0
- package/promise/timeout.d.ts +109 -0
- package/promise/timeout.js +103 -0
- package/promise/timeout.js.map +1 -0
- package/promise/try-catch.cjs +12 -0
- package/promise/try-catch.d.ts +45 -0
- package/promise/try-catch.js +11 -0
- package/promise/try-catch.js.map +1 -0
- package/promise/try-one-by-one.cjs +59 -0
- package/promise/try-one-by-one.d.ts +42 -0
- package/promise/try-one-by-one.js +58 -0
- package/promise/try-one-by-one.js.map +1 -0
- package/set/proto/difference.cjs +6 -0
- package/set/proto/difference.d.ts +26 -0
- package/set/proto/difference.js +6 -0
- package/set/proto/difference.js.map +1 -0
- package/set/proto/intersection.cjs +6 -0
- package/set/proto/intersection.d.ts +26 -0
- package/set/proto/intersection.js +6 -0
- package/set/proto/intersection.js.map +1 -0
- package/smart-map.cjs +108 -0
- package/smart-map.d.ts +81 -0
- package/smart-map.js +106 -0
- package/smart-map.js.map +1 -0
- package/string/index.cjs +4 -0
- package/string/index.d.ts +1 -0
- package/string/index.js +3 -0
- package/string/index.js.map +1 -0
- package/string/limit.cjs +35 -0
- package/string/limit.d.ts +29 -0
- package/string/limit.js +34 -0
- package/string/limit.js.map +1 -0
- package/string/proto/limit.cjs +6 -0
- package/string/proto/limit.d.ts +30 -0
- package/string/proto/limit.js +6 -0
- package/string/proto/limit.js.map +1 -0
- package/time/days.cjs +21 -0
- package/time/days.d.ts +15 -0
- package/time/days.js +20 -0
- package/time/days.js.map +1 -0
- package/time/hours.cjs +21 -0
- package/time/hours.d.ts +15 -0
- package/time/hours.js +20 -0
- package/time/hours.js.map +1 -0
- package/time/index.cjs +10 -0
- package/time/index.d.ts +7 -0
- package/time/index.js +9 -0
- package/time/index.js.map +1 -0
- package/time/minutes.cjs +21 -0
- package/time/minutes.d.ts +15 -0
- package/time/minutes.js +20 -0
- package/time/minutes.js.map +1 -0
- package/time/months.cjs +21 -0
- package/time/months.d.ts +15 -0
- package/time/months.js +20 -0
- package/time/months.js.map +1 -0
- package/time/seconds.cjs +20 -0
- package/time/seconds.d.ts +15 -0
- package/time/seconds.js +19 -0
- package/time/seconds.js.map +1 -0
- package/time/weeks.cjs +21 -0
- package/time/weeks.d.ts +15 -0
- package/time/weeks.js +20 -0
- package/time/weeks.js.map +1 -0
- package/time/years.cjs +21 -0
- package/time/years.d.ts +15 -0
- package/time/years.js +20 -0
- package/time/years.js.map +1 -0
- package/unique-id.cjs +24 -0
- package/unique-id.d.ts +21 -0
- package/unique-id.js +23 -0
- package/unique-id.js.map +1 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { isTruthy } from '@telperion/js-utils/is-truthy';
|
|
2
|
+
declare global {
|
|
3
|
+
export interface BooleanConstructor {
|
|
4
|
+
/**
|
|
5
|
+
* #### Returns whether the entered value is truthy
|
|
6
|
+
*
|
|
7
|
+
* * * *
|
|
8
|
+
* Example usage:
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import "@telperion/js-utils/boolean/static/is-truthy";
|
|
11
|
+
*
|
|
12
|
+
* Boolean.isTruthy(undefined); // false
|
|
13
|
+
* Boolean.isTruthy(true); // true
|
|
14
|
+
* Boolean.isTruthy([]) // true
|
|
15
|
+
*
|
|
16
|
+
* const truthyValues = ["a", undefined, "b", "", "c"].filter(Boolean.isTruthy); // ["a", "b", "c"]
|
|
17
|
+
* ```
|
|
18
|
+
* * * *
|
|
19
|
+
* @param value value to be checked
|
|
20
|
+
*/
|
|
21
|
+
isTruthy: typeof isTruthy;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/js-utils/src/boolean/static/is-truthy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,+BAA+B,CAAC;AAyBzD,OAAO,CAAC,QAAQ,GAAG,QAAQ,CAAC","file":"is-truthy.js","sourcesContent":["import { isTruthy } from '@telperion/js-utils/is-truthy';\n\ndeclare global {\n export interface BooleanConstructor {\n /**\n * #### Returns whether the entered value is truthy\n *\n * * * *\n * Example usage:\n * ```typescript\n * import \"@telperion/js-utils/boolean/static/is-truthy\";\n *\n * Boolean.isTruthy(undefined); // false\n * Boolean.isTruthy(true); // true\n * Boolean.isTruthy([]) // true\n *\n * const truthyValues = [\"a\", undefined, \"b\", \"\", \"c\"].filter(Boolean.isTruthy); // [\"a\", \"b\", \"c\"]\n * ```\n * * * *\n * @param value value to be checked\n */\n isTruthy: typeof isTruthy;\n }\n}\n\nBoolean.isTruthy = isTruthy;\n"]}
|
package/class/index.cjs
ADDED
package/class/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './mixin';
|
package/class/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/js-utils/src/class/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC","file":"index.js","sourcesContent":["export * from './mixin';\n"]}
|
package/class/mixin.cjs
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.mixin = mixin;
|
|
5
|
+
const PROPS_TO_FILTER = ['constructor'];
|
|
6
|
+
/**
|
|
7
|
+
* #### Mixin
|
|
8
|
+
*
|
|
9
|
+
* Creates a class that combines two classes using the mixin pattern.
|
|
10
|
+
* The resulting class extends the first class and includes all properties and methods from the second class.
|
|
11
|
+
*
|
|
12
|
+
* * * *
|
|
13
|
+
* Example:
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { mixin } from "@telperion/js-utils/class";
|
|
16
|
+
*
|
|
17
|
+
* class Timestamped {
|
|
18
|
+
* timestamp = Date.now();
|
|
19
|
+
* getAge() {
|
|
20
|
+
* return Date.now() - this.timestamp;
|
|
21
|
+
* }
|
|
22
|
+
* }
|
|
23
|
+
*
|
|
24
|
+
* class User {
|
|
25
|
+
* constructor(public name: string) {}
|
|
26
|
+
* greet() {
|
|
27
|
+
* return `Hello, ${this.name}`;
|
|
28
|
+
* }
|
|
29
|
+
* }
|
|
30
|
+
*
|
|
31
|
+
* class TimestampedUser extends mixin(User, Timestamped) {}
|
|
32
|
+
*
|
|
33
|
+
* const user = new TimestampedUser(['John'], []);
|
|
34
|
+
* console.log(user.greet()); // "Hello, John"
|
|
35
|
+
* console.log(user.getAge()); // Time since creation
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @param Base The base class to extend
|
|
39
|
+
* @param Mixin The mixin class to combine
|
|
40
|
+
* @returns A new class that combines both classes
|
|
41
|
+
*/
|
|
42
|
+
function mixin(Base, Mixin) {
|
|
43
|
+
// @ts-ignore - TypeScript doesn't fully understand the mixin pattern
|
|
44
|
+
class Mixed extends Base {
|
|
45
|
+
constructor(baseArgs, mixinArgs) {
|
|
46
|
+
super(...baseArgs);
|
|
47
|
+
// Create an instance of the mixin class and copy its properties
|
|
48
|
+
const mixinInstance = new Mixin(...mixinArgs);
|
|
49
|
+
for (const key of Reflect.ownKeys(mixinInstance)) {
|
|
50
|
+
this[key] = mixinInstance[key];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
// Copy prototype methods from mixin to the mixed class
|
|
55
|
+
const mixinKeys = Reflect.ownKeys(Mixin.prototype).filter((key) => !PROPS_TO_FILTER.includes(key));
|
|
56
|
+
for (const prop of mixinKeys) {
|
|
57
|
+
Mixed.prototype[prop] = Mixin.prototype[prop];
|
|
58
|
+
}
|
|
59
|
+
return Mixed;
|
|
60
|
+
}
|
package/class/mixin.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type helpers for Mixin
|
|
3
|
+
*/
|
|
4
|
+
type Constructor<T extends object = object, U extends any[] = any[]> = new (...args: U) => T;
|
|
5
|
+
type ConstructorParams<T> = T extends new (...args: infer U) => any ? U : never;
|
|
6
|
+
type InstanceType<T> = T extends new (...args: any[]) => infer U ? U : never;
|
|
7
|
+
type MergeInstances<T, U> = {
|
|
8
|
+
[P in keyof T]: P extends keyof U ? U[P] : T[P];
|
|
9
|
+
} & U;
|
|
10
|
+
/**
|
|
11
|
+
* #### Mixin
|
|
12
|
+
*
|
|
13
|
+
* Creates a class that combines two classes using the mixin pattern.
|
|
14
|
+
* The resulting class extends the first class and includes all properties and methods from the second class.
|
|
15
|
+
*
|
|
16
|
+
* * * *
|
|
17
|
+
* Example:
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { mixin } from "@telperion/js-utils/class";
|
|
20
|
+
*
|
|
21
|
+
* class Timestamped {
|
|
22
|
+
* timestamp = Date.now();
|
|
23
|
+
* getAge() {
|
|
24
|
+
* return Date.now() - this.timestamp;
|
|
25
|
+
* }
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* class User {
|
|
29
|
+
* constructor(public name: string) {}
|
|
30
|
+
* greet() {
|
|
31
|
+
* return `Hello, ${this.name}`;
|
|
32
|
+
* }
|
|
33
|
+
* }
|
|
34
|
+
*
|
|
35
|
+
* class TimestampedUser extends mixin(User, Timestamped) {}
|
|
36
|
+
*
|
|
37
|
+
* const user = new TimestampedUser(['John'], []);
|
|
38
|
+
* console.log(user.greet()); // "Hello, John"
|
|
39
|
+
* console.log(user.getAge()); // Time since creation
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @param Base The base class to extend
|
|
43
|
+
* @param Mixin The mixin class to combine
|
|
44
|
+
* @returns A new class that combines both classes
|
|
45
|
+
*/
|
|
46
|
+
export declare function mixin<T extends Constructor<any>, U extends Constructor<any>>(Base: T, Mixin: U): Constructor<MergeInstances<InstanceType<T>, InstanceType<U>>, [
|
|
47
|
+
ConstructorParams<T>,
|
|
48
|
+
ConstructorParams<U>
|
|
49
|
+
]>;
|
|
50
|
+
export {};
|
package/class/mixin.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
const PROPS_TO_FILTER = ['constructor'];
|
|
3
|
+
/**
|
|
4
|
+
* #### Mixin
|
|
5
|
+
*
|
|
6
|
+
* Creates a class that combines two classes using the mixin pattern.
|
|
7
|
+
* The resulting class extends the first class and includes all properties and methods from the second class.
|
|
8
|
+
*
|
|
9
|
+
* * * *
|
|
10
|
+
* Example:
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { mixin } from "@telperion/js-utils/class";
|
|
13
|
+
*
|
|
14
|
+
* class Timestamped {
|
|
15
|
+
* timestamp = Date.now();
|
|
16
|
+
* getAge() {
|
|
17
|
+
* return Date.now() - this.timestamp;
|
|
18
|
+
* }
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* class User {
|
|
22
|
+
* constructor(public name: string) {}
|
|
23
|
+
* greet() {
|
|
24
|
+
* return `Hello, ${this.name}`;
|
|
25
|
+
* }
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* class TimestampedUser extends mixin(User, Timestamped) {}
|
|
29
|
+
*
|
|
30
|
+
* const user = new TimestampedUser(['John'], []);
|
|
31
|
+
* console.log(user.greet()); // "Hello, John"
|
|
32
|
+
* console.log(user.getAge()); // Time since creation
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @param Base The base class to extend
|
|
36
|
+
* @param Mixin The mixin class to combine
|
|
37
|
+
* @returns A new class that combines both classes
|
|
38
|
+
*/
|
|
39
|
+
export function mixin(Base, Mixin) {
|
|
40
|
+
// @ts-ignore - TypeScript doesn't fully understand the mixin pattern
|
|
41
|
+
class Mixed extends Base {
|
|
42
|
+
constructor(baseArgs, mixinArgs) {
|
|
43
|
+
super(...baseArgs);
|
|
44
|
+
// Create an instance of the mixin class and copy its properties
|
|
45
|
+
const mixinInstance = new Mixin(...mixinArgs);
|
|
46
|
+
for (const key of Reflect.ownKeys(mixinInstance)) {
|
|
47
|
+
this[key] = mixinInstance[key];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
// Copy prototype methods from mixin to the mixed class
|
|
52
|
+
const mixinKeys = Reflect.ownKeys(Mixin.prototype).filter((key) => !PROPS_TO_FILTER.includes(key));
|
|
53
|
+
for (const prop of mixinKeys) {
|
|
54
|
+
Mixed.prototype[prop] = Mixin.prototype[prop];
|
|
55
|
+
}
|
|
56
|
+
return Mixed;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
//# sourceMappingURL=mixin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/js-utils/src/class/mixin.ts"],"names":[],"mappings":"AAAA,uDAAuD;AAUvD,MAAM,eAAe,GAAG,CAAC,aAAa,CAAC,CAAC;AAExC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,UAAU,KAAK,CACnB,IAAO,EACP,KAAQ;IAKR,qEAAqE;IACrE,MAAM,KAAM,SAAQ,IAAI;QACtB,YAAY,QAA8B,EAAE,SAA+B;YACzE,KAAK,CAAC,GAAG,QAAQ,CAAC,CAAC;YAEnB,gEAAgE;YAChE,MAAM,aAAa,GAAG,IAAI,KAAK,CAAC,GAAG,SAAS,CAAC,CAAC;YAE9C,KAAK,MAAM,GAAG,IAAI,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC,EAAE,CAAC;gBAChD,IAAY,CAAC,GAAG,CAAC,GAAI,aAAqB,CAAC,GAAG,CAAC,CAAC;YACnD,CAAC;QACH,CAAC;KACF;IAED,uDAAuD;IACvD,MAAM,SAAS,GAAG,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,MAAM,CACvD,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,eAAe,CAAC,QAAQ,CAAC,GAAa,CAAC,CAClD,CAAC;IAEF,KAAK,MAAM,IAAI,IAAI,SAAS,EAAE,CAAC;QAC5B,KAAK,CAAC,SAAiB,CAAC,IAAI,CAAC,GAAI,KAAK,CAAC,SAAiB,CAAC,IAAI,CAAC,CAAC;IAClE,CAAC;IAED,OAAO,KAAY,CAAC;AACtB,CAAC","file":"mixin.js","sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n\n/**\n * Type helpers for Mixin\n */\ntype Constructor<T extends object = object, U extends any[] = any[]> = new (...args: U) => T;\ntype ConstructorParams<T> = T extends new (...args: infer U) => any ? U : never;\ntype InstanceType<T> = T extends new (...args: any[]) => infer U ? U : never;\ntype MergeInstances<T, U> = { [P in keyof T]: P extends keyof U ? U[P] : T[P] } & U;\n\nconst PROPS_TO_FILTER = ['constructor'];\n\n/**\n * #### Mixin\n *\n * Creates a class that combines two classes using the mixin pattern.\n * The resulting class extends the first class and includes all properties and methods from the second class.\n *\n * * * *\n * Example:\n * ```typescript\n * import { mixin } from \"@telperion/js-utils/class\";\n *\n * class Timestamped {\n * timestamp = Date.now();\n * getAge() {\n * return Date.now() - this.timestamp;\n * }\n * }\n *\n * class User {\n * constructor(public name: string) {}\n * greet() {\n * return `Hello, ${this.name}`;\n * }\n * }\n *\n * class TimestampedUser extends mixin(User, Timestamped) {}\n *\n * const user = new TimestampedUser(['John'], []);\n * console.log(user.greet()); // \"Hello, John\"\n * console.log(user.getAge()); // Time since creation\n * ```\n *\n * @param Base The base class to extend\n * @param Mixin The mixin class to combine\n * @returns A new class that combines both classes\n */\nexport function mixin<T extends Constructor<any>, U extends Constructor<any>>(\n Base: T,\n Mixin: U\n): Constructor<\n MergeInstances<InstanceType<T>, InstanceType<U>>,\n [ConstructorParams<T>, ConstructorParams<U>]\n> {\n // @ts-ignore - TypeScript doesn't fully understand the mixin pattern\n class Mixed extends Base implements InstanceType<U> {\n constructor(baseArgs: ConstructorParams<T>, mixinArgs: ConstructorParams<U>) {\n super(...baseArgs);\n\n // Create an instance of the mixin class and copy its properties\n const mixinInstance = new Mixin(...mixinArgs);\n\n for (const key of Reflect.ownKeys(mixinInstance)) {\n (this as any)[key] = (mixinInstance as any)[key];\n }\n }\n }\n\n // Copy prototype methods from mixin to the mixed class\n const mixinKeys = Reflect.ownKeys(Mixin.prototype).filter(\n (key) => !PROPS_TO_FILTER.includes(key as string)\n );\n\n for (const prop of mixinKeys) {\n (Mixed.prototype as any)[prop] = (Mixin.prototype as any)[prop];\n }\n\n return Mixed as any;\n}\n"]}
|
package/compact.cjs
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.compact = compact;
|
|
4
|
+
const compact_1 = require("@telperion/js-utils/object/compact");
|
|
5
|
+
const compact_2 = require("@telperion/js-utils/array/compact");
|
|
6
|
+
/**
|
|
7
|
+
* #### Compact
|
|
8
|
+
*
|
|
9
|
+
* Filters falsy values of the given array
|
|
10
|
+
* Removes `null` and `undefined` values and their keys from an object
|
|
11
|
+
*
|
|
12
|
+
* * * *
|
|
13
|
+
* Example usage:
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { compact } from "@telperion/js-utils";
|
|
16
|
+
*
|
|
17
|
+
* const arr = [undefined, "", false, 0, 1, "1"];
|
|
18
|
+
* const compacted = compact(arr); // [1, "1"];
|
|
19
|
+
*
|
|
20
|
+
* const object = {
|
|
21
|
+
* x: null,
|
|
22
|
+
* y: undefined,
|
|
23
|
+
* z: 20
|
|
24
|
+
* };
|
|
25
|
+
*
|
|
26
|
+
* const compacted = compact(object); // {z: 20}
|
|
27
|
+
* ```
|
|
28
|
+
* * * *
|
|
29
|
+
* @param arrayOrObject Array or Object to compact
|
|
30
|
+
*/
|
|
31
|
+
function compact(arrayOrObject) {
|
|
32
|
+
if (arrayOrObject instanceof Array) {
|
|
33
|
+
return (0, compact_2.compact)(arrayOrObject);
|
|
34
|
+
}
|
|
35
|
+
if (arrayOrObject instanceof Object) {
|
|
36
|
+
return (0, compact_1.compact)(arrayOrObject);
|
|
37
|
+
}
|
|
38
|
+
throw new TypeError('Value is not object nor array');
|
|
39
|
+
}
|
package/compact.d.ts
ADDED
package/compact.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { compact as objCompact } from '@telperion/js-utils/object/compact';
|
|
2
|
+
import { compact as arrCompact } from '@telperion/js-utils/array/compact';
|
|
3
|
+
/**
|
|
4
|
+
* #### Compact
|
|
5
|
+
*
|
|
6
|
+
* Filters falsy values of the given array
|
|
7
|
+
* Removes `null` and `undefined` values and their keys from an object
|
|
8
|
+
*
|
|
9
|
+
* * * *
|
|
10
|
+
* Example usage:
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { compact } from "@telperion/js-utils";
|
|
13
|
+
*
|
|
14
|
+
* const arr = [undefined, "", false, 0, 1, "1"];
|
|
15
|
+
* const compacted = compact(arr); // [1, "1"];
|
|
16
|
+
*
|
|
17
|
+
* const object = {
|
|
18
|
+
* x: null,
|
|
19
|
+
* y: undefined,
|
|
20
|
+
* z: 20
|
|
21
|
+
* };
|
|
22
|
+
*
|
|
23
|
+
* const compacted = compact(object); // {z: 20}
|
|
24
|
+
* ```
|
|
25
|
+
* * * *
|
|
26
|
+
* @param arrayOrObject Array or Object to compact
|
|
27
|
+
*/
|
|
28
|
+
export function compact(arrayOrObject) {
|
|
29
|
+
if (arrayOrObject instanceof Array) {
|
|
30
|
+
return arrCompact(arrayOrObject);
|
|
31
|
+
}
|
|
32
|
+
if (arrayOrObject instanceof Object) {
|
|
33
|
+
return objCompact(arrayOrObject);
|
|
34
|
+
}
|
|
35
|
+
throw new TypeError('Value is not object nor array');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
//# sourceMappingURL=compact.js.map
|
package/compact.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/js-utils/src/compact.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,oCAAoC,CAAC;AAC3E,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,mCAAmC,CAAC;AAI1E;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,OAAO,CAA6B,aAAgB;IAClE,IAAI,aAAa,YAAY,KAAK,EAAE,CAAC;QACnC,OAAO,UAAU,CAAC,aAAa,CAAQ,CAAC;IAC1C,CAAC;IAED,IAAI,aAAa,YAAY,MAAM,EAAE,CAAC;QACpC,OAAO,UAAU,CAAC,aAAa,CAAQ,CAAC;IAC1C,CAAC;IAED,MAAM,IAAI,SAAS,CAAC,+BAA+B,CAAC,CAAC;AACvD,CAAC","file":"compact.js","sourcesContent":["import { compact as objCompact } from '@telperion/js-utils/object/compact';\nimport { compact as arrCompact } from '@telperion/js-utils/array/compact';\n\nexport function compact<T = any>(array: T[]): T[];\nexport function compact<T extends Object>(object: T): Partial<T>;\n/**\n * #### Compact\n *\n * Filters falsy values of the given array\n * Removes `null` and `undefined` values and their keys from an object\n *\n * * * *\n * Example usage:\n * ```typescript\n * import { compact } from \"@telperion/js-utils\";\n *\n * const arr = [undefined, \"\", false, 0, 1, \"1\"];\n * const compacted = compact(arr); // [1, \"1\"];\n *\n * const object = {\n * x: null,\n * y: undefined,\n * z: 20\n * };\n *\n * const compacted = compact(object); // {z: 20}\n * ```\n * * * *\n * @param arrayOrObject Array or Object to compact\n */\nexport function compact<T extends (any[] | Object)>(arrayOrObject: T): T extends Array<infer U> ? U[] : Partial<T> {\n if (arrayOrObject instanceof Array) {\n return arrCompact(arrayOrObject) as any;\n }\n\n if (arrayOrObject instanceof Object) {\n return objCompact(arrayOrObject) as any;\n }\n\n throw new TypeError('Value is not object nor array');\n}\n\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function call<T>(func: (...args: any[]) => T): T;
|
package/function/call.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/js-utils/src/function/call.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,IAAI,CAAI,IAA2B;IACjD,OAAO,IAAI,EAAE,CAAC;AAChB,CAAC","file":"call.js","sourcesContent":["export function call<T>(func: (...args: any[]) => T): T {\n return func();\n}\n"]}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_DEBOUNCE_TIME = void 0;
|
|
4
|
+
exports.debounceWithKey = debounceWithKey;
|
|
5
|
+
exports.debounce = debounce;
|
|
6
|
+
const timeout_1 = require("@telperion/js-utils/promise/timeout");
|
|
7
|
+
const promisify_1 = require("@telperion/js-utils/promise/promisify");
|
|
8
|
+
const open_promise_1 = require("@telperion/js-utils/open-promise");
|
|
9
|
+
exports.DEFAULT_DEBOUNCE_TIME = 180;
|
|
10
|
+
const KEY_REFERENCES = new Map();
|
|
11
|
+
const CACHE = new Map();
|
|
12
|
+
function debounceWithKey(key, callback, time = exports.DEFAULT_DEBOUNCE_TIME, thisObject = null, ...args) {
|
|
13
|
+
let timeoutPromise;
|
|
14
|
+
let promise;
|
|
15
|
+
if (CACHE.has(key)) {
|
|
16
|
+
const stocked = CACHE.get(key);
|
|
17
|
+
timeoutPromise = stocked.timeout;
|
|
18
|
+
promise = stocked.promise;
|
|
19
|
+
timeout_1.timeout.cancel(timeoutPromise);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
promise = new open_promise_1.OpenPromise();
|
|
23
|
+
}
|
|
24
|
+
timeoutPromise = (0, timeout_1.timeout)(time);
|
|
25
|
+
CACHE.set(key, { promise, timeout: timeoutPromise });
|
|
26
|
+
timeoutPromise
|
|
27
|
+
.then(() => {
|
|
28
|
+
CACHE.delete(key);
|
|
29
|
+
const result = callback.call(thisObject, ...args);
|
|
30
|
+
promise.bindTo((0, promisify_1.promisify)(result));
|
|
31
|
+
return result;
|
|
32
|
+
})
|
|
33
|
+
.catch(err => {
|
|
34
|
+
if (err !== timeout_1.timeout.TIMEOUT_CANCELLED) {
|
|
35
|
+
promise.reject(err);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
return promise;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* #### Debounces a function that delays invoking until after given time have elapsed since the last time the debounced function was invoked
|
|
42
|
+
*
|
|
43
|
+
* * * *
|
|
44
|
+
* Example usage:
|
|
45
|
+
* ```typescript
|
|
46
|
+
* import { debounce } from "@telperion/js-utils/promise";
|
|
47
|
+
*
|
|
48
|
+
* function foo() {
|
|
49
|
+
* console.log("hello");
|
|
50
|
+
* }
|
|
51
|
+
*
|
|
52
|
+
* for (let i = 0; i < 5; i++) {
|
|
53
|
+
* debounce(foo);
|
|
54
|
+
* }
|
|
55
|
+
*
|
|
56
|
+
* // logs "hello" only once
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* Static usage example:
|
|
60
|
+
* ```typescript
|
|
61
|
+
* import "@telperion/js-utils/dist/as-proto/debounce";
|
|
62
|
+
*
|
|
63
|
+
* function foo() {
|
|
64
|
+
* console.log("hello");
|
|
65
|
+
* }
|
|
66
|
+
*
|
|
67
|
+
* for (let i = 0; i < 5; i++) {
|
|
68
|
+
* foo.debounce();
|
|
69
|
+
* }
|
|
70
|
+
*
|
|
71
|
+
* // logs "hello" only once
|
|
72
|
+
* ```
|
|
73
|
+
* * * *
|
|
74
|
+
* @param callback The function to execute only last of multiple execute requests by given time
|
|
75
|
+
* @param [time = 180] Time for debouncing
|
|
76
|
+
* @param [thisObject = null] This object to execute the callback function with
|
|
77
|
+
* @param args Function arguments
|
|
78
|
+
* @return A promise which resolves right after the debouncing sequence has been finished
|
|
79
|
+
*/
|
|
80
|
+
function debounce(callback, time = exports.DEFAULT_DEBOUNCE_TIME, thisObject = null, ...args) {
|
|
81
|
+
if (!KEY_REFERENCES.has(callback)) {
|
|
82
|
+
KEY_REFERENCES.set(callback, Symbol());
|
|
83
|
+
}
|
|
84
|
+
return debounceWithKey(KEY_REFERENCES.get(callback), callback, time, thisObject, ...args);
|
|
85
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export type DebounceFunction<T> = (...args: any[]) => (T | Promise<T>);
|
|
2
|
+
export declare const DEFAULT_DEBOUNCE_TIME = 180;
|
|
3
|
+
export declare function debounceWithKey<T>(key: symbol, callback: DebounceFunction<T>, time?: number, thisObject?: any, ...args: any[]): Promise<T>;
|
|
4
|
+
/**
|
|
5
|
+
* #### Debounces a function that delays invoking until after given time have elapsed since the last time the debounced function was invoked
|
|
6
|
+
*
|
|
7
|
+
* * * *
|
|
8
|
+
* Example usage:
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { debounce } from "@telperion/js-utils/promise";
|
|
11
|
+
*
|
|
12
|
+
* function foo() {
|
|
13
|
+
* console.log("hello");
|
|
14
|
+
* }
|
|
15
|
+
*
|
|
16
|
+
* for (let i = 0; i < 5; i++) {
|
|
17
|
+
* debounce(foo);
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* // logs "hello" only once
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* Static usage example:
|
|
24
|
+
* ```typescript
|
|
25
|
+
* import "@telperion/js-utils/dist/as-proto/debounce";
|
|
26
|
+
*
|
|
27
|
+
* function foo() {
|
|
28
|
+
* console.log("hello");
|
|
29
|
+
* }
|
|
30
|
+
*
|
|
31
|
+
* for (let i = 0; i < 5; i++) {
|
|
32
|
+
* foo.debounce();
|
|
33
|
+
* }
|
|
34
|
+
*
|
|
35
|
+
* // logs "hello" only once
|
|
36
|
+
* ```
|
|
37
|
+
* * * *
|
|
38
|
+
* @param callback The function to execute only last of multiple execute requests by given time
|
|
39
|
+
* @param [time = 180] Time for debouncing
|
|
40
|
+
* @param [thisObject = null] This object to execute the callback function with
|
|
41
|
+
* @param args Function arguments
|
|
42
|
+
* @return A promise which resolves right after the debouncing sequence has been finished
|
|
43
|
+
*/
|
|
44
|
+
export declare function debounce<T>(callback: DebounceFunction<T>, time?: number, thisObject?: any, ...args: any[]): Promise<T>;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { timeout } from '@telperion/js-utils/promise/timeout';
|
|
2
|
+
import { promisify } from '@telperion/js-utils/promise/promisify';
|
|
3
|
+
import { OpenPromise } from '@telperion/js-utils/open-promise';
|
|
4
|
+
export const DEFAULT_DEBOUNCE_TIME = 180;
|
|
5
|
+
const KEY_REFERENCES = new Map();
|
|
6
|
+
const CACHE = new Map();
|
|
7
|
+
export function debounceWithKey(key, callback, time = DEFAULT_DEBOUNCE_TIME, thisObject = null, ...args) {
|
|
8
|
+
let timeoutPromise;
|
|
9
|
+
let promise;
|
|
10
|
+
if (CACHE.has(key)) {
|
|
11
|
+
const stocked = CACHE.get(key);
|
|
12
|
+
timeoutPromise = stocked.timeout;
|
|
13
|
+
promise = stocked.promise;
|
|
14
|
+
timeout.cancel(timeoutPromise);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
promise = new OpenPromise();
|
|
18
|
+
}
|
|
19
|
+
timeoutPromise = timeout(time);
|
|
20
|
+
CACHE.set(key, { promise, timeout: timeoutPromise });
|
|
21
|
+
timeoutPromise
|
|
22
|
+
.then(() => {
|
|
23
|
+
CACHE.delete(key);
|
|
24
|
+
const result = callback.call(thisObject, ...args);
|
|
25
|
+
promise.bindTo(promisify(result));
|
|
26
|
+
return result;
|
|
27
|
+
})
|
|
28
|
+
.catch(err => {
|
|
29
|
+
if (err !== timeout.TIMEOUT_CANCELLED) {
|
|
30
|
+
promise.reject(err);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
return promise;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* #### Debounces a function that delays invoking until after given time have elapsed since the last time the debounced function was invoked
|
|
37
|
+
*
|
|
38
|
+
* * * *
|
|
39
|
+
* Example usage:
|
|
40
|
+
* ```typescript
|
|
41
|
+
* import { debounce } from "@telperion/js-utils/promise";
|
|
42
|
+
*
|
|
43
|
+
* function foo() {
|
|
44
|
+
* console.log("hello");
|
|
45
|
+
* }
|
|
46
|
+
*
|
|
47
|
+
* for (let i = 0; i < 5; i++) {
|
|
48
|
+
* debounce(foo);
|
|
49
|
+
* }
|
|
50
|
+
*
|
|
51
|
+
* // logs "hello" only once
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* Static usage example:
|
|
55
|
+
* ```typescript
|
|
56
|
+
* import "@telperion/js-utils/dist/as-proto/debounce";
|
|
57
|
+
*
|
|
58
|
+
* function foo() {
|
|
59
|
+
* console.log("hello");
|
|
60
|
+
* }
|
|
61
|
+
*
|
|
62
|
+
* for (let i = 0; i < 5; i++) {
|
|
63
|
+
* foo.debounce();
|
|
64
|
+
* }
|
|
65
|
+
*
|
|
66
|
+
* // logs "hello" only once
|
|
67
|
+
* ```
|
|
68
|
+
* * * *
|
|
69
|
+
* @param callback The function to execute only last of multiple execute requests by given time
|
|
70
|
+
* @param [time = 180] Time for debouncing
|
|
71
|
+
* @param [thisObject = null] This object to execute the callback function with
|
|
72
|
+
* @param args Function arguments
|
|
73
|
+
* @return A promise which resolves right after the debouncing sequence has been finished
|
|
74
|
+
*/
|
|
75
|
+
export function debounce(callback, time = DEFAULT_DEBOUNCE_TIME, thisObject = null, ...args) {
|
|
76
|
+
if (!KEY_REFERENCES.has(callback)) {
|
|
77
|
+
KEY_REFERENCES.set(callback, Symbol());
|
|
78
|
+
}
|
|
79
|
+
return debounceWithKey(KEY_REFERENCES.get(callback), callback, time, thisObject, ...args);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
//# sourceMappingURL=debounce.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/js-utils/src/function/debounce.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qCAAqC,CAAC;AAC9D,OAAO,EAAE,SAAS,EAAE,MAAM,uCAAuC,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,MAAM,kCAAkC,CAAC;AAS/D,MAAM,CAAC,MAAM,qBAAqB,GAAG,GAAG,CAAC;AAEzC,MAAM,cAAc,GAAG,IAAI,GAAG,EAAoB,CAAC;AACnD,MAAM,KAAK,GAAG,IAAI,GAAG,EAA4B,CAAC;AAElD,MAAM,UAAU,eAAe,CAC7B,GAAW,EACX,QAA6B,EAC7B,IAAI,GAAG,qBAAqB,EAC5B,aAAkB,IAAI,EACtB,GAAG,IAAW;IAEd,IAAI,cAA4B,CAAC;IACjC,IAAI,OAAoB,CAAC;IAEzB,IAAI,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;QACnB,MAAM,OAAO,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,CAAE,CAAC;QAEhC,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC;QACjC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;QAC1B,OAAO,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;IACjC,CAAC;SAAM,CAAC;QACN,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;IAC9B,CAAC;IAED,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC/B,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,CAAC,CAAC;IAErD,cAAc;SACX,IAAI,CAAC,GAAG,EAAE;QACT,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAElB,MAAM,MAAM,GAAG,QAAQ,CAAC,IAAI,CAAC,UAAU,EAAE,GAAG,IAAI,CAAC,CAAC;QAElD,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC;QAElC,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;SACD,KAAK,CAAC,GAAG,CAAC,EAAE;QACX,IAAI,GAAG,KAAK,OAAO,CAAC,iBAAiB,EAAE,CAAC;YACtC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACtB,CAAC;IACH,CAAC,CAAC,CAAC;IAEL,OAAO,OAAO,CAAC;AACjB,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,MAAM,UAAU,QAAQ,CACtB,QAA6B,EAC7B,IAAI,GAAG,qBAAqB,EAC5B,aAAkB,IAAI,EACtB,GAAG,IAAW;IAEd,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;QAClC,cAAc,CAAC,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC;IACzC,CAAC;IAED,OAAO,eAAe,CAAC,cAAc,CAAC,GAAG,CAAC,QAAQ,CAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,UAAU,EAAE,GAAG,IAAI,CAAC,CAAC;AAC7F,CAAC","file":"debounce.js","sourcesContent":["import { timeout } from '@telperion/js-utils/promise/timeout';\nimport { promisify } from '@telperion/js-utils/promise/promisify';\nimport { OpenPromise } from '@telperion/js-utils/open-promise';\n\nexport type DebounceFunction<T> = (...args: any[]) => (T | Promise<T>);\n\ninterface CacheObject<T> {\n timeout: Promise<T>;\n promise: OpenPromise;\n}\n\nexport const DEFAULT_DEBOUNCE_TIME = 180;\n\nconst KEY_REFERENCES = new Map<Function, symbol>();\nconst CACHE = new Map<symbol, CacheObject<any>>();\n\nexport function debounceWithKey<T>(\n key: symbol,\n callback: DebounceFunction<T>,\n time = DEFAULT_DEBOUNCE_TIME,\n thisObject: any = null,\n ...args: any[]\n): Promise<T> {\n let timeoutPromise: Promise<any>;\n let promise: OpenPromise;\n\n if (CACHE.has(key)) {\n const stocked = CACHE.get(key)!;\n\n timeoutPromise = stocked.timeout;\n promise = stocked.promise;\n timeout.cancel(timeoutPromise);\n } else {\n promise = new OpenPromise();\n }\n\n timeoutPromise = timeout(time);\n CACHE.set(key, { promise, timeout: timeoutPromise });\n\n timeoutPromise\n .then(() => {\n CACHE.delete(key);\n\n const result = callback.call(thisObject, ...args);\n\n promise.bindTo(promisify(result));\n\n return result;\n })\n .catch(err => {\n if (err !== timeout.TIMEOUT_CANCELLED) {\n promise.reject(err);\n }\n });\n\n return promise;\n}\n\n/**\n * #### Debounces a function that delays invoking until after given time have elapsed since the last time the debounced function was invoked\n *\n * * * *\n * Example usage:\n * ```typescript\n * import { debounce } from \"@telperion/js-utils/promise\";\n *\n * function foo() {\n * console.log(\"hello\");\n * }\n *\n * for (let i = 0; i < 5; i++) {\n * debounce(foo);\n * }\n *\n * // logs \"hello\" only once\n * ```\n *\n * Static usage example:\n * ```typescript\n * import \"@telperion/js-utils/dist/as-proto/debounce\";\n *\n * function foo() {\n * console.log(\"hello\");\n * }\n *\n * for (let i = 0; i < 5; i++) {\n * foo.debounce();\n * }\n *\n * // logs \"hello\" only once\n * ```\n * * * *\n * @param callback The function to execute only last of multiple execute requests by given time\n * @param [time = 180] Time for debouncing\n * @param [thisObject = null] This object to execute the callback function with\n * @param args Function arguments\n * @return A promise which resolves right after the debouncing sequence has been finished\n */\nexport function debounce<T>(\n callback: DebounceFunction<T>,\n time = DEFAULT_DEBOUNCE_TIME,\n thisObject: any = null,\n ...args: any[]\n): Promise<T> {\n if (!KEY_REFERENCES.has(callback)) {\n KEY_REFERENCES.set(callback, Symbol());\n }\n\n return debounceWithKey(KEY_REFERENCES.get(callback)!, callback, time, thisObject, ...args);\n}\n"]}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defer = defer;
|
|
4
|
+
const noop_1 = require("@telperion/js-utils/function/noop");
|
|
5
|
+
/**
|
|
6
|
+
* #### Delays the execution of the passed function to increase the render performance
|
|
7
|
+
*
|
|
8
|
+
* * * *
|
|
9
|
+
* Example usage:
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { defer } from "@telperion/js-utils/function";
|
|
12
|
+
*
|
|
13
|
+
* defer(() => aFunctionToDefer())
|
|
14
|
+
* .then(res => ...)
|
|
15
|
+
* .catch(err => ...);
|
|
16
|
+
* ```
|
|
17
|
+
* Example as promise static method
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import "@telperion/js-utils/function/static/defer";
|
|
20
|
+
*
|
|
21
|
+
* Function.defer(() => aFunctionToDefer())
|
|
22
|
+
* .then(res => ...)
|
|
23
|
+
* .catch(err => ...);
|
|
24
|
+
* ```
|
|
25
|
+
* * * *
|
|
26
|
+
* @param callback Callback function to be executed
|
|
27
|
+
* @typeparam T type of the return value of the callback
|
|
28
|
+
* @returns A promise which resolves with the value of the callback right after the execution
|
|
29
|
+
*/
|
|
30
|
+
function defer(callback = noop_1.noop) {
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
setTimeout(() => {
|
|
33
|
+
try {
|
|
34
|
+
const returnValue = callback();
|
|
35
|
+
resolve(returnValue);
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
reject(error);
|
|
39
|
+
}
|
|
40
|
+
}, 0);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #### Delays the execution of the passed function to increase the render performance
|
|
3
|
+
*
|
|
4
|
+
* * * *
|
|
5
|
+
* Example usage:
|
|
6
|
+
* ```typescript
|
|
7
|
+
* import { defer } from "@telperion/js-utils/function";
|
|
8
|
+
*
|
|
9
|
+
* defer(() => aFunctionToDefer())
|
|
10
|
+
* .then(res => ...)
|
|
11
|
+
* .catch(err => ...);
|
|
12
|
+
* ```
|
|
13
|
+
* Example as promise static method
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import "@telperion/js-utils/function/static/defer";
|
|
16
|
+
*
|
|
17
|
+
* Function.defer(() => aFunctionToDefer())
|
|
18
|
+
* .then(res => ...)
|
|
19
|
+
* .catch(err => ...);
|
|
20
|
+
* ```
|
|
21
|
+
* * * *
|
|
22
|
+
* @param callback Callback function to be executed
|
|
23
|
+
* @typeparam T type of the return value of the callback
|
|
24
|
+
* @returns A promise which resolves with the value of the callback right after the execution
|
|
25
|
+
*/
|
|
26
|
+
export declare function defer<T = any>(callback?: () => T): Promise<T>;
|