@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,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #### Remove
|
|
3
|
+
*
|
|
4
|
+
* Removes an item from the array
|
|
5
|
+
*
|
|
6
|
+
* Removes all item references if multi is set to `true`
|
|
7
|
+
*
|
|
8
|
+
* * * *
|
|
9
|
+
* Example:
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { remove } from "@telperion/js-utils/array";
|
|
12
|
+
*
|
|
13
|
+
* const array = ["a", "b", "c", "a", "b", "c"];
|
|
14
|
+
*
|
|
15
|
+
* remove(array, "b"); // ["a", "c", "a", "b", "c"]
|
|
16
|
+
* remove(array, "b", true); // ["a", "c", "a", "c"]
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* Prototype Example:
|
|
20
|
+
* ```typescript
|
|
21
|
+
* import "@telperion/js-utils/array/proto/remove";
|
|
22
|
+
*
|
|
23
|
+
* const array = ["a", "b", "c", "a", "b", "c"];
|
|
24
|
+
*
|
|
25
|
+
* array.remove("b"); // ["a", "c", "a", "b", "c"]
|
|
26
|
+
* array.remove("b", true); // ["a", "c", "a", "c"]
|
|
27
|
+
* ```
|
|
28
|
+
* * * *
|
|
29
|
+
* @param array: Array to remove the item
|
|
30
|
+
* @param itemToRemove Item to remove
|
|
31
|
+
* @return New array
|
|
32
|
+
*/
|
|
33
|
+
export declare function remove<T = unknown>(array: T[], item: T, multi?: boolean): T[];
|
package/array/remove.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #### Remove
|
|
3
|
+
*
|
|
4
|
+
* Removes an item from the array
|
|
5
|
+
*
|
|
6
|
+
* Removes all item references if multi is set to `true`
|
|
7
|
+
*
|
|
8
|
+
* * * *
|
|
9
|
+
* Example:
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { remove } from "@telperion/js-utils/array";
|
|
12
|
+
*
|
|
13
|
+
* const array = ["a", "b", "c", "a", "b", "c"];
|
|
14
|
+
*
|
|
15
|
+
* remove(array, "b"); // ["a", "c", "a", "b", "c"]
|
|
16
|
+
* remove(array, "b", true); // ["a", "c", "a", "c"]
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* Prototype Example:
|
|
20
|
+
* ```typescript
|
|
21
|
+
* import "@telperion/js-utils/array/proto/remove";
|
|
22
|
+
*
|
|
23
|
+
* const array = ["a", "b", "c", "a", "b", "c"];
|
|
24
|
+
*
|
|
25
|
+
* array.remove("b"); // ["a", "c", "a", "b", "c"]
|
|
26
|
+
* array.remove("b", true); // ["a", "c", "a", "c"]
|
|
27
|
+
* ```
|
|
28
|
+
* * * *
|
|
29
|
+
* @param array: Array to remove the item
|
|
30
|
+
* @param itemToRemove Item to remove
|
|
31
|
+
* @return New array
|
|
32
|
+
*/
|
|
33
|
+
export function remove(array, item, multi = false) {
|
|
34
|
+
let index = array.indexOf(item);
|
|
35
|
+
if (index < 0) {
|
|
36
|
+
return [...array];
|
|
37
|
+
}
|
|
38
|
+
do {
|
|
39
|
+
array = [...array.slice(0, index), ...array.slice(index + 1)];
|
|
40
|
+
index = array.indexOf(item);
|
|
41
|
+
} while (multi && index > -1);
|
|
42
|
+
return array;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
//# sourceMappingURL=remove.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/js-utils/src/array/remove.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,UAAU,MAAM,CAAc,KAAU,EAAE,IAAO,EAAE,KAAK,GAAG,KAAK;IACpE,IAAI,KAAK,GAAW,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAExC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QACd,OAAO,CAAC,GAAG,KAAK,CAAC,CAAC;IACpB,CAAC;IAED,GAAG,CAAC;QACF,KAAK,GAAG,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,GAAG,KAAK,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC;QAC9D,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAC9B,CAAC,QAAQ,KAAK,IAAI,KAAK,GAAG,CAAC,CAAC,EAAE;IAE9B,OAAO,KAAK,CAAC;AACf,CAAC","file":"remove.js","sourcesContent":["/**\n * #### Remove\n *\n * Removes an item from the array\n *\n * Removes all item references if multi is set to `true`\n *\n * * * *\n * Example:\n * ```typescript\n * import { remove } from \"@telperion/js-utils/array\";\n *\n * const array = [\"a\", \"b\", \"c\", \"a\", \"b\", \"c\"];\n *\n * remove(array, \"b\"); // [\"a\", \"c\", \"a\", \"b\", \"c\"]\n * remove(array, \"b\", true); // [\"a\", \"c\", \"a\", \"c\"]\n * ```\n *\n * Prototype Example:\n * ```typescript\n * import \"@telperion/js-utils/array/proto/remove\";\n *\n * const array = [\"a\", \"b\", \"c\", \"a\", \"b\", \"c\"];\n *\n * array.remove(\"b\"); // [\"a\", \"c\", \"a\", \"b\", \"c\"]\n * array.remove(\"b\", true); // [\"a\", \"c\", \"a\", \"c\"]\n * ```\n * * * *\n * @param array: Array to remove the item\n * @param itemToRemove Item to remove\n * @return New array\n */\nexport function remove<T = unknown>(array: T[], item: T, multi = false): T[] {\n let index: number = array.indexOf(item);\n\n if (index < 0) {\n return [...array];\n }\n\n do {\n array = [...array.slice(0, index), ...array.slice(index + 1)];\n index = array.indexOf(item);\n } while (multi && index > -1);\n\n return array;\n}\n"]}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.replace = replace;
|
|
4
|
+
/**
|
|
5
|
+
* Default Options for Replace Items Mode
|
|
6
|
+
*/
|
|
7
|
+
const REPLACE_ITEMS_DEFAULT_OPTIONS = {
|
|
8
|
+
deleteCount: 1,
|
|
9
|
+
itemsToReplace: [],
|
|
10
|
+
startingIndex: 0
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Default Options for Replace By Map Mode
|
|
14
|
+
*/
|
|
15
|
+
const REPLACE_BY_MAP_DEFAULT_OPTIONS = {
|
|
16
|
+
itemsToReplace: new Map(),
|
|
17
|
+
multi: false
|
|
18
|
+
};
|
|
19
|
+
function replace(array, options, itemToReplace) {
|
|
20
|
+
if (arguments.length > 2) {
|
|
21
|
+
options = {
|
|
22
|
+
startingIndex: array.indexOf(options),
|
|
23
|
+
itemsToReplace: [itemToReplace]
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
if (options.itemsToReplace instanceof Array) {
|
|
27
|
+
return _replaceItems(array, options);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
return _replaceByMap(array, options);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Replace items by 'replace items' mode
|
|
35
|
+
*
|
|
36
|
+
* @param array Array to replace items
|
|
37
|
+
* @param options Replace options
|
|
38
|
+
*/
|
|
39
|
+
function _replaceItems(array, options) {
|
|
40
|
+
const { startingIndex, deleteCount, itemsToReplace } = Object.assign({}, REPLACE_ITEMS_DEFAULT_OPTIONS, options);
|
|
41
|
+
const newArray = [...array];
|
|
42
|
+
// If item not found or index number is negative; do nothing
|
|
43
|
+
if (startingIndex < 0) {
|
|
44
|
+
return newArray;
|
|
45
|
+
}
|
|
46
|
+
newArray.splice(startingIndex, deleteCount, ...itemsToReplace);
|
|
47
|
+
return newArray;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Replace items by 'replace by map' mode
|
|
51
|
+
*
|
|
52
|
+
* @param array Array to replace items
|
|
53
|
+
* @param options Replace options
|
|
54
|
+
*/
|
|
55
|
+
function _replaceByMap(array, options) {
|
|
56
|
+
const { itemsToReplace, multi } = Object.assign({}, REPLACE_BY_MAP_DEFAULT_OPTIONS, options);
|
|
57
|
+
const iterationMethod = multi ? Array.prototype.forEach : Array.prototype.some;
|
|
58
|
+
const newArray = [...array];
|
|
59
|
+
itemsToReplace.forEach((value, key) => {
|
|
60
|
+
iterationMethod.call(newArray, (item, index) => {
|
|
61
|
+
const matches = item === key;
|
|
62
|
+
if (matches) {
|
|
63
|
+
newArray.splice(index, 1, value);
|
|
64
|
+
}
|
|
65
|
+
return matches;
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
return newArray;
|
|
69
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Replace Items Options Interface
|
|
3
|
+
*
|
|
4
|
+
* @template T typeof the items of the array
|
|
5
|
+
*/
|
|
6
|
+
export interface ReplaceItemsOptions<T> {
|
|
7
|
+
/**
|
|
8
|
+
* Starting index of the array for replacement operation
|
|
9
|
+
*
|
|
10
|
+
* @default 0
|
|
11
|
+
*/
|
|
12
|
+
startingIndex?: number;
|
|
13
|
+
/**
|
|
14
|
+
* Determines how many items will be deleted from the array
|
|
15
|
+
*
|
|
16
|
+
* @default 1
|
|
17
|
+
*/
|
|
18
|
+
deleteCount?: number;
|
|
19
|
+
/**
|
|
20
|
+
* Items to replace
|
|
21
|
+
*
|
|
22
|
+
* @default []
|
|
23
|
+
*/
|
|
24
|
+
itemsToReplace?: T[];
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Replace by Item Matcher Map Options Interface
|
|
28
|
+
*
|
|
29
|
+
* @template T typeof the items of the array
|
|
30
|
+
*/
|
|
31
|
+
export interface ReplaceByMapOptions<T> {
|
|
32
|
+
/**
|
|
33
|
+
* Items' Matcher Map to replace
|
|
34
|
+
*/
|
|
35
|
+
itemsToReplace: Map<T, T>;
|
|
36
|
+
/**
|
|
37
|
+
* Set as `true` to replace all items in the array if matches with the key of the matcher map while iterating
|
|
38
|
+
*
|
|
39
|
+
* @default false
|
|
40
|
+
*/
|
|
41
|
+
multi?: boolean;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* #### Replace
|
|
45
|
+
*
|
|
46
|
+
* Replaces an item with passed one of an array
|
|
47
|
+
*
|
|
48
|
+
* * * *
|
|
49
|
+
* Example:
|
|
50
|
+
* ```typescript
|
|
51
|
+
* import { replace } from "@telperion/js-utils/array";
|
|
52
|
+
*
|
|
53
|
+
* const array = ["a", "b", "c", "a", "b", "c"];
|
|
54
|
+
*
|
|
55
|
+
* replace(array, "b", "x"); // ["a", "x", "c", "a", "b", "c"]
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* Array Prototype Example:
|
|
59
|
+
* ```typescript
|
|
60
|
+
* import "@telperion/js-utils/array/proto/replace";
|
|
61
|
+
*
|
|
62
|
+
* const array = ["a", "b", "c", "a", "b", "c"];
|
|
63
|
+
*
|
|
64
|
+
* array.replace("b", "x"); // ["a", "x", "c", "a", "b", "c"]
|
|
65
|
+
* ```
|
|
66
|
+
* * * *
|
|
67
|
+
* @param array Array to replace its item
|
|
68
|
+
* @param itemToRemove Item to remove
|
|
69
|
+
* @param itemToReplace Item to replace with
|
|
70
|
+
* @template T Typeof array items
|
|
71
|
+
* @return New replaced array
|
|
72
|
+
*/
|
|
73
|
+
export declare function replace<T>(array: T[], itemToRemove: T, itemToReplace: T): T[];
|
|
74
|
+
/**
|
|
75
|
+
* #### Replace
|
|
76
|
+
*
|
|
77
|
+
* Deletes items and replaces new ones from passed starting index of an array
|
|
78
|
+
*
|
|
79
|
+
* * * *
|
|
80
|
+
* Example:
|
|
81
|
+
* ```typescript
|
|
82
|
+
* import { replace } from "@telperion/js-utils";
|
|
83
|
+
*
|
|
84
|
+
* const array = ["a", "b", "c", "a", "b", "c"];
|
|
85
|
+
*
|
|
86
|
+
* replace(array, {startingIndex: 3, deleteCount: 1, itemsToReplace: ['x', 'y']}); // ["a", "b", "c", "x", "y", "b", "c"];
|
|
87
|
+
* ```
|
|
88
|
+
* Array Prototype Example:
|
|
89
|
+
* ```typescript
|
|
90
|
+
* import "@telperion/js-utils/dist/as-proto/array-replace";
|
|
91
|
+
*
|
|
92
|
+
* const array = ["a", "b", "c", "a", "b", "c"];
|
|
93
|
+
*
|
|
94
|
+
* array.replace({startingIndex: 3, deleteCount: 1, itemsToReplace: ['x', 'y']}); // ["a", "b", "c", "x", "y", "b", "c"];
|
|
95
|
+
* ```
|
|
96
|
+
* * * *
|
|
97
|
+
* @param array Array to replace its item
|
|
98
|
+
* @param replaceOptions Replace options
|
|
99
|
+
* @template T Typeof array items
|
|
100
|
+
* @return New replaced array
|
|
101
|
+
*/
|
|
102
|
+
export declare function replace<T>(array: T[], replaceOptions: ReplaceItemsOptions<T>): T[];
|
|
103
|
+
/**
|
|
104
|
+
* #### Replace
|
|
105
|
+
*
|
|
106
|
+
* Deletes items and replaces new ones by passed matcher map
|
|
107
|
+
*
|
|
108
|
+
* * * *
|
|
109
|
+
* Example:
|
|
110
|
+
* ```typescript
|
|
111
|
+
* import { replace } from "@telperion/js-utils";
|
|
112
|
+
*
|
|
113
|
+
* const array = ["a", "b", "c", "a", "b", "c"];
|
|
114
|
+
* const map = new Map();
|
|
115
|
+
* map.set("a", "x")
|
|
116
|
+
* map.set("b", "y");
|
|
117
|
+
*
|
|
118
|
+
* replace(array, {itemsToReplace: map}); // ["x", "y", "c", "a", "b", "c"];
|
|
119
|
+
* replace(array, {itemsToReplace: map, multi: true}); // ["x", "y", "c", "x", "y", "c"];
|
|
120
|
+
* ```
|
|
121
|
+
* Array Prototype Example:
|
|
122
|
+
* ```typescript
|
|
123
|
+
* import "@telperion/js-utils/dist/as-proto/array-replace";
|
|
124
|
+
*
|
|
125
|
+
* const array = ["a", "b", "c", "a", "b", "c"];
|
|
126
|
+
* const map = new Map();
|
|
127
|
+
* map.set("a", "x")
|
|
128
|
+
* map.set("b", "y");
|
|
129
|
+
*
|
|
130
|
+
* array.replace({itemsToReplace: map}); // ["x", "y", "c", "a", "b", "c"];
|
|
131
|
+
* array.replace({itemsToReplace: map, multi: true}); // ["x", "y", "c", "x", "y", "c"];
|
|
132
|
+
* ```
|
|
133
|
+
* * * *
|
|
134
|
+
* @param array Array to replace its item
|
|
135
|
+
* @param replaceOptions Replace options
|
|
136
|
+
* @template T Typeof array items
|
|
137
|
+
* @return New replaced array
|
|
138
|
+
*/
|
|
139
|
+
export declare function replace<T>(array: T[], replaceOptions: ReplaceByMapOptions<T>): T[];
|
package/array/replace.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default Options for Replace Items Mode
|
|
3
|
+
*/
|
|
4
|
+
const REPLACE_ITEMS_DEFAULT_OPTIONS = {
|
|
5
|
+
deleteCount: 1,
|
|
6
|
+
itemsToReplace: [],
|
|
7
|
+
startingIndex: 0
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Default Options for Replace By Map Mode
|
|
11
|
+
*/
|
|
12
|
+
const REPLACE_BY_MAP_DEFAULT_OPTIONS = {
|
|
13
|
+
itemsToReplace: new Map(),
|
|
14
|
+
multi: false
|
|
15
|
+
};
|
|
16
|
+
export function replace(array, options, itemToReplace) {
|
|
17
|
+
if (arguments.length > 2) {
|
|
18
|
+
options = {
|
|
19
|
+
startingIndex: array.indexOf(options),
|
|
20
|
+
itemsToReplace: [itemToReplace]
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
if (options.itemsToReplace instanceof Array) {
|
|
24
|
+
return _replaceItems(array, options);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
return _replaceByMap(array, options);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Replace items by 'replace items' mode
|
|
32
|
+
*
|
|
33
|
+
* @param array Array to replace items
|
|
34
|
+
* @param options Replace options
|
|
35
|
+
*/
|
|
36
|
+
function _replaceItems(array, options) {
|
|
37
|
+
const { startingIndex, deleteCount, itemsToReplace } = Object.assign({}, REPLACE_ITEMS_DEFAULT_OPTIONS, options);
|
|
38
|
+
const newArray = [...array];
|
|
39
|
+
// If item not found or index number is negative; do nothing
|
|
40
|
+
if (startingIndex < 0) {
|
|
41
|
+
return newArray;
|
|
42
|
+
}
|
|
43
|
+
newArray.splice(startingIndex, deleteCount, ...itemsToReplace);
|
|
44
|
+
return newArray;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Replace items by 'replace by map' mode
|
|
48
|
+
*
|
|
49
|
+
* @param array Array to replace items
|
|
50
|
+
* @param options Replace options
|
|
51
|
+
*/
|
|
52
|
+
function _replaceByMap(array, options) {
|
|
53
|
+
const { itemsToReplace, multi } = Object.assign({}, REPLACE_BY_MAP_DEFAULT_OPTIONS, options);
|
|
54
|
+
const iterationMethod = multi ? Array.prototype.forEach : Array.prototype.some;
|
|
55
|
+
const newArray = [...array];
|
|
56
|
+
itemsToReplace.forEach((value, key) => {
|
|
57
|
+
iterationMethod.call(newArray, (item, index) => {
|
|
58
|
+
const matches = item === key;
|
|
59
|
+
if (matches) {
|
|
60
|
+
newArray.splice(index, 1, value);
|
|
61
|
+
}
|
|
62
|
+
return matches;
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
return newArray;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
//# sourceMappingURL=replace.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/js-utils/src/array/replace.ts"],"names":[],"mappings":"AA+CA;;GAEG;AACH,MAAM,6BAA6B,GAAuC;IACxE,WAAW,EAAE,CAAC;IACd,cAAc,EAAE,EAAE;IAClB,aAAa,EAAE,CAAC;CACjB,CAAC;AACF;;GAEG;AACH,MAAM,8BAA8B,GAAuC;IACzE,cAAc,EAAE,IAAI,GAAG,EAAE;IACzB,KAAK,EAAE,KAAK;CACb,CAAC;AAmGF,MAAM,UAAU,OAAO,CACrB,KAAU,EACV,OAA4D,EAC5D,aAAiB;IAEjB,IAAI,SAAS,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACzB,OAAO,GAA2B;YAChC,aAAa,EAAE,KAAK,CAAC,OAAO,CAAI,OAAO,CAAC;YACxC,cAAc,EAAE,CAAC,aAAa,CAAC;SAChC,CAAC;IACJ,CAAC;IAED,IAAsD,OAAQ,CAAC,cAAc,YAAY,KAAK,EAAE,CAAC;QAC/F,OAAO,aAAa,CAAC,KAAK,EAA0B,OAAO,CAAC,CAAC;IAC/D,CAAC;SAAM,CAAC;QACN,OAAO,aAAa,CAAC,KAAK,EAA0B,OAAO,CAAC,CAAC;IAC/D,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,SAAS,aAAa,CAAI,KAAU,EAAE,OAA+B;IACnE,MAAM,EAAE,aAAa,EAAE,WAAW,EAAE,cAAc,EAAE,GAAqC,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,6BAA6B,EAAE,OAAO,CAAC,CAAC;IACnJ,MAAM,QAAQ,GAAG,CAAC,GAAG,KAAK,CAAC,CAAC;IAE5B,4DAA4D;IAC5D,IAAI,aAAa,GAAG,CAAC,EAAE,CAAC;QACtB,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED,QAAQ,CAAC,MAAM,CAAC,aAAa,EAAE,WAAW,EAAE,GAAG,cAAc,CAAC,CAAC;IAE/D,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED;;;;;GAKG;AACH,SAAS,aAAa,CAAI,KAAU,EAAE,OAA+B;IACnE,MAAM,EAAE,cAAc,EAAE,KAAK,EAAE,GAAqC,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,8BAA8B,EAAE,OAAO,CAAC,CAAC;IAC/H,MAAM,eAAe,GAAG,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,SAAS,CAAC,IAAI,CAAC;IAC/E,MAAM,QAAQ,GAAG,CAAC,GAAG,KAAK,CAAC,CAAC;IAE5B,cAAc,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;QACpC,eAAe,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE;YAC7C,MAAM,OAAO,GAAG,IAAI,KAAK,GAAG,CAAC;YAE7B,IAAI,OAAO,EAAE,CAAC;gBACZ,QAAQ,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,EAAE,KAAK,CAAC,CAAC;YACnC,CAAC;YAED,OAAO,OAAO,CAAC;QACjB,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,OAAO,QAAQ,CAAC;AAClB,CAAC","file":"replace.js","sourcesContent":["/**\n * Replace Items Options Interface\n *\n * @template T typeof the items of the array\n */\nexport interface ReplaceItemsOptions<T> {\n /**\n * Starting index of the array for replacement operation\n *\n * @default 0\n */\n startingIndex?: number;\n\n /**\n * Determines how many items will be deleted from the array\n *\n * @default 1\n */\n deleteCount?: number;\n\n /**\n * Items to replace\n *\n * @default []\n */\n itemsToReplace?: T[];\n}\n\n/**\n * Replace by Item Matcher Map Options Interface\n *\n * @template T typeof the items of the array\n */\nexport interface ReplaceByMapOptions<T> {\n /**\n * Items' Matcher Map to replace\n */\n itemsToReplace: Map<T, T>;\n\n /**\n * Set as `true` to replace all items in the array if matches with the key of the matcher map while iterating\n *\n * @default false\n */\n multi?: boolean;\n}\n\n/**\n * Default Options for Replace Items Mode\n */\nconst REPLACE_ITEMS_DEFAULT_OPTIONS: Required<ReplaceItemsOptions<any>> = {\n deleteCount: 1,\n itemsToReplace: [],\n startingIndex: 0\n};\n/**\n * Default Options for Replace By Map Mode\n */\nconst REPLACE_BY_MAP_DEFAULT_OPTIONS: Required<ReplaceByMapOptions<any>> = {\n itemsToReplace: new Map(),\n multi: false\n};\n\n/**\n * #### Replace\n *\n * Replaces an item with passed one of an array\n *\n * * * *\n * Example:\n * ```typescript\n * import { replace } from \"@telperion/js-utils/array\";\n *\n * const array = [\"a\", \"b\", \"c\", \"a\", \"b\", \"c\"];\n *\n * replace(array, \"b\", \"x\"); // [\"a\", \"x\", \"c\", \"a\", \"b\", \"c\"]\n * ```\n *\n * Array Prototype Example:\n * ```typescript\n * import \"@telperion/js-utils/array/proto/replace\";\n *\n * const array = [\"a\", \"b\", \"c\", \"a\", \"b\", \"c\"];\n *\n * array.replace(\"b\", \"x\"); // [\"a\", \"x\", \"c\", \"a\", \"b\", \"c\"]\n * ```\n * * * *\n * @param array Array to replace its item\n * @param itemToRemove Item to remove\n * @param itemToReplace Item to replace with\n * @template T Typeof array items\n * @return New replaced array\n */\nexport function replace<T>(array: T[], itemToRemove: T, itemToReplace: T): T[];\n/**\n * #### Replace\n *\n * Deletes items and replaces new ones from passed starting index of an array\n *\n * * * *\n * Example:\n * ```typescript\n * import { replace } from \"@telperion/js-utils\";\n *\n * const array = [\"a\", \"b\", \"c\", \"a\", \"b\", \"c\"];\n *\n * replace(array, {startingIndex: 3, deleteCount: 1, itemsToReplace: ['x', 'y']}); // [\"a\", \"b\", \"c\", \"x\", \"y\", \"b\", \"c\"];\n * ```\n * Array Prototype Example:\n * ```typescript\n * import \"@telperion/js-utils/dist/as-proto/array-replace\";\n *\n * const array = [\"a\", \"b\", \"c\", \"a\", \"b\", \"c\"];\n *\n * array.replace({startingIndex: 3, deleteCount: 1, itemsToReplace: ['x', 'y']}); // [\"a\", \"b\", \"c\", \"x\", \"y\", \"b\", \"c\"];\n * ```\n * * * *\n * @param array Array to replace its item\n * @param replaceOptions Replace options\n * @template T Typeof array items\n * @return New replaced array\n */\nexport function replace<T>(array: T[], replaceOptions: ReplaceItemsOptions<T>): T[];\n/**\n * #### Replace\n *\n * Deletes items and replaces new ones by passed matcher map\n *\n * * * *\n * Example:\n * ```typescript\n * import { replace } from \"@telperion/js-utils\";\n *\n * const array = [\"a\", \"b\", \"c\", \"a\", \"b\", \"c\"];\n * const map = new Map();\n * map.set(\"a\", \"x\")\n * map.set(\"b\", \"y\");\n *\n * replace(array, {itemsToReplace: map}); // [\"x\", \"y\", \"c\", \"a\", \"b\", \"c\"];\n * replace(array, {itemsToReplace: map, multi: true}); // [\"x\", \"y\", \"c\", \"x\", \"y\", \"c\"];\n * ```\n * Array Prototype Example:\n * ```typescript\n * import \"@telperion/js-utils/dist/as-proto/array-replace\";\n *\n * const array = [\"a\", \"b\", \"c\", \"a\", \"b\", \"c\"];\n * const map = new Map();\n * map.set(\"a\", \"x\")\n * map.set(\"b\", \"y\");\n *\n * array.replace({itemsToReplace: map}); // [\"x\", \"y\", \"c\", \"a\", \"b\", \"c\"];\n * array.replace({itemsToReplace: map, multi: true}); // [\"x\", \"y\", \"c\", \"x\", \"y\", \"c\"];\n * ```\n * * * *\n * @param array Array to replace its item\n * @param replaceOptions Replace options\n * @template T Typeof array items\n * @return New replaced array\n */\nexport function replace<T>(array: T[], replaceOptions: ReplaceByMapOptions<T>): T[];\nexport function replace<T>(\n array: T[],\n options: T | ReplaceItemsOptions<T> | ReplaceByMapOptions<T>,\n itemToReplace?: T\n): T[] {\n if (arguments.length > 2) {\n options = <ReplaceItemsOptions<T>>{\n startingIndex: array.indexOf(<T>options),\n itemsToReplace: [itemToReplace]\n };\n }\n\n if ((<ReplaceByMapOptions<T> | ReplaceItemsOptions<T>>options).itemsToReplace instanceof Array) {\n return _replaceItems(array, <ReplaceItemsOptions<T>>options);\n } else {\n return _replaceByMap(array, <ReplaceByMapOptions<T>>options);\n }\n}\n\n/**\n * Replace items by 'replace items' mode\n *\n * @param array Array to replace items\n * @param options Replace options\n */\nfunction _replaceItems<T>(array: T[], options: ReplaceItemsOptions<T>): T[] {\n const { startingIndex, deleteCount, itemsToReplace }: Required<ReplaceItemsOptions<T>> = Object.assign({}, REPLACE_ITEMS_DEFAULT_OPTIONS, options);\n const newArray = [...array];\n\n // If item not found or index number is negative; do nothing\n if (startingIndex < 0) {\n return newArray;\n }\n\n newArray.splice(startingIndex, deleteCount, ...itemsToReplace);\n\n return newArray;\n}\n\n/**\n * Replace items by 'replace by map' mode\n *\n * @param array Array to replace items\n * @param options Replace options\n */\nfunction _replaceByMap<T>(array: T[], options: ReplaceByMapOptions<T>): T[] {\n const { itemsToReplace, multi }: Required<ReplaceByMapOptions<T>> = Object.assign({}, REPLACE_BY_MAP_DEFAULT_OPTIONS, options);\n const iterationMethod = multi ? Array.prototype.forEach : Array.prototype.some;\n const newArray = [...array];\n\n itemsToReplace.forEach((value, key) => {\n iterationMethod.call(newArray, (item, index) => {\n const matches = item === key;\n\n if (matches) {\n newArray.splice(index, 1, value);\n }\n\n return matches;\n });\n });\n\n return newArray;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/js-utils/src/array/static/arrayize.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,oCAAoC,CAAC;AAQ9D,KAAK,CAAC,QAAQ,GAAG,QAAQ,CAAC","file":"arrayize.js","sourcesContent":["import { arrayize } from \"@telperion/js-utils/array/arrayize\";\n\ndeclare global {\n export interface ArrayConstructor {\n arrayize: typeof arrayize;\n }\n}\n\nArray.arrayize = arrayize;\n"]}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.uniquifyByKey = uniquifyByKey;
|
|
4
|
+
/**
|
|
5
|
+
* #### Uniquify By Key
|
|
6
|
+
*
|
|
7
|
+
* Removes objects from the array which the value of its specifed key included before by another
|
|
8
|
+
*
|
|
9
|
+
* * * *
|
|
10
|
+
* Example:
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { uniquifyByKey } "@telperion/js-utils/array";
|
|
13
|
+
*
|
|
14
|
+
* const array = [{a: 1}, {a: 1}, {a: 2}, {a: 3}, {a: 3}, {a: 4}];
|
|
15
|
+
*
|
|
16
|
+
* uniquifyByKey(array, 'a'); // [{a: 1}, {a: 2}, {a: 3}, {a: 4}]
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* Prototype Example:
|
|
20
|
+
* ```typescript
|
|
21
|
+
* import "@telperion/js-utils/array/proto/uniquify-by-key";
|
|
22
|
+
*
|
|
23
|
+
* const array = [{a: 1}, {a: 1}, {a: 2}, {a: 3}, {a: 3}, {a: 4}];
|
|
24
|
+
*
|
|
25
|
+
* array.uniquifyByKey('a'); // [{a: 1}, {a: 2}, {a: 3}, {a: 4}]
|
|
26
|
+
* ```
|
|
27
|
+
* * * *
|
|
28
|
+
* @param array Collection of the objects
|
|
29
|
+
* @param key Key to search the value on
|
|
30
|
+
* @return The new uniquified array
|
|
31
|
+
*/
|
|
32
|
+
function uniquifyByKey(array, key) {
|
|
33
|
+
const cache = new Set();
|
|
34
|
+
return array.filter(item => {
|
|
35
|
+
if (cache.has(item[key])) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
cache.add(item[key]);
|
|
39
|
+
return true;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #### Uniquify By Key
|
|
3
|
+
*
|
|
4
|
+
* Removes objects from the array which the value of its specifed key included before by another
|
|
5
|
+
*
|
|
6
|
+
* * * *
|
|
7
|
+
* Example:
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { uniquifyByKey } "@telperion/js-utils/array";
|
|
10
|
+
*
|
|
11
|
+
* const array = [{a: 1}, {a: 1}, {a: 2}, {a: 3}, {a: 3}, {a: 4}];
|
|
12
|
+
*
|
|
13
|
+
* uniquifyByKey(array, 'a'); // [{a: 1}, {a: 2}, {a: 3}, {a: 4}]
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* Prototype Example:
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import "@telperion/js-utils/array/proto/uniquify-by-key";
|
|
19
|
+
*
|
|
20
|
+
* const array = [{a: 1}, {a: 1}, {a: 2}, {a: 3}, {a: 3}, {a: 4}];
|
|
21
|
+
*
|
|
22
|
+
* array.uniquifyByKey('a'); // [{a: 1}, {a: 2}, {a: 3}, {a: 4}]
|
|
23
|
+
* ```
|
|
24
|
+
* * * *
|
|
25
|
+
* @param array Collection of the objects
|
|
26
|
+
* @param key Key to search the value on
|
|
27
|
+
* @return The new uniquified array
|
|
28
|
+
*/
|
|
29
|
+
export declare function uniquifyByKey<T extends Object, P extends keyof T>(array: T[], key: P): T[];
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #### Uniquify By Key
|
|
3
|
+
*
|
|
4
|
+
* Removes objects from the array which the value of its specifed key included before by another
|
|
5
|
+
*
|
|
6
|
+
* * * *
|
|
7
|
+
* Example:
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { uniquifyByKey } "@telperion/js-utils/array";
|
|
10
|
+
*
|
|
11
|
+
* const array = [{a: 1}, {a: 1}, {a: 2}, {a: 3}, {a: 3}, {a: 4}];
|
|
12
|
+
*
|
|
13
|
+
* uniquifyByKey(array, 'a'); // [{a: 1}, {a: 2}, {a: 3}, {a: 4}]
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* Prototype Example:
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import "@telperion/js-utils/array/proto/uniquify-by-key";
|
|
19
|
+
*
|
|
20
|
+
* const array = [{a: 1}, {a: 1}, {a: 2}, {a: 3}, {a: 3}, {a: 4}];
|
|
21
|
+
*
|
|
22
|
+
* array.uniquifyByKey('a'); // [{a: 1}, {a: 2}, {a: 3}, {a: 4}]
|
|
23
|
+
* ```
|
|
24
|
+
* * * *
|
|
25
|
+
* @param array Collection of the objects
|
|
26
|
+
* @param key Key to search the value on
|
|
27
|
+
* @return The new uniquified array
|
|
28
|
+
*/
|
|
29
|
+
export function uniquifyByKey(array, key) {
|
|
30
|
+
const cache = new Set();
|
|
31
|
+
return array.filter(item => {
|
|
32
|
+
if (cache.has(item[key])) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
cache.add(item[key]);
|
|
36
|
+
return true;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
//# sourceMappingURL=uniquify-by-key.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/js-utils/src/array/uniquify-by-key.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,UAAU,aAAa,CAAsC,KAAU,EAAE,GAAM;IACnF,MAAM,KAAK,GAAG,IAAI,GAAG,EAAQ,CAAC;IAE9B,OAAO,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE;QACzB,IAAI,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;YACzB,OAAO,KAAK,CAAC;QACf,CAAC;QAED,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;QAErB,OAAO,IAAI,CAAC;IACd,CAAC,CAAC,CAAC;AACL,CAAC","file":"uniquify-by-key.js","sourcesContent":["/**\n * #### Uniquify By Key\n *\n * Removes objects from the array which the value of its specifed key included before by another\n *\n * * * *\n * Example:\n * ```typescript\n * import { uniquifyByKey } \"@telperion/js-utils/array\";\n *\n * const array = [{a: 1}, {a: 1}, {a: 2}, {a: 3}, {a: 3}, {a: 4}];\n *\n * uniquifyByKey(array, 'a'); // [{a: 1}, {a: 2}, {a: 3}, {a: 4}]\n * ```\n *\n * Prototype Example:\n * ```typescript\n * import \"@telperion/js-utils/array/proto/uniquify-by-key\";\n *\n * const array = [{a: 1}, {a: 1}, {a: 2}, {a: 3}, {a: 3}, {a: 4}];\n *\n * array.uniquifyByKey('a'); // [{a: 1}, {a: 2}, {a: 3}, {a: 4}]\n * ```\n * * * *\n * @param array Collection of the objects\n * @param key Key to search the value on\n * @return The new uniquified array\n */\nexport function uniquifyByKey<T extends Object, P extends keyof T>(array: T[], key: P): T[] {\n const cache = new Set<T[P]>();\n\n return array.filter(item => {\n if (cache.has(item[key])) {\n return false;\n }\n\n cache.add(item[key]);\n\n return true;\n });\n}\n"]}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.uniquify = uniquify;
|
|
4
|
+
/**
|
|
5
|
+
* #### Uniquify
|
|
6
|
+
*
|
|
7
|
+
* Removes repeated items from the array
|
|
8
|
+
*
|
|
9
|
+
* * * *
|
|
10
|
+
* Example:
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { uniquify } "@telperion/js-utils/array";
|
|
13
|
+
*
|
|
14
|
+
* const array = ["a", "b", "c", "a", "b", "c"];
|
|
15
|
+
*
|
|
16
|
+
* uniquify(array); // ["a", "b", "c"]
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* Prototype Example:
|
|
20
|
+
* ```typescript
|
|
21
|
+
* import "@telperion/js-utils/array/proto/uniquify";
|
|
22
|
+
*
|
|
23
|
+
* const array = ["a", "b", "c", "a", "b", "c"];
|
|
24
|
+
*
|
|
25
|
+
* array.uniquify(); // ["a", "b", "c"]
|
|
26
|
+
* ```
|
|
27
|
+
* * * *
|
|
28
|
+
* @return The new uniquified array
|
|
29
|
+
*/
|
|
30
|
+
function uniquify(array) {
|
|
31
|
+
return [...new Set(array)];
|
|
32
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #### Uniquify
|
|
3
|
+
*
|
|
4
|
+
* Removes repeated items from the array
|
|
5
|
+
*
|
|
6
|
+
* * * *
|
|
7
|
+
* Example:
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { uniquify } "@telperion/js-utils/array";
|
|
10
|
+
*
|
|
11
|
+
* const array = ["a", "b", "c", "a", "b", "c"];
|
|
12
|
+
*
|
|
13
|
+
* uniquify(array); // ["a", "b", "c"]
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* Prototype Example:
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import "@telperion/js-utils/array/proto/uniquify";
|
|
19
|
+
*
|
|
20
|
+
* const array = ["a", "b", "c", "a", "b", "c"];
|
|
21
|
+
*
|
|
22
|
+
* array.uniquify(); // ["a", "b", "c"]
|
|
23
|
+
* ```
|
|
24
|
+
* * * *
|
|
25
|
+
* @return The new uniquified array
|
|
26
|
+
*/
|
|
27
|
+
export declare function uniquify<T>(array: T[]): T[];
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #### Uniquify
|
|
3
|
+
*
|
|
4
|
+
* Removes repeated items from the array
|
|
5
|
+
*
|
|
6
|
+
* * * *
|
|
7
|
+
* Example:
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { uniquify } "@telperion/js-utils/array";
|
|
10
|
+
*
|
|
11
|
+
* const array = ["a", "b", "c", "a", "b", "c"];
|
|
12
|
+
*
|
|
13
|
+
* uniquify(array); // ["a", "b", "c"]
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* Prototype Example:
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import "@telperion/js-utils/array/proto/uniquify";
|
|
19
|
+
*
|
|
20
|
+
* const array = ["a", "b", "c", "a", "b", "c"];
|
|
21
|
+
*
|
|
22
|
+
* array.uniquify(); // ["a", "b", "c"]
|
|
23
|
+
* ```
|
|
24
|
+
* * * *
|
|
25
|
+
* @return The new uniquified array
|
|
26
|
+
*/
|
|
27
|
+
export function uniquify(array) {
|
|
28
|
+
return [...new Set(array)];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
//# sourceMappingURL=uniquify.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/js-utils/src/array/uniquify.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,UAAU,QAAQ,CAAI,KAAU;IACpC,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC;AAC7B,CAAC","file":"uniquify.js","sourcesContent":["/**\n * #### Uniquify\n *\n * Removes repeated items from the array\n *\n * * * *\n * Example:\n * ```typescript\n * import { uniquify } \"@telperion/js-utils/array\";\n *\n * const array = [\"a\", \"b\", \"c\", \"a\", \"b\", \"c\"];\n *\n * uniquify(array); // [\"a\", \"b\", \"c\"]\n * ```\n *\n * Prototype Example:\n * ```typescript\n * import \"@telperion/js-utils/array/proto/uniquify\";\n *\n * const array = [\"a\", \"b\", \"c\", \"a\", \"b\", \"c\"];\n *\n * array.uniquify(); // [\"a\", \"b\", \"c\"]\n * ```\n * * * *\n * @return The new uniquified array\n */\nexport function uniquify<T>(array: T[]): T[] {\n return [...new Set(array)];\n}\n"]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { isFalsy } from '@telperion/js-utils/is-falsy';
|
|
2
|
+
declare global {
|
|
3
|
+
export interface BooleanConstructor {
|
|
4
|
+
/**
|
|
5
|
+
* #### Returns whether the entered value is falsy
|
|
6
|
+
*
|
|
7
|
+
* * * *
|
|
8
|
+
* Example usage:
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import "@telperion/js-utils/boolean/static/is-falsy";
|
|
11
|
+
*
|
|
12
|
+
* Boolean.isFalsy(undefined); // true
|
|
13
|
+
* Boolean.isFalsy(true); // false
|
|
14
|
+
* Boolean.isFalsy([]) // false
|
|
15
|
+
*
|
|
16
|
+
* const falsyValues = ["a", undefined, "b", "", "c"].filter(Boolean.isFalsy); // [undefined, ""]
|
|
17
|
+
* ```
|
|
18
|
+
* * * *
|
|
19
|
+
* @param value value to be checked
|
|
20
|
+
*/
|
|
21
|
+
isFalsy: typeof isFalsy;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/js-utils/src/boolean/static/is-falsy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,8BAA8B,CAAC;AAyBvD,OAAO,CAAC,OAAO,GAAG,OAAO,CAAC","file":"is-falsy.js","sourcesContent":["import { isFalsy } from '@telperion/js-utils/is-falsy';\n\ndeclare global {\n export interface BooleanConstructor {\n /**\n * #### Returns whether the entered value is falsy\n *\n * * * *\n * Example usage:\n * ```typescript\n * import \"@telperion/js-utils/boolean/static/is-falsy\";\n *\n * Boolean.isFalsy(undefined); // true\n * Boolean.isFalsy(true); // false\n * Boolean.isFalsy([]) // false\n *\n * const falsyValues = [\"a\", undefined, \"b\", \"\", \"c\"].filter(Boolean.isFalsy); // [undefined, \"\"]\n * ```\n * * * *\n * @param value value to be checked\n */\n isFalsy: typeof isFalsy;\n }\n}\n\nBoolean.isFalsy = isFalsy;\n"]}
|