@augment-vir/common 22.0.0 → 22.1.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/dist/cjs/augments/debounce.js +38 -0
- package/dist/cjs/augments/object/merge-deep.js +23 -34
- package/dist/cjs/index.js +1 -0
- package/dist/esm/augments/debounce.js +34 -0
- package/dist/esm/augments/object/merge-deep.js +23 -34
- package/dist/esm/index.js +1 -0
- package/dist/types/augments/debounce.d.ts +15 -0
- package/dist/types/index.d.ts +1 -0
- package/package.json +4 -4
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createDebounce = exports.DebounceStyle = void 0;
|
|
4
|
+
var DebounceStyle;
|
|
5
|
+
(function (DebounceStyle) {
|
|
6
|
+
/**
|
|
7
|
+
* Fires on the first call, then waits the given amount of milliseconds until a subsequent call
|
|
8
|
+
* can be made.
|
|
9
|
+
*/
|
|
10
|
+
DebounceStyle["FirstThenWait"] = "first-then-wait";
|
|
11
|
+
/**
|
|
12
|
+
* Waits the given amount of milliseconds after the first call and then fires the latest
|
|
13
|
+
* assigned callback.
|
|
14
|
+
*/
|
|
15
|
+
DebounceStyle["AfterWait"] = "after-wait";
|
|
16
|
+
})(DebounceStyle || (exports.DebounceStyle = DebounceStyle = {}));
|
|
17
|
+
function createDebounce(debounceStyle, debounceDuration) {
|
|
18
|
+
let nextCallTimestamp = 0;
|
|
19
|
+
let latestCallback = undefined;
|
|
20
|
+
return (callback) => {
|
|
21
|
+
latestCallback = callback;
|
|
22
|
+
const now = Date.now();
|
|
23
|
+
if (nextCallTimestamp > now) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (debounceStyle === DebounceStyle.FirstThenWait) {
|
|
27
|
+
latestCallback();
|
|
28
|
+
}
|
|
29
|
+
else if (debounceStyle === DebounceStyle.AfterWait) {
|
|
30
|
+
setTimeout(() => {
|
|
31
|
+
/** Use whatever the latest latestCallback is. */
|
|
32
|
+
latestCallback?.();
|
|
33
|
+
}, debounceDuration.milliseconds);
|
|
34
|
+
}
|
|
35
|
+
nextCallTimestamp = now + debounceDuration.milliseconds;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
exports.createDebounce = createDebounce;
|
|
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.mergeDeep = void 0;
|
|
4
4
|
const run_time_assertions_1 = require("run-time-assertions");
|
|
5
5
|
const tuple_1 = require("../tuple");
|
|
6
|
-
const object_1 = require("./object");
|
|
7
6
|
/**
|
|
8
7
|
* Accepts multiple objects and merges their key-value pairs recursively. Any values set to
|
|
9
8
|
* undefined will be removed.
|
|
@@ -22,42 +21,32 @@ function mergeDeep(...inputs) {
|
|
|
22
21
|
let result = undefined;
|
|
23
22
|
const mergeProps = {};
|
|
24
23
|
inputs.forEach((individualInput) => {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
Object.entries(individualInput).forEach(([key, value,]) => {
|
|
32
|
-
if (!mergeProps[key]) {
|
|
33
|
-
mergeProps[key] = [];
|
|
34
|
-
}
|
|
35
|
-
mergeProps[key].push(value);
|
|
36
|
-
});
|
|
37
|
-
if (!result) {
|
|
38
|
-
if ((0, run_time_assertions_1.isRunTimeType)(individualInput, 'array')) {
|
|
39
|
-
result = [...individualInput];
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
result = { ...individualInput };
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
catch (error) {
|
|
47
|
-
/** Ignore errors, such as individualInput not actually being an object. */
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
Object.entries(mergeProps).forEach(([key, mergeValues,]) => {
|
|
51
|
-
const newValue = mergeDeep(...mergeValues);
|
|
52
|
-
if (newValue === undefined && key in result) {
|
|
53
|
-
delete result[key];
|
|
24
|
+
if (!(0, run_time_assertions_1.isRunTimeType)(individualInput, 'object')) {
|
|
25
|
+
/** If not an object, we can't merge. So overwrite instead. */
|
|
26
|
+
result = individualInput;
|
|
27
|
+
return;
|
|
54
28
|
}
|
|
55
|
-
else if (
|
|
56
|
-
result
|
|
29
|
+
else if (!(0, run_time_assertions_1.isRunTimeType)(result, 'object')) {
|
|
30
|
+
/** If result isn't an object then we need to make it into one. */
|
|
31
|
+
result = { ...individualInput };
|
|
57
32
|
}
|
|
33
|
+
Object.entries(individualInput).forEach(([key, value,]) => {
|
|
34
|
+
if (!mergeProps[key]) {
|
|
35
|
+
mergeProps[key] = [];
|
|
36
|
+
}
|
|
37
|
+
mergeProps[key].push(value);
|
|
38
|
+
});
|
|
58
39
|
});
|
|
59
|
-
if ((0, run_time_assertions_1.isRunTimeType)(result, '
|
|
60
|
-
|
|
40
|
+
if ((0, run_time_assertions_1.isRunTimeType)(result, 'object')) {
|
|
41
|
+
Object.entries(mergeProps).forEach(([key, mergeValues,]) => {
|
|
42
|
+
const newValue = mergeDeep(...mergeValues);
|
|
43
|
+
if (newValue === undefined && key in result) {
|
|
44
|
+
delete result[key];
|
|
45
|
+
}
|
|
46
|
+
else if (newValue !== undefined) {
|
|
47
|
+
result[key] = newValue;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
61
50
|
}
|
|
62
51
|
return result;
|
|
63
52
|
}
|
package/dist/cjs/index.js
CHANGED
|
@@ -20,6 +20,7 @@ __exportStar(require("./augments/async"), exports);
|
|
|
20
20
|
__exportStar(require("./augments/boolean"), exports);
|
|
21
21
|
__exportStar(require("./augments/common-number"), exports);
|
|
22
22
|
__exportStar(require("./augments/common-string"), exports);
|
|
23
|
+
__exportStar(require("./augments/debounce"), exports);
|
|
23
24
|
__exportStar(require("./augments/environment"), exports);
|
|
24
25
|
__exportStar(require("./augments/error"), exports);
|
|
25
26
|
__exportStar(require("./augments/function"), exports);
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export var DebounceStyle;
|
|
2
|
+
(function (DebounceStyle) {
|
|
3
|
+
/**
|
|
4
|
+
* Fires on the first call, then waits the given amount of milliseconds until a subsequent call
|
|
5
|
+
* can be made.
|
|
6
|
+
*/
|
|
7
|
+
DebounceStyle["FirstThenWait"] = "first-then-wait";
|
|
8
|
+
/**
|
|
9
|
+
* Waits the given amount of milliseconds after the first call and then fires the latest
|
|
10
|
+
* assigned callback.
|
|
11
|
+
*/
|
|
12
|
+
DebounceStyle["AfterWait"] = "after-wait";
|
|
13
|
+
})(DebounceStyle || (DebounceStyle = {}));
|
|
14
|
+
export function createDebounce(debounceStyle, debounceDuration) {
|
|
15
|
+
let nextCallTimestamp = 0;
|
|
16
|
+
let latestCallback = undefined;
|
|
17
|
+
return (callback) => {
|
|
18
|
+
latestCallback = callback;
|
|
19
|
+
const now = Date.now();
|
|
20
|
+
if (nextCallTimestamp > now) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (debounceStyle === DebounceStyle.FirstThenWait) {
|
|
24
|
+
latestCallback();
|
|
25
|
+
}
|
|
26
|
+
else if (debounceStyle === DebounceStyle.AfterWait) {
|
|
27
|
+
setTimeout(() => {
|
|
28
|
+
/** Use whatever the latest latestCallback is. */
|
|
29
|
+
latestCallback?.();
|
|
30
|
+
}, debounceDuration.milliseconds);
|
|
31
|
+
}
|
|
32
|
+
nextCallTimestamp = now + debounceDuration.milliseconds;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { isRunTimeType } from 'run-time-assertions';
|
|
2
2
|
import { isLengthAtLeast } from '../tuple';
|
|
3
|
-
import { isObject } from './object';
|
|
4
3
|
/**
|
|
5
4
|
* Accepts multiple objects and merges their key-value pairs recursively. Any values set to
|
|
6
5
|
* undefined will be removed.
|
|
@@ -19,42 +18,32 @@ export function mergeDeep(...inputs) {
|
|
|
19
18
|
let result = undefined;
|
|
20
19
|
const mergeProps = {};
|
|
21
20
|
inputs.forEach((individualInput) => {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
Object.entries(individualInput).forEach(([key, value,]) => {
|
|
29
|
-
if (!mergeProps[key]) {
|
|
30
|
-
mergeProps[key] = [];
|
|
31
|
-
}
|
|
32
|
-
mergeProps[key].push(value);
|
|
33
|
-
});
|
|
34
|
-
if (!result) {
|
|
35
|
-
if (isRunTimeType(individualInput, 'array')) {
|
|
36
|
-
result = [...individualInput];
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
result = { ...individualInput };
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
catch (error) {
|
|
44
|
-
/** Ignore errors, such as individualInput not actually being an object. */
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
Object.entries(mergeProps).forEach(([key, mergeValues,]) => {
|
|
48
|
-
const newValue = mergeDeep(...mergeValues);
|
|
49
|
-
if (newValue === undefined && key in result) {
|
|
50
|
-
delete result[key];
|
|
21
|
+
if (!isRunTimeType(individualInput, 'object')) {
|
|
22
|
+
/** If not an object, we can't merge. So overwrite instead. */
|
|
23
|
+
result = individualInput;
|
|
24
|
+
return;
|
|
51
25
|
}
|
|
52
|
-
else if (
|
|
53
|
-
result
|
|
26
|
+
else if (!isRunTimeType(result, 'object')) {
|
|
27
|
+
/** If result isn't an object then we need to make it into one. */
|
|
28
|
+
result = { ...individualInput };
|
|
54
29
|
}
|
|
30
|
+
Object.entries(individualInput).forEach(([key, value,]) => {
|
|
31
|
+
if (!mergeProps[key]) {
|
|
32
|
+
mergeProps[key] = [];
|
|
33
|
+
}
|
|
34
|
+
mergeProps[key].push(value);
|
|
35
|
+
});
|
|
55
36
|
});
|
|
56
|
-
if (isRunTimeType(result, '
|
|
57
|
-
|
|
37
|
+
if (isRunTimeType(result, 'object')) {
|
|
38
|
+
Object.entries(mergeProps).forEach(([key, mergeValues,]) => {
|
|
39
|
+
const newValue = mergeDeep(...mergeValues);
|
|
40
|
+
if (newValue === undefined && key in result) {
|
|
41
|
+
delete result[key];
|
|
42
|
+
}
|
|
43
|
+
else if (newValue !== undefined) {
|
|
44
|
+
result[key] = newValue;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
58
47
|
}
|
|
59
48
|
return result;
|
|
60
49
|
}
|
package/dist/esm/index.js
CHANGED
|
@@ -4,6 +4,7 @@ export * from './augments/async';
|
|
|
4
4
|
export * from './augments/boolean';
|
|
5
5
|
export * from './augments/common-number';
|
|
6
6
|
export * from './augments/common-string';
|
|
7
|
+
export * from './augments/debounce';
|
|
7
8
|
export * from './augments/environment';
|
|
8
9
|
export * from './augments/error';
|
|
9
10
|
export * from './augments/function';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare enum DebounceStyle {
|
|
2
|
+
/**
|
|
3
|
+
* Fires on the first call, then waits the given amount of milliseconds until a subsequent call
|
|
4
|
+
* can be made.
|
|
5
|
+
*/
|
|
6
|
+
FirstThenWait = "first-then-wait",
|
|
7
|
+
/**
|
|
8
|
+
* Waits the given amount of milliseconds after the first call and then fires the latest
|
|
9
|
+
* assigned callback.
|
|
10
|
+
*/
|
|
11
|
+
AfterWait = "after-wait"
|
|
12
|
+
}
|
|
13
|
+
export declare function createDebounce(debounceStyle: DebounceStyle, debounceDuration: {
|
|
14
|
+
milliseconds: number;
|
|
15
|
+
}): (callback: () => void) => void;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export * from './augments/async';
|
|
|
4
4
|
export * from './augments/boolean';
|
|
5
5
|
export * from './augments/common-number';
|
|
6
6
|
export * from './augments/common-string';
|
|
7
|
+
export * from './augments/debounce';
|
|
7
8
|
export * from './augments/environment';
|
|
8
9
|
export * from './augments/error';
|
|
9
10
|
export * from './augments/function';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@augment-vir/common",
|
|
3
|
-
"version": "22.
|
|
3
|
+
"version": "22.1.0",
|
|
4
4
|
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common",
|
|
5
5
|
"bugs": {
|
|
6
6
|
"url": "https://github.com/electrovir/augment-vir/issues"
|
|
@@ -25,11 +25,11 @@
|
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"browser-or-node": "^2.1.1",
|
|
28
|
-
"run-time-assertions": "^0.2.
|
|
29
|
-
"type-fest": "^4.8.
|
|
28
|
+
"run-time-assertions": "^0.2.1",
|
|
29
|
+
"type-fest": "^4.8.2"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"typescript": "
|
|
32
|
+
"typescript": "5.2.2"
|
|
33
33
|
},
|
|
34
34
|
"publishConfig": {
|
|
35
35
|
"access": "public"
|