@aglyn/shared-util-tools 1.0.0-beta.143 → 1.0.0-beta.144
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/README.md +74 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,7 +1,78 @@
|
|
|
1
1
|
# @aglyn/shared-util-tools
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
General-purpose TypeScript helpers: type guards, array and object utilities, deep get/set, cloning, JSON and base64 serialization, plus a few larger standalone modules (a linear-time regex engine, an AES-GCM secret box, WCAG contrast math, HTML escaping). It is a dependency of most Aglyn packages and is usable on its own.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
> Beta. Published from the Aglyn monorepo under the `beta` dist-tag; APIs can change between beta releases.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
npm install @aglyn/shared-util-tools@beta
|
|
10
|
+
|
|
11
|
+
No peer dependencies.
|
|
12
|
+
|
|
13
|
+
## Read this first: the root import patches `Array.prototype`
|
|
14
|
+
|
|
15
|
+
`package.json` lists `./src/lib/array/array-overrides.*` under `sideEffects`, and the package root re-exports that module. So `import ... from '@aglyn/shared-util-tools'` runs it, and bundlers are told not to drop it.
|
|
16
|
+
|
|
17
|
+
What it does, exactly: using `Object.defineProperty` it defines eight methods on `Array.prototype`, each non-enumerable, writable and configurable, and it declares them on the global `Array<T>` TypeScript interface. It defines them unconditionally, so a same-named property already present would be overwritten.
|
|
18
|
+
|
|
19
|
+
| Method | Behavior |
|
|
20
|
+
| -- | -- |
|
|
21
|
+
| `$_cloneShallow()` | shallow copy (lodash `clone`) |
|
|
22
|
+
| `$_cloneDeep()` | deep copy (lodash `cloneDeep`) |
|
|
23
|
+
| `$_moveAtIndex(index, newIndex)` | moves an item, mutating the array; appends when `newIndex` is `NaN` |
|
|
24
|
+
| `$_pushAtIndex(index, ...items)` | inserts at `index`, mutating; appends when `index` is `NaN` |
|
|
25
|
+
| `$_removeItem(item)` | removes the first strictly-equal item, mutating |
|
|
26
|
+
| `$_removeAtIndex(index)` | removes one item, mutating |
|
|
27
|
+
| `$_replaceAtIndex(index, item)` | replaces one item, mutating |
|
|
28
|
+
| `$_truthy()` | returns a new array without falsy items |
|
|
29
|
+
|
|
30
|
+
The mutating methods return the same array. Because the properties are non-enumerable they do not show up in `for...in`, `Object.keys` or `JSON.stringify`. No built-in method is replaced. (The global type declaration gives `$_pushAtIndex` the signature `(index, newIndex)`; the implementation is `(index, ...items)`.)
|
|
31
|
+
|
|
32
|
+
To avoid the patch, import by subpath instead of from the root. No other module in this package imports `array-overrides`, and every helper the patch wraps is available as a plain function, for example `@aglyn/shared-util-tools/array/array-move-at-index`. Note that `@aglyn/shared-util-dom` and `@aglyn/shared-util-errors` import the root, so they apply the patch too.
|
|
33
|
+
|
|
34
|
+
## What's in it
|
|
35
|
+
|
|
36
|
+
From the package root:
|
|
37
|
+
|
|
38
|
+
- Guards, all `_`-prefixed: `_isArr`, `_isObj`, `_isNum`, `_isBool`, `_isNull`, `_isUndOrNull`, `_isStrEmpty`, `_isPromiseLike`, `_hasOwnProperty` and the rest of `src/lib/guards/lib`.
|
|
39
|
+
- Arrays: `arraySafe`, `arrayFrom`, `arrayFromLength`, `arrayCopyShallow`, `arrayCopyDeep`, `arrayMoveAtIndex`, `arrayPushAtIndex`, `arrayRemoveItem`, `arrayRemoveAtIndex`, `arrayUpdate`, `arrayUpdateAtIndex`, `arraySortBy`, `arraySortByDeepProperty`, `arrayOfEntriesToObject`.
|
|
40
|
+
- Objects: `objectGetDeepProperty`, `objectSetDeepProperty`, `objectClone`, `objectCloneDeep`, `objectDeleteProperty`, `objectRemap`, `objectSafe`, `objectUpdate`, `objectGetKeysAndSymbolProperties`, `getProperty`, `getStaticField`.
|
|
41
|
+
- Values: `truthy`, `truth`, `falsy`, `noop`, `str`, `trim`, `length`, `toNum`, `numberToHexadecimal`, `numberFromHexadecimal`, `numeronym`, `splitDisplayName`, and the `compare` operator helper with its operator types.
|
|
42
|
+
- Bit flags: `bitwiseHasAttribute`, `bitwiseHasAllAttributes`, `bitwiseHasOnlyAttributes`.
|
|
43
|
+
- Serialization: `jsonSerialize`, `jsonDeserialize`, `base64IsomorphicEncode`, `base64IsomorphicDecode`.
|
|
44
|
+
- Classes and functions: `applyMixins`, `createChainedFunction`, `getDisplayName`, `interopDefault`, `noSideEffects`, `cloneDeep`, `copyShallow`, `Crud`, `Normalized`, `CSS`.
|
|
45
|
+
|
|
46
|
+
By subpath only (kept off the root so they are not pulled into every consumer's bundle):
|
|
47
|
+
|
|
48
|
+
- `@aglyn/shared-util-tools/linear-regex` - `compileLinearPattern`, `compileLinearTest` and their `explain*` counterparts. A regex engine that parses the pattern itself and runs an NFA simulation, so matching time is bounded by input length times program length. Intended for patterns written by someone you do not trust. Backreferences and lookaround are not supported.
|
|
49
|
+
- `@aglyn/shared-util-tools/secret-box` - `createSecretBoxKey`, `parseSecretBoxKeyring`, `sealSecret`, `openSecret`, `needsReseal`, `sealedSecretKeyId`, `SecretBoxError`. AES-256-GCM sealing of short secrets with key ids, key rotation and an optional authenticated context. Imports `node:crypto`; server-only.
|
|
50
|
+
- `@aglyn/shared-util-tools/escape-html` - `escapeHtml(value)`, escaping `& < > " '`.
|
|
51
|
+
- `@aglyn/shared-util-tools/contrast` - `relativeLuminanceOfRgb`, `rgbChannelsOfHex`, `prefersDarkInk` and related sRGB helpers.
|
|
52
|
+
- `@aglyn/shared-util-tools/serialize/php-serial-*` - wrappers over `php-serialize`.
|
|
53
|
+
|
|
54
|
+
## Usage
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
// Subpath imports: no prototype patch.
|
|
58
|
+
import { objectGetDeepProperty } from '@aglyn/shared-util-tools/object/object-get-deep-property'
|
|
59
|
+
import { arraySafe } from '@aglyn/shared-util-tools/array/array-safe'
|
|
60
|
+
import { escapeHtml } from '@aglyn/shared-util-tools/escape-html'
|
|
61
|
+
|
|
62
|
+
const city = objectGetDeepProperty<string>(user, 'address.city')
|
|
63
|
+
const tags = arraySafe(input.tags) // always an array
|
|
64
|
+
const html = `<p>${escapeHtml(comment)}</p>`
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
// Root import: same helpers, and Array.prototype gains the $_ methods.
|
|
69
|
+
import { _isArr, truthy } from '@aglyn/shared-util-tools'
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## How it fits
|
|
73
|
+
|
|
74
|
+
A `shared` package: generic, with no knowledge of Aglyn's model. Shared packages may only import other shared packages; this one depends on `@aglyn/shared-data-types`, `lodash-es` and `php-serialize`. Most of the monorepo depends on it, including `@aglyn/aglyn`, `@aglyn/besigner`, the shared UI packages and several plugins.
|
|
75
|
+
|
|
76
|
+
## License
|
|
77
|
+
|
|
78
|
+
Apache-2.0. Source: https://github.com/aglyn/aglyn/tree/main/libs/shared/util/tools
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aglyn/shared-util-tools",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.144",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"homepage": "https://aglyn.com",
|
|
6
6
|
"repository": {
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"./package.json": "./package.json"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@aglyn/shared-data-types": "1.0.0-beta.
|
|
28
|
+
"@aglyn/shared-data-types": "1.0.0-beta.144",
|
|
29
29
|
"@swc/helpers": "0.5.23",
|
|
30
30
|
"lodash-es": "^4.18.1",
|
|
31
31
|
"php-serialize": "^5.1.3"
|