yummies 5.4.6 → 5.4.7
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/async.d.ts +28 -0
- package/async.d.ts.map +1 -0
- package/async.js +62 -0
- package/{src/common.ts → common.d.ts} +3 -14
- package/common.d.ts.map +1 -0
- package/common.js +14 -0
- package/complex/counter.d.ts +15 -0
- package/complex/counter.d.ts.map +1 -0
- package/complex/counter.js +17 -0
- package/complex/global-config.d.ts +11 -0
- package/complex/global-config.d.ts.map +1 -0
- package/complex/global-config.js +41 -0
- package/complex/index.d.ts +4 -0
- package/complex/index.d.ts.map +1 -0
- package/complex/modules-factory.d.ts +49 -0
- package/complex/modules-factory.d.ts.map +1 -0
- package/complex/modules-factory.js +46 -0
- package/cookie.d.ts +3 -0
- package/cookie.d.ts.map +1 -0
- package/cookie.js +9 -0
- package/css.d.ts +36 -0
- package/css.d.ts.map +1 -0
- package/css.js +20 -0
- package/data.d.ts +4 -0
- package/data.d.ts.map +1 -0
- package/data.js +52 -0
- package/date-time.d.ts +28 -0
- package/date-time.d.ts.map +1 -0
- package/date-time.js +160 -0
- package/device.d.ts +8 -0
- package/device.d.ts.map +1 -0
- package/device.js +21 -0
- package/encodings.d.ts +2 -0
- package/encodings.d.ts.map +1 -0
- package/encodings.js +267 -0
- package/errors.d.ts +19 -0
- package/errors.d.ts.map +1 -0
- package/errors.js +30 -0
- package/file.d.ts +3 -0
- package/file.d.ts.map +1 -0
- package/file.js +24 -0
- package/format/_exports.d.ts +5 -0
- package/format/_exports.d.ts.map +1 -0
- package/format/constants.d.ts +4 -0
- package/format/constants.d.ts.map +1 -0
- package/{src/format/constants.ts → format/constants.js} +0 -2
- package/format/index.d.ts +3 -0
- package/format/index.d.ts.map +1 -0
- package/{src/format/index.ts → format/index.js} +0 -1
- package/format/number.d.ts +36 -0
- package/format/number.d.ts.map +1 -0
- package/format/number.js +44 -0
- package/format/percent.d.ts +15 -0
- package/format/percent.d.ts.map +1 -0
- package/format/percent.js +23 -0
- package/format/skip-spaces.d.ts +5 -0
- package/format/skip-spaces.d.ts.map +1 -0
- package/format/skip-spaces.js +4 -0
- package/html.d.ts +44 -0
- package/html.d.ts.map +1 -0
- package/html.js +182 -0
- package/id.d.ts +63 -0
- package/id.d.ts.map +1 -0
- package/{src/id.ts → id.js} +6 -16
- package/imports.d.ts +15 -0
- package/imports.d.ts.map +1 -0
- package/imports.js +40 -0
- package/math.d.ts +13 -0
- package/math.d.ts.map +1 -0
- package/math.js +17 -0
- package/media.d.ts +20 -0
- package/media.d.ts.map +1 -0
- package/media.js +103 -0
- package/mobx/apply-observable.d.ts +4 -0
- package/mobx/apply-observable.d.ts.map +1 -0
- package/mobx/apply-observable.js +12 -0
- package/mobx/create-enhanced-atom.d.ts +11 -0
- package/mobx/create-enhanced-atom.d.ts.map +1 -0
- package/mobx/create-enhanced-atom.js +12 -0
- package/mobx/deep-observable-struct.d.ts +7 -0
- package/mobx/deep-observable-struct.d.ts.map +1 -0
- package/mobx/deep-observable-struct.js +57 -0
- package/mobx/get-mobx-administration.d.ts +6 -0
- package/mobx/get-mobx-administration.d.ts.map +1 -0
- package/mobx/get-mobx-administration.js +2 -0
- package/mobx/index.d.ts +6 -0
- package/mobx/index.d.ts.map +1 -0
- package/mobx/lazy-observe.d.ts +8 -0
- package/mobx/lazy-observe.d.ts.map +1 -0
- package/mobx/lazy-observe.js +43 -0
- package/ms.d.ts +19 -0
- package/ms.d.ts.map +1 -0
- package/ms.js +18 -0
- package/number.d.ts +8 -0
- package/number.d.ts.map +1 -0
- package/number.js +13 -0
- package/package.json +399 -3
- package/parser/_exports.d.ts +4 -0
- package/parser/_exports.d.ts.map +1 -0
- package/parser/index.d.ts +3 -0
- package/parser/index.d.ts.map +1 -0
- package/{src/parser/index.ts → parser/index.js} +0 -1
- package/parser/number.d.ts +21 -0
- package/parser/number.d.ts.map +1 -0
- package/parser/number.js +44 -0
- package/parser/percent.d.ts +4 -0
- package/parser/percent.d.ts.map +1 -0
- package/parser/percent.js +4 -0
- package/parser/string.d.ts +7 -0
- package/parser/string.d.ts.map +1 -0
- package/parser/string.js +14 -0
- package/price.d.ts +6 -0
- package/price.d.ts.map +1 -0
- package/price.js +17 -0
- package/random.d.ts +9 -0
- package/random.d.ts.map +1 -0
- package/random.js +14 -0
- package/react/hooks/index.d.ts +22 -0
- package/react/hooks/index.d.ts.map +1 -0
- package/react/hooks/use-abort-controller.d.ts +2 -0
- package/react/hooks/use-abort-controller.d.ts.map +1 -0
- package/react/hooks/use-abort-controller.js +11 -0
- package/react/hooks/use-abort-signal.d.ts +2 -0
- package/react/hooks/use-abort-signal.d.ts.map +1 -0
- package/{src/react/hooks/use-abort-signal.ts → react/hooks/use-abort-signal.js} +1 -2
- package/react/hooks/use-click-outside.d.ts +9 -0
- package/react/hooks/use-click-outside.d.ts.map +1 -0
- package/react/hooks/use-click-outside.js +13 -0
- package/react/hooks/use-constant.d.ts +9 -0
- package/react/hooks/use-constant.d.ts.map +1 -0
- package/{src/react/hooks/use-constant.ts → react/hooks/use-constant.js} +6 -9
- package/react/hooks/use-define-ref.d.ts +10 -0
- package/react/hooks/use-define-ref.d.ts.map +1 -0
- package/{src/react/hooks/use-define-ref.ts → react/hooks/use-define-ref.js} +7 -10
- package/react/hooks/use-element-ref.d.ts +2 -0
- package/react/hooks/use-element-ref.d.ts.map +1 -0
- package/react/hooks/use-element-ref.js +8 -0
- package/react/hooks/use-event-listener.d.ts +8 -0
- package/react/hooks/use-event-listener.d.ts.map +1 -0
- package/react/hooks/use-event-listener.js +13 -0
- package/react/hooks/use-event.d.ts +3 -0
- package/react/hooks/use-event.d.ts.map +1 -0
- package/react/hooks/use-event.js +19 -0
- package/react/hooks/use-flag.d.ts +8 -0
- package/react/hooks/use-flag.d.ts.map +1 -0
- package/react/hooks/use-flag.js +15 -0
- package/react/hooks/use-force-update.d.ts +2 -0
- package/react/hooks/use-force-update.d.ts.map +1 -0
- package/react/hooks/use-force-update.js +7 -0
- package/react/hooks/use-initial-height.d.ts +5 -0
- package/react/hooks/use-initial-height.d.ts.map +1 -0
- package/react/hooks/use-initial-height.js +11 -0
- package/react/hooks/use-instance.d.ts +27 -0
- package/react/hooks/use-instance.d.ts.map +1 -0
- package/{src/react/hooks/use-instance.ts → react/hooks/use-instance.js} +7 -29
- package/react/hooks/use-intersection-observer.d.ts +2 -0
- package/react/hooks/use-intersection-observer.d.ts.map +1 -0
- package/react/hooks/use-intersection-observer.js +10 -0
- package/react/hooks/use-last-defined-value.d.ts +2 -0
- package/react/hooks/use-last-defined-value.d.ts.map +1 -0
- package/react/hooks/use-last-defined-value.js +8 -0
- package/react/hooks/use-last-value-ref.d.ts +2 -0
- package/react/hooks/use-last-value-ref.d.ts.map +1 -0
- package/react/hooks/use-last-value-ref.js +8 -0
- package/react/hooks/use-life-cycle.d.ts +5 -0
- package/react/hooks/use-life-cycle.d.ts.map +1 -0
- package/react/hooks/use-life-cycle.js +10 -0
- package/react/hooks/use-resize-observer.d.ts +2 -0
- package/react/hooks/use-resize-observer.d.ts.map +1 -0
- package/react/hooks/use-resize-observer.js +11 -0
- package/react/hooks/use-sync-ref.d.ts +2 -0
- package/react/hooks/use-sync-ref.d.ts.map +1 -0
- package/react/hooks/use-sync-ref.js +6 -0
- package/react/hooks/use-toggle.d.ts +2 -0
- package/react/hooks/use-toggle.d.ts.map +1 -0
- package/react/hooks/use-toggle.js +6 -0
- package/react/hooks/use-value.d.ts +5 -0
- package/react/hooks/use-value.d.ts.map +1 -0
- package/react/hooks/use-value.js +8 -0
- package/react/hooks/use-visibility-state.d.ts +2 -0
- package/react/hooks/use-visibility-state.d.ts.map +1 -0
- package/react/hooks/use-visibility-state.js +14 -0
- package/react/index.d.ts +2 -0
- package/react/index.d.ts.map +1 -0
- package/sound.d.ts +7 -0
- package/sound.d.ts.map +1 -0
- package/sound.js +12 -0
- package/storage.d.ts +39 -0
- package/storage.d.ts.map +1 -0
- package/storage.js +43 -0
- package/text.d.ts +15 -0
- package/text.d.ts.map +1 -0
- package/text.js +48 -0
- package/type-guard/_exports.d.ts +86 -0
- package/type-guard/_exports.d.ts.map +1 -0
- package/type-guard/_exports.js +125 -0
- package/type-guard/index.d.ts +3 -0
- package/type-guard/index.d.ts.map +1 -0
- package/{src/type-guard/index.ts → type-guard/index.js} +0 -1
- package/utility-types.d.ts +395 -0
- package/utils/types.d.ts +395 -0
- package/utils/types.d.ts.map +1 -0
- package/utils/types.js +1 -0
- package/vibrate.d.ts +5 -0
- package/vibrate.d.ts.map +1 -0
- package/vibrate.js +8 -0
- package/.changeset/README.md +0 -8
- package/.changeset/config.json +0 -11
- package/.github/FUNDING.yml +0 -1
- package/.github/workflows/main.yml +0 -34
- package/.github/workflows/version-or-publish.yml +0 -45
- package/.nvmrc +0 -1
- package/.vscode/settings.json +0 -19
- package/CHANGELOG.md +0 -215
- package/CONTRIBUTING.md +0 -8
- package/Makefile +0 -7
- package/biome.json +0 -3
- package/commitfmt.toml +0 -18
- package/lefthook.yml +0 -14
- package/scripts/post-build.ts +0 -71
- package/src/async.ts +0 -86
- package/src/complex/counter.test.ts +0 -41
- package/src/complex/counter.ts +0 -40
- package/src/complex/global-config.ts +0 -55
- package/src/complex/modules-factory.ts +0 -65
- package/src/cookie.ts +0 -11
- package/src/css.ts +0 -60
- package/src/data.test.ts +0 -99
- package/src/data.ts +0 -65
- package/src/date-time.test.ts +0 -119
- package/src/date-time.ts +0 -236
- package/src/device.ts +0 -42
- package/src/encodings.ts +0 -270
- package/src/errors.ts +0 -40
- package/src/file.ts +0 -25
- package/src/format/number.test.ts +0 -16
- package/src/format/number.ts +0 -96
- package/src/format/percent.ts +0 -40
- package/src/format/skip-spaces.ts +0 -4
- package/src/html.ts +0 -238
- package/src/imports.ts +0 -52
- package/src/math.ts +0 -20
- package/src/media.ts +0 -134
- package/src/mobx/apply-observable.ts +0 -20
- package/src/mobx/create-enhanced-atom.ts +0 -28
- package/src/mobx/deep-observable-struct.test.ts +0 -69
- package/src/mobx/deep-observable-struct.ts +0 -69
- package/src/mobx/get-mobx-administration.ts +0 -10
- package/src/mobx/lazy-observe.ts +0 -59
- package/src/ms.ts +0 -20
- package/src/number.ts +0 -14
- package/src/parser/number.test.ts +0 -38
- package/src/parser/number.ts +0 -73
- package/src/parser/percent.ts +0 -11
- package/src/parser/string.ts +0 -29
- package/src/price.ts +0 -33
- package/src/random.ts +0 -27
- package/src/react/hooks/use-abort-controller.ts +0 -15
- package/src/react/hooks/use-click-outside.ts +0 -27
- package/src/react/hooks/use-element-ref.ts +0 -11
- package/src/react/hooks/use-event-listener.ts +0 -29
- package/src/react/hooks/use-event.ts +0 -23
- package/src/react/hooks/use-flag.ts +0 -27
- package/src/react/hooks/use-force-update.ts +0 -9
- package/src/react/hooks/use-initial-height.ts +0 -16
- package/src/react/hooks/use-intersection-observer.ts +0 -18
- package/src/react/hooks/use-last-defined-value.ts +0 -9
- package/src/react/hooks/use-last-value-ref.ts +0 -11
- package/src/react/hooks/use-life-cycle.ts +0 -17
- package/src/react/hooks/use-resize-observer.ts +0 -14
- package/src/react/hooks/use-sync-ref.ts +0 -7
- package/src/react/hooks/use-toggle.ts +0 -9
- package/src/react/hooks/use-value.ts +0 -10
- package/src/react/hooks/use-visibility-state.ts +0 -19
- package/src/sound.ts +0 -15
- package/src/storage.ts +0 -137
- package/src/text.test.ts +0 -91
- package/src/text.ts +0 -60
- package/src/type-guard/_exports.ts +0 -154
- package/src/type-guard/index.test.ts +0 -127
- package/src/vibrate.ts +0 -8
- package/tsconfig.json +0 -26
- package/tsconfig.test.json +0 -33
- package/vitest.config.ts +0 -20
- /package/{src/complex/index.ts → complex/index.js} +0 -0
- /package/{src/format/_exports.ts → format/_exports.js} +0 -0
- /package/{src/mobx/index.ts → mobx/index.js} +0 -0
- /package/{src/parser/_exports.ts → parser/_exports.js} +0 -0
- /package/{src/react/hooks/index.ts → react/hooks/index.js} +0 -0
- /package/{src/react/index.ts → react/index.js} +0 -0
package/src/ms.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export const unitsToMs = {
|
|
2
|
-
ms: 1,
|
|
3
|
-
sec: 1000,
|
|
4
|
-
min: 1000 * 60,
|
|
5
|
-
hour: 1000 * 60 * 60,
|
|
6
|
-
day: 1000 * 60 * 60 * 24,
|
|
7
|
-
week: 1000 * 60 * 60 * 24 * 7,
|
|
8
|
-
} as const;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Переводит значение в юнитах в миллисекунды
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```ts
|
|
15
|
-
* ms(1, 'min') // 60_000
|
|
16
|
-
* ms(30, 'sec') // 30_000
|
|
17
|
-
* ```
|
|
18
|
-
*/
|
|
19
|
-
export const ms = (value: number, unit: keyof typeof unitsToMs = 'ms') =>
|
|
20
|
-
value * unitsToMs[unit];
|
package/src/number.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Works like `parseFloat(number.toFixed(4))` but performance better
|
|
3
|
-
*
|
|
4
|
-
* @example
|
|
5
|
-
* round(191.212999999999999999999999, 4) // 191.213
|
|
6
|
-
*/
|
|
7
|
-
export function round(value: number, decimalPlaces: number = 0): number {
|
|
8
|
-
if (!decimalPlaces) {
|
|
9
|
-
return Math.round(value);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const factor = 10 ** decimalPlaces;
|
|
13
|
-
return Math.round(value * factor) / factor;
|
|
14
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from 'vitest';
|
|
2
|
-
import { number } from './number';
|
|
3
|
-
|
|
4
|
-
describe('parser.number', () => {
|
|
5
|
-
test('empty string should return 0', () => {
|
|
6
|
-
expect(number('')).toBe(0);
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
test('empty string with fallback should return fallback', () => {
|
|
10
|
-
expect(number('', { fallback: 'kek' })).toBe('kek');
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
test('Checking the logic of the function', () => {
|
|
14
|
-
expect(number(1)).toBe(1);
|
|
15
|
-
expect(number(100)).toBe(100);
|
|
16
|
-
expect(number(500)).toBe(500);
|
|
17
|
-
expect(number(1, { clamped: [400] })).toBe(400);
|
|
18
|
-
expect(number(1001, { clamped: [null, 1000] })).toBe(1000);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
test('Rounding down', () => {
|
|
22
|
-
expect(number(5.9, { floor: true })).toBe(5);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
test('Rounding up', () => {
|
|
26
|
-
expect(number(5.1, { ceil: true })).toBe(6);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
test('Processing invalid values', () => {
|
|
30
|
-
expect(number(null)).toBe(0);
|
|
31
|
-
expect(number(undefined)).toBe(0);
|
|
32
|
-
expect(number('-')).toBe(0);
|
|
33
|
-
expect(number('test')).toBe(0);
|
|
34
|
-
|
|
35
|
-
expect(number(null, null)).toBe(0);
|
|
36
|
-
expect(number(undefined, undefined)).toBe(0);
|
|
37
|
-
});
|
|
38
|
-
});
|
package/src/parser/number.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { skipSpaces } from '../format/skip-spaces.js';
|
|
2
|
-
import { typeGuard } from '../type-guard/index.js';
|
|
3
|
-
import type { Maybe } from '../utils/types.js';
|
|
4
|
-
|
|
5
|
-
export interface NumberParserSettings<TFallback = number> {
|
|
6
|
-
digits?: number;
|
|
7
|
-
fallback?: TFallback;
|
|
8
|
-
/**
|
|
9
|
-
* Round to upper boundary
|
|
10
|
-
* 5.1 -> 6
|
|
11
|
-
*/
|
|
12
|
-
ceil?: boolean;
|
|
13
|
-
/**
|
|
14
|
-
* Round to bottom boundary
|
|
15
|
-
* 5.9 -> 5
|
|
16
|
-
*/
|
|
17
|
-
floor?: boolean;
|
|
18
|
-
clamped?: [min?: Maybe<number>, max?: Maybe<number>];
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export const number = <TFallback = number>(
|
|
22
|
-
input: Maybe<unknown>,
|
|
23
|
-
userSettings?: Maybe<NumberParserSettings<TFallback>>,
|
|
24
|
-
): number | TFallback => {
|
|
25
|
-
const settings = {
|
|
26
|
-
...number.defaultSettings,
|
|
27
|
-
...userSettings,
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
const fallback = settings?.fallback ?? 0;
|
|
31
|
-
|
|
32
|
-
let result: number;
|
|
33
|
-
|
|
34
|
-
if (typeGuard.isNumber(input)) {
|
|
35
|
-
result = input;
|
|
36
|
-
} else if (typeGuard.isString(input)) {
|
|
37
|
-
const formattedInput = skipSpaces(input).replace(',', '.');
|
|
38
|
-
if (formattedInput === '') {
|
|
39
|
-
result = fallback as any;
|
|
40
|
-
} else {
|
|
41
|
-
result = Number(formattedInput);
|
|
42
|
-
}
|
|
43
|
-
} else {
|
|
44
|
-
result = fallback as any;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (typeGuard.isNumber(result)) {
|
|
48
|
-
if (settings?.clamped != null) {
|
|
49
|
-
result = Math.max(
|
|
50
|
-
settings.clamped[0] ?? -Infinity,
|
|
51
|
-
Math.min(result, settings.clamped[1] ?? Infinity),
|
|
52
|
-
);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (settings?.ceil != null) {
|
|
56
|
-
result = Math.ceil(result);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (settings?.floor != null) {
|
|
60
|
-
result = Math.floor(result);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (settings?.digits != null) {
|
|
64
|
-
result = +result.toFixed(settings.digits);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return result;
|
|
68
|
-
} else {
|
|
69
|
-
return fallback;
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
number.defaultSettings = {} as NumberParserSettings;
|
package/src/parser/percent.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { Maybe } from '../utils/types.js';
|
|
2
|
-
|
|
3
|
-
import { type NumberParserSettings, number } from './number.js';
|
|
4
|
-
|
|
5
|
-
export const percent = <TFallback = number>(
|
|
6
|
-
value: Maybe<string | number>,
|
|
7
|
-
maxValue?: Maybe<string | number>,
|
|
8
|
-
settings?: Maybe<NumberParserSettings<TFallback>>,
|
|
9
|
-
) => {
|
|
10
|
-
return number<TFallback>((Number(value) / Number(maxValue)) * 100, settings);
|
|
11
|
-
};
|
package/src/parser/string.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { typeGuard } from '../type-guard/index.js';
|
|
2
|
-
import type { Maybe } from '../utils/types.js';
|
|
3
|
-
|
|
4
|
-
export interface StringParserSettings<TFallback = string> {
|
|
5
|
-
fallback?: TFallback;
|
|
6
|
-
prettyJson?: boolean;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export const string = <TFallback = string>(
|
|
10
|
-
input: Maybe<unknown>,
|
|
11
|
-
settings?: Maybe<StringParserSettings<TFallback>>,
|
|
12
|
-
): string | TFallback => {
|
|
13
|
-
const fallback =
|
|
14
|
-
settings && 'fallback' in settings ? (settings.fallback as TFallback) : '';
|
|
15
|
-
|
|
16
|
-
if (input == null) {
|
|
17
|
-
return fallback;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (typeGuard.isObject(input)) {
|
|
21
|
-
if (settings?.prettyJson) {
|
|
22
|
-
return JSON.stringify(input, null, 2);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return JSON.stringify(input);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return String(input);
|
|
29
|
-
};
|
package/src/price.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
export interface PriceFormatOptions
|
|
2
|
-
extends Partial<Omit<Intl.NumberFormatOptions, 'currency'>> {
|
|
3
|
-
withoutSymbol?: boolean;
|
|
4
|
-
customSymbol?: string;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export const formatPrice = (
|
|
8
|
-
price: number,
|
|
9
|
-
locale: string,
|
|
10
|
-
currency?: string,
|
|
11
|
-
{ withoutSymbol, customSymbol, ...options }: PriceFormatOptions = {},
|
|
12
|
-
) => {
|
|
13
|
-
const priceFormatter = new Intl.NumberFormat(locale, {
|
|
14
|
-
style: 'currency',
|
|
15
|
-
currency,
|
|
16
|
-
minimumFractionDigits: 0,
|
|
17
|
-
currencyDisplay: 'narrowSymbol',
|
|
18
|
-
...options,
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
const zeroPrice = priceFormatter.format(0);
|
|
22
|
-
const currencySymbol = zeroPrice.replace('0', '');
|
|
23
|
-
const rawPrice = priceFormatter.format(price);
|
|
24
|
-
const priceWithoutCurrency = rawPrice.replace(currencySymbol, '');
|
|
25
|
-
|
|
26
|
-
if (withoutSymbol) {
|
|
27
|
-
return priceWithoutCurrency;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
return `${priceWithoutCurrency} ${
|
|
31
|
-
customSymbol ?? (currency === 'RUB' ? 'р' : currencySymbol)
|
|
32
|
-
}`.replace(/\s{2,}/, ' ');
|
|
33
|
-
};
|
package/src/random.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
export const getRandomFloat = <T extends number = number>(
|
|
2
|
-
min = 0,
|
|
3
|
-
max = 1,
|
|
4
|
-
): T => (Math.random() * (max - min) + min) as T;
|
|
5
|
-
|
|
6
|
-
export const getRandomInt = <T extends number = number>(min = 0, max = 1): T =>
|
|
7
|
-
min === max ? (min as T) : (Math.round(getRandomFloat(min, max)) as T);
|
|
8
|
-
|
|
9
|
-
export const getRandomChoice = <T>(arr: T[]): T =>
|
|
10
|
-
arr[getRandomInt(0, arr.length - 1)];
|
|
11
|
-
|
|
12
|
-
export const getRandomSizeArray = (min = 0, max = 10) =>
|
|
13
|
-
Array.from({ length: getRandomInt(min, max) }).fill(null);
|
|
14
|
-
|
|
15
|
-
export const getRandomBool = () => getRandomInt(0, 1) === 1;
|
|
16
|
-
|
|
17
|
-
export const getMajorRandomBool = () => {
|
|
18
|
-
return getRandomInt(0, 10) <= 6;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export const getMinorRandomBool = () => {
|
|
22
|
-
return !getMajorRandomBool();
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export const getFrequencyValue = (frequency: number) => {
|
|
26
|
-
return Math.random() < frequency;
|
|
27
|
-
};
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { useEffect } from 'react';
|
|
2
|
-
|
|
3
|
-
import { useConstant } from './use-constant.js';
|
|
4
|
-
|
|
5
|
-
export const useAbortController = () => {
|
|
6
|
-
const controller = useConstant(() => new AbortController());
|
|
7
|
-
|
|
8
|
-
useEffect(() => {
|
|
9
|
-
return () => {
|
|
10
|
-
controller.abort();
|
|
11
|
-
};
|
|
12
|
-
}, []);
|
|
13
|
-
|
|
14
|
-
return controller;
|
|
15
|
-
};
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { MutableRefObject } from 'react';
|
|
2
|
-
import { useEventListener } from './use-event-listener.js';
|
|
3
|
-
|
|
4
|
-
type ClickOutsideInput = {
|
|
5
|
-
contentRef: MutableRefObject<HTMLElement | null>;
|
|
6
|
-
onClick: VoidFunction;
|
|
7
|
-
options?: AddEventListenerOptions;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export const useClickOutside = ({
|
|
11
|
-
contentRef,
|
|
12
|
-
onClick,
|
|
13
|
-
options,
|
|
14
|
-
}: ClickOutsideInput) => {
|
|
15
|
-
useEventListener({
|
|
16
|
-
event: 'mousedown',
|
|
17
|
-
handler: (event) => {
|
|
18
|
-
if (
|
|
19
|
-
contentRef.current &&
|
|
20
|
-
!contentRef.current.contains(event.target as Node)
|
|
21
|
-
) {
|
|
22
|
-
onClick();
|
|
23
|
-
}
|
|
24
|
-
},
|
|
25
|
-
options,
|
|
26
|
-
});
|
|
27
|
-
};
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
2
|
-
import { useEffect } from 'react';
|
|
3
|
-
import { useSyncRef } from './use-sync-ref.js';
|
|
4
|
-
|
|
5
|
-
export const useEventListener = <EventName extends keyof HTMLElementEventMap>({
|
|
6
|
-
event,
|
|
7
|
-
handler,
|
|
8
|
-
options,
|
|
9
|
-
deps = [],
|
|
10
|
-
node = document,
|
|
11
|
-
}: {
|
|
12
|
-
event: EventName;
|
|
13
|
-
handler: (e: HTMLElementEventMap[EventName]) => void;
|
|
14
|
-
options?: boolean | AddEventListenerOptions;
|
|
15
|
-
deps?: unknown[];
|
|
16
|
-
node?: HTMLElement | Document | Window;
|
|
17
|
-
}) => {
|
|
18
|
-
const handlerRef = useSyncRef(handler);
|
|
19
|
-
|
|
20
|
-
useEffect(() => {
|
|
21
|
-
const handleEvent = (e: HTMLElementEventMap[EventName]) =>
|
|
22
|
-
handlerRef.current(e);
|
|
23
|
-
|
|
24
|
-
// @ts-expect-error
|
|
25
|
-
node.addEventListener(event, handleEvent, options);
|
|
26
|
-
// @ts-expect-error
|
|
27
|
-
return () => node.removeEventListener(event, handleEvent, options);
|
|
28
|
-
}, deps);
|
|
29
|
-
};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A Hook to define an event handler with an always-stable function identity.
|
|
3
|
-
*
|
|
4
|
-
* borrowed from @gaeron
|
|
5
|
-
* https://github.com/reactjs/rfcs/blob/useevent/text/0000-useevent.md
|
|
6
|
-
*/
|
|
7
|
-
import { useCallback, useLayoutEffect, useRef } from 'react';
|
|
8
|
-
import type { AnyFunction } from '../../utils/types.js';
|
|
9
|
-
|
|
10
|
-
export const useEvent = <H extends AnyFunction>(handler: H): H => {
|
|
11
|
-
const handlerRef = useRef<H>(handler);
|
|
12
|
-
|
|
13
|
-
// In a real implementation, this would run before layout effects
|
|
14
|
-
useLayoutEffect(() => {
|
|
15
|
-
handlerRef.current = handler;
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
return useCallback((...args: unknown[]) => {
|
|
19
|
-
// In a real implementation, this would throw if called during render
|
|
20
|
-
const fn = handlerRef.current as AnyFunction;
|
|
21
|
-
return fn(...args);
|
|
22
|
-
}, []) as unknown as H;
|
|
23
|
-
};
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { useCallback, useRef, useState } from 'react';
|
|
2
|
-
|
|
3
|
-
export interface FlagHook {
|
|
4
|
-
enabled: boolean;
|
|
5
|
-
toggle: VoidFunction;
|
|
6
|
-
enable: VoidFunction;
|
|
7
|
-
disable: VoidFunction;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export const useFlag = (defaultValue = false): FlagHook => {
|
|
11
|
-
const [enabled, setEnabled] = useState(defaultValue);
|
|
12
|
-
|
|
13
|
-
const toggle = useCallback(() => setEnabled((value) => !value), []);
|
|
14
|
-
const enable = useCallback(() => setEnabled(true), []);
|
|
15
|
-
const disable = useCallback(() => setEnabled(false), []);
|
|
16
|
-
|
|
17
|
-
const flagObjRef = useRef<FlagHook>({
|
|
18
|
-
enabled,
|
|
19
|
-
toggle,
|
|
20
|
-
enable,
|
|
21
|
-
disable,
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
flagObjRef.current.enabled = enabled;
|
|
25
|
-
|
|
26
|
-
return flagObjRef.current;
|
|
27
|
-
};
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { useEffect, useRef, useState } from 'react';
|
|
2
|
-
|
|
3
|
-
export const useInitialHeight = <T extends HTMLElement>() => {
|
|
4
|
-
const ref = useRef<T | null>(null);
|
|
5
|
-
const [initialHeight, setInitialHeight] = useState<number | undefined>(
|
|
6
|
-
undefined,
|
|
7
|
-
);
|
|
8
|
-
|
|
9
|
-
useEffect(() => {
|
|
10
|
-
if (ref.current && !initialHeight) {
|
|
11
|
-
setInitialHeight(ref.current.offsetHeight);
|
|
12
|
-
}
|
|
13
|
-
}, [initialHeight]);
|
|
14
|
-
|
|
15
|
-
return { ref, initialHeight };
|
|
16
|
-
};
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { useEffect, useState } from 'react';
|
|
2
|
-
|
|
3
|
-
export const useIntersectionObserver = (
|
|
4
|
-
callback: IntersectionObserverCallback,
|
|
5
|
-
options?: IntersectionObserverInit,
|
|
6
|
-
) => {
|
|
7
|
-
const [intersectionObserver] = useState(
|
|
8
|
-
() => new IntersectionObserver(callback, options),
|
|
9
|
-
);
|
|
10
|
-
|
|
11
|
-
useEffect(() => {
|
|
12
|
-
return () => {
|
|
13
|
-
intersectionObserver.disconnect();
|
|
14
|
-
};
|
|
15
|
-
}, []);
|
|
16
|
-
|
|
17
|
-
return intersectionObserver;
|
|
18
|
-
};
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { useEffect } from 'react';
|
|
2
|
-
import { useSyncRef } from './use-sync-ref.js';
|
|
3
|
-
|
|
4
|
-
export const useLifeCycle = (
|
|
5
|
-
fn: () => {
|
|
6
|
-
mount?: VoidFunction;
|
|
7
|
-
unmount?: VoidFunction;
|
|
8
|
-
},
|
|
9
|
-
) => {
|
|
10
|
-
const fnRef = useSyncRef(fn);
|
|
11
|
-
|
|
12
|
-
useEffect(() => {
|
|
13
|
-
const fnOperation = fnRef.current();
|
|
14
|
-
fnOperation.mount?.();
|
|
15
|
-
return fnOperation.unmount?.();
|
|
16
|
-
}, []);
|
|
17
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { useLayoutEffect } from 'react';
|
|
2
|
-
import { useDefineRef } from './use-define-ref.js';
|
|
3
|
-
|
|
4
|
-
export const useResizeObserver = (callback: ResizeObserverCallback) => {
|
|
5
|
-
const resizeObserverRef = useDefineRef(() => new ResizeObserver(callback));
|
|
6
|
-
|
|
7
|
-
useLayoutEffect(() => {
|
|
8
|
-
return () => {
|
|
9
|
-
resizeObserverRef.current.disconnect();
|
|
10
|
-
};
|
|
11
|
-
}, []);
|
|
12
|
-
|
|
13
|
-
return resizeObserverRef;
|
|
14
|
-
};
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { useCallback, useState } from 'react';
|
|
2
|
-
|
|
3
|
-
export const useToggle = (initialState?: boolean) => {
|
|
4
|
-
const [toggled, setToggled] = useState(!!initialState);
|
|
5
|
-
|
|
6
|
-
const toggle = useCallback(() => setToggled((toggled) => !toggled), []);
|
|
7
|
-
|
|
8
|
-
return [toggled, toggle, setToggled] as const;
|
|
9
|
-
};
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { useEffect, useState } from 'react';
|
|
2
|
-
|
|
3
|
-
export const useVisibilityState = () => {
|
|
4
|
-
const [state, setState] = useState<DocumentVisibilityState>();
|
|
5
|
-
|
|
6
|
-
useEffect(() => {
|
|
7
|
-
const handleVisibilityChange = () => {
|
|
8
|
-
setState(document.visibilityState);
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
document.addEventListener('visibilitychange', handleVisibilityChange);
|
|
12
|
-
|
|
13
|
-
return () => {
|
|
14
|
-
document.removeEventListener('visibilitychange', handleVisibilityChange);
|
|
15
|
-
};
|
|
16
|
-
}, []);
|
|
17
|
-
|
|
18
|
-
return state;
|
|
19
|
-
};
|
package/src/sound.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Воспроизводит звук из файла
|
|
3
|
-
*/
|
|
4
|
-
export const playSound = async (
|
|
5
|
-
file: string,
|
|
6
|
-
{ volume = 1 }: { volume?: number } = {},
|
|
7
|
-
) => {
|
|
8
|
-
let audio = new Audio(file);
|
|
9
|
-
audio.volume = volume;
|
|
10
|
-
audio.muted = !volume;
|
|
11
|
-
await audio.play();
|
|
12
|
-
audio.remove();
|
|
13
|
-
// @ts-expect-error
|
|
14
|
-
audio = null;
|
|
15
|
-
};
|