@straylightagency/utils 1.1.8 → 1.2.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 +469 -0
- package/package.json +4 -3
- package/src/array/difference.js +13 -0
- package/src/array/flatten.js +18 -0
- package/src/array/groupBy.js +22 -0
- package/src/array/index.js +8 -1
- package/src/array/initial.js +4 -4
- package/src/array/intersection.js +18 -0
- package/src/array/last.js +3 -4
- package/src/array/move.js +27 -0
- package/src/array/remove.js +11 -8
- package/src/array/replace.js +6 -6
- package/src/array/shuffle.js +23 -0
- package/src/array/unique.js +10 -0
- package/src/date/getWeekNumber.js +1 -1
- package/src/date/reviver.js +2 -1
- package/src/dom/addEventListeners.js +2 -2
- package/src/dom/closestElement.js +1 -1
- package/src/dom/delegate.js +11 -8
- package/src/dom/index.js +4 -1
- package/src/dom/onEachElements.js +1 -1
- package/src/dom/stringToHtml.js +1 -1
- package/src/dom/unlessElementExists.js +1 -1
- package/src/dom/whenIntersect.js +24 -0
- package/src/dom/whenMutation.js +28 -0
- package/src/dom/whenResize.js +14 -13
- package/src/dom/whenScroll.js +14 -13
- package/src/dom/whenVisible.js +30 -0
- package/src/index.js +54 -23
- package/src/misc/attempt.js +2 -2
- package/src/misc/base64.js +8 -5
- package/src/misc/clipboard.js +1 -1
- package/src/misc/debounce.js +9 -7
- package/src/misc/index.js +7 -14
- package/src/misc/is.js +20 -15
- package/src/misc/match.js +36 -21
- package/src/misc/milliseconds.js +1 -1
- package/src/misc/once.js +24 -0
- package/src/misc/replacer.js +4 -3
- package/src/misc/restoreAfter.js +28 -0
- package/src/misc/retry.js +4 -4
- package/src/misc/reviver.js +3 -2
- package/src/misc/seconds.js +1 -1
- package/src/misc/sleep.js +34 -0
- package/src/misc/throttle.js +4 -2
- package/src/number/clamp.js +11 -0
- package/src/number/format.js +7 -5
- package/src/number/index.js +1 -0
- package/src/number/random.js +14 -4
- package/src/object/deepClone.js +71 -0
- package/src/object/deepEqual.js +57 -0
- package/src/object/entries.js +10 -0
- package/src/object/filter.js +2 -2
- package/src/object/get.js +9 -6
- package/src/object/has.js +16 -8
- package/src/object/index.js +11 -1
- package/src/object/invert.js +22 -0
- package/src/object/isObject.js +9 -0
- package/src/object/isPlainObject.js +9 -0
- package/src/object/isType.js +14 -0
- package/src/object/map.js +2 -2
- package/src/object/merge.js +46 -0
- package/src/object/omit.js +14 -0
- package/src/object/only.js +3 -3
- package/src/object/pick.js +14 -0
- package/src/object/serialize.js +12 -3
- package/src/object/set.js +4 -4
- package/src/object/unserialize.js +30 -5
- package/src/string/capitalFirst.js +2 -2
- package/src/string/capitalWords.js +2 -2
- package/src/string/interpolate.js +3 -3
- package/src/string/slugify.js +11 -19
- package/readme.MD +0 -0
- package/src/misc/restoreBooleanAfter.js +0 -17
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Straylight Agency
|
|
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,469 @@
|
|
|
1
|
+
# @straylightagency/utils
|
|
2
|
+
|
|
3
|
+
A collection of lightweight JavaScript utility functions for everyday development.
|
|
4
|
+
|
|
5
|
+
`@straylightagency/utils` provides a set of reusable helpers for working with arrays, objects, strings, numbers, dates, the DOM, and common JavaScript tasks.
|
|
6
|
+
|
|
7
|
+
The library is designed to keep things simple: small utilities, a straightforward API, and no external dependencies.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- Native ES modules.
|
|
12
|
+
- Zero runtime dependencies.
|
|
13
|
+
- Lightweight and modular.
|
|
14
|
+
- Utilities grouped by category.
|
|
15
|
+
- Import individual helpers or use the main entry point.
|
|
16
|
+
- Designed for modern JavaScript environments.
|
|
17
|
+
|
|
18
|
+
## Table of contents
|
|
19
|
+
|
|
20
|
+
- [Installation](#installation)
|
|
21
|
+
- [Quick start](#quick-start)
|
|
22
|
+
- [Available functions](#available-functions)
|
|
23
|
+
- [Array](#array-1)
|
|
24
|
+
- [Date](#date-1)
|
|
25
|
+
- [DOM](#dom-1)
|
|
26
|
+
- [Miscellaneous](#miscellaneous-1)
|
|
27
|
+
- [Number](#number-1)
|
|
28
|
+
- [Object](#object-1)
|
|
29
|
+
- [String](#string-1)
|
|
30
|
+
- [Contributing](#contributing)
|
|
31
|
+
- [License](#license)
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
Install the package using npm:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npm install @straylightagency/utils
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Quick start
|
|
42
|
+
|
|
43
|
+
Import the helpers you need from the package:
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
import {
|
|
47
|
+
difference,
|
|
48
|
+
deepClone,
|
|
49
|
+
slugify,
|
|
50
|
+
clamp,
|
|
51
|
+
} from '@straylightagency/utils';
|
|
52
|
+
|
|
53
|
+
const values = difference([1, 2, 3], [2]);
|
|
54
|
+
|
|
55
|
+
const copy = deepClone({
|
|
56
|
+
name: 'Straylight',
|
|
57
|
+
services: ['design', 'development'],
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const slug = slugify('Hello World');
|
|
61
|
+
|
|
62
|
+
const value = clamp(120, 0, 100);
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
You can also import helpers from their respective category entry points when available.
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## Available functions
|
|
70
|
+
|
|
71
|
+
### Array
|
|
72
|
+
|
|
73
|
+
Utilities for manipulating and transforming arrays.
|
|
74
|
+
|
|
75
|
+
| Function | Description |
|
|
76
|
+
| --- | --- |
|
|
77
|
+
| [`difference()`](#difference) | Returns the elements present in one array but not the other |
|
|
78
|
+
| [`flatten()`](#flatten) | Flattens nested arrays |
|
|
79
|
+
| [`groupBy()`](#groupby) | Groups array elements by a property or callback |
|
|
80
|
+
| [`initial()`](#initial) | Returns all elements except the last |
|
|
81
|
+
| [`intersection()`](#intersection) | Returns the common elements between arrays |
|
|
82
|
+
| [`last()`](#last) | Returns the last element of an array |
|
|
83
|
+
| [`move()`](#move) | Moves an element from one position to another |
|
|
84
|
+
| [`remove()`](#remove) | Removes an element from an array |
|
|
85
|
+
| [`replace()`](#replace) | Replaces an element in an array |
|
|
86
|
+
| [`shuffle()`](#shuffle) | Randomly shuffles array elements |
|
|
87
|
+
| [`unique()`](#unique) | Returns an array containing unique values |
|
|
88
|
+
|
|
89
|
+
### Date
|
|
90
|
+
|
|
91
|
+
Utilities for working with dates and date-related data.
|
|
92
|
+
|
|
93
|
+
| Function | Description |
|
|
94
|
+
| --- | --- |
|
|
95
|
+
| [`clone()`](#clone) | Clones a Date instance |
|
|
96
|
+
| [`getWeekNumber()`](#getweeknumber) | Returns the week number of a date |
|
|
97
|
+
|
|
98
|
+
### DOM
|
|
99
|
+
|
|
100
|
+
Utilities for interacting with DOM elements and browser events.
|
|
101
|
+
|
|
102
|
+
| Function | Description |
|
|
103
|
+
| --- | --- |
|
|
104
|
+
| [`addEventListeners()`](#addeventlisteners) | Registers multiple event listeners |
|
|
105
|
+
| [`closestElement()`](#closestelement) | Finds the closest matching element |
|
|
106
|
+
| [`delegate()`](#delegate) | Handles events through delegation |
|
|
107
|
+
| [`getElementSizing()`](#getelementsizing) | Retrieves element sizing information |
|
|
108
|
+
| [`getFieldsValues()`](#getfieldsvalues) | Retrieves values from form fields |
|
|
109
|
+
| [`onEachElements()`](#oneachelements) | Executes a callback for matching elements |
|
|
110
|
+
| [`stringToHtml()`](#stringtohtml) | Converts an HTML string into DOM elements |
|
|
111
|
+
| [`unlessElementExists()`](#unlesselementexists) | Executes logic when an element is not found |
|
|
112
|
+
| [`whenIntersect()`](#whenintersect) | Observes element intersection |
|
|
113
|
+
| [`whenMutation()`](#whenmutation) | Observes DOM mutations |
|
|
114
|
+
| [`whenReady()`](#whenready) | Executes a callback when the DOM is ready |
|
|
115
|
+
| [`whenResize()`](#whenresize) | Observes element or window resizing |
|
|
116
|
+
| [`whenScroll()`](#whenscroll) | Observes scrolling |
|
|
117
|
+
| [`whenVisible()`](#whenvisible) | Executes logic when an element becomes visible |
|
|
118
|
+
|
|
119
|
+
### Miscellaneous
|
|
120
|
+
|
|
121
|
+
General-purpose utilities for common JavaScript tasks.
|
|
122
|
+
|
|
123
|
+
| Function | Description |
|
|
124
|
+
| --- | --- |
|
|
125
|
+
| [`attempt()`](#attempt) | Executes a function and handles errors |
|
|
126
|
+
| [`clipboard()`](#clipboard) | Copies content to the clipboard |
|
|
127
|
+
| [`debounce()`](#debounce) | Delays execution until calls stop |
|
|
128
|
+
| [`match()`](#match) | Matches a value against a pattern |
|
|
129
|
+
| [`milliseconds()`](#milliseconds) | Converts a time value to milliseconds |
|
|
130
|
+
| [`once()`](#once) | Ensures a function runs only once |
|
|
131
|
+
| [`restoreAfter()`](#restoreafter) | Restores a value after a delay |
|
|
132
|
+
| [`retry()`](#retry) | Retries an operation |
|
|
133
|
+
| [`seconds()`](#seconds) | Converts a time value to seconds |
|
|
134
|
+
| [`sleep()`](#sleep) | Pauses asynchronous execution |
|
|
135
|
+
| [`throttle()`](#throttle) | Limits the execution frequency of a function |
|
|
136
|
+
|
|
137
|
+
### Number
|
|
138
|
+
|
|
139
|
+
Utilities for working with numbers.
|
|
140
|
+
|
|
141
|
+
| Function | Description |
|
|
142
|
+
| --- | --- |
|
|
143
|
+
| [`clamp()`](#clamp) | Restricts a value to a range |
|
|
144
|
+
| [`format()`](#format) | Formats a number |
|
|
145
|
+
| [`random()`](#random) | Generates a random number |
|
|
146
|
+
|
|
147
|
+
### Object
|
|
148
|
+
|
|
149
|
+
Utilities for manipulating and inspecting objects.
|
|
150
|
+
|
|
151
|
+
| Function | Description |
|
|
152
|
+
| --- | --- |
|
|
153
|
+
| [`deepClone()`](#deepclone) | Creates a deep copy of a value |
|
|
154
|
+
| [`deepEqual()`](#deepequal) | Compares two values for deep equality |
|
|
155
|
+
| [`entries()`](#entries) | Retrieves object entries |
|
|
156
|
+
| [`filter()`](#filter) | Filters object properties |
|
|
157
|
+
| [`get()`](#get) | Retrieves a nested property |
|
|
158
|
+
| [`has()`](#has) | Checks whether a property exists |
|
|
159
|
+
| [`invert()`](#invert) | Reverses object keys and values |
|
|
160
|
+
| [`isObject()`](#isobject) | Checks whether a value is an object |
|
|
161
|
+
| [`isPlainObject()`](#isplainobject) | Checks whether a value is a plain object |
|
|
162
|
+
| [`isType()`](#istype) | Checks the type of a value |
|
|
163
|
+
| [`map()`](#map) | Maps object properties |
|
|
164
|
+
| [`merge()`](#merge) | Merges objects |
|
|
165
|
+
| [`omit()`](#omit) | Omits specified properties |
|
|
166
|
+
| [`only()`](#only) | Keeps specified properties |
|
|
167
|
+
| [`pick()`](#pick) | Selects specified properties |
|
|
168
|
+
| [`serialize()`](#serialize) | Serializes a value |
|
|
169
|
+
| [`set()`](#set) | Sets a nested property |
|
|
170
|
+
| [`unserialize()`](#unserialize) | Restores a serialized value |
|
|
171
|
+
|
|
172
|
+
### String
|
|
173
|
+
|
|
174
|
+
Utilities for manipulating strings.
|
|
175
|
+
|
|
176
|
+
| Function | Description |
|
|
177
|
+
| --- | --- |
|
|
178
|
+
| [`capitalFirst()`](#capitalfirst) | Capitalizes the first character |
|
|
179
|
+
| [`capitalWords()`](#capitalwords) | Capitalizes words in a string |
|
|
180
|
+
| [`interpolate()`](#interpolate) | Interpolates values into a string |
|
|
181
|
+
| [`slugify()`](#slugify) | Converts a string into a slug |
|
|
182
|
+
|
|
183
|
+
## Array
|
|
184
|
+
|
|
185
|
+
### `difference()`
|
|
186
|
+
|
|
187
|
+
Returns the elements from the first array that are not present in the second.
|
|
188
|
+
|
|
189
|
+
### `flatten()`
|
|
190
|
+
|
|
191
|
+
Flattens a nested array structure into a single array.
|
|
192
|
+
|
|
193
|
+
### `groupBy()`
|
|
194
|
+
|
|
195
|
+
Groups array elements according to a property or grouping function.
|
|
196
|
+
|
|
197
|
+
### `initial()`
|
|
198
|
+
|
|
199
|
+
Returns a new array containing every element except the last one.
|
|
200
|
+
|
|
201
|
+
### `intersection()`
|
|
202
|
+
|
|
203
|
+
Returns the elements shared by two arrays.
|
|
204
|
+
|
|
205
|
+
### `last()`
|
|
206
|
+
|
|
207
|
+
Returns the last element of an array.
|
|
208
|
+
|
|
209
|
+
### `move()`
|
|
210
|
+
|
|
211
|
+
Moves an element from one index to another.
|
|
212
|
+
|
|
213
|
+
### `remove()`
|
|
214
|
+
|
|
215
|
+
Removes an element from an array.
|
|
216
|
+
|
|
217
|
+
### `replace()`
|
|
218
|
+
|
|
219
|
+
Replaces an element in an array.
|
|
220
|
+
|
|
221
|
+
### `shuffle()`
|
|
222
|
+
|
|
223
|
+
Randomly reorders the elements of an array.
|
|
224
|
+
|
|
225
|
+
### `unique()`
|
|
226
|
+
|
|
227
|
+
Returns an array with duplicate values removed.
|
|
228
|
+
|
|
229
|
+
## Date
|
|
230
|
+
|
|
231
|
+
### `clone()`
|
|
232
|
+
|
|
233
|
+
Creates a copy of a Date instance.
|
|
234
|
+
|
|
235
|
+
### `getWeekNumber()`
|
|
236
|
+
|
|
237
|
+
Returns the week number corresponding to a date.
|
|
238
|
+
|
|
239
|
+
## DOM
|
|
240
|
+
|
|
241
|
+
### `addEventListeners()`
|
|
242
|
+
|
|
243
|
+
Registers multiple event listeners on an element.
|
|
244
|
+
|
|
245
|
+
### `closestElement()`
|
|
246
|
+
|
|
247
|
+
Finds the closest ancestor matching a selector.
|
|
248
|
+
|
|
249
|
+
### `delegate()`
|
|
250
|
+
|
|
251
|
+
Registers delegated event listeners.
|
|
252
|
+
|
|
253
|
+
### `getElementSizing()`
|
|
254
|
+
|
|
255
|
+
Retrieves sizing information from a DOM element.
|
|
256
|
+
|
|
257
|
+
### `getFieldsValues()`
|
|
258
|
+
|
|
259
|
+
Collects values from form fields.
|
|
260
|
+
|
|
261
|
+
### `onEachElements()`
|
|
262
|
+
|
|
263
|
+
Runs a callback for each element matching a selector.
|
|
264
|
+
|
|
265
|
+
### `stringToHtml()`
|
|
266
|
+
|
|
267
|
+
Converts an HTML string into DOM elements.
|
|
268
|
+
|
|
269
|
+
### `unlessElementExists()`
|
|
270
|
+
|
|
271
|
+
Runs a callback or operation if the target element exists.
|
|
272
|
+
|
|
273
|
+
### `whenIntersect()`
|
|
274
|
+
|
|
275
|
+
Observes when an element intersects with a target or viewport.
|
|
276
|
+
|
|
277
|
+
### `whenMutation()`
|
|
278
|
+
|
|
279
|
+
Observes changes in the DOM using a MutationObserver.
|
|
280
|
+
|
|
281
|
+
### `whenReady()`
|
|
282
|
+
|
|
283
|
+
Runs a callback once the document is ready.
|
|
284
|
+
|
|
285
|
+
### `whenResize()`
|
|
286
|
+
|
|
287
|
+
Observes resize events on a target element or the window.
|
|
288
|
+
|
|
289
|
+
### `whenScroll()`
|
|
290
|
+
|
|
291
|
+
Observes scroll events.
|
|
292
|
+
|
|
293
|
+
### `whenVisible()`
|
|
294
|
+
|
|
295
|
+
Runs logic when an element becomes visible.
|
|
296
|
+
|
|
297
|
+
## Miscellaneous
|
|
298
|
+
|
|
299
|
+
### `attempt()`
|
|
300
|
+
|
|
301
|
+
Executes a function while providing a way to handle potential errors.
|
|
302
|
+
|
|
303
|
+
### `clipboard()`
|
|
304
|
+
|
|
305
|
+
Copies content to the clipboard.
|
|
306
|
+
|
|
307
|
+
### `debounce()`
|
|
308
|
+
|
|
309
|
+
Delays the execution of a function until a specified period has elapsed without further calls.
|
|
310
|
+
|
|
311
|
+
### `match()`
|
|
312
|
+
|
|
313
|
+
Matches a value against a pattern or condition.
|
|
314
|
+
|
|
315
|
+
### `milliseconds()`
|
|
316
|
+
|
|
317
|
+
Converts a time value into milliseconds.
|
|
318
|
+
|
|
319
|
+
### `once()`
|
|
320
|
+
|
|
321
|
+
Creates a function that can only be executed once.
|
|
322
|
+
|
|
323
|
+
### `restoreAfter()`
|
|
324
|
+
|
|
325
|
+
Restores a value or state after a specified delay.
|
|
326
|
+
|
|
327
|
+
### `retry()`
|
|
328
|
+
|
|
329
|
+
Retries an operation according to the provided configuration.
|
|
330
|
+
|
|
331
|
+
### `seconds()`
|
|
332
|
+
|
|
333
|
+
Converts a time value into seconds.
|
|
334
|
+
|
|
335
|
+
### `sleep()`
|
|
336
|
+
|
|
337
|
+
Returns a Promise that resolves after a specified delay.
|
|
338
|
+
|
|
339
|
+
### `throttle()`
|
|
340
|
+
|
|
341
|
+
Ensures a function is not executed more frequently than a specified interval.
|
|
342
|
+
|
|
343
|
+
## Number
|
|
344
|
+
|
|
345
|
+
### `clamp()`
|
|
346
|
+
|
|
347
|
+
Restricts a number to a specified minimum and maximum range.
|
|
348
|
+
|
|
349
|
+
### `format()`
|
|
350
|
+
|
|
351
|
+
Formats a number according to the provided options.
|
|
352
|
+
|
|
353
|
+
### `random()`
|
|
354
|
+
|
|
355
|
+
Generates a random number within the specified range.
|
|
356
|
+
|
|
357
|
+
## Object
|
|
358
|
+
|
|
359
|
+
### `deepClone()`
|
|
360
|
+
|
|
361
|
+
Creates a deep copy of a value.
|
|
362
|
+
|
|
363
|
+
### `deepEqual()`
|
|
364
|
+
|
|
365
|
+
Compares two values to determine whether they are deeply equal.
|
|
366
|
+
|
|
367
|
+
### `entries()`
|
|
368
|
+
|
|
369
|
+
Returns the entries of an object.
|
|
370
|
+
|
|
371
|
+
### `filter()`
|
|
372
|
+
|
|
373
|
+
Filters an object's properties according to a condition.
|
|
374
|
+
|
|
375
|
+
### `get()`
|
|
376
|
+
|
|
377
|
+
Retrieves a nested property using a path.
|
|
378
|
+
|
|
379
|
+
### `has()`
|
|
380
|
+
|
|
381
|
+
Checks whether an object contains a property at a given path.
|
|
382
|
+
|
|
383
|
+
### `invert()`
|
|
384
|
+
|
|
385
|
+
Creates a new object with keys and values reversed.
|
|
386
|
+
|
|
387
|
+
### `isObject()`
|
|
388
|
+
|
|
389
|
+
Checks whether a value is an object.
|
|
390
|
+
|
|
391
|
+
### `isPlainObject()`
|
|
392
|
+
|
|
393
|
+
Checks whether a value is a plain object.
|
|
394
|
+
|
|
395
|
+
### `isType()`
|
|
396
|
+
|
|
397
|
+
Checks whether a value matches a specified type.
|
|
398
|
+
|
|
399
|
+
### `map()`
|
|
400
|
+
|
|
401
|
+
Transforms the properties of an object using a callback.
|
|
402
|
+
|
|
403
|
+
### `merge()`
|
|
404
|
+
|
|
405
|
+
Merges multiple objects.
|
|
406
|
+
|
|
407
|
+
### `omit()`
|
|
408
|
+
|
|
409
|
+
Creates an object without the specified properties.
|
|
410
|
+
|
|
411
|
+
### `only()`
|
|
412
|
+
|
|
413
|
+
Creates an object containing only the specified properties.
|
|
414
|
+
|
|
415
|
+
### `pick()`
|
|
416
|
+
|
|
417
|
+
Selects specific properties from an object.
|
|
418
|
+
|
|
419
|
+
### `serialize()`
|
|
420
|
+
|
|
421
|
+
Serializes a value into a format suitable for storage or transmission.
|
|
422
|
+
|
|
423
|
+
### `set()`
|
|
424
|
+
|
|
425
|
+
Sets a nested property using a path.
|
|
426
|
+
|
|
427
|
+
### `unserialize()`
|
|
428
|
+
|
|
429
|
+
Restores a value from its serialized representation.
|
|
430
|
+
|
|
431
|
+
## String
|
|
432
|
+
|
|
433
|
+
### `capitalFirst()`
|
|
434
|
+
|
|
435
|
+
Capitalizes the first character of a string.
|
|
436
|
+
|
|
437
|
+
### `capitalWords()`
|
|
438
|
+
|
|
439
|
+
Capitalizes words in a string.
|
|
440
|
+
|
|
441
|
+
### `interpolate()`
|
|
442
|
+
|
|
443
|
+
Replaces placeholders in a string with values.
|
|
444
|
+
|
|
445
|
+
### `slugify()`
|
|
446
|
+
|
|
447
|
+
Converts a string into a URL-friendly slug.
|
|
448
|
+
|
|
449
|
+
---
|
|
450
|
+
|
|
451
|
+
## Contributing
|
|
452
|
+
|
|
453
|
+
Contributions, bug reports, and feature requests are welcome.
|
|
454
|
+
|
|
455
|
+
- Fork the repository.
|
|
456
|
+
- Create a feature branch.
|
|
457
|
+
- Make your changes.
|
|
458
|
+
- Test the affected functionality.
|
|
459
|
+
- Submit a pull request.
|
|
460
|
+
|
|
461
|
+
Repository: https://github.com/straylightagency/js-utils
|
|
462
|
+
|
|
463
|
+
Report issues: https://github.com/straylightagency/js-utils/issues
|
|
464
|
+
|
|
465
|
+
## License
|
|
466
|
+
|
|
467
|
+
`@straylightagency/utils` is released under the MIT License.
|
|
468
|
+
|
|
469
|
+
See the [LICENSE](./LICENSE) file for details.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@straylightagency/utils",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"description": "A collection of lightweight JavaScript utility functions for everyday development.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"exports": {
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
"./dom": "./src/dom/index.js",
|
|
12
12
|
"./misc": "./src/misc/index.js",
|
|
13
13
|
"./object": "./src/object/index.js",
|
|
14
|
-
"./string": "./src/string/index.js"
|
|
14
|
+
"./string": "./src/string/index.js",
|
|
15
|
+
"./number": "./src/number/index.js"
|
|
15
16
|
},
|
|
16
17
|
"files": [
|
|
17
18
|
"src"
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns values from `array` that are not present in `other`.
|
|
3
|
+
* Uses SameValueZero equality and preserves the order and duplicates
|
|
4
|
+
* from the first array.
|
|
5
|
+
*
|
|
6
|
+
* @param {Array} arrau
|
|
7
|
+
* @param {Array} other
|
|
8
|
+
* @returns {Array}
|
|
9
|
+
*/
|
|
10
|
+
export default function difference(array, other) {
|
|
11
|
+
const excluded = new Set(other);
|
|
12
|
+
return array.filter(value => !excluded.has(value));
|
|
13
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flattens nested arrays to the requested depth (Infinity by default).
|
|
3
|
+
* Does not mutate the input.
|
|
4
|
+
*
|
|
5
|
+
* @param {Array} array
|
|
6
|
+
* @param {Number} depth
|
|
7
|
+
* @returns {Array}
|
|
8
|
+
*/
|
|
9
|
+
export default function flatten(array, depth = Infinity) {
|
|
10
|
+
if (!Array.isArray(array)) {
|
|
11
|
+
throw new TypeError('flatten() expects an array');
|
|
12
|
+
}
|
|
13
|
+
if (depth !== Infinity && (!Number.isInteger(depth) || depth < 0)) {
|
|
14
|
+
throw new RangeError('flatten() depth must be a non-negative integer or Infinity');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return array.flat(depth);
|
|
18
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Groups array items by a property name or an iteratee.
|
|
3
|
+
* Returns an object whose groups are arrays; input is not mutated.
|
|
4
|
+
*
|
|
5
|
+
* @param {Array} array
|
|
6
|
+
* @param {Function|string} iteratee
|
|
7
|
+
* @returns {Array}
|
|
8
|
+
*/
|
|
9
|
+
export default function groupBy(array, iteratee) {
|
|
10
|
+
if (typeof iteratee !== 'function' && typeof iteratee !== 'string') {
|
|
11
|
+
throw new TypeError('groupBy() iteratee must be a function or property name');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const result = Object.create(null);
|
|
15
|
+
|
|
16
|
+
for (const item of array) {
|
|
17
|
+
const key = String(typeof iteratee === 'function' ? iteratee(item) : item?.[iteratee]);
|
|
18
|
+
(result[key] ??= []).push(item);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return result;
|
|
22
|
+
}
|
package/src/array/index.js
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
export { default as last } from "./last.js";
|
|
2
2
|
export { default as initial } from "./initial.js";
|
|
3
3
|
export { default as remove } from "./remove.js";
|
|
4
|
-
export { default as replace } from "./replace.js";
|
|
4
|
+
export { default as replace } from "./replace.js";
|
|
5
|
+
export { default as difference } from './difference.js';
|
|
6
|
+
export { default as flatten } from './flatten.js';
|
|
7
|
+
export { default as groupBy } from './groupBy.js';
|
|
8
|
+
export { default as intersection } from './intersection.js';
|
|
9
|
+
export { default as move } from './move.js';
|
|
10
|
+
export { default as shuffle } from './shuffle.js';
|
|
11
|
+
export { default as unique } from './unique.js';
|
package/src/array/initial.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Return the array without the last element.
|
|
3
3
|
*
|
|
4
|
-
* @param
|
|
5
|
-
* @returns {
|
|
4
|
+
* @param {Array} array
|
|
5
|
+
* @returns {Array}
|
|
6
6
|
*/
|
|
7
7
|
export default function initial(array) {
|
|
8
|
-
const length = array == null ? 0 : array.length
|
|
9
|
-
return length ? array.slice( 0, -1 ) : []
|
|
8
|
+
const length = array == null ? 0 : array.length;
|
|
9
|
+
return length ? array.slice( 0, -1 ) : [];
|
|
10
10
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns unique values present in both arrays.
|
|
3
|
+
* Preserves the order of the first array; uses SameValueZero equality.
|
|
4
|
+
*
|
|
5
|
+
* @param {Array} array
|
|
6
|
+
* @param {Array} other
|
|
7
|
+
* @returns {Array}
|
|
8
|
+
*/
|
|
9
|
+
export default function intersection(array, other) {
|
|
10
|
+
const candidates = new Set(other);
|
|
11
|
+
const included = new Set();
|
|
12
|
+
|
|
13
|
+
return array.filter(value => {
|
|
14
|
+
if (!candidates.has(value) || included.has(value)) return false;
|
|
15
|
+
included.add(value);
|
|
16
|
+
return true;
|
|
17
|
+
});
|
|
18
|
+
}
|
package/src/array/last.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Return the last element of an array.
|
|
3
3
|
*
|
|
4
|
-
* @param {
|
|
4
|
+
* @param {Array} array
|
|
5
5
|
* @returns {*|undefined}
|
|
6
6
|
*/
|
|
7
7
|
export default function last(array) {
|
|
8
|
-
const length = array == null ? 0 : array.length
|
|
9
|
-
|
|
10
|
-
return length ? array[ length - 1 ] : undefined
|
|
8
|
+
const length = array == null ? 0 : array.length;
|
|
9
|
+
return length ? array[ length - 1 ] : undefined;
|
|
11
10
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a copy of `array` with the item at `from` moved to `to`.
|
|
3
|
+
* Negative indices count from the end. Out-of-range indices are clamped.
|
|
4
|
+
*
|
|
5
|
+
* @param {Array} array
|
|
6
|
+
* @param {Number} from
|
|
7
|
+
* @param {Number} to
|
|
8
|
+
* @returns {Array}
|
|
9
|
+
*/
|
|
10
|
+
export default function move(array, from, to) {
|
|
11
|
+
const result = [...array];
|
|
12
|
+
const length = result.length;
|
|
13
|
+
|
|
14
|
+
const normalize = index => {
|
|
15
|
+
index = Math.trunc(index);
|
|
16
|
+
return Math.max(0, Math.min(length - 1, index < 0 ? length + index : index));
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
if (!length) return result;
|
|
20
|
+
|
|
21
|
+
const start = normalize(from);
|
|
22
|
+
const end = normalize(to);
|
|
23
|
+
const [item] = result.splice(start, 1);
|
|
24
|
+
result.splice(end, 0, item);
|
|
25
|
+
|
|
26
|
+
return result;
|
|
27
|
+
}
|
package/src/array/remove.js
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Remove a value from the given array, returning a copy.
|
|
3
3
|
*
|
|
4
|
-
* @param {
|
|
5
|
-
* @param {
|
|
6
|
-
* @returns {
|
|
4
|
+
* @param {Array} array
|
|
5
|
+
* @param {any} value
|
|
6
|
+
* @returns {Array}
|
|
7
7
|
*/
|
|
8
8
|
export default function remove(array, value) {
|
|
9
|
-
const index = array.indexOf(
|
|
9
|
+
const index = array.indexOf(value);
|
|
10
10
|
|
|
11
|
-
if (
|
|
12
|
-
array
|
|
11
|
+
if (index === -1) {
|
|
12
|
+
return [...array];
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
return
|
|
16
|
-
|
|
15
|
+
return [
|
|
16
|
+
...array.slice(0, index),
|
|
17
|
+
...array.slice(index + 1)
|
|
18
|
+
];
|
|
19
|
+
}
|