Skip to content

Commit d85ab8b

Browse files
test helper.filterArrayUntilFirstValue
1 parent 03fba17 commit d85ab8b

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/utils/helper.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ helper.getArraysDiff = function (arr1, arr2) {
4949
};
5050
helper.filterArrayUntilFirstValue = (arr, callback, isRightToLeft) => {
5151
isRightToLeft && arr.reverse();
52-
for (let i = 0, l = arr.length; i < l; i++) if (callback(arr[i], i, arr)) return arr[i];
52+
for (let i = 0, l = arr.length; i < l; i++) {
53+
if (callback(arr[i], i, arr)) return arr[i];
54+
}
5355
return null;
5456
};
5557
helper.throwMissingParam = (FnName) => {

src/utils/helper.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,30 @@ describe('helper : ', () => {
3030
expect(copyState2.openTabIDs != state2.openTabIDs).toBe(true);
3131
});
3232
});
33+
describe('helper.filterArrayUntilFirstValue : ', () => {
34+
test('filterArrayUntilFirstValue function should work correctly and it may change the original array', () => {
35+
expect.assertions(3);
36+
const arr = ['1', '33', '2', '3', '22', '4'];
37+
const result1 = helper.filterArrayUntilFirstValue(arr, (item, index, _arr) => {
38+
if (item.includes('3')) {
39+
expect(_arr).toEqual(arr);
40+
}
41+
return item.includes('3');
42+
});
43+
expect(result1).toBe('33');
44+
const result2 = helper.filterArrayUntilFirstValue(arr, (item) => item.includes('2'), true);
45+
expect(result2).toBe('22');
46+
});
47+
test('it may change the original array', () => {
48+
const arr = ['1', '33', '2', '3', '22', '4'];
49+
const originalArr = [...arr];
50+
expect(originalArr).toEqual(arr);
51+
helper.filterArrayUntilFirstValue(arr, (item) => item.includes('2'), true);
52+
expect(originalArr).not.toEqual(arr);
53+
});
54+
test('it should return null if there is not desired element in the array', () => {
55+
const arr = ['1', '2'];
56+
const result = helper.filterArrayUntilFirstValue(arr, (item) => item > 5);
57+
expect(result).toBe(null);
58+
});
59+
});

0 commit comments

Comments
 (0)