-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathensureType.test.ts
134 lines (121 loc) · 4.36 KB
/
ensureType.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import * as assert from "node:assert";
import { ensureType } from "./index.ts";
describe("ensureType", () => {
describe("ensure booleans", () => {
it("should cast the value into boolean true", () => {
assert.ok(ensureType("true") === true);
assert.ok(ensureType("yes") === true);
assert.ok(ensureType("on") === true);
});
it("should cast the value into boolean false", () => {
assert.ok(ensureType("false") === false);
assert.ok(ensureType("no") === false);
assert.ok(ensureType("off") === false);
});
});
describe("ensure null", () => {
it("should cast the value into null", () => {
assert.ok(ensureType("null") === null);
assert.ok(ensureType("nil") === null);
assert.ok(ensureType("none") === null);
assert.ok(ensureType("void") === null);
assert.ok(ensureType("undefined") === null);
});
});
describe("ensure numbers", () => {
it("should cast the value into a number", () => {
assert.ok(ensureType("12345") === 12345);
assert.ok(ensureType("12345.123") === 12345.123);
assert.ok(ensureType("-12345.123") === -12345.123);
assert.ok(ensureType("9007199254740991") === Number.MAX_SAFE_INTEGER);
assert.ok(ensureType("-9007199254740991") === Number.MIN_SAFE_INTEGER);
assert.ok(Object.is(ensureType("NaN"), NaN));
assert.ok(ensureType("Infinity") === Infinity);
assert.ok(ensureType("-Infinity") === -Infinity);
});
it("should not cast if the numeric is greater than Number.MAX_SAFE_INTEGER", () => {
assert.ok(ensureType("9007199254740992") === "9007199254740992");
});
it("should not cast if the numeric is lower than Number.MAX_SAFE_INTEGER", () => {
assert.ok(ensureType("-9007199254740992") === "-9007199254740992");
});
it("should not cast if the numeric might be a phone number", () => {
assert.ok(ensureType("+8913800774500") === "+8913800774500");
});
});
describe("ensure objects", () => {
it("should recursively cast elements in an array", () => {
let source = [
"",
"string",
"true",
"false",
"null",
"12345",
[
"yes",
"no",
"nil"
]
];
let expected = [
"",
"string",
true,
false,
null,
12345,
[
true,
false,
null
]
];
assert.deepStrictEqual(ensureType(source), expected);
});
it("should recursively cast properties in an object", () => {
let source = {
str1: "",
str2: "string",
bool1: "true",
bool2: "false",
nil: "null",
num1: "12345",
num2: "12.345",
re: "/[a-z]/i",
child: {
bool3: "yes",
bool4: "no",
nil2: "nil"
}
};
let expected = {
str1: "",
str2: "string",
bool1: true,
bool2: false,
nil: null,
num1: 12345,
num2: 12.345,
re: /[a-z]/i,
child: {
bool3: true,
bool4: false,
nil2: null
}
};
assert.deepStrictEqual(ensureType(source), expected);
});
});
describe("ensure no cast", () => {
it("should not cast if the input value is not a string", () => {
assert.ok(ensureType(12345) === 12345);
assert.ok(ensureType(true) === true);
assert.ok(ensureType(null) === null);
});
it("should not cast if the string doesn't match any special pattern", () => {
assert.ok(ensureType("") === "")
assert.ok(ensureType("Hello, World!") === "Hello, World!");
});
});
});