-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsplit.test.ts
More file actions
181 lines (161 loc) · 5.75 KB
/
split.test.ts
File metadata and controls
181 lines (161 loc) · 5.75 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import * as assert from "node:assert";
import { split } from "./index.ts";
describe("split", () => {
describe("Split strings", () => {
it("should split a string by a substring", () => {
assert.deepStrictEqual(
split("Hello, World!", ""),
["H", "e", "l", "l", "o", ",", " ", "W", "o", "r", "l", "d", "!"]
);
assert.deepStrictEqual(split("Hello, World!", ", "), ["Hello", "World!"]);
});
it("should split a string by a regular expression", () => {
assert.deepStrictEqual(
split("Hello, World!", /,\s*/),
["Hello", "World!"]
);
});
it("should split a string by a length", () => {
assert.deepStrictEqual(
split("Hello, World!", 3),
["Hel", "lo,", " Wo", "rld", "!"]
);
});
});
describe("Split number", () => {
it("should split a number into stepping serials", () => {
assert.deepStrictEqual(
split(100, 10),
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
);
assert.deepStrictEqual(
split(105, 10),
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 105]
);
});
});
describe("Split Array", () => {
it("should split an array into chunks", () => {
assert.deepStrictEqual(
split(["Hello", "World", "Hi", "Ayon"], 2),
[["Hello", "World"], ["Hi", "Ayon"]]
);
});
it("should split an array-like object into chunks", () => {
let args: ArrayLike<number> | null = null;
// @ts-ignore
(function () { args = arguments; })(1, 2, 3, 4, 5, 6, 7, 8, 9);
assert.deepStrictEqual(
split(args, 2),
[[1, 2], [3, 4], [5, 6], [7, 8], [9]]
);
});
});
describe("Split Buffer", () => {
it("should split a Buffer into chunks", function () {
if (!(typeof Buffer === "function")) {
this.skip();
}
assert.deepStrictEqual(
split(Buffer.from([1, 2, 3, 4]), 2),
[Buffer.from([1, 2]), Buffer.from([3, 4])]
);
});
it("should split a Buffer into chunks by a separator", function () {
if (!(typeof Buffer === "function")) {
this.skip();
}
assert.deepStrictEqual(
split(Buffer.from("Hello, World, Hi, Ayon"), ", "),
[
Buffer.from("Hello"),
Buffer.from("World"),
Buffer.from("Hi"),
Buffer.from("Ayon")
]
);
assert.deepStrictEqual(
split(Buffer.from("Hello, World, Hi, Ayon"), Buffer.from(", ")),
[
Buffer.from("Hello"),
Buffer.from("World"),
Buffer.from("Hi"),
Buffer.from("Ayon")
]
);
});
});
describe("Split TypedArray", () => {
it("should split a TypedArray instance into chunks", () => {
assert.deepStrictEqual(
split(Uint8Array.from([1, 2, 3, 4]), 2),
[Uint8Array.from([1, 2]), Uint8Array.from([3, 4])]
);
});
});
describe("Split ArrayBuffer", () => {
it("should split a ArrayBuffer into chunks", () => {
assert.deepStrictEqual(
split(new ArrayBuffer(8), 2),
[
new ArrayBuffer(2),
new ArrayBuffer(2),
new ArrayBuffer(2),
new ArrayBuffer(2)
]
);
});
if (typeof SharedArrayBuffer === "function") {
it("should split a ShareArrayBuffer instance into chunks", () => {
assert.deepStrictEqual(
split(new SharedArrayBuffer(8), 2),
[
new SharedArrayBuffer(2),
new SharedArrayBuffer(2),
new SharedArrayBuffer(2),
new SharedArrayBuffer(2)
]
);
});
}
});
describe("Split Collection", () => {
it("should split a Set instance into chunks", () => {
assert.deepStrictEqual(
split(new Set(["Hello", "World", "Hi", "Ayon"]), 2),
[new Set(["Hello", "World"]), new Set(["Hi", "Ayon"])]
);
});
it("should split a Map instance into chunks", () => {
assert.deepStrictEqual(
split(new Map([["Hello", "World"], ["Hi", "Ayon"]]), 1),
[new Map([["Hello", "World"]]), new Map([["Hi", "Ayon"]])]
);
});
});
describe("Split Object", () => {
it("should split a plain object into chunks", () => {
assert.deepStrictEqual(
split({ hello: "world", hi: "ayon", foo: "bar" }, 2),
[
{ hello: "world", hi: "ayon" },
{ foo: "bar" }
]
);
});
it("should split a class instance into chunks", () => {
class Test {
constructor(data: any) {
Object.assign(this, data);
}
}
assert.deepStrictEqual(
split(new Test({ hello: "world", hi: "ayon", foo: "bar" }), 2),
[
new Test({ hello: "world", hi: "ayon" }),
new Test({ foo: "bar" })
]
);
});
});
});