Skip to content

Commit 85087ae

Browse files
committed
feat(pkg:string): added API String.toChunks
1 parent 8b934cb commit 85087ae

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as NodeTest from 'node:test';
2+
import * as NodeAssert from 'node:assert';
3+
import { toChunks} from './ToChunks';
4+
5+
NodeTest.describe('Function String.toChunks', () => {
6+
7+
NodeTest.it('Should return empty if the string is empty', () => {
8+
9+
NodeAssert.deepStrictEqual(toChunks('', 1), []);
10+
NodeAssert.deepStrictEqual(toChunks('', 1000), []);
11+
});
12+
13+
NodeTest.it('Should throw exception if chunk size is not a positive integer', () => {
14+
15+
NodeAssert.throws(() => toChunks('abc', 0), RangeError);
16+
NodeAssert.throws(() => toChunks('abc', -1), RangeError);
17+
NodeAssert.throws(() => toChunks('abc', 1.5), RangeError);
18+
NodeAssert.throws(() => toChunks('abc', NaN), RangeError);
19+
NodeAssert.throws(() => toChunks('abc', Infinity), RangeError);
20+
});
21+
22+
NodeTest.it('Should return chunks of the specified size', () => {
23+
24+
NodeAssert.deepStrictEqual(toChunks('abcde', 2), ['ab', 'cd', 'e']);
25+
NodeAssert.deepStrictEqual(toChunks('abcdefghij', 3), ['abc', 'def', 'ghi', 'j']);
26+
NodeAssert.deepStrictEqual(toChunks('abcdefghij', 5), ['abcde', 'fghij']);
27+
NodeAssert.deepStrictEqual(toChunks('abcdefghij', 10), ['abcdefghij']);
28+
NodeAssert.deepStrictEqual(toChunks('abcdefghi', 10), ['abcdefghi']);
29+
});
30+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright 2025 Angus.Fenying <[email protected]>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* This function splits a string into chunks of specified size.
19+
* If the string length is not a multiple of the chunk size,
20+
* the last chunk will contain the remaining characters.
21+
*
22+
* @param text The string to be split into chunks.
23+
* @param chunkSize The size of each chunk. Must be a positive integer.
24+
*
25+
* @returns An array of strings, each representing a chunk of the original string.
26+
* @throws RangeError if the chunk size is not a positive integer.
27+
* @example `toChunks('abcdefghij', 3)` returns `['abc', 'def', 'ghi', 'j']`
28+
*/
29+
export function toChunks(text: string, chunkSize: number): string[] {
30+
31+
if (chunkSize <= 0 || !Number.isSafeInteger(chunkSize)) {
32+
33+
throw new RangeError('Chunk size must be a positive integer.');
34+
}
35+
36+
const chunks: string[] = new Array<string>(Math.ceil(text.length / chunkSize));
37+
const length = text.length;
38+
39+
for (let i = 0, o = 0; i < length; i += chunkSize) {
40+
chunks[o++] = text.slice(i, i + chunkSize);
41+
}
42+
43+
return chunks;
44+
}

packages/partials/string/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ export * from './Functions/EvilSpace';
1919
export * from './Functions/Random';
2020
export * from './Functions/RegexpEscape';
2121
export * from './Functions/Html';
22+
export * from './Functions/ToChunks';
2223
export * from './Classes/UnitParser';

0 commit comments

Comments
 (0)