-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1064 from terrestris/add-array-util
Add array util
- Loading branch information
Showing
2 changed files
with
37 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { joinArrayWith } from './ArrayUtil'; | ||
|
||
describe('ArrayUtil', () => { | ||
|
||
it('is defined', () => { | ||
expect(joinArrayWith).not.toBe(undefined); | ||
}); | ||
|
||
it('works as expected', () => { | ||
expect(joinArrayWith([1, 2, 3], 'a')).toStrictEqual([1, 'a', 2, 'a', 3]); | ||
}); | ||
|
||
it('works with an empty array', () => { | ||
expect(joinArrayWith([], 'a')).toStrictEqual([]); | ||
}); | ||
|
||
}); |
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,20 @@ | ||
/** | ||
* Works like the standard .join, except it returns a new array with the value inserted between all values of the array | ||
* instead of a string. | ||
* | ||
* @param list any array | ||
* @param value any value | ||
* @returns the new array | ||
*/ | ||
export const joinArrayWith = (list: any[], value: any) => { | ||
const newList: any[] = []; | ||
list.forEach((item, idx) => { | ||
if (idx === 0) { | ||
newList.push(item); | ||
} else { | ||
newList.push(value); | ||
newList.push(item); | ||
} | ||
}); | ||
return newList; | ||
}; |