@takeshape/util 11.127.2 → 11.130.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.
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export function coerceToString(value) {
|
|
2
|
+
if (typeof value === 'string') {
|
|
3
|
+
return value;
|
|
4
|
+
}
|
|
5
|
+
if (typeof value === 'number' || typeof value === 'boolean') {
|
|
6
|
+
return String(value);
|
|
7
|
+
}
|
|
8
|
+
if (value === null || value === undefined) {
|
|
9
|
+
return '';
|
|
10
|
+
}
|
|
11
|
+
throw new Error(`Cannot coerce ${typeof value} to string`);
|
|
12
|
+
}
|
|
13
|
+
export function coerceToNumber(value) {
|
|
14
|
+
if (typeof value === 'number') {
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
if (typeof value === 'string') {
|
|
18
|
+
const parsed = Number.parseFloat(value);
|
|
19
|
+
if (!Number.isNaN(parsed)) {
|
|
20
|
+
return parsed;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
else if (typeof value === 'boolean') {
|
|
24
|
+
return value ? 1 : 0;
|
|
25
|
+
}
|
|
26
|
+
throw new Error(`Cannot convert "${value}" to number`);
|
|
27
|
+
}
|
|
28
|
+
export function coerceToInteger(value) {
|
|
29
|
+
const num = coerceToNumber(value);
|
|
30
|
+
return Math.round(num);
|
|
31
|
+
}
|
|
32
|
+
export function coerceToBoolean(value) {
|
|
33
|
+
if (typeof value === 'boolean') {
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
if (typeof value === 'string') {
|
|
37
|
+
const lower = value.toLowerCase().trim();
|
|
38
|
+
if (lower === 'true' || lower === '1' || lower === 'yes') {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
if (lower === 'false' || lower === '0' || lower === 'no' || lower === '') {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
throw new Error(`Cannot convert "${value}" to boolean`);
|
|
45
|
+
}
|
|
46
|
+
if (typeof value === 'number') {
|
|
47
|
+
return value !== 0;
|
|
48
|
+
}
|
|
49
|
+
throw new Error(`Cannot coerce ${typeof value} to boolean`);
|
|
50
|
+
}
|
package/dist/common/index.d.ts
CHANGED
package/dist/common/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@takeshape/util",
|
|
3
|
-
"version": "11.
|
|
3
|
+
"version": "11.130.0",
|
|
4
4
|
"description": "Shared utilities",
|
|
5
5
|
"homepage": "https://www.takeshape.io",
|
|
6
6
|
"repository": {
|
|
@@ -45,8 +45,8 @@
|
|
|
45
45
|
"tiny-invariant": "1.3.3",
|
|
46
46
|
"uint8array-extras": "1.4.0",
|
|
47
47
|
"url-parse": "1.5.3",
|
|
48
|
-
"@takeshape/
|
|
49
|
-
"@takeshape/
|
|
48
|
+
"@takeshape/routing": "11.130.0",
|
|
49
|
+
"@takeshape/prism": "11.130.0"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@types/classnames": "2.2.11",
|