-
Notifications
You must be signed in to change notification settings - Fork 89
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
Deyan Totev
committed
Jan 25, 2024
1 parent
98ae0bc
commit f23f54e
Showing
5 changed files
with
99 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,23 @@ | ||
import { insert } from './insert'; | ||
|
||
describe('insert', () => { | ||
it('inserts an element into the given list', () => { | ||
const list = ['a', 'b', 'c', 'd', 'e']; | ||
expect(insert(2, 'x', list)).toEqual(['a', 'b', 'x', 'c', 'd', 'e']); | ||
}); | ||
it('inserts an element into the given list', () => { | ||
const list = ['a', 'b', 'c', 'd', 'e']; | ||
expect(insert(2, 'x', list)).toEqual(['a', 'b', 'x', 'c', 'd', 'e']); | ||
}); | ||
|
||
it('inserts another list as an element', () => { | ||
const list = ['a', 'b', 'c', 'd', 'e']; | ||
expect(insert(2, ['s', 't'], list)).toEqual([ | ||
'a', | ||
'b', | ||
['s', 't'], | ||
'c', | ||
'd', | ||
'e', | ||
]); | ||
}); | ||
it('inserts another list as an element', () => { | ||
const list = ['a', 'b', 'c', 'd', 'e']; | ||
expect(insert(2, ['s', 't'], list)).toEqual([ | ||
'a', | ||
'b', | ||
['s', 't'], | ||
'c', | ||
'd', | ||
'e', | ||
]); | ||
}); | ||
|
||
it('appends to the end of the list if the index is too large', () => { | ||
const list = ['a', 'b', 'c', 'd', 'e']; | ||
expect(insert(8, 'z', list)).toEqual(['a', 'b', 'c', 'd', 'e', 'z']); | ||
}); | ||
it('appends to the end of the list if the index is too large', () => { | ||
const list = ['a', 'b', 'c', 'd', 'e']; | ||
expect(insert(8, 'z', list)).toEqual(['a', 'b', 'c', 'd', 'e', 'z']); | ||
}); |
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,7 @@ | ||
import { curry } from './curry'; | ||
|
||
export function insertAllFn(index, listToInsert, list) { | ||
return [...list.slice(0, index), ...listToInsert, ...list.slice(index)]; | ||
} | ||
|
||
export const insertAll = curry(insertAllFn); |
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,29 @@ | ||
import { insertAll } from './insertAll'; | ||
|
||
it('inserts a list of elements into the given list', () => { | ||
const list = ['a', 'b', 'c', 'd', 'e']; | ||
expect(insertAll(2, ['x', 'y', 'z'], list)).toEqual([ | ||
'a', | ||
'b', | ||
'x', | ||
'y', | ||
'z', | ||
'c', | ||
'd', | ||
'e', | ||
]); | ||
}); | ||
|
||
it('appends to the end of the list if the index is too large', () => { | ||
const list = ['a', 'b', 'c', 'd', 'e']; | ||
expect(insertAll(8, ['p', 'q', 'r'], list)).toEqual([ | ||
'a', | ||
'b', | ||
'c', | ||
'd', | ||
'e', | ||
'p', | ||
'q', | ||
'r', | ||
]); | ||
}); |