@brandup/ui-helpers 2.0.1 → 2.0.3
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 +14 -32
- package/dist/cjs/helpers/func.js +158 -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 +151 -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 +27 -40
- package/package.json +9 -1
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ npm i @brandup/ui-helpers@latest
|
|
|
12
12
|
|
|
13
13
|
## Helper groups
|
|
14
14
|
|
|
15
|
-
The package exposes the following named helper groups
|
|
15
|
+
The package exposes the following named helper groups:
|
|
16
16
|
|
|
17
17
|
| Import | Module |
|
|
18
18
|
| --- | --- |
|
|
@@ -21,8 +21,7 @@ The package exposes the following named helper groups, plus a set of global prot
|
|
|
21
21
|
| `FuncHelper` | timing / async helpers |
|
|
22
22
|
| `WordHelper` | word pluralization |
|
|
23
23
|
| `Guid` | GUID generation |
|
|
24
|
-
|
|
|
25
|
-
| (string) | `formatText` |
|
|
24
|
+
| `formatText` | string template formatting |
|
|
26
25
|
|
|
27
26
|
```TypeScript
|
|
28
27
|
import { ObjectHelper, TypeHelper, FuncHelper, WordHelper, Guid, formatText } from "@brandup/ui-helpers";
|
|
@@ -46,36 +45,15 @@ Format with positional arguments (placeholders are zero-based indexes):
|
|
|
46
45
|
const result = formatText("Hello, {0}", "Dmitry"); // Hello, Dmitry
|
|
47
46
|
```
|
|
48
47
|
|
|
49
|
-
##
|
|
48
|
+
## Format text
|
|
50
49
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
### Format text
|
|
50
|
+
`formatText` substitutes `{...}` placeholders in a template — by name from a model object, or by zero-based index from positional arguments.
|
|
54
51
|
|
|
55
52
|
```TypeScript
|
|
56
|
-
|
|
57
|
-
const result = text.format({ name: "Dmitry" }); // Hello, Dmitry
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
Format with arguments:
|
|
61
|
-
|
|
62
|
-
```TypeScript
|
|
63
|
-
const text = "Hello, {0}";
|
|
64
|
-
const result = text.format("Dmitry"); // Hello, Dmitry
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
### Get value by property path
|
|
68
|
-
|
|
69
|
-
```TypeScript
|
|
70
|
-
const model = {
|
|
71
|
-
header: {
|
|
72
|
-
value: "Item"
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const value = Object.prop(model, "header.value"); // return "Item"
|
|
53
|
+
import { formatText } from "@brandup/ui-helpers";
|
|
77
54
|
|
|
78
|
-
|
|
55
|
+
formatText("Hello, {name}", { name: "Dmitry" }); // "Hello, Dmitry"
|
|
56
|
+
formatText("Hello, {0}", "Dmitry"); // "Hello, Dmitry"
|
|
79
57
|
```
|
|
80
58
|
|
|
81
59
|
## Object helpers
|
|
@@ -151,6 +129,9 @@ const data = await FuncHelper.minWaitAsync(() => loadData(), 1000);
|
|
|
151
129
|
|
|
152
130
|
// Reject with TimeoutError if the request takes longer than 5000ms
|
|
153
131
|
const result = await FuncHelper.timeout(fetch("/api"), 5000);
|
|
132
|
+
|
|
133
|
+
// Make the wait abortable without stopping the underlying work
|
|
134
|
+
const value = await FuncHelper.abortable(longRunning(), abortController.signal);
|
|
154
135
|
```
|
|
155
136
|
|
|
156
137
|
Detect a timeout by checking the error type:
|
|
@@ -168,7 +149,8 @@ try {
|
|
|
168
149
|
```
|
|
169
150
|
|
|
170
151
|
- `minWait(func, minTime?)` — wraps a callback so it runs no sooner than `minTime` ms after wrapping.
|
|
171
|
-
- `minWaitAsync(func, minTime?, abort?)` — awaits an async operation, padding so it settles no sooner than `minTime` ms.
|
|
172
|
-
- `delay(
|
|
173
|
-
- `timeout(promise,
|
|
152
|
+
- `minWaitAsync(func, minTime?, abort?)` — awaits an async operation, padding so it settles no sooner than `minTime` ms. An already-aborted signal rejects immediately, before `func` runs.
|
|
153
|
+
- `delay(ms, abort?)` — a promise resolved after `ms` ms; rejects on abort. Throws synchronously if `ms` is negative (`0` is allowed).
|
|
154
|
+
- `timeout(promise, ms, abort?)` — races `promise` against `ms` ms; rejects with a `TimeoutError` on timeout, or with the signal's reason on abort. Throws synchronously if `ms ≤ 0`. The underlying `promise` is not cancelled — only the wait ends.
|
|
155
|
+
- `abortable(promise, abort?)` — makes *waiting* for `promise` abortable: rejects with the signal's reason on abort. Does not stop the underlying work; without a signal the promise is awaited as-is.
|
|
174
156
|
- `TimeoutError` — error class thrown by `timeout` when the time limit is exceeded.
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Wraps a callback so that, when invoked, it runs no sooner than `minTime` milliseconds
|
|
5
|
+
* after this wrapper was created.
|
|
6
|
+
*
|
|
7
|
+
* The deadline is measured from the moment `minWait` is called. If `minTime` is omitted
|
|
8
|
+
* or falsy, the original function is returned unchanged.
|
|
9
|
+
*
|
|
10
|
+
* @param func Callback to defer.
|
|
11
|
+
* @param minTime Minimum delay in milliseconds before the callback may run.
|
|
12
|
+
* @returns A wrapped function with the same arguments as `func`.
|
|
13
|
+
*/
|
|
14
|
+
const minWait = (func, minTime) => {
|
|
15
|
+
if (!minTime)
|
|
16
|
+
return func;
|
|
17
|
+
const beginTime = Date.now();
|
|
18
|
+
const ret = (...args) => {
|
|
19
|
+
const rightTime = getRightTime(beginTime, minTime);
|
|
20
|
+
if (rightTime)
|
|
21
|
+
setTimeout(() => func(...args), rightTime);
|
|
22
|
+
else
|
|
23
|
+
func(...args);
|
|
24
|
+
};
|
|
25
|
+
return ret;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Awaits an async operation and guarantees the returned promise settles no sooner than
|
|
29
|
+
* `minTime` milliseconds after invocation, padding with an extra delay when needed.
|
|
30
|
+
*
|
|
31
|
+
* Useful for keeping spinners/loading states visible for a minimum duration. If `minTime`
|
|
32
|
+
* is omitted or falsy, `func` is awaited and its result returned without padding.
|
|
33
|
+
*
|
|
34
|
+
* An already-aborted signal rejects immediately, before `func` runs; aborting later cancels
|
|
35
|
+
* the padding delay (but not `func` itself, which can't be cancelled here).
|
|
36
|
+
*
|
|
37
|
+
* @typeParam TResult Result type produced by `func`.
|
|
38
|
+
* @param func Factory returning the promise to await.
|
|
39
|
+
* @param minTime Minimum total duration in milliseconds.
|
|
40
|
+
* @param abort Optional signal used to cancel the wait.
|
|
41
|
+
* @returns The result produced by `func`.
|
|
42
|
+
*/
|
|
43
|
+
async function minWaitAsync(func, minTime, abort) {
|
|
44
|
+
abort?.throwIfAborted();
|
|
45
|
+
if (!minTime)
|
|
46
|
+
return func();
|
|
47
|
+
const beginTime = Date.now();
|
|
48
|
+
const result = await func();
|
|
49
|
+
const rightTime = getRightTime(beginTime, minTime);
|
|
50
|
+
if (rightTime)
|
|
51
|
+
await delay(rightTime, abort);
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
/** @internal */
|
|
55
|
+
const getRightTime = (start, minTime) => {
|
|
56
|
+
const finishTime = Date.now();
|
|
57
|
+
const w = minTime - (finishTime - start);
|
|
58
|
+
// Skip padding when the leftover is within 10% of `minTime`: the wait is already
|
|
59
|
+
// "close enough", and a sub-tick delay would only add jitter without a visible effect.
|
|
60
|
+
return w > minTime * 0.1 ? w : 0;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Returns a promise that resolves after the given number of milliseconds.
|
|
64
|
+
*
|
|
65
|
+
* If an already-aborted signal is supplied the promise rejects immediately; otherwise
|
|
66
|
+
* aborting before the delay elapses clears the timer and rejects with the abort reason.
|
|
67
|
+
*
|
|
68
|
+
* @param ms Delay in milliseconds; must not be negative. Values above 2147483647 (~24.8 days)
|
|
69
|
+
* overflow the timer and fire on the next tick — a `setTimeout` limitation.
|
|
70
|
+
* @param abort Optional signal used to cancel the delay.
|
|
71
|
+
* @returns A promise that resolves when the delay elapses.
|
|
72
|
+
* @throws {Error} When `ms` is negative.
|
|
73
|
+
*/
|
|
74
|
+
function delay(ms, abort) {
|
|
75
|
+
if (ms < 0)
|
|
76
|
+
throw new Error("Invalid delay value.");
|
|
77
|
+
return new Promise((resolve, reject) => {
|
|
78
|
+
abort?.throwIfAborted();
|
|
79
|
+
const onAbort = () => {
|
|
80
|
+
clearTimeout(timer);
|
|
81
|
+
// `onAbort` only runs once the listener has fired, which means `abort` exists.
|
|
82
|
+
reject(abort.reason);
|
|
83
|
+
};
|
|
84
|
+
const timer = setTimeout(() => {
|
|
85
|
+
abort?.removeEventListener("abort", onAbort);
|
|
86
|
+
resolve();
|
|
87
|
+
}, ms);
|
|
88
|
+
abort?.addEventListener("abort", onAbort, { once: true });
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Races a promise against a timeout.
|
|
93
|
+
*
|
|
94
|
+
* Resolves/rejects with the original promise if it settles in time. If the timeout elapses
|
|
95
|
+
* first the returned promise rejects with a {@link TimeoutError}. Aborting via `abort`
|
|
96
|
+
* rejects with the signal's reason.
|
|
97
|
+
*
|
|
98
|
+
* @typeParam T Resolved value type of the wrapped promise.
|
|
99
|
+
* @param promise Promise to guard with a timeout.
|
|
100
|
+
* @param ms Timeout in milliseconds; must be greater than `0`. Values above 2147483647
|
|
101
|
+
* (~24.8 days) overflow the timer and fire on the next tick — a `setTimeout` limitation.
|
|
102
|
+
* @param abort Optional signal used to cancel the wait.
|
|
103
|
+
* @returns A promise mirroring `promise` unless the timeout or abort fires first.
|
|
104
|
+
* @throws {Error} When `ms` is not greater than `0`.
|
|
105
|
+
*/
|
|
106
|
+
function timeout(promise, ms, abort) {
|
|
107
|
+
if (ms <= 0)
|
|
108
|
+
throw new Error("Invalid timeout value.");
|
|
109
|
+
// Own controller so the `delay` timer can be torn down once the race is decided,
|
|
110
|
+
// independently of the caller's `abort`.
|
|
111
|
+
const timer = new AbortController();
|
|
112
|
+
// Wins the race only by timing out (rejects with TimeoutError). If the timer is cancelled
|
|
113
|
+
// instead — the guarded work settled, or the caller aborted — `delay` rejects, and we turn
|
|
114
|
+
// that into a never-settling promise so `expire` simply stands aside: the race never produces
|
|
115
|
+
// a stray rejection that nobody is listening for.
|
|
116
|
+
const expire = delay(ms, timer.signal).then(() => Promise.reject(new TimeoutError()), () => new Promise(() => { }));
|
|
117
|
+
return abortable(Promise.race([promise, expire]), abort)
|
|
118
|
+
.finally(() => timer.abort());
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Makes *waiting* for a promise abortable: rejects with the signal's reason on abort.
|
|
122
|
+
* Does not stop the underlying work the promise performs.
|
|
123
|
+
*
|
|
124
|
+
* If no signal is supplied the promise is awaited as-is. An already-aborted signal rejects
|
|
125
|
+
* immediately; otherwise the abort listener is removed once the promise settles.
|
|
126
|
+
*
|
|
127
|
+
* @typeParam T Resolved value type of the wrapped promise.
|
|
128
|
+
* @param promise Promise (or thenable) whose wait should become abortable.
|
|
129
|
+
* @param abort Optional signal used to cancel the wait.
|
|
130
|
+
* @returns A promise mirroring `promise` unless the abort fires first.
|
|
131
|
+
*/
|
|
132
|
+
function abortable(promise, abort) {
|
|
133
|
+
if (!abort)
|
|
134
|
+
return Promise.resolve(promise);
|
|
135
|
+
if (abort.aborted)
|
|
136
|
+
return Promise.reject(abort.reason);
|
|
137
|
+
return new Promise((resolve, reject) => {
|
|
138
|
+
const onAbort = () => reject(abort.reason);
|
|
139
|
+
abort.addEventListener("abort", onAbort, { once: true });
|
|
140
|
+
const cleanup = () => abort.removeEventListener("abort", onAbort);
|
|
141
|
+
Promise.resolve(promise).then(v => { cleanup(); resolve(v); }, e => { cleanup(); reject(e); });
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
/** Thrown by {@link timeout} when the time limit is exceeded. */
|
|
145
|
+
class TimeoutError extends Error {
|
|
146
|
+
constructor() {
|
|
147
|
+
super("Timeout");
|
|
148
|
+
this.name = "TimeoutError";
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
exports.TimeoutError = TimeoutError;
|
|
153
|
+
exports.abortable = abortable;
|
|
154
|
+
exports.delay = delay;
|
|
155
|
+
exports.minWait = minWait;
|
|
156
|
+
exports.minWaitAsync = minWaitAsync;
|
|
157
|
+
exports.timeout = timeout;
|
|
158
|
+
//# sourceMappingURL=func.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"func.js","sources":["../../../../source/helpers/func.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAAA;;;;;;;;;;AAUG;AACH,MAAM,OAAO,GAAG,CAAC,IAA8B,EAAE,OAAgB,KAAI;AACpE,IAAA,IAAI,CAAC,OAAO;AACX,QAAA,OAAO,IAAI;AAEZ,IAAA,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE;AAE5B,IAAA,MAAM,GAAG,GAAG,CAAC,GAAG,IAAW,KAAI;QAC9B,MA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C;IAEpC,OAAO,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,KAAI;QACzC,MAAM,OAAO,GAAG,MAAM,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC;AAC1C,QAAA,KAAK,CAAC,gBAAgB,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;AAExD,QAAA,MAAM,OAAO,GAAG,MAAM,KAAK,CAAC,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC;AACjE,QAAA,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,IAAI,CAC5B,CAAC,MAAM,OAAO,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAC/B,CAAC,IAAG,EAAG,OAAO,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAC9B;AACF,IAAA,CAAC,CAAC;AACH;AAEA;AACM,MAAO,YAAa,SAAQ,KAAK,CAAA;AACtC,IAAA,WAAA,GAAA;QACC,KAAK,CAAC,SAAS,CAAC;AAChB,QAAA,IAAI,CAAC,IAAI,GAAG,cAAc;IAC3B;AACA;;;;;;;;;"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Generates a RFC 4122 UUID v4 string using `crypto.randomUUID()`.
|
|
5
|
+
*
|
|
6
|
+
* @returns A newly generated UUID string in `xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx` form.
|
|
7
|
+
* @example
|
|
8
|
+
* createGuid(); // e.g. "3f2a1b4c-9d8e-4a23-b123-456789abcdef"
|
|
9
|
+
*/
|
|
10
|
+
const createGuid = () => crypto.randomUUID();
|
|
11
|
+
/** The empty (all-zero) GUID value `"00000000-0000-0000-0000-000000000000"`. */
|
|
12
|
+
const empty = "00000000-0000-0000-0000-000000000000";
|
|
13
|
+
|
|
14
|
+
exports.createGuid = createGuid;
|
|
15
|
+
exports.empty = empty;
|
|
16
|
+
//# sourceMappingURL=guid.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"guid.js","sources":["../../../../source/helpers/guid.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAAA;;;;;;AAMG;AACH,MAAM,UAAU,GAAG,MAAc,MAAM,CAAC,UAAU;AAElD;AACA,MAAM,KAAK,GAAG;;;;;"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Reads a nested property value from an object by a dot-separated path.
|
|
5
|
+
*
|
|
6
|
+
* @param obj Source object to read from.
|
|
7
|
+
* @param path Dot-separated property path, e.g. `"header.value"`.
|
|
8
|
+
* @returns The resolved value; `null` when `obj` is falsy, or `undefined` when
|
|
9
|
+
* any segment of the path does not exist.
|
|
10
|
+
* @example
|
|
11
|
+
* getProperty({ header: { value: "Item" } }, "header.value"); // "Item"
|
|
12
|
+
*/
|
|
13
|
+
function getProperty(obj, path) {
|
|
14
|
+
if (!obj)
|
|
15
|
+
return null;
|
|
16
|
+
const props = path.split('.');
|
|
17
|
+
for (let i = 0; i < props.length; i++) {
|
|
18
|
+
const name = props[i];
|
|
19
|
+
if (obj == null || (typeof obj !== "object" && typeof obj !== "function"))
|
|
20
|
+
return undefined;
|
|
21
|
+
if (!(name in obj))
|
|
22
|
+
return undefined;
|
|
23
|
+
obj = obj[name];
|
|
24
|
+
}
|
|
25
|
+
return obj;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Determines whether an object has a nested property at the given dot-separated path.
|
|
29
|
+
*
|
|
30
|
+
* @param obj Source object to inspect.
|
|
31
|
+
* @param path Dot-separated property path, e.g. `"header.value"`.
|
|
32
|
+
* @returns `true` if every segment of the path exists, otherwise `false`.
|
|
33
|
+
* @example
|
|
34
|
+
* hasProperty({ header: { value: "Item" } }, "header.value"); // true
|
|
35
|
+
*/
|
|
36
|
+
function hasProperty(obj, path) {
|
|
37
|
+
if (!obj)
|
|
38
|
+
return false;
|
|
39
|
+
const props = path.split('.');
|
|
40
|
+
for (let i = 0; i < props.length; i++) {
|
|
41
|
+
const name = props[i];
|
|
42
|
+
if (obj == null || (typeof obj !== "object" && typeof obj !== "function"))
|
|
43
|
+
return false;
|
|
44
|
+
if (!(name in obj))
|
|
45
|
+
return false;
|
|
46
|
+
obj = obj[name];
|
|
47
|
+
}
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
exports.getProperty = getProperty;
|
|
52
|
+
exports.hasProperty = hasProperty;
|
|
53
|
+
//# sourceMappingURL=object.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object.js","sources":["../../../../source/helpers/object.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAAA;;;;;;;;;AASG;AACH,SAAS,WAAW,CAAC,GAAQ,EAAE,IAAY,EAAA;AAC1C,IAAA,IAAI,CAAC,GAAG;AACP,QAAA,OAAO,IAAI;IAEZ,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC;AAE7B,IAAA,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;AACtC,QAAA,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC;AACrB,QAAA,IAAI,GAAG,IAAI,IAAI,KAAK,OAAO,GAAG,KAAK,QAAQ,IAAI,OAAO,GAAG,KAAK,UAAU,CAAC;AACxE,YAAA,OAAO,SAAS;AAEjB,QAAA,IAAI,EAAE,IAAI,IAAI,GAAG,CAAC;AACjB,YAAA,OAAO,SAAS;AAEjB,QAAA,GAAG,GAAG,GAAG,CAAC,IAAI,CAAC;IAChB;AAEA,IAAA,OAAO,GAAG;AACX;AAEA;;;;;;;;AAQG;AACH,SAAS,WAAW,CAAC,GAAQ,EAAE,IAAY,EAAA;AAC1C,IAAA,IAAI,CAAC,GAAG;AACP,QAAA,OAAO,KAAK;IAEb,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC;AAE7B,IAAA,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;AACtC,QAAA,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC;AACrB,QAAA,IAAI,GAAG,IAAI,IAAI,KAAK,OAAO,GAAG,KAAK,QAAQ,IAAI,OAAO,GAAG,KAAK,UAAU,CAAC;AACxE,YAAA,OAAO,KAAK;AAEb,QAAA,IAAI,EAAE,IAAI,IAAI,GAAG,CAAC;AACjB,YAAA,OAAO,KAAK;AAEb,QAAA,GAAG,GAAG,GAAG,CAAC,IAAI,CAAC;IAChB;AAEA,IAAA,OAAO,IAAI;AACZ;;;;;"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var object = require('./object.js');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Formats a template string by substituting `{...}` placeholders.
|
|
7
|
+
*
|
|
8
|
+
* When the first argument is an object, placeholders are treated as dot-separated
|
|
9
|
+
* property paths resolved against that object (see {@link getProperty}). Otherwise
|
|
10
|
+
* placeholders are treated as zero-based indexes into the remaining arguments.
|
|
11
|
+
* Unresolved placeholders are replaced with an empty string.
|
|
12
|
+
*
|
|
13
|
+
* @param template Template containing `{name}` or `{0}` placeholders.
|
|
14
|
+
* @param args Either a single model object, or positional arguments.
|
|
15
|
+
* @returns The formatted string.
|
|
16
|
+
* @example
|
|
17
|
+
* formatText("Hello, {name}", { name: "Dmitry" }); // "Hello, Dmitry"
|
|
18
|
+
* formatText("Hello, {0}", "Dmitry"); // "Hello, Dmitry"
|
|
19
|
+
*/
|
|
20
|
+
function formatText(template, ...args) {
|
|
21
|
+
if (!args.length)
|
|
22
|
+
return template;
|
|
23
|
+
const obj = typeof args[0] === "object" ? args[0] : null;
|
|
24
|
+
return template.replace(/\{([^}]+)\}/g, (_match, key) => {
|
|
25
|
+
if (obj)
|
|
26
|
+
return object.getProperty(obj, key) ?? "";
|
|
27
|
+
else {
|
|
28
|
+
const paramIndex = parseInt(key);
|
|
29
|
+
if (!isNaN(paramIndex) && paramIndex < args.length)
|
|
30
|
+
return args[paramIndex] ?? "";
|
|
31
|
+
}
|
|
32
|
+
return "";
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
exports.formatText = formatText;
|
|
37
|
+
//# sourceMappingURL=string.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"string.js","sources":["../../../../source/helpers/string.ts"],"sourcesContent":[null],"names":["getProperty"],"mappings":";;;;AAEA;;;;;;;;;;;;;;AAcG;AACH,SAAS,UAAU,CAAC,QAAgB,EAAE,GAAG,IAAW,EAAA;IACnD,IAAI,CAAC,IAAI,CAAC,MAAM;AAAE,QAAA,OAAO,QAAQ;IAEjC,MAAM,GAAG,GAAG,OAAO,IAAI,CAAC,CAAC,CAAC,KAAK,QAAQ,GAAG,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI;IAExD,OAAO,QAAQ,CAAC,OAAO,CAAC,cAAc,EAAE,CAAC,MAAM,EAAE,GAAG,KAAI;AACvD,QAAA,IAAI,GAAG;YAAE,OAAOA,kBAAW,CAAC,GAAG,EAAE,GAAG,CAAC,IAAI,EAAE;aACtC;AACJ,YAAA,MAAM,UAAU,GAAG,QAAQ,CAAC,GAAG,CAAC;YAChC,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,IAAI,UAAU,GAAG,IAAI,CAAC,MAAM;AACjD,gBAAA,OAAO,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE;QAC/B;AAEA,QAAA,OAAO,EAAE;AACV,IAAA,CAAC,CAAC;AACH;;;;"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Determines whether the given value is a function.
|
|
5
|
+
*
|
|
6
|
+
* @param value Value to test.
|
|
7
|
+
* @returns `true` if the value is a function, otherwise `false`.
|
|
8
|
+
*/
|
|
9
|
+
function isFunction(value) {
|
|
10
|
+
return (typeof value === "function");
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Determines whether the given value is a string.
|
|
14
|
+
*
|
|
15
|
+
* Returns `true` for both string primitives and `String` object instances.
|
|
16
|
+
*
|
|
17
|
+
* @param value Value to test.
|
|
18
|
+
* @returns `true` if the value is a string, otherwise `false`.
|
|
19
|
+
*/
|
|
20
|
+
function isString(value) {
|
|
21
|
+
return (typeof value === "string" || value instanceof String);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
exports.isFunction = isFunction;
|
|
25
|
+
exports.isString = isString;
|
|
26
|
+
//# sourceMappingURL=type.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sources":["../../../../source/helpers/type.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAAA;;;;;AAKG;AACH,SAAS,UAAU,CAAC,KAAU,EAAA;AAC7B,IAAA,QAAQ,OAAO,KAAK,KAAK,UAAU;AACpC;AAEA;;;;;;;AAOG;AACH,SAAS,QAAQ,CAAC,KAAU,EAAA;IAC3B,QAAQ,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,YAAY,MAAM;AAC7D;;;;;"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns a word combined with the grammatical ending that agrees with the given count.
|
|
5
|
+
*
|
|
6
|
+
* Implements Russian-style pluralization rules: the ending is chosen depending on
|
|
7
|
+
* whether the count ends in 1, in 2–4, or in 0/5–9 (with 11–14 treated as the "five" form).
|
|
8
|
+
*
|
|
9
|
+
* @param count Quantity that the word refers to.
|
|
10
|
+
* @param word Word stem to which the ending is appended.
|
|
11
|
+
* @param one Ending used for counts ending in 1 (but not 11).
|
|
12
|
+
* @param two Ending used for counts ending in 2–4 (but not 12–14).
|
|
13
|
+
* @param five Ending used for counts ending in 0, 5–9 and 11–20.
|
|
14
|
+
* @returns The word with the appropriate ending appended.
|
|
15
|
+
* @example
|
|
16
|
+
* getWordEnd(1, "товар", "", "а", "ов"); // "товар"
|
|
17
|
+
* getWordEnd(3, "товар", "", "а", "ов"); // "товара"
|
|
18
|
+
* getWordEnd(5, "товар", "", "а", "ов"); // "товаров"
|
|
19
|
+
*/
|
|
20
|
+
function getWordEnd(count, word, one, two, five) {
|
|
21
|
+
const tt = count % 100;
|
|
22
|
+
if (tt >= 5 && tt <= 20)
|
|
23
|
+
return word + (five || "");
|
|
24
|
+
const t = count % 10;
|
|
25
|
+
return (t === 1 ?
|
|
26
|
+
(word + (one || "")) : ((t >= 2 && t <= 4) ? (word + (two || "")) : (word + (five || ""))));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
exports.getWordEnd = getWordEnd;
|
|
30
|
+
//# sourceMappingURL=word.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"word.js","sources":["../../../../source/helpers/word.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAAA;;;;;;;;;;;;;;;;AAgBG;AACH,SAAS,UAAU,CAAC,KAAa,EAAE,IAAY,EAAE,GAAY,EAAE,GAAY,EAAE,IAAa,EAAA;AACzF,IAAA,MAAM,EAAE,GAAG,KAAK,GAAG,GAAG;AACtB,IAAA,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE;AACtB,QAAA,OAAO,IAAI,IAAI,IAAI,IAAI,EAAE,CAAC;AAE3B,IAAA,MAAM,CAAC,GAAG,KAAK,GAAG,EAAE;AAEpB,IAAA,QAAQ,CAAC,KAAK,CAAC;SACb,IAAI,IAAI,GAAG,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,IAAI,IAAI,GAAG,IAAI,EAAE,CAAC,KAAK,IAAI,IAAI,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC;AAE5F;;;;"}
|