Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Removed

## [1.9.0] - 2021-11-18

### Added

- `chunk`

### Changed

### Fixed

### Removed

## [1.8.1] - 2021-11-13

### Added
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ npm i @techmmunity/utils

| function | description |
| ---------- | ----------------------------------------------- |
| `chunk` | Chunks an array |
| `cleanObj` | Remove undefined and null values from an object |
| `sleep` | Await the amount of time specified (in seconds) |
| `nest` | Nest objects that have been unnested |
Expand Down
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ module.exports = {
resetMocks: true,
coverageThreshold: {
global: {
statements: 99.1,
statements: 99.15,
branches: 97.5,
functions: 100,
lines: 99.68,
lines: 99.7,
},
},
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@techmmunity/utils",
"version": "1.8.1",
"version": "1.9.0",
"main": "index.js",
"types": "index.d.ts",
"license": "Apache-2.0",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* ---------------------------------------------------------------------------
*/

export * from "./lib/chunk";
export * from "./lib/clean-obj";
export * from "./lib/sleep";
export * from "./lib/nest";
Expand Down
24 changes: 24 additions & 0 deletions src/lib/chunk/index.ts
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;
};
67 changes: 67 additions & 0 deletions src/tests/chunk.spec.ts
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");
});
});
});