-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
66 lines (60 loc) · 2.45 KB
/
test.js
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
// Adapted to work in both Node.js and the browser
import jsSubtype from './index.js';
function testSubtypes() {
const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined';
let testValues = [
"", // empty string
" ", // whitespace string
"Hello, World!", // character string
0, // integer
42.5, // float
NaN, // NaN
Infinity, // Infinity
-Infinity, // -Infinity
-0, // -0
true, // boolean true
false, // boolean false
undefined, // undefined
null, // null
[], // array
new Date(), // date
/abc/, // regexp
new Map(), // map
new Set(), // set
new WeakMap(), // weakmap
new WeakSet(), // weakset
typeof WeakRef !== "undefined" ? new WeakRef({}) : null, // weakref
new Promise((resolve) => resolve()), // promise
new Error("Test Error"), // error
new ArrayBuffer(8), // arraybuffer
typeof SharedArrayBuffer !== "undefined" ? new SharedArrayBuffer(8) : null, // sharedarraybuffer
new DataView(new ArrayBuffer(16)), // dataview
new Int8Array(8), // typed array
function () { }, // function
async function () { }, // async function
function* () { }, // generator function
Symbol("test"), // symbol
BigInt(12345), // bigint
typeof Intl !== "undefined" && Intl.Collator ? new Intl.Collator() : null, // intl collator
typeof Intl !== "undefined" && Intl.DateTimeFormat ? new Intl.DateTimeFormat() : null, // intl datetimeformat
typeof Intl !== "undefined" && Intl.NumberFormat ? new Intl.NumberFormat() : null, // intl numberformat
Object.create(null), // object without prototype
Object.create({}), // object with custom prototype
{ a: 1, b: 2 }, // JSON-like object
typeof Proxy !== "undefined" ? new Proxy({}, {}) : null, // Proxy
{ toString: () => { throw new Error('non-serializable'); } } // non-serializable object
];
if (isBrowser) {
testValues.push(
document, // document
document.createElement("div"), // html element
window // window
);
}
const filteredTestValues = testValues.filter(value => value !== null);
filteredTestValues.forEach((value, index) => {
const result = jsSubtype(value);
console.log(`Test #${index + 1}:`, result);
});
}
testSubtypes();