@webdeveric/utils 1.4.0 → 1.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/dist/cjs/index.js +2 -0
- package/dist/cjs/lazyProp.js +29 -0
- package/dist/cjs/lazyRecord.js +30 -0
- package/dist/mjs/index.js +2 -0
- package/dist/mjs/lazyProp.js +26 -0
- package/dist/mjs/lazyRecord.js +27 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/lazyProp.d.ts +13 -0
- package/dist/types/lazyRecord.d.ts +19 -0
- package/package.json +5 -5
package/dist/cjs/index.js
CHANGED
|
@@ -68,6 +68,8 @@ __exportStar(require("./isEmpty.js"), exports);
|
|
|
68
68
|
__exportStar(require("./iterateForever.js"), exports);
|
|
69
69
|
__exportStar(require("./joinStrings.js"), exports);
|
|
70
70
|
__exportStar(require("./jsonParse.js"), exports);
|
|
71
|
+
__exportStar(require("./lazyProp.js"), exports);
|
|
72
|
+
__exportStar(require("./lazyRecord.js"), exports);
|
|
71
73
|
__exportStar(require("./looksLikeURL.js"), exports);
|
|
72
74
|
__exportStar(require("./memo.js"), exports);
|
|
73
75
|
__exportStar(require("./normalize.js"), exports);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.lazyProp = lazyProp;
|
|
4
|
+
/**
|
|
5
|
+
* Create a property descriptor that lazily computes its value.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const data = {};
|
|
10
|
+
*
|
|
11
|
+
* Object.defineProperty(data, 'value', lazyProp('value', () => {
|
|
12
|
+
* return Math.random();
|
|
13
|
+
* }));
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
function lazyProp(key, getter) {
|
|
17
|
+
return {
|
|
18
|
+
configurable: true,
|
|
19
|
+
get() {
|
|
20
|
+
const value = getter();
|
|
21
|
+
Object.defineProperty(this, key, {
|
|
22
|
+
value,
|
|
23
|
+
writable: true,
|
|
24
|
+
configurable: true,
|
|
25
|
+
});
|
|
26
|
+
return value;
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.lazyRecord = lazyRecord;
|
|
4
|
+
const lazyProp_js_1 = require("./lazyProp.js");
|
|
5
|
+
/**
|
|
6
|
+
* Create an object whose properties are computed lazily.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const data = lazyRecord({
|
|
11
|
+
* name: 'Name',
|
|
12
|
+
* now() {
|
|
13
|
+
* return Date.now();
|
|
14
|
+
* },
|
|
15
|
+
* });
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
function lazyRecord(properties) {
|
|
19
|
+
const record = Object.create(null);
|
|
20
|
+
for (const [key, getter] of Object.entries(properties)) {
|
|
21
|
+
Object.defineProperty(record, key, typeof getter === 'function'
|
|
22
|
+
? (0, lazyProp_js_1.lazyProp)(key, getter)
|
|
23
|
+
: {
|
|
24
|
+
value: getter,
|
|
25
|
+
writable: true,
|
|
26
|
+
configurable: true,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return record;
|
|
30
|
+
}
|
package/dist/mjs/index.js
CHANGED
|
@@ -52,6 +52,8 @@ export * from './isEmpty.js';
|
|
|
52
52
|
export * from './iterateForever.js';
|
|
53
53
|
export * from './joinStrings.js';
|
|
54
54
|
export * from './jsonParse.js';
|
|
55
|
+
export * from './lazyProp.js';
|
|
56
|
+
export * from './lazyRecord.js';
|
|
55
57
|
export * from './looksLikeURL.js';
|
|
56
58
|
export * from './memo.js';
|
|
57
59
|
export * from './normalize.js';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a property descriptor that lazily computes its value.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* const data = {};
|
|
7
|
+
*
|
|
8
|
+
* Object.defineProperty(data, 'value', lazyProp('value', () => {
|
|
9
|
+
* return Math.random();
|
|
10
|
+
* }));
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export function lazyProp(key, getter) {
|
|
14
|
+
return {
|
|
15
|
+
configurable: true,
|
|
16
|
+
get() {
|
|
17
|
+
const value = getter();
|
|
18
|
+
Object.defineProperty(this, key, {
|
|
19
|
+
value,
|
|
20
|
+
writable: true,
|
|
21
|
+
configurable: true,
|
|
22
|
+
});
|
|
23
|
+
return value;
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { lazyProp } from './lazyProp.js';
|
|
2
|
+
/**
|
|
3
|
+
* Create an object whose properties are computed lazily.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* const data = lazyRecord({
|
|
8
|
+
* name: 'Name',
|
|
9
|
+
* now() {
|
|
10
|
+
* return Date.now();
|
|
11
|
+
* },
|
|
12
|
+
* });
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export function lazyRecord(properties) {
|
|
16
|
+
const record = Object.create(null);
|
|
17
|
+
for (const [key, getter] of Object.entries(properties)) {
|
|
18
|
+
Object.defineProperty(record, key, typeof getter === 'function'
|
|
19
|
+
? lazyProp(key, getter)
|
|
20
|
+
: {
|
|
21
|
+
value: getter,
|
|
22
|
+
writable: true,
|
|
23
|
+
configurable: true,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
return record;
|
|
27
|
+
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -50,6 +50,8 @@ export * from './isEmpty.js';
|
|
|
50
50
|
export * from './iterateForever.js';
|
|
51
51
|
export * from './joinStrings.js';
|
|
52
52
|
export * from './jsonParse.js';
|
|
53
|
+
export * from './lazyProp.js';
|
|
54
|
+
export * from './lazyRecord.js';
|
|
53
55
|
export * from './looksLikeURL.js';
|
|
54
56
|
export * from './memo.js';
|
|
55
57
|
export * from './normalize.js';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a property descriptor that lazily computes its value.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* const data = {};
|
|
7
|
+
*
|
|
8
|
+
* Object.defineProperty(data, 'value', lazyProp('value', () => {
|
|
9
|
+
* return Math.random();
|
|
10
|
+
* }));
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export declare function lazyProp<Key extends PropertyKey, Value>(key: Key, getter: () => Value): TypedPropertyDescriptor<Value>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { AnyFunction } from './types/common.js';
|
|
2
|
+
import type { UnknownRecord } from './types/records.js';
|
|
3
|
+
export type LazyRecordProperties<Type extends UnknownRecord> = {
|
|
4
|
+
[Key in keyof Type]: Exclude<Type[Key], AnyFunction> | (() => Type[Key]);
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Create an object whose properties are computed lazily.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* const data = lazyRecord({
|
|
12
|
+
* name: 'Name',
|
|
13
|
+
* now() {
|
|
14
|
+
* return Date.now();
|
|
15
|
+
* },
|
|
16
|
+
* });
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare function lazyRecord<Type extends UnknownRecord>(properties: LazyRecordProperties<Type>): Type;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webdeveric/utils",
|
|
3
3
|
"description": "Just another collection of utilities.",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.5.0",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
@@ -66,7 +66,7 @@
|
|
|
66
66
|
"url": "https://github.com/webdeveric/utils/issues"
|
|
67
67
|
},
|
|
68
68
|
"homepage": "https://github.com/webdeveric/utils/#readme",
|
|
69
|
-
"packageManager": "pnpm@11.
|
|
69
|
+
"packageManager": "pnpm@11.25.0+sha512.5cde925b4f075f725eb71fbae18a42ffe784524789f19b61c731cb8721ec28aaee160e01a8d5af4fedb2a42cdbf300efe23db356b0d4a17b4d63e11f8ab7c956",
|
|
70
70
|
"scripts": {
|
|
71
71
|
"clean": "rimraf ./dist/ ./cache/ ./coverage/",
|
|
72
72
|
"prebuild": "pnpm clean",
|
|
@@ -97,20 +97,20 @@
|
|
|
97
97
|
"commitlint": "^21.2.2",
|
|
98
98
|
"commitlint-plugin-cspell": "^0.9.4",
|
|
99
99
|
"conventional-changelog-conventionalcommits": "^9.3.1",
|
|
100
|
-
"cspell": "^10.
|
|
100
|
+
"cspell": "^10.1.1",
|
|
101
101
|
"eslint": "^8.57.1",
|
|
102
102
|
"eslint-config-prettier": "^10.1.8",
|
|
103
103
|
"eslint-import-resolver-typescript": "^4.4.5",
|
|
104
104
|
"eslint-plugin-import": "^2.32.0",
|
|
105
105
|
"husky": "^9.1.7",
|
|
106
106
|
"jsdom": "^30.0.1",
|
|
107
|
-
"lint-staged": "^17.
|
|
107
|
+
"lint-staged": "^17.4.1",
|
|
108
108
|
"prettier": "^3.9.6",
|
|
109
109
|
"rimraf": "^6.1.3",
|
|
110
110
|
"semantic-release": "^25.0.9",
|
|
111
111
|
"typedoc": "^0.28.20",
|
|
112
112
|
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
113
|
-
"validate-package-exports": "^1.
|
|
113
|
+
"validate-package-exports": "^1.4.0",
|
|
114
114
|
"vitest": "^4.1.11"
|
|
115
115
|
}
|
|
116
116
|
}
|