@straylightagency/utils 1.1.5 → 1.1.7
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/package.json +1 -1
- package/src/array/index.js +2 -1
- package/src/array/replace.js +17 -0
- package/src/index.js +2 -0
- package/src/misc/index.js +1 -0
- package/src/misc/match.js +33 -0
- package/src/object/get.js +1 -1
package/package.json
CHANGED
package/src/array/index.js
CHANGED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Replace a value from the given array by another one using a callback to find the index
|
|
3
|
+
*
|
|
4
|
+
* @param {[]} array
|
|
5
|
+
* @param {*} callback
|
|
6
|
+
* @param {*} value
|
|
7
|
+
* @returns {[]}
|
|
8
|
+
*/
|
|
9
|
+
export default function replace(array, callback, value) {
|
|
10
|
+
const index = array.findIndex( callback );
|
|
11
|
+
|
|
12
|
+
if ( index !== -1 ) {
|
|
13
|
+
array[ index ] = value;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return array;
|
|
17
|
+
}
|
package/src/index.js
CHANGED
|
@@ -13,6 +13,7 @@ export { default as get } from "./object/get.js";
|
|
|
13
13
|
export { default as set } from "./object/set.js";
|
|
14
14
|
export { default as has } from "./object/has.js";
|
|
15
15
|
export { default as clipboard } from "./misc/clipboard.js";
|
|
16
|
+
export { default as match } from "./misc/match.js";
|
|
16
17
|
export { default as debounce } from "./misc/debounce.js";
|
|
17
18
|
export { default as nextTick } from "./misc/nextTick.js";
|
|
18
19
|
export { default as seconds } from "./misc/seconds.js";
|
|
@@ -37,6 +38,7 @@ export { default as dateReviver } from "./date/reviver.js";
|
|
|
37
38
|
export { default as initial } from "./array/initial.js";
|
|
38
39
|
export { default as last } from "./array/last.js";
|
|
39
40
|
export { default as remove } from "./array/remove.js";
|
|
41
|
+
export { default as replace } from "./array/replace.js";
|
|
40
42
|
|
|
41
43
|
import {isClass, isObject, isPlainObject} from "./misc/is.js";
|
|
42
44
|
import {decodeBase64, encodeBase64, isBase64} from "./misc/base64.js";
|
package/src/misc/index.js
CHANGED
|
@@ -6,6 +6,7 @@ export { default as attempt } from "./attempt.js";
|
|
|
6
6
|
export { default as retry } from "./retry.js";
|
|
7
7
|
export { default as reviver } from "./reviver.js";
|
|
8
8
|
export { default as replacer } from "./replacer.js";
|
|
9
|
+
export { default as match } from "./match.js";
|
|
9
10
|
export { default as seconds } from "./seconds.js";
|
|
10
11
|
export { default as milliseconds } from "./milliseconds.js";
|
|
11
12
|
export { default as restoreBooleanAfter } from "./restoreBooleanAfter.js";
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Remove a value from the given array, returning a copy.
|
|
3
|
+
*
|
|
4
|
+
* @param {*} value
|
|
5
|
+
* @returns {Object}
|
|
6
|
+
*/
|
|
7
|
+
export default function match(value) {
|
|
8
|
+
const matchable = function () {
|
|
9
|
+
this.value = value;
|
|
10
|
+
this.heap = new Map;
|
|
11
|
+
this.defaultCallback = () => this.value;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
matchable.prototype.is = function (key, callback) {
|
|
15
|
+
this.heap.set( key, callback );
|
|
16
|
+
|
|
17
|
+
return this;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
matchable.prototype.default = function (callback) {
|
|
21
|
+
this.defaultCallback = callback;
|
|
22
|
+
|
|
23
|
+
return this;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
matchable.prototype.return = function () {
|
|
27
|
+
const callback = this.heap.has( this.value ) ? this.heap.get( this.value ) : this.defaultCallback;
|
|
28
|
+
|
|
29
|
+
return callback();
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
return new matchable( value );
|
|
33
|
+
}
|
package/src/object/get.js
CHANGED