@lumx/core 3.20.1-alpha.28 → 3.20.1-alpha.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,13 @@
1
1
  /** Chunk array in slices of given size */
2
2
  function chunk(input, size) {
3
- return input.reduce((arr, item, idx) => {
4
- return idx % size === 0 ? [...arr, [item]] : [...arr.slice(0, -1), [...arr.slice(-1)[0], item]];
5
- }, []);
3
+ if (size <= 0) {
4
+ throw new Error('Size must be greater than 0');
5
+ }
6
+ const result = [];
7
+ for (let i = 0; i < input.length; i += size) {
8
+ result.push(input.slice(i, i + size));
9
+ }
10
+ return result;
6
11
  }
7
12
 
8
13
  export { chunk };
@@ -10,4 +10,19 @@ describe(chunk, () => {
10
10
  it('should chunk array with size not perfectly dividing the array length', () => {
11
11
  expect(chunk([1, 2, 3, 4, 5], 2)).toEqual([[1, 2], [3, 4], [5]]);
12
12
  });
13
+ it('should chunk array with size perfectly dividing the array length', () => {
14
+ expect(chunk([1, 2, 3, 4], 2)).toEqual([[1, 2], [3, 4]]);
15
+ });
16
+ it('should work with size of 1', () => {
17
+ expect(chunk([1, 2, 3], 1)).toEqual([[1], [2], [3]]);
18
+ });
19
+ it('should work with size equal to array length', () => {
20
+ expect(chunk([1, 2, 3], 3)).toEqual([[1, 2, 3]]);
21
+ });
22
+ it('should throw error when size is 0', () => {
23
+ expect(() => chunk([1, 2, 3], 0)).toThrow('Size must be greater than 0');
24
+ });
25
+ it('should throw error when size is negative', () => {
26
+ expect(() => chunk([1, 2, 3], -1)).toThrow('Size must be greater than 0');
27
+ });
13
28
  });
@@ -12,4 +12,27 @@ describe(chunk, () => {
12
12
  it('should chunk array with size not perfectly dividing the array length', () => {
13
13
  expect(chunk([1, 2, 3, 4, 5], 2)).toEqual([[1, 2], [3, 4], [5]]);
14
14
  });
15
+
16
+ it('should chunk array with size perfectly dividing the array length', () => {
17
+ expect(chunk([1, 2, 3, 4], 2)).toEqual([
18
+ [1, 2],
19
+ [3, 4],
20
+ ]);
21
+ });
22
+
23
+ it('should work with size of 1', () => {
24
+ expect(chunk([1, 2, 3], 1)).toEqual([[1], [2], [3]]);
25
+ });
26
+
27
+ it('should work with size equal to array length', () => {
28
+ expect(chunk([1, 2, 3], 3)).toEqual([[1, 2, 3]]);
29
+ });
30
+
31
+ it('should throw error when size is 0', () => {
32
+ expect(() => chunk([1, 2, 3], 0)).toThrow('Size must be greater than 0');
33
+ });
34
+
35
+ it('should throw error when size is negative', () => {
36
+ expect(() => chunk([1, 2, 3], -1)).toThrow('Size must be greater than 0');
37
+ });
15
38
  });
@@ -1,6 +1,11 @@
1
1
  /** Chunk array in slices of given size */
2
2
  export function chunk<T>(input: Array<T>, size: number): T[][] {
3
- return input.reduce((arr, item, idx) => {
4
- return idx % size === 0 ? [...arr, [item]] : [...arr.slice(0, -1), [...arr.slice(-1)[0], item]];
5
- }, [] as T[][]);
3
+ if (size <= 0) {
4
+ throw new Error('Size must be greater than 0');
5
+ }
6
+ const result: T[][] = [];
7
+ for (let i = 0; i < input.length; i += size) {
8
+ result.push(input.slice(i, i + size));
9
+ }
10
+ return result;
6
11
  }
@@ -6,6 +6,7 @@ describe(isEmpty, () => {
6
6
  expect(isEmpty(null)).toBe(true);
7
7
  expect(isEmpty(0)).toBe(true);
8
8
  expect(isEmpty('')).toBe(true);
9
+ expect(isEmpty(false)).toBe(true);
9
10
  });
10
11
  it('should return true for empty object or array', () => {
11
12
  expect(isEmpty([])).toBe(true);
@@ -6,6 +6,7 @@ describe(isEmpty, () => {
6
6
  expect(isEmpty(null)).toBe(true);
7
7
  expect(isEmpty(0)).toBe(true);
8
8
  expect(isEmpty('')).toBe(true);
9
+ expect(isEmpty(false)).toBe(true);
9
10
  });
10
11
 
11
12
  it('should return true for empty object or array', () => {
@@ -1,4 +1,4 @@
1
1
  import type { Falsy } from '../../types';
2
2
 
3
3
  /** Check if object or array is empty (true on falsy values) */
4
- export const isEmpty = (obj: {} | Falsy) => !obj || Object.entries(obj).length === 0;
4
+ export const isEmpty = (obj: NonNullable<unknown> | Falsy) => !obj || Object.entries(obj).length === 0;
@@ -0,0 +1,19 @@
1
+ import { last } from '@lumx/core/js/utils/collection/last';
2
+
3
+ describe(last, () => {
4
+ it('should return undefined for empty array', () => {
5
+ const input = [];
6
+ const output = last(input);
7
+ expect(output).toBeUndefined();
8
+ });
9
+ it('should return last element from array with single element', () => {
10
+ const input = [42];
11
+ const output = last(input);
12
+ expect(output).toBe(42);
13
+ });
14
+ it('should return last element from array with multiple elements', () => {
15
+ const input = [1, 2, 3, 4, 5];
16
+ const output = last(input);
17
+ expect(output).toBe(5);
18
+ });
19
+ });
@@ -0,0 +1,21 @@
1
+ import { last } from '@lumx/core/js/utils/collection/last';
2
+
3
+ describe(last, () => {
4
+ it('should return undefined for empty array', () => {
5
+ const input: number[] = [];
6
+ const output = last(input);
7
+ expect(output).toBeUndefined();
8
+ });
9
+
10
+ it('should return last element from array with single element', () => {
11
+ const input = [42];
12
+ const output = last(input);
13
+ expect(output).toBe(42);
14
+ });
15
+
16
+ it('should return last element from array with multiple elements', () => {
17
+ const input = [1, 2, 3, 4, 5];
18
+ const output = last(input);
19
+ expect(output).toBe(5);
20
+ });
21
+ });
@@ -2,6 +2,7 @@ import { range } from './range.js';
2
2
 
3
3
  describe(range, () => {
4
4
  it('should generate a number range', () => {
5
+ expect(range(-2)).toEqual([]);
5
6
  expect(range(0)).toEqual([]);
6
7
  expect(range(1)).toEqual([0]);
7
8
  expect(range(5)).toEqual([0, 1, 2, 3, 4]);
@@ -2,6 +2,7 @@ import { range } from './range';
2
2
 
3
3
  describe(range, () => {
4
4
  it('should generate a number range', () => {
5
+ expect(range(-2)).toEqual([]);
5
6
  expect(range(0)).toEqual([]);
6
7
  expect(range(1)).toEqual([0]);
7
8
  expect(range(5)).toEqual([0, 1, 2, 3, 4]);
package/package.json CHANGED
@@ -35,7 +35,7 @@
35
35
  "update-version-changelog": "yarn version-changelog ../../CHANGELOG.md"
36
36
  },
37
37
  "sideEffects": false,
38
- "version": "3.20.1-alpha.28",
38
+ "version": "3.20.1-alpha.29",
39
39
  "devDependencies": {
40
40
  "@babel/preset-typescript": "^7.26.0",
41
41
  "@rollup/plugin-babel": "^6.0.4",
@@ -1,9 +0,0 @@
1
- /** Pull an element from an array (inverse of array.push) */
2
- const pull = (array, element) => {
3
- const index = array.indexOf(element);
4
- if (index > -1) {
5
- array.splice(index, 1);
6
- }
7
- };
8
-
9
- export { pull };
@@ -1,16 +0,0 @@
1
- import { pull } from '@lumx/core/js/utils/collection//pull';
2
-
3
- describe(pull, () => {
4
- it('should do nothing if element does not exist', () => {
5
- const a = [1, 2];
6
- pull(a, 0);
7
- expect(a).toBe(a);
8
- expect(a).toEqual([1, 2]);
9
- });
10
- it('should pull an element from the array', () => {
11
- const a = [1, 2];
12
- pull(a, 1);
13
- expect(a).toBe(a);
14
- expect(a).toEqual([2]);
15
- });
16
- });
@@ -1,17 +0,0 @@
1
- import { pull } from '@lumx/core/js/utils/collection//pull';
2
-
3
- describe(pull, () => {
4
- it('should do nothing if element does not exist', () => {
5
- const a = [1, 2];
6
- pull(a, 0);
7
- expect(a).toBe(a);
8
- expect(a).toEqual([1, 2]);
9
- });
10
-
11
- it('should pull an element from the array', () => {
12
- const a = [1, 2];
13
- pull(a, 1);
14
- expect(a).toBe(a);
15
- expect(a).toEqual([2]);
16
- });
17
- });
@@ -1,7 +0,0 @@
1
- /** Pull an element from an array (inverse of array.push) */
2
- export const pull = <T>(array: Array<T>, element: T) => {
3
- const index = array.indexOf(element);
4
- if (index > -1) {
5
- array.splice(index, 1);
6
- }
7
- };