@brandup/ui-helpers 2.0.1 → 2.0.2
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 +7 -29
- package/dist/cjs/helpers/func.js +130 -0
- package/dist/cjs/helpers/func.js.map +1 -0
- package/dist/cjs/helpers/guid.js +16 -0
- package/dist/cjs/helpers/guid.js.map +1 -0
- package/dist/cjs/helpers/object.js +53 -0
- package/dist/cjs/helpers/object.js.map +1 -0
- package/dist/cjs/helpers/string.js +37 -0
- package/dist/cjs/helpers/string.js.map +1 -0
- package/dist/cjs/helpers/type.js +26 -0
- package/dist/cjs/helpers/type.js.map +1 -0
- package/dist/cjs/helpers/word.js +30 -0
- package/dist/cjs/helpers/word.js.map +1 -0
- package/dist/cjs/index.js +9 -301
- package/dist/cjs/index.js.map +1 -1
- package/dist/mjs/helpers/func.js +124 -0
- package/dist/mjs/helpers/func.js.map +1 -0
- package/dist/mjs/helpers/guid.js +13 -0
- package/dist/mjs/helpers/guid.js.map +1 -0
- package/dist/mjs/helpers/object.js +50 -0
- package/dist/mjs/helpers/object.js.map +1 -0
- package/dist/mjs/helpers/string.js +35 -0
- package/dist/mjs/helpers/string.js.map +1 -0
- package/dist/mjs/helpers/type.js +23 -0
- package/dist/mjs/helpers/type.js.map +1 -0
- package/dist/mjs/helpers/word.js +28 -0
- package/dist/mjs/helpers/word.js.map +1 -0
- package/dist/mjs/index.js +11 -302
- package/dist/mjs/index.js.map +1 -1
- package/dist/types.d.ts +0 -34
- package/package.json +9 -1
package/dist/mjs/index.js
CHANGED
|
@@ -1,303 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
|
|
5
|
-
*
|
|
6
|
-
|
|
7
|
-
*
|
|
8
|
-
|
|
9
|
-
*
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
if (!obj)
|
|
13
|
-
return null;
|
|
14
|
-
const props = path.split('.');
|
|
15
|
-
for (let i = 0; i < props.length; i++) {
|
|
16
|
-
const name = props[i];
|
|
17
|
-
if (obj == null || (typeof obj !== "object" && typeof obj !== "function"))
|
|
18
|
-
return undefined;
|
|
19
|
-
if (!(name in obj))
|
|
20
|
-
return undefined;
|
|
21
|
-
obj = obj[name];
|
|
22
|
-
}
|
|
23
|
-
return obj;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Determines whether an object has a nested property at the given dot-separated path.
|
|
27
|
-
*
|
|
28
|
-
* @param obj Source object to inspect.
|
|
29
|
-
* @param path Dot-separated property path, e.g. `"header.value"`.
|
|
30
|
-
* @returns `true` if every segment of the path exists, otherwise `false`.
|
|
31
|
-
* @example
|
|
32
|
-
* hasProperty({ header: { value: "Item" } }, "header.value"); // true
|
|
33
|
-
*/
|
|
34
|
-
function hasProperty(obj, path) {
|
|
35
|
-
if (!obj)
|
|
36
|
-
return false;
|
|
37
|
-
const props = path.split('.');
|
|
38
|
-
for (let i = 0; i < props.length; i++) {
|
|
39
|
-
const name = props[i];
|
|
40
|
-
if (obj == null || (typeof obj !== "object" && typeof obj !== "function"))
|
|
41
|
-
return false;
|
|
42
|
-
if (!(name in obj))
|
|
43
|
-
return false;
|
|
44
|
-
obj = obj[name];
|
|
45
|
-
}
|
|
46
|
-
return true;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
var object = /*#__PURE__*/Object.freeze({
|
|
50
|
-
__proto__: null,
|
|
51
|
-
getProperty: getProperty,
|
|
52
|
-
hasProperty: hasProperty
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Determines whether the given value is a function.
|
|
57
|
-
*
|
|
58
|
-
* @param value Value to test.
|
|
59
|
-
* @returns `true` if the value is a function, otherwise `false`.
|
|
60
|
-
*/
|
|
61
|
-
function isFunction(value) {
|
|
62
|
-
return (typeof value === "function");
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Determines whether the given value is a string.
|
|
66
|
-
*
|
|
67
|
-
* Returns `true` for both string primitives and `String` object instances.
|
|
68
|
-
*
|
|
69
|
-
* @param value Value to test.
|
|
70
|
-
* @returns `true` if the value is a string, otherwise `false`.
|
|
71
|
-
*/
|
|
72
|
-
function isString(value) {
|
|
73
|
-
return (typeof value === "string" || value instanceof String);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
var type = /*#__PURE__*/Object.freeze({
|
|
77
|
-
__proto__: null,
|
|
78
|
-
isFunction: isFunction,
|
|
79
|
-
isString: isString
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Wraps a callback so that, when invoked, it runs no sooner than `minTime` milliseconds
|
|
84
|
-
* after this wrapper was created.
|
|
85
|
-
*
|
|
86
|
-
* The deadline is measured from the moment `minWait` is called. If `minTime` is omitted
|
|
87
|
-
* or falsy, the original function is returned unchanged.
|
|
88
|
-
*
|
|
89
|
-
* @param func Callback to defer.
|
|
90
|
-
* @param minTime Minimum delay in milliseconds before the callback may run.
|
|
91
|
-
* @returns A wrapped function with the same arguments as `func`.
|
|
92
|
-
*/
|
|
93
|
-
const minWait = (func, minTime) => {
|
|
94
|
-
if (!minTime)
|
|
95
|
-
return func;
|
|
96
|
-
const beginTime = Date.now();
|
|
97
|
-
const ret = (...args) => {
|
|
98
|
-
const rightTime = getRightTime(beginTime, minTime);
|
|
99
|
-
if (rightTime)
|
|
100
|
-
setTimeout(() => func(...args), rightTime);
|
|
101
|
-
else
|
|
102
|
-
func(...args);
|
|
103
|
-
};
|
|
104
|
-
return ret;
|
|
105
|
-
};
|
|
106
|
-
/**
|
|
107
|
-
* Awaits an async operation and guarantees the returned promise settles no sooner than
|
|
108
|
-
* `minTime` milliseconds after invocation, padding with an extra delay when needed.
|
|
109
|
-
*
|
|
110
|
-
* Useful for keeping spinners/loading states visible for a minimum duration. If `minTime`
|
|
111
|
-
* is omitted or falsy, `func` is awaited and its result returned without padding.
|
|
112
|
-
*
|
|
113
|
-
* @typeParam TResult Result type produced by `func`.
|
|
114
|
-
* @param func Factory returning the promise to await.
|
|
115
|
-
* @param minTime Minimum total duration in milliseconds.
|
|
116
|
-
* @param abort Optional signal used to cancel the padding delay.
|
|
117
|
-
* @returns The result produced by `func`.
|
|
118
|
-
*/
|
|
119
|
-
async function minWaitAsync(func, minTime, abort) {
|
|
120
|
-
if (!minTime)
|
|
121
|
-
return func();
|
|
122
|
-
const beginTime = Date.now();
|
|
123
|
-
const result = await func();
|
|
124
|
-
const rightTime = getRightTime(beginTime, minTime);
|
|
125
|
-
if (rightTime)
|
|
126
|
-
await delay(rightTime, abort);
|
|
127
|
-
return result;
|
|
128
|
-
}
|
|
129
|
-
/** @internal */
|
|
130
|
-
const getRightTime = (start, minTime) => {
|
|
131
|
-
const finishTime = Date.now();
|
|
132
|
-
const w = minTime - (finishTime - start);
|
|
133
|
-
return w > minTime * 0.1 ? w : 0;
|
|
134
|
-
};
|
|
135
|
-
/**
|
|
136
|
-
* Returns a promise that resolves after the given number of milliseconds.
|
|
137
|
-
*
|
|
138
|
-
* If an already-aborted signal is supplied the promise rejects immediately; otherwise
|
|
139
|
-
* aborting before the delay elapses clears the timer and rejects with the abort reason.
|
|
140
|
-
*
|
|
141
|
-
* @param time Delay in milliseconds.
|
|
142
|
-
* @param abort Optional signal used to cancel the delay.
|
|
143
|
-
* @returns A promise that resolves when the delay elapses.
|
|
144
|
-
*/
|
|
145
|
-
function delay(time, abort) {
|
|
146
|
-
return new Promise((resolve, reject) => {
|
|
147
|
-
abort?.throwIfAborted();
|
|
148
|
-
const onAbort = () => {
|
|
149
|
-
clearTimeout(timer);
|
|
150
|
-
reject(abort?.reason);
|
|
151
|
-
};
|
|
152
|
-
const timer = setTimeout(() => {
|
|
153
|
-
abort?.removeEventListener("abort", onAbort);
|
|
154
|
-
resolve();
|
|
155
|
-
}, time);
|
|
156
|
-
abort?.addEventListener("abort", onAbort, { once: true });
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* Races a promise against a timeout.
|
|
161
|
-
*
|
|
162
|
-
* Resolves/rejects with the original promise if it settles in time. If the timeout elapses
|
|
163
|
-
* first the returned promise rejects with a {@link TimeoutError}. Aborting via `abort`
|
|
164
|
-
* rejects with the signal's reason.
|
|
165
|
-
*
|
|
166
|
-
* @typeParam T Resolved value type of the wrapped promise.
|
|
167
|
-
* @param promise Promise to guard with a timeout.
|
|
168
|
-
* @param timeout Timeout in milliseconds; must be greater than `0`.
|
|
169
|
-
* @param abort Optional signal used to cancel the wait.
|
|
170
|
-
* @returns A promise mirroring `promise` unless the timeout or abort fires first.
|
|
171
|
-
* @throws {Error} When `timeout` is not greater than `0`.
|
|
172
|
-
*/
|
|
173
|
-
function timeout(promise, timeout, abort) {
|
|
174
|
-
if (timeout <= 0)
|
|
175
|
-
throw new Error("Invalid timeout value.");
|
|
176
|
-
return new Promise((resolve, reject) => {
|
|
177
|
-
abort?.throwIfAborted();
|
|
178
|
-
const onAbort = () => {
|
|
179
|
-
clearTimeout(timer);
|
|
180
|
-
reject(abort?.reason);
|
|
181
|
-
};
|
|
182
|
-
const timer = setTimeout(() => {
|
|
183
|
-
abort?.removeEventListener("abort", onAbort);
|
|
184
|
-
reject(new TimeoutError());
|
|
185
|
-
}, timeout);
|
|
186
|
-
abort?.addEventListener("abort", onAbort, { once: true });
|
|
187
|
-
promise
|
|
188
|
-
.then(result => resolve(result))
|
|
189
|
-
.catch(reason => reject(reason))
|
|
190
|
-
.finally(() => {
|
|
191
|
-
clearTimeout(timer);
|
|
192
|
-
abort?.removeEventListener("abort", onAbort);
|
|
193
|
-
});
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
/** Thrown by {@link timeout} when the time limit is exceeded. */
|
|
197
|
-
class TimeoutError extends Error {
|
|
198
|
-
constructor() {
|
|
199
|
-
super("Timeout");
|
|
200
|
-
this.name = "TimeoutError";
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
var func = /*#__PURE__*/Object.freeze({
|
|
205
|
-
__proto__: null,
|
|
206
|
-
TimeoutError: TimeoutError,
|
|
207
|
-
delay: delay,
|
|
208
|
-
minWait: minWait,
|
|
209
|
-
minWaitAsync: minWaitAsync,
|
|
210
|
-
timeout: timeout
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Returns a word combined with the grammatical ending that agrees with the given count.
|
|
215
|
-
*
|
|
216
|
-
* Implements Russian-style pluralization rules: the ending is chosen depending on
|
|
217
|
-
* whether the count ends in 1, in 2–4, or in 0/5–9 (with 11–14 treated as the "five" form).
|
|
218
|
-
*
|
|
219
|
-
* @param count Quantity that the word refers to.
|
|
220
|
-
* @param word Word stem to which the ending is appended.
|
|
221
|
-
* @param one Ending used for counts ending in 1 (but not 11).
|
|
222
|
-
* @param two Ending used for counts ending in 2–4 (but not 12–14).
|
|
223
|
-
* @param five Ending used for counts ending in 0, 5–9 and 11–20.
|
|
224
|
-
* @returns The word with the appropriate ending appended.
|
|
225
|
-
* @example
|
|
226
|
-
* getWordEnd(1, "товар", "", "а", "ов"); // "товар"
|
|
227
|
-
* getWordEnd(3, "товар", "", "а", "ов"); // "товара"
|
|
228
|
-
* getWordEnd(5, "товар", "", "а", "ов"); // "товаров"
|
|
229
|
-
*/
|
|
230
|
-
function getWordEnd(count, word, one, two, five) {
|
|
231
|
-
const tt = count % 100;
|
|
232
|
-
if (tt >= 5 && tt <= 20)
|
|
233
|
-
return word + (five || "");
|
|
234
|
-
const t = count % 10;
|
|
235
|
-
return (t === 1 ?
|
|
236
|
-
(word + (one || "")) : ((t >= 2 && t <= 4) ? (word + (two || "")) : (word + (five || ""))));
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
var word = /*#__PURE__*/Object.freeze({
|
|
240
|
-
__proto__: null,
|
|
241
|
-
getWordEnd: getWordEnd
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* Generates a RFC 4122 UUID v4 string using `crypto.randomUUID()`.
|
|
246
|
-
*
|
|
247
|
-
* @returns A newly generated UUID string in `xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx` form.
|
|
248
|
-
* @example
|
|
249
|
-
* createGuid(); // e.g. "3f2a1b4c-9d8e-4a23-b123-456789abcdef"
|
|
250
|
-
*/
|
|
251
|
-
const createGuid = () => crypto.randomUUID();
|
|
252
|
-
/** The empty (all-zero) GUID value `"00000000-0000-0000-0000-000000000000"`. */
|
|
253
|
-
const empty = "00000000-0000-0000-0000-000000000000";
|
|
254
|
-
|
|
255
|
-
var guid = /*#__PURE__*/Object.freeze({
|
|
256
|
-
__proto__: null,
|
|
257
|
-
createGuid: createGuid,
|
|
258
|
-
empty: empty
|
|
259
|
-
});
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* Formats a template string by substituting `{...}` placeholders.
|
|
263
|
-
*
|
|
264
|
-
* When the first argument is an object, placeholders are treated as dot-separated
|
|
265
|
-
* property paths resolved against that object (see {@link getProperty}). Otherwise
|
|
266
|
-
* placeholders are treated as zero-based indexes into the remaining arguments.
|
|
267
|
-
* Unresolved placeholders are replaced with an empty string.
|
|
268
|
-
*
|
|
269
|
-
* @param template Template containing `{name}` or `{0}` placeholders.
|
|
270
|
-
* @param args Either a single model object, or positional arguments.
|
|
271
|
-
* @returns The formatted string.
|
|
272
|
-
* @example
|
|
273
|
-
* formatText("Hello, {name}", { name: "Dmitry" }); // "Hello, Dmitry"
|
|
274
|
-
* formatText("Hello, {0}", "Dmitry"); // "Hello, Dmitry"
|
|
275
|
-
*/
|
|
276
|
-
function formatText(template, ...args) {
|
|
277
|
-
if (!args.length)
|
|
278
|
-
return template;
|
|
279
|
-
const obj = typeof args[0] === "object" ? args[0] : null;
|
|
280
|
-
return template.replace(/\{([^}]+)\}/g, (_match, key) => {
|
|
281
|
-
if (obj)
|
|
282
|
-
return getProperty(obj, key) ?? "";
|
|
283
|
-
else {
|
|
284
|
-
const paramIndex = parseInt(key);
|
|
285
|
-
if (!isNaN(paramIndex) && paramIndex < args.length)
|
|
286
|
-
return args[paramIndex] ?? "";
|
|
287
|
-
}
|
|
288
|
-
return "";
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
Object.prop = function (obj, path) {
|
|
293
|
-
return getProperty(obj, path);
|
|
294
|
-
};
|
|
295
|
-
Object.hasProp = function (obj, path) {
|
|
296
|
-
return hasProperty(obj, path);
|
|
297
|
-
};
|
|
298
|
-
String.prototype.format = function (...args) {
|
|
299
|
-
return formatText(this.toString(), ...args);
|
|
300
|
-
};
|
|
301
|
-
|
|
302
|
-
export { func as FuncHelper, guid as Guid, object as ObjectHelper, type as TypeHelper, word as WordHelper, formatText };
|
|
1
|
+
import * as object from './helpers/object.js';
|
|
2
|
+
export { object as ObjectHelper };
|
|
3
|
+
import * as type from './helpers/type.js';
|
|
4
|
+
export { type as TypeHelper };
|
|
5
|
+
import * as func from './helpers/func.js';
|
|
6
|
+
export { func as FuncHelper };
|
|
7
|
+
import * as word from './helpers/word.js';
|
|
8
|
+
export { word as WordHelper };
|
|
9
|
+
import * as guid from './helpers/guid.js';
|
|
10
|
+
export { guid as Guid };
|
|
11
|
+
export { formatText } from './helpers/string.js';
|
|
303
12
|
//# sourceMappingURL=index.js.map
|
package/dist/mjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;"}
|
package/dist/types.d.ts
CHANGED
|
@@ -174,40 +174,6 @@ declare namespace guid {
|
|
|
174
174
|
};
|
|
175
175
|
}
|
|
176
176
|
|
|
177
|
-
declare global {
|
|
178
|
-
interface String {
|
|
179
|
-
/**
|
|
180
|
-
* Formats this string by substituting `{...}` placeholders.
|
|
181
|
-
*
|
|
182
|
-
* @param args Either a single model object whose properties fill named placeholders,
|
|
183
|
-
* or positional arguments referenced by zero-based index. See {@link formatText}.
|
|
184
|
-
* @returns The formatted string.
|
|
185
|
-
* @example
|
|
186
|
-
* "Hello, {name}".format({ name: "Dmitry" }); // "Hello, Dmitry"
|
|
187
|
-
* "Hello, {0}".format("Dmitry"); // "Hello, Dmitry"
|
|
188
|
-
*/
|
|
189
|
-
format(...args: any[]): string;
|
|
190
|
-
}
|
|
191
|
-
interface ObjectConstructor {
|
|
192
|
-
/**
|
|
193
|
-
* Reads a nested property value by a dot-separated path. See {@link getProperty}.
|
|
194
|
-
*
|
|
195
|
-
* @param obj Source object.
|
|
196
|
-
* @param path Dot-separated property path, e.g. `"header.value"`.
|
|
197
|
-
* @returns The resolved value, or `undefined`/`null` when not found.
|
|
198
|
-
*/
|
|
199
|
-
prop(obj: any, path: string): any;
|
|
200
|
-
/**
|
|
201
|
-
* Determines whether a nested property exists at a dot-separated path. See {@link hasProperty}.
|
|
202
|
-
*
|
|
203
|
-
* @param obj Source object.
|
|
204
|
-
* @param path Dot-separated property path, e.g. `"header.value"`.
|
|
205
|
-
* @returns `true` if the property exists, otherwise `false`.
|
|
206
|
-
*/
|
|
207
|
-
hasProp(obj: any, path: string): boolean;
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
177
|
/**
|
|
212
178
|
* Formats a template string by substituting `{...}` placeholders.
|
|
213
179
|
*
|
package/package.json
CHANGED
|
@@ -22,10 +22,18 @@
|
|
|
22
22
|
"email": "it@brandup.online"
|
|
23
23
|
},
|
|
24
24
|
"license": "Apache-2.0",
|
|
25
|
-
"version": "2.0.
|
|
25
|
+
"version": "2.0.2",
|
|
26
26
|
"main": "dist/cjs/index.js",
|
|
27
27
|
"module": "dist/mjs/index.js",
|
|
28
28
|
"types": "dist/types.d.ts",
|
|
29
|
+
"sideEffects": false,
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"types": "./dist/types.d.ts",
|
|
33
|
+
"import": "./dist/mjs/index.js",
|
|
34
|
+
"require": "./dist/cjs/index.js"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
29
37
|
"files": [
|
|
30
38
|
"dist",
|
|
31
39
|
"tsconfig.json",
|