generated from techmmunity/base-project-packages
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5aa8f69
commit c5ecedb
Showing
7 changed files
with
108 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* eslint-disable no-mixed-operators */ | ||
import { getTypeof } from "../get-typeof"; | ||
|
||
export const chunk = <T extends Array<any>>( | ||
arr: T, | ||
length: number, | ||
): Array<T> => { | ||
if (getTypeof(arr) !== "array") { | ||
throw new Error("Value must be an object"); | ||
} | ||
|
||
const acc: Array<T> = []; | ||
|
||
const chunks = Math.ceil(arr.length / length); | ||
|
||
for (let i = 0; i < chunks; i++) { | ||
const start = i * length; | ||
const end = start + length; | ||
|
||
acc.push(arr.slice(start, end) as T); | ||
} | ||
|
||
return acc; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* eslint-disable sonarjs/no-duplicate-string */ | ||
|
||
import { chunk } from "../lib/chunk"; | ||
|
||
describe("chunk Util", () => { | ||
const arr = ["foo", "bar", "abc", "xyz"]; | ||
|
||
describe("With exact number of items", () => { | ||
it("should return the chunked object", () => { | ||
let result: any; | ||
|
||
try { | ||
result = chunk(arr, 4); | ||
} catch (err: any) { | ||
result = err; | ||
} | ||
|
||
expect(result).toStrictEqual([["foo", "bar", "abc", "xyz"]]); | ||
}); | ||
}); | ||
|
||
describe("With exact number of items to multiple chunks", () => { | ||
it("should return the chunked object", () => { | ||
let result: any; | ||
|
||
try { | ||
result = chunk(arr, 2); | ||
} catch (err: any) { | ||
result = err; | ||
} | ||
|
||
expect(result).toStrictEqual([ | ||
["foo", "bar"], | ||
["abc", "xyz"], | ||
]); | ||
}); | ||
}); | ||
|
||
describe("With NOT exact number of items to multiple chunks", () => { | ||
it("should return the chunked object", () => { | ||
let result: any; | ||
|
||
try { | ||
result = chunk([...arr, "hgi"], 2); | ||
} catch (err: any) { | ||
result = err; | ||
} | ||
|
||
expect(result).toStrictEqual([["foo", "bar"], ["abc", "xyz"], ["hgi"]]); | ||
}); | ||
}); | ||
|
||
describe("General errors", () => { | ||
it("should return error with not array", () => { | ||
let result: any; | ||
|
||
try { | ||
result = chunk("foo" as any, 2); | ||
} catch (err: any) { | ||
result = err; | ||
} | ||
|
||
expect(result instanceof Error).toBeTruthy(); | ||
expect(result.message).toBe("Value must be an object"); | ||
}); | ||
}); | ||
}); |