@nemigo/helpers 0.4.4 → 0.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/cases.d.ts +6 -6
- package/dist/cases.js +9 -9
- package/dist/format.js +1 -1
- package/dist/xod.js +3 -3
- package/package.json +1 -1
package/dist/cases.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export type CaseResult<V, T> = (value: V |
|
|
1
|
+
export type CaseResult<V, T> = (value: V | undefined, mutate: boolean) => T;
|
|
2
2
|
/**
|
|
3
3
|
* Отлов строковых значений
|
|
4
4
|
*
|
|
5
5
|
* - `string` → `string`, `false`
|
|
6
6
|
* - `number` → `string`, `true`
|
|
7
|
-
* - `NaN` → `
|
|
8
|
-
* - `ETC` → `
|
|
7
|
+
* - `NaN` → `undefined`, `false`
|
|
8
|
+
* - `ETC` → `undefined`, `false`
|
|
9
9
|
*/
|
|
10
10
|
export declare const caseString: <T>(value: unknown, call: CaseResult<string, T>) => T;
|
|
11
11
|
/**
|
|
@@ -13,8 +13,8 @@ export declare const caseString: <T>(value: unknown, call: CaseResult<string, T>
|
|
|
13
13
|
*
|
|
14
14
|
* - `number` → `number`, `false`
|
|
15
15
|
* - `string` → `number`, `true`
|
|
16
|
-
* - `NaN` → `
|
|
17
|
-
* - `ETC` → `
|
|
16
|
+
* - `NaN` → `undefined`, `false`
|
|
17
|
+
* - `ETC` → `undefined`, `false`
|
|
18
18
|
*/
|
|
19
19
|
export declare const caseNumber: <T>(value: unknown, call: CaseResult<number, T>) => T;
|
|
20
20
|
/**
|
|
@@ -25,6 +25,6 @@ export declare const caseNumber: <T>(value: unknown, call: CaseResult<number, T>
|
|
|
25
25
|
* - `"true"` -> `boolean`, `true`
|
|
26
26
|
* - `0` -> `boolean`, `true`
|
|
27
27
|
* - `1` -> `boolean`, `true`
|
|
28
|
-
* - `ETC` → `
|
|
28
|
+
* - `ETC` → `undefined`, `false`
|
|
29
29
|
*/
|
|
30
30
|
export declare const caseBoolean: <T>(value: unknown, call: CaseResult<boolean, T>) => T;
|
package/dist/cases.js
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
*
|
|
4
4
|
* - `string` → `string`, `false`
|
|
5
5
|
* - `number` → `string`, `true`
|
|
6
|
-
* - `NaN` → `
|
|
7
|
-
* - `ETC` → `
|
|
6
|
+
* - `NaN` → `undefined`, `false`
|
|
7
|
+
* - `ETC` → `undefined`, `false`
|
|
8
8
|
*/
|
|
9
9
|
export const caseString = (value, call) => {
|
|
10
10
|
switch (typeof value) {
|
|
@@ -14,20 +14,20 @@ export const caseString = (value, call) => {
|
|
|
14
14
|
if (!isNaN(value))
|
|
15
15
|
return call(String(value), true);
|
|
16
16
|
}
|
|
17
|
-
return call(
|
|
17
|
+
return call(undefined, false);
|
|
18
18
|
};
|
|
19
19
|
/**
|
|
20
20
|
* Отлов числовых значений
|
|
21
21
|
*
|
|
22
22
|
* - `number` → `number`, `false`
|
|
23
23
|
* - `string` → `number`, `true`
|
|
24
|
-
* - `NaN` → `
|
|
25
|
-
* - `ETC` → `
|
|
24
|
+
* - `NaN` → `undefined`, `false`
|
|
25
|
+
* - `ETC` → `undefined`, `false`
|
|
26
26
|
*/
|
|
27
27
|
export const caseNumber = (value, call) => {
|
|
28
28
|
switch (typeof value) {
|
|
29
29
|
case "number":
|
|
30
|
-
return call(isNaN(value) ?
|
|
30
|
+
return call(isNaN(value) ? undefined : value, false);
|
|
31
31
|
case "string": {
|
|
32
32
|
const trimmed = value.trim();
|
|
33
33
|
const number = Number(trimmed);
|
|
@@ -35,7 +35,7 @@ export const caseNumber = (value, call) => {
|
|
|
35
35
|
return call(number, true);
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
|
-
return call(
|
|
38
|
+
return call(undefined, false);
|
|
39
39
|
};
|
|
40
40
|
/**
|
|
41
41
|
* Отлов булевых значений
|
|
@@ -45,7 +45,7 @@ export const caseNumber = (value, call) => {
|
|
|
45
45
|
* - `"true"` -> `boolean`, `true`
|
|
46
46
|
* - `0` -> `boolean`, `true`
|
|
47
47
|
* - `1` -> `boolean`, `true`
|
|
48
|
-
* - `ETC` → `
|
|
48
|
+
* - `ETC` → `undefined`, `false`
|
|
49
49
|
*/
|
|
50
50
|
export const caseBoolean = (value, call) => {
|
|
51
51
|
switch (typeof value) {
|
|
@@ -65,5 +65,5 @@ export const caseBoolean = (value, call) => {
|
|
|
65
65
|
return call(true, true);
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
|
-
return call(
|
|
68
|
+
return call(undefined, false);
|
|
69
69
|
};
|
package/dist/format.js
CHANGED
|
@@ -7,7 +7,7 @@ export const FormatNumberRegExp = /\B(?=(\d{3})+(?!\d))/g;
|
|
|
7
7
|
* @example "1 234" или "1 234,56"
|
|
8
8
|
*/
|
|
9
9
|
export const formatNumber = (amount, { fraction = 2, fallback = "---", zero = "value", postfix } = {}) => caseNumber(amount, (v) => {
|
|
10
|
-
if (v ===
|
|
10
|
+
if (v === undefined)
|
|
11
11
|
return zero === "always" ? (postfix ? "0 " + postfix : "0") : fallback;
|
|
12
12
|
if (v === 0)
|
|
13
13
|
return zero === "never" ? fallback : postfix ? "0 " + postfix : "0";
|
package/dist/xod.js
CHANGED
|
@@ -27,7 +27,7 @@ export const nullSchema = z.union([
|
|
|
27
27
|
}),
|
|
28
28
|
]);
|
|
29
29
|
//...
|
|
30
|
-
export const booleanSchema = z.unknown().transform((value, ctx) => caseBoolean(value, (result) => result ===
|
|
30
|
+
export const booleanSchema = z.unknown().transform((value, ctx) => caseBoolean(value, (result) => result === undefined
|
|
31
31
|
? invalid(value, ctx, {
|
|
32
32
|
message: "It isn't a boolean",
|
|
33
33
|
expected: "boolean (or string / number like boolean)",
|
|
@@ -35,7 +35,7 @@ export const booleanSchema = z.unknown().transform((value, ctx) => caseBoolean(v
|
|
|
35
35
|
: result));
|
|
36
36
|
export const booleanSchemaOptional = z.union([nullSchema, booleanSchema]);
|
|
37
37
|
//...
|
|
38
|
-
export const numberSchema = z.unknown().transform((value, ctx) => caseNumber(value, (result) => result ===
|
|
38
|
+
export const numberSchema = z.unknown().transform((value, ctx) => caseNumber(value, (result) => result === undefined
|
|
39
39
|
? invalid(value, ctx, {
|
|
40
40
|
message: "It isn't a number",
|
|
41
41
|
expected: "number (or string like number)",
|
|
@@ -44,7 +44,7 @@ export const numberSchema = z.unknown().transform((value, ctx) => caseNumber(val
|
|
|
44
44
|
export const numberSchemaOptional = z.union([nullSchema, numberSchema]);
|
|
45
45
|
//...
|
|
46
46
|
export const stringSchema = z.unknown().transform((value, ctx) => caseString(value, (result) => {
|
|
47
|
-
const trimmed = result ===
|
|
47
|
+
const trimmed = result === undefined ? "" : result.trim();
|
|
48
48
|
return trimmed === ""
|
|
49
49
|
? invalid(value, ctx, {
|
|
50
50
|
message: "It isn't a string or empty string",
|