@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
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Telperion Technology
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,621 @@
|
|
|
1
|
+
# @telperion/js-utils
|
|
2
|
+
Javascript utility functions for web development
|
|
3
|
+
|
|
4
|
+
[](https://www.npmjs.com/package/@telperion/js-utils)
|
|
5
|
+
[](https://www.npmjs.com/package/@telperion/js-utils)
|
|
6
|
+
[](https://github.com/ellerbrock/typescript-badges/)
|
|
7
|
+
[](https://github.com/telperiontech/telperion/blob/main/LICENSE)
|
|
8
|
+
|
|
9
|
+
## Motivation
|
|
10
|
+
|
|
11
|
+
Collecting commonly used utility functions in a package.
|
|
12
|
+
|
|
13
|
+
## Goals
|
|
14
|
+
|
|
15
|
+
* Typescript support
|
|
16
|
+
* Tree-shaking
|
|
17
|
+
* No dependencies
|
|
18
|
+
* High performance
|
|
19
|
+
* Documentation
|
|
20
|
+
* High test coverage
|
|
21
|
+
* Static/Prototype method support
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
* npm: `npm install @telperion/js-utils --save`
|
|
25
|
+
* yarn: `yarn add @telperion/js-utils`
|
|
26
|
+
* pnpm: `pnpm i -S @telperion/js-utils`
|
|
27
|
+
|
|
28
|
+
## Documentation
|
|
29
|
+
|
|
30
|
+
See: [open-source.telperion.tr/js-utils](https://open-source.telperion.tr/js-utils)
|
|
31
|
+
|
|
32
|
+
## Functions
|
|
33
|
+
|
|
34
|
+
### Array
|
|
35
|
+
|
|
36
|
+
#### [Async Map](https://open-source.telperion.tr/js-utils/modules/_array_async_map_.html)
|
|
37
|
+
Maps an array asynchronously
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { asyncMap } "@telperion/js-utils/array";
|
|
41
|
+
|
|
42
|
+
const array = [1, 2, 3];
|
|
43
|
+
|
|
44
|
+
const result = await asyncMap(array, async value => {
|
|
45
|
+
return await addOneAfterASecond(value);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
console.log(result); // [2, 3, 4]
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
#### [Compact](https://open-source.telperion.tr/js-utils/modules/_array_compact_.html)
|
|
52
|
+
Filters falsy values of an array
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import { compact } from "@telperion/js-utils/array";
|
|
56
|
+
|
|
57
|
+
const arr = [undefined, "", false, 0, 1, "1", null];
|
|
58
|
+
const compacted = compact(arr); // [1, "1"];
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
#### [Difference](https://open-source.telperion.tr/js-utils/modules/_array_difference_.html)
|
|
62
|
+
Gets the difference of the two arrays or sets
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import { difference } from "@telperion/js-utils/array";
|
|
66
|
+
|
|
67
|
+
const base = ["a", "b", "c", "d", "a", "b", "c", "d"];
|
|
68
|
+
|
|
69
|
+
difference(base, ["a", "b"]); // ["c", "d", "c", "d"]
|
|
70
|
+
difference(base, ["a", "b"], true); // ["c", "d", "a", "b", "c", "d"]
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
#### [Find By Key](https://open-source.telperion.tr/js-utils/modules/_array_find_by_key_.html)
|
|
74
|
+
Finds an object in an array by matching the value set on the key
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { findByKey } from "@telperion/js-utils/array";
|
|
78
|
+
|
|
79
|
+
const array = [{a: 1}, {a: 2}, {a: 3}];
|
|
80
|
+
|
|
81
|
+
findByKey(array, "a", 2); // {a: 2}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
#### [Intersection](https://open-source.telperion.tr/js-utils/modules/_array_intersection_.html)
|
|
85
|
+
Gets the intersection of the two arrays or sets
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
import { intersection } from "@telperion/js-utils/array";
|
|
89
|
+
|
|
90
|
+
const base = ["a", "b", "c", "d", "a", "b", "c", "d"];
|
|
91
|
+
|
|
92
|
+
intersection(base, ["a", "b", "x"]); // ["a", "b", "a", "b"]
|
|
93
|
+
intersection(base, ["a", "b", "x"], false); // ["a", "b"]
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
#### [Limit](https://open-source.telperion.tr/js-utils/modules/_array_limit_.html)
|
|
97
|
+
Returns first `n` children of an array
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
import { limit } from "@telperion/js-utils/array";
|
|
101
|
+
|
|
102
|
+
const array = ["a", "b", "c", "d", "e", "f"];
|
|
103
|
+
|
|
104
|
+
limit(array, 3); // ["a", "b", "c"]
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
#### [Remove](https://open-source.telperion.tr/js-utils/modules/_array_remove_.html)
|
|
108
|
+
Removes an item from an array
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
import { remove } from "@telperion/js-utils/array";
|
|
112
|
+
|
|
113
|
+
const array = ["a", "b", "c", "a", "b", "c"];
|
|
114
|
+
|
|
115
|
+
remove(array, "b"); // ["a", "c", "a", "b", "c"]
|
|
116
|
+
remove(array, "b", true); // ["a", "c", "a", "c"]
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
#### [Replace](https://open-source.telperion.tr/js-utils/modules/_array_replace_.html)
|
|
120
|
+
Replaces an item with passed one of an array
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
import { replace } from "@telperion/js-utils/array";
|
|
124
|
+
|
|
125
|
+
const array = ["a", "b", "c", "a", "b", "c"];
|
|
126
|
+
|
|
127
|
+
replace(array, "b", "x"); // ["a", "x", "c", "a", "b", "c"]
|
|
128
|
+
replace(array, {startingIndex: 3, deleteCount: 1, itemsToReplace: ['x', 'y']}); // ["a", "b", "c", "x", "y", "b", "c"];
|
|
129
|
+
|
|
130
|
+
const config = new Map();
|
|
131
|
+
config.set("a", "x")
|
|
132
|
+
config.set("b", "y");
|
|
133
|
+
|
|
134
|
+
replace(array, {itemsToReplace: config}); // ["x", "y", "c", "a", "b", "c"];
|
|
135
|
+
replace(array, {itemsToReplace: config, multi: true}); // ["x", "y", "c", "x", "y", "c"];
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
#### [Uniquify](https://open-source.telperion.tr/js-utils/modules/_array_uniquify_.html)
|
|
139
|
+
Removes repeated items from the array
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
import { uniquify } "@telperion/js-utils/array";
|
|
143
|
+
|
|
144
|
+
const array = ["a", "b", "c", "a", "b", "c"];
|
|
145
|
+
|
|
146
|
+
uniquify(array); // ["a", "b", "c"]
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
#### [Uniquify By Key](https://open-source.telperion.tr/js-utils/modules/_array_uniquify_by_key_.html)
|
|
150
|
+
Removes objects from the array which the value of its specifed key included before by another
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
import { uniquifyByKey } "@telperion/js-utils/array";
|
|
154
|
+
|
|
155
|
+
const array = [{a: 1}, {a: 1}, {a: 2}, {a: 3}, {a: 3}, {a: 4}];
|
|
156
|
+
|
|
157
|
+
uniquifyByKey(array, 'a'); // [{a: 1}, {a: 2}, {a: 3}, {a: 4}]
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Class
|
|
161
|
+
|
|
162
|
+
#### [Mixin](https://open-source.telperion.tr/js-utils/modules/_class_mixin_.html)
|
|
163
|
+
Creates a class that combines two classes using the mixin pattern
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
import { mixin } from "@telperion/js-utils/class";
|
|
167
|
+
|
|
168
|
+
class Timestamped {
|
|
169
|
+
timestamp = Date.now();
|
|
170
|
+
getAge() {
|
|
171
|
+
return Date.now() - this.timestamp;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
class User {
|
|
176
|
+
constructor(public name: string) {}
|
|
177
|
+
greet() {
|
|
178
|
+
return `Hello, ${this.name}`;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
class TimestampedUser extends mixin(User, Timestamped) {}
|
|
183
|
+
|
|
184
|
+
const user = new TimestampedUser(['John'], []);
|
|
185
|
+
console.log(user.greet()); // "Hello, John"
|
|
186
|
+
console.log(user.getAge()); // Time since creation
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### Function
|
|
190
|
+
|
|
191
|
+
#### [Debounce](https://open-source.telperion.tr/js-utils/modules/_function_debounce_.html)
|
|
192
|
+
Debounces a function that delays invoking until after configured time have elapsed since the last time the debounced function was invoked
|
|
193
|
+
|
|
194
|
+
```typescript
|
|
195
|
+
import { debounce } from "@telperion/js-utils/promise";
|
|
196
|
+
|
|
197
|
+
function foo() {
|
|
198
|
+
console.log("hello");
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
for (let i = 0; i < 10; i++) {
|
|
202
|
+
debounce(foo);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// logs "hello" only once
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
#### [Defer](https://open-source.telperion.tr/js-utils/modules/_function_defer_.html)
|
|
209
|
+
Delays the execution of the passed function
|
|
210
|
+
|
|
211
|
+
```typescript
|
|
212
|
+
import { defer } from "@telperion/js-utils/function";
|
|
213
|
+
|
|
214
|
+
const result = await defer(() => aFunctionToDeferThatReturnsHello());
|
|
215
|
+
|
|
216
|
+
console.log(result); // 'hello'
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
#### [Noop](https://open-source.telperion.tr/js-utils/modules/_function_noop_.html)
|
|
220
|
+
Noop function
|
|
221
|
+
|
|
222
|
+
```typescript
|
|
223
|
+
import { noop } from "@telperion/js-utils/function";
|
|
224
|
+
|
|
225
|
+
noop();
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
#### [Of](https://open-source.telperion.tr/js-utils/modules/_function_of_.html)
|
|
229
|
+
Creates a function which returns the specified value
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
import { of } from "@telperion/js-utils/function";
|
|
233
|
+
|
|
234
|
+
const base = [1, 2, 5, {}, "x", "y"];
|
|
235
|
+
|
|
236
|
+
base.map(of('hi')); // ["hi", "hi", "hi", "hi", "hi", "hi"]
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### Map
|
|
240
|
+
|
|
241
|
+
#### [Merge](https://open-source.telperion.tr/js-utils/modules/_map_merge_.html)
|
|
242
|
+
Merges two maps
|
|
243
|
+
|
|
244
|
+
```typescript
|
|
245
|
+
import { merge } from "@telperion/js-utils/map";
|
|
246
|
+
|
|
247
|
+
const first = new Map();
|
|
248
|
+
first.set("a", 1);
|
|
249
|
+
|
|
250
|
+
const second = new Map();
|
|
251
|
+
second.set("b", 2);
|
|
252
|
+
merge(first, second); // [{key: "a", value: 1}, {key: "b", value: 2}]
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### Math
|
|
256
|
+
|
|
257
|
+
#### [Min-Max](https://open-source.telperion.tr/js-utils/modules/_math_min_max_.html)
|
|
258
|
+
Limits the value by specified range
|
|
259
|
+
|
|
260
|
+
```typescript
|
|
261
|
+
import { minMax } from "@telperion/js-utils/math";
|
|
262
|
+
|
|
263
|
+
const limitedValue = minMax(200, 300, Math.random() * 1000); // Outputs between 200-300
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
### Object
|
|
267
|
+
|
|
268
|
+
#### [Clone](https://open-source.telperion.tr/js-utils/modules/_object_clone_.html)
|
|
269
|
+
A function to deep clone anything (recursively)
|
|
270
|
+
|
|
271
|
+
```typescript
|
|
272
|
+
import { clone } from "@telperion/js-utils/object";
|
|
273
|
+
|
|
274
|
+
const object = {a: 1, b: {c: true, d: ["x", "y"]}};
|
|
275
|
+
|
|
276
|
+
const clonedObject = clone(object);
|
|
277
|
+
// {a: 1, b: {c: true, d: ["x", "y"]}}
|
|
278
|
+
// object.b.d === clonedObject.b.d // false
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
#### [Compact](https://open-source.telperion.tr/js-utils/modules/_object_compact_.html)
|
|
282
|
+
Removes `null` and `undefined` values and their keys from an object
|
|
283
|
+
|
|
284
|
+
```typescript
|
|
285
|
+
import { compact } from "@telperion/js-utils/object";
|
|
286
|
+
|
|
287
|
+
const a = {
|
|
288
|
+
x: null,
|
|
289
|
+
y: undefined,
|
|
290
|
+
z: 20
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
compact(a); // {z: 20}
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
#### [Deepest](https://open-source.telperion.tr/js-utils/modules/_object_deepest_.html)
|
|
297
|
+
Get deepest value in an object chain
|
|
298
|
+
|
|
299
|
+
```typescript
|
|
300
|
+
import { deepest } from "@telperion/js-utils/object";
|
|
301
|
+
|
|
302
|
+
const a = {x: null};
|
|
303
|
+
const b = {x: a};
|
|
304
|
+
const c = {x: b};
|
|
305
|
+
|
|
306
|
+
deepest(c, 'x'); // {x: null} (a)
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
### Promise
|
|
310
|
+
|
|
311
|
+
#### [Chain](https://open-source.telperion.tr/js-utils/modules/_promise_chain_.html)
|
|
312
|
+
Chains promises in a sequential order
|
|
313
|
+
|
|
314
|
+
```typescript
|
|
315
|
+
import { chain } from '@telperion/js-utils/promise';
|
|
316
|
+
|
|
317
|
+
const lastResult = await chain([() => getUserDetails(), ({photoUrl}) => getUserPhoto(photoUrl)]);
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
#### [Never](https://open-source.telperion.tr/js-utils/modules/_promise_never_.html)
|
|
321
|
+
A promise which never resolves
|
|
322
|
+
|
|
323
|
+
```typescript
|
|
324
|
+
import { never, NEVER } from '@telperion/js-utils/promise';
|
|
325
|
+
|
|
326
|
+
function foo(promise = never()) {
|
|
327
|
+
promise.then(val => {
|
|
328
|
+
...
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// or
|
|
333
|
+
|
|
334
|
+
function foo(promise = NEVER) {
|
|
335
|
+
promise.then(val => {
|
|
336
|
+
...
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
#### [Revert](https://open-source.telperion.tr/js-utils/modules/_promise_revert_.html)
|
|
342
|
+
Exchanges resolve state with rejection of a promise
|
|
343
|
+
|
|
344
|
+
```typescript
|
|
345
|
+
import { revert } from "@telperion/js-utils/promise";
|
|
346
|
+
|
|
347
|
+
const errorPromise = Promise.reject(new Error('foo'));
|
|
348
|
+
|
|
349
|
+
revert(errorPromise)
|
|
350
|
+
.then(err => {
|
|
351
|
+
console.log("this will be logged", err);
|
|
352
|
+
})
|
|
353
|
+
.catch(res => {
|
|
354
|
+
console.log("this won't be logged", res);
|
|
355
|
+
});
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
#### [Timeout](https://open-source.telperion.tr/js-utils/modules/_promise_timeout_.html)
|
|
359
|
+
Returns a promise which resolves after specified time
|
|
360
|
+
|
|
361
|
+
```typescript
|
|
362
|
+
import { timeout } from "@telperion/js-utils/promise";
|
|
363
|
+
|
|
364
|
+
timeout(1000)
|
|
365
|
+
.then(() => console.log("will be logged after a second"));
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
#### [Try Catch](https://open-source.telperion.tr/js-utils/modules/_promise_try_catch_.html)
|
|
369
|
+
Merges result and error in the same callback
|
|
370
|
+
|
|
371
|
+
```typescript
|
|
372
|
+
import { tryCatch } from "@telperion/js-utils/promise";
|
|
373
|
+
|
|
374
|
+
const promise = anAsyncCall();
|
|
375
|
+
|
|
376
|
+
const [error, result] = await tryCatch(promise);
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
#### [Try One By One](https://open-source.telperion.tr/js-utils/modules/_promise_try_one_by_one_.html)
|
|
380
|
+
Tries a set of promises one by one with given order. Breaks the call when a promise resolved. Otherwise keeps trying incoming promises until the list is finished.
|
|
381
|
+
|
|
382
|
+
```typescript
|
|
383
|
+
import { tryOneByOne } from "@telperion/js-utils/promise";
|
|
384
|
+
|
|
385
|
+
async function fooFunction() {
|
|
386
|
+
const foo = await tryOneByOne([
|
|
387
|
+
() => someCall(),
|
|
388
|
+
(err) => anotherCall(),
|
|
389
|
+
(err) => fooPromise()
|
|
390
|
+
]);
|
|
391
|
+
|
|
392
|
+
// do stuff
|
|
393
|
+
}
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
### String
|
|
397
|
+
|
|
398
|
+
#### [Limit](https://open-source.telperion.tr/js-utils/modules/_string_limit_.html)
|
|
399
|
+
Limits the string to `n` character
|
|
400
|
+
|
|
401
|
+
```typescript
|
|
402
|
+
import { limit } from "@telperion/js-utils/string";
|
|
403
|
+
|
|
404
|
+
const str = 'foobarbaz';
|
|
405
|
+
|
|
406
|
+
limit(str, 3); // 'foo'
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
### Time
|
|
410
|
+
|
|
411
|
+
Time utility functions for converting time units to milliseconds. These are particularly useful for setTimeout, setInterval, and Date operations.
|
|
412
|
+
|
|
413
|
+
#### [Seconds](https://open-source.telperion.tr/js-utils/modules/_time_seconds_.html)
|
|
414
|
+
Converts seconds to milliseconds
|
|
415
|
+
|
|
416
|
+
```typescript
|
|
417
|
+
import { seconds } from "@telperion/js-utils/time";
|
|
418
|
+
|
|
419
|
+
setTimeout(() => console.log('5 seconds later'), seconds(5));
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
#### [Minutes](https://open-source.telperion.tr/js-utils/modules/_time_minutes_.html)
|
|
423
|
+
Converts minutes to milliseconds
|
|
424
|
+
|
|
425
|
+
```typescript
|
|
426
|
+
import { minutes } from "@telperion/js-utils/time";
|
|
427
|
+
|
|
428
|
+
setTimeout(() => console.log('2 minutes later'), minutes(2));
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
#### [Hours](https://open-source.telperion.tr/js-utils/modules/_time_hours_.html)
|
|
432
|
+
Converts hours to milliseconds
|
|
433
|
+
|
|
434
|
+
```typescript
|
|
435
|
+
import { hours } from "@telperion/js-utils/time";
|
|
436
|
+
|
|
437
|
+
const sessionDuration = hours(2); // 7200000 ms
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
#### [Days](https://open-source.telperion.tr/js-utils/modules/_time_days_.html)
|
|
441
|
+
Converts days to milliseconds
|
|
442
|
+
|
|
443
|
+
```typescript
|
|
444
|
+
import { days } from "@telperion/js-utils/time";
|
|
445
|
+
|
|
446
|
+
const expiryDate = Date.now() + days(7); // 7 days from now
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
#### [Weeks](https://open-source.telperion.tr/js-utils/modules/_time_weeks_.html)
|
|
450
|
+
Converts weeks to milliseconds
|
|
451
|
+
|
|
452
|
+
```typescript
|
|
453
|
+
import { weeks } from "@telperion/js-utils/time";
|
|
454
|
+
|
|
455
|
+
const trialPeriod = weeks(2); // 2 weeks in milliseconds
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
#### [Months](https://open-source.telperion.tr/js-utils/modules/_time_months_.html)
|
|
459
|
+
Converts months to milliseconds (assumes 30 days per month)
|
|
460
|
+
|
|
461
|
+
```typescript
|
|
462
|
+
import { months } from "@telperion/js-utils/time";
|
|
463
|
+
|
|
464
|
+
const subscriptionPeriod = months(3); // ~3 months in milliseconds
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
#### [Years](https://open-source.telperion.tr/js-utils/modules/_time_years_.html)
|
|
468
|
+
Converts years to milliseconds (assumes 365 days per year)
|
|
469
|
+
|
|
470
|
+
```typescript
|
|
471
|
+
import { years } from "@telperion/js-utils/time";
|
|
472
|
+
|
|
473
|
+
const licenseExpiry = Date.now() + years(1); // ~1 year from now
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
### Etc.
|
|
477
|
+
|
|
478
|
+
#### [Arrayize](https://open-source.telperion.tr/js-utils/modules/_arrayize_.html)
|
|
479
|
+
Encapsulates a non array value with an array that contains it unless the value is already an array
|
|
480
|
+
|
|
481
|
+
```typescript
|
|
482
|
+
import { arrayize } from "@telperion/js-utils";
|
|
483
|
+
|
|
484
|
+
const foo = 'foo';
|
|
485
|
+
const bar = ['bar'];
|
|
486
|
+
const fooArr = arrayize(foo); // ['foo'];
|
|
487
|
+
const barArr = arrayize(bar); // ['bar'];
|
|
488
|
+
```
|
|
489
|
+
|
|
490
|
+
#### [Compact](https://open-source.telperion.tr/js-utils/modules/_compact_.html)
|
|
491
|
+
Filters falsy values of the given array
|
|
492
|
+
Removes `null` and `undefined` values and their keys from an object
|
|
493
|
+
|
|
494
|
+
```typescript
|
|
495
|
+
import { compact } from "@telperion/js-utils";
|
|
496
|
+
|
|
497
|
+
const arr = [undefined, "", false, 0, 1, "1"];
|
|
498
|
+
const compacted = compact(arr); // [1, "1"];
|
|
499
|
+
|
|
500
|
+
const object = {
|
|
501
|
+
x: null,
|
|
502
|
+
y: undefined,
|
|
503
|
+
z: 20
|
|
504
|
+
};
|
|
505
|
+
|
|
506
|
+
const compacted = compact(object); // {z: 20}
|
|
507
|
+
```
|
|
508
|
+
|
|
509
|
+
#### [Is Falsy](https://open-source.telperion.tr/js-utils/modules/_is_falsy_.html)
|
|
510
|
+
Returns whether the entered value is falsy
|
|
511
|
+
|
|
512
|
+
```typescript
|
|
513
|
+
import { isFalsy } from "@telperion/js-utils";
|
|
514
|
+
|
|
515
|
+
isFalsy(undefined); // true
|
|
516
|
+
isFalsy(true); // false
|
|
517
|
+
```
|
|
518
|
+
|
|
519
|
+
#### [Is Truthy](https://open-source.telperion.tr/js-utils/modules/_is_truthy_.html)
|
|
520
|
+
Returns whether the entered value is truthy
|
|
521
|
+
|
|
522
|
+
```typescript
|
|
523
|
+
import { isTruthy } from "@telperion/js-utils";
|
|
524
|
+
|
|
525
|
+
isTruthy(undefined); // false
|
|
526
|
+
isTruthy(true); // true
|
|
527
|
+
```
|
|
528
|
+
|
|
529
|
+
#### [Limit](https://open-source.telperion.tr/js-utils/modules/_limit_.html)
|
|
530
|
+
Limits the string or array to `n` character
|
|
531
|
+
|
|
532
|
+
```typescript
|
|
533
|
+
import { limit } from "@telperion/js-utils";
|
|
534
|
+
|
|
535
|
+
const str = 'foobarbaz';
|
|
536
|
+
const array = ["a", "b", "c", "d", "e", "f"];
|
|
537
|
+
|
|
538
|
+
limit(str, 3); // 'foo'
|
|
539
|
+
limit(array, 3); // ["a", "b", "c"]
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
#### [Open Promise](https://open-source.telperion.tr/js-utils/modules/_open-promise_.html)
|
|
543
|
+
A promise constructor to resolve or reject from outside
|
|
544
|
+
|
|
545
|
+
```typescript
|
|
546
|
+
import { OpenPromise } from "@telperion/js-utils";
|
|
547
|
+
|
|
548
|
+
const aPromiseWillBeResolvedLater = new OpenPromise();
|
|
549
|
+
|
|
550
|
+
aPromiseWillBeResolvedLater.then(val => console.log(val));
|
|
551
|
+
|
|
552
|
+
aPromiseWillBeResolvedLater.resolve({x: 1});
|
|
553
|
+
// logs `{x: 1}`
|
|
554
|
+
```
|
|
555
|
+
|
|
556
|
+
#### [Smart Map](https://open-source.telperion.tr/js-utils/modules/_smart_map_.html)
|
|
557
|
+
Like WeakMap but can also store values using primitive keys
|
|
558
|
+
|
|
559
|
+
See: [WeakMap](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap)
|
|
560
|
+
|
|
561
|
+
```typescript
|
|
562
|
+
import { SmartMap } from "@telperion/js-utils";
|
|
563
|
+
|
|
564
|
+
const aMap = new SmartMap();
|
|
565
|
+
|
|
566
|
+
aMap.set("foo", "foo");
|
|
567
|
+
aMap.set(1, "thales rocks");
|
|
568
|
+
console.log(aMap.size) // 2
|
|
569
|
+
|
|
570
|
+
aMap.set({}, "thales rocks again");
|
|
571
|
+
console.log(aMap.size) // 2
|
|
572
|
+
|
|
573
|
+
const anObject = {};
|
|
574
|
+
aMap.set(anObject, "thales rocks again and again");
|
|
575
|
+
console.log(aMap.size) // 3
|
|
576
|
+
console.log(aMap.get(anObject)) // "thales rocks again and again"
|
|
577
|
+
```
|
|
578
|
+
|
|
579
|
+
#### [Unique Id](https://open-source.telperion.tr/js-utils/modules/_unique-id_.html)
|
|
580
|
+
Starts a new counter for every unique prefix and if a prefix is given, returns the id by prefixing it, otherwise returns the id as number
|
|
581
|
+
|
|
582
|
+
```typescript
|
|
583
|
+
import { uniqueId } from "@telperion/js-utils";
|
|
584
|
+
|
|
585
|
+
uniqueId(); // 0
|
|
586
|
+
uniqueId(); // 1
|
|
587
|
+
uniqueId("some-str"); // "some-str-0";
|
|
588
|
+
uniqueId("some-str"); // "some-str-1";
|
|
589
|
+
uniqueId(); // 3
|
|
590
|
+
```
|
|
591
|
+
|
|
592
|
+
## Static/Prototype Methods
|
|
593
|
+
|
|
594
|
+
You may use any of these methods by adding them to the constructors or prototypes to native objects in main file.
|
|
595
|
+
|
|
596
|
+
Prototype Example:
|
|
597
|
+
|
|
598
|
+
```typescript
|
|
599
|
+
// main.ts
|
|
600
|
+
import "@telperion/js-utils/array/proto/compact";
|
|
601
|
+
|
|
602
|
+
// somewhere else
|
|
603
|
+
const arr = [undefined, "", false, 0, 1, "1", null];
|
|
604
|
+
const compacted = arr.compact(); // [1, "1"];
|
|
605
|
+
|
|
606
|
+
```
|
|
607
|
+
|
|
608
|
+
Static Example:
|
|
609
|
+
|
|
610
|
+
```typescript
|
|
611
|
+
// main.ts
|
|
612
|
+
import "@telperion/js-utils/promise/static/timeout";
|
|
613
|
+
|
|
614
|
+
// somewhere else
|
|
615
|
+
Promise.timeout(1000)
|
|
616
|
+
.then(() => console.log("will be logged after a second"));
|
|
617
|
+
```
|
|
618
|
+
|
|
619
|
+
## License
|
|
620
|
+
|
|
621
|
+
[MIT](./LICENSE)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.arrayize = arrayize;
|
|
4
|
+
/**
|
|
5
|
+
* Encapsulates a non array value with an array that contains it unless the value is already an array
|
|
6
|
+
*
|
|
7
|
+
* * * *
|
|
8
|
+
* Example usage:
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { arrayize } from "@telperion/js-utils";
|
|
11
|
+
*
|
|
12
|
+
* const foo = 'foo';
|
|
13
|
+
* const bar = ['bar'];
|
|
14
|
+
* const fooArr = arrayize(foo); // ['foo'];
|
|
15
|
+
* const barArr = arrayize(bar); // ['bar'];
|
|
16
|
+
* ```
|
|
17
|
+
* * * *
|
|
18
|
+
* @param value Array or single value to capsulate
|
|
19
|
+
*/
|
|
20
|
+
function arrayize(value) {
|
|
21
|
+
return value instanceof Array ? value : [value];
|
|
22
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encapsulates a non array value with an array that contains it unless the value is already an array
|
|
3
|
+
*
|
|
4
|
+
* * * *
|
|
5
|
+
* Example usage:
|
|
6
|
+
* ```typescript
|
|
7
|
+
* import { arrayize } from "@telperion/js-utils";
|
|
8
|
+
*
|
|
9
|
+
* const foo = 'foo';
|
|
10
|
+
* const bar = ['bar'];
|
|
11
|
+
* const fooArr = arrayize(foo); // ['foo'];
|
|
12
|
+
* const barArr = arrayize(bar); // ['bar'];
|
|
13
|
+
* ```
|
|
14
|
+
* * * *
|
|
15
|
+
* @param value Array or single value to capsulate
|
|
16
|
+
*/
|
|
17
|
+
export declare function arrayize<T>(value: T | T[]): T[];
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encapsulates a non array value with an array that contains it unless the value is already an array
|
|
3
|
+
*
|
|
4
|
+
* * * *
|
|
5
|
+
* Example usage:
|
|
6
|
+
* ```typescript
|
|
7
|
+
* import { arrayize } from "@telperion/js-utils";
|
|
8
|
+
*
|
|
9
|
+
* const foo = 'foo';
|
|
10
|
+
* const bar = ['bar'];
|
|
11
|
+
* const fooArr = arrayize(foo); // ['foo'];
|
|
12
|
+
* const barArr = arrayize(bar); // ['bar'];
|
|
13
|
+
* ```
|
|
14
|
+
* * * *
|
|
15
|
+
* @param value Array or single value to capsulate
|
|
16
|
+
*/
|
|
17
|
+
export function arrayize(value) {
|
|
18
|
+
return value instanceof Array ? value : [value];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
//# sourceMappingURL=arrayize.js.map
|