Skip to content

Commit 18d98f5

Browse files
committed
feat(pkg:string): added API String.toChunksBackward
1 parent 85087ae commit 18d98f5

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

packages/partials/string/src/Functions/ToChunks.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as NodeTest from 'node:test';
22
import * as NodeAssert from 'node:assert';
3-
import { toChunks} from './ToChunks';
3+
import { toChunks, toChunksBackward} from './ToChunks';
44

55
NodeTest.describe('Function String.toChunks', () => {
66

@@ -26,5 +26,35 @@ NodeTest.describe('Function String.toChunks', () => {
2626
NodeAssert.deepStrictEqual(toChunks('abcdefghij', 5), ['abcde', 'fghij']);
2727
NodeAssert.deepStrictEqual(toChunks('abcdefghij', 10), ['abcdefghij']);
2828
NodeAssert.deepStrictEqual(toChunks('abcdefghi', 10), ['abcdefghi']);
29+
NodeAssert.deepStrictEqual(toChunks('abcdefghij', 1), ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']);
30+
});
31+
});
32+
33+
NodeTest.describe('Function String.toChunksBackward', () => {
34+
35+
NodeTest.it('Should return empty if the string is empty', () => {
36+
37+
NodeAssert.deepStrictEqual(toChunksBackward('', 1), []);
38+
NodeAssert.deepStrictEqual(toChunksBackward('', 1000), []);
39+
});
40+
41+
NodeTest.it('Should throw exception if chunk size is not a positive integer', () => {
42+
43+
NodeAssert.throws(() => toChunksBackward('abc', 0), RangeError);
44+
NodeAssert.throws(() => toChunksBackward('abc', -1), RangeError);
45+
NodeAssert.throws(() => toChunksBackward('abc', 1.5), RangeError);
46+
NodeAssert.throws(() => toChunksBackward('abc', NaN), RangeError);
47+
NodeAssert.throws(() => toChunksBackward('abc', Infinity), RangeError);
48+
});
49+
50+
NodeTest.it('Should return chunks of the specified size', () => {
51+
52+
NodeAssert.deepStrictEqual(toChunksBackward('abcde', 2), ['a', 'bc', 'de']);
53+
NodeAssert.deepStrictEqual(toChunksBackward('abcdefghij', 3), ['a', 'bcd', 'efg', 'hij']);
54+
NodeAssert.deepStrictEqual(toChunksBackward('abcdefghij', 5), ['abcde', 'fghij']);
55+
NodeAssert.deepStrictEqual(toChunksBackward('abcdefghij', 10), ['abcdefghij']);
56+
NodeAssert.deepStrictEqual(toChunksBackward('abcdefghi', 10), ['abcdefghi']);
57+
NodeAssert.deepStrictEqual(toChunksBackward('abcdefghij', 1), ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']);
58+
NodeAssert.deepStrictEqual(toChunksBackward('abcdefghij', 11), ['abcdefghij']);
2959
});
3060
});

packages/partials/string/src/Functions/ToChunks.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,16 @@
2323
* @param chunkSize The size of each chunk. Must be a positive integer.
2424
*
2525
* @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']`
26+
* @throws `RangeError` if the chunk size is not a positive integer.
27+
* @example `toChunks('abcdefghij', 3) -> ['abc', 'def', 'ghi', 'j']`
2828
*/
2929
export function toChunks(text: string, chunkSize: number): string[] {
3030

31+
if (chunkSize === 1) {
32+
33+
return text.split('');
34+
}
35+
3136
if (chunkSize <= 0 || !Number.isSafeInteger(chunkSize)) {
3237

3338
throw new RangeError('Chunk size must be a positive integer.');
@@ -42,3 +47,39 @@ export function toChunks(text: string, chunkSize: number): string[] {
4247

4348
return chunks;
4449
}
50+
51+
/**
52+
* This function splits a string into chunks of specified size.
53+
* Different from `toChunks`, this function reverses the order of the chunks,
54+
* which means the first chunk will contain the last characters of the string,
55+
* when the input string is not a multiple of the chunk size,
56+
*
57+
* @param text The string to be split into chunks.
58+
* @param chunkSize The size of each chunk. Must be a positive integer.
59+
*
60+
* @returns An array of strings, each representing a chunk of the original string.
61+
* @throws `RangeError` if the chunk size is not a positive integer.
62+
* @example `toChunksBackward('abcdefghij', 3) -> ['a', 'bcd', 'efg', 'hij']`
63+
*/
64+
export function toChunksBackward(text: string, chunkSize: number): string[] {
65+
66+
if (chunkSize === 1) {
67+
68+
return text.split('');
69+
}
70+
71+
if (chunkSize <= 0 || !Number.isSafeInteger(chunkSize)) {
72+
73+
throw new RangeError('Chunk size must be a positive integer.');
74+
}
75+
76+
const chunks: string[] = new Array<string>(Math.ceil(text.length / chunkSize));
77+
const length = text.length;
78+
79+
for (let i = length, o = chunks.length; i > 0; i -= chunkSize) {
80+
81+
chunks[--o] = text.slice(Math.max(0, i - chunkSize), i);
82+
}
83+
84+
return chunks;
85+
}

0 commit comments

Comments
 (0)