@webframe/utils 0.0.0-stage → 0.1.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 +10 -2
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +35 -0
- package/dist/index.js.map +1 -0
- package/package.json +27 -4
- package/src/index.ts +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 tw050x
|
|
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
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @webframe/utils
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Small helpers for any JavaScript runtime: Node, browsers, workers.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm install @webframe/utils
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
The full documentation is at [webframe.tw050x.net/packages/utils](https://webframe.tw050x.net/packages/utils).
|
|
10
|
+
|
|
11
|
+
Part of [webframe](https://webframe.tw050x.net), MIT licensed.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** Re-exported from `@webframe/assert-unreachable`. */
|
|
2
|
+
export { assertUnreachable } from '@webframe/assert-unreachable';
|
|
3
|
+
/**
|
|
4
|
+
* Normalises an email address: trimmed, in lower case, without a `+tag`.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* normaliseEmailAddress(' User+news@Mail.com '); // 'user@mail.com'
|
|
8
|
+
*/
|
|
9
|
+
export declare const normaliseEmailAddress: (email: string) => string;
|
|
10
|
+
/**
|
|
11
|
+
* Sets a value at a dot path in a JSON string, making the objects on the way as needed.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* updateJSON('{"menu":{}}', 'menu.open', true); // '{"menu":{"open":true}}'
|
|
15
|
+
*/
|
|
16
|
+
export declare function updateJSON(jsonString: string, dotPath: string, value: unknown): string;
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AAEjE;;;;;GAKG;AACH,eAAO,MAAM,qBAAqB,GAAI,OAAO,MAAM,KAAG,MAErD,CAAA;AAID;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,MAAM,CAgBtF"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** Re-exported from `@webframe/assert-unreachable`. */
|
|
2
|
+
export { assertUnreachable } from '@webframe/assert-unreachable';
|
|
3
|
+
/**
|
|
4
|
+
* Normalises an email address: trimmed, in lower case, without a `+tag`.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* normaliseEmailAddress(' User+news@Mail.com '); // 'user@mail.com'
|
|
8
|
+
*/
|
|
9
|
+
export const normaliseEmailAddress = (email) => {
|
|
10
|
+
return email.trim().toLowerCase().replace(/\+[^@]*@/, '@');
|
|
11
|
+
};
|
|
12
|
+
const UNSAFE_KEYS = new Set(['__proto__', 'prototype', 'constructor']);
|
|
13
|
+
/**
|
|
14
|
+
* Sets a value at a dot path in a JSON string, making the objects on the way as needed.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* updateJSON('{"menu":{}}', 'menu.open', true); // '{"menu":{"open":true}}'
|
|
18
|
+
*/
|
|
19
|
+
export function updateJSON(jsonString, dotPath, value) {
|
|
20
|
+
const keys = dotPath.split('.');
|
|
21
|
+
if (keys.some((key) => UNSAFE_KEYS.has(key))) {
|
|
22
|
+
throw new Error(`Invalid dot path: ${dotPath}`);
|
|
23
|
+
}
|
|
24
|
+
const root = JSON.parse(jsonString || '{}');
|
|
25
|
+
let focus = root;
|
|
26
|
+
for (const key of keys.slice(0, -1)) {
|
|
27
|
+
if (typeof focus[key] !== 'object' || focus[key] === null) {
|
|
28
|
+
focus[key] = {};
|
|
29
|
+
}
|
|
30
|
+
focus = focus[key];
|
|
31
|
+
}
|
|
32
|
+
focus[keys.at(-1)] = value;
|
|
33
|
+
return JSON.stringify(root);
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACv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AAC,SAAS,CAAC,IAAI,CAAC,CAAC;AAC9B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webframe/utils",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Small helpers for any JavaScript runtime.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"src"
|
|
16
|
+
],
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@webframe/assert-unreachable": "^0.3.0"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/tw050x/webframe.git",
|
|
23
|
+
"directory": "packages/universal/utils"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://webframe.tw050x.net/packages/utils",
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
}
|
|
29
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/** Re-exported from `@webframe/assert-unreachable`. */
|
|
2
|
+
export { assertUnreachable } from '@webframe/assert-unreachable';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Normalises an email address: trimmed, in lower case, without a `+tag`.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* normaliseEmailAddress(' User+news@Mail.com '); // 'user@mail.com'
|
|
9
|
+
*/
|
|
10
|
+
export const normaliseEmailAddress = (email: string): string => {
|
|
11
|
+
return email.trim().toLowerCase().replace(/\+[^@]*@/, '@');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const UNSAFE_KEYS = new Set(['__proto__', 'prototype', 'constructor']);
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Sets a value at a dot path in a JSON string, making the objects on the way as needed.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* updateJSON('{"menu":{}}', 'menu.open', true); // '{"menu":{"open":true}}'
|
|
21
|
+
*/
|
|
22
|
+
export function updateJSON(jsonString: string, dotPath: string, value: unknown): string {
|
|
23
|
+
const keys = dotPath.split('.');
|
|
24
|
+
if (keys.some((key) => UNSAFE_KEYS.has(key))) {
|
|
25
|
+
throw new Error(`Invalid dot path: ${dotPath}`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const root = JSON.parse(jsonString || '{}');
|
|
29
|
+
let focus = root;
|
|
30
|
+
for (const key of keys.slice(0, -1)) {
|
|
31
|
+
if (typeof focus[key] !== 'object' || focus[key] === null) {
|
|
32
|
+
focus[key] = {};
|
|
33
|
+
}
|
|
34
|
+
focus = focus[key];
|
|
35
|
+
}
|
|
36
|
+
focus[keys.at(-1)!] = value;
|
|
37
|
+
return JSON.stringify(root);
|
|
38
|
+
}
|