-
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 29, 2024
1 parent
89dff0c
commit 51262b4
Showing
3 changed files
with
101 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
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,42 @@ | ||
import { isArray } from './_internals/isArray.js'; | ||
import { curry } from './curry.js'; | ||
|
||
export function swapArrayOrString(indexA, indexB, iterable) { | ||
const actualIndexA = indexA < 0 ? iterable.length + indexA : indexA; | ||
const actualIndexB = indexB < 0 ? iterable.length + indexB : indexB; | ||
if ( | ||
actualIndexA === actualIndexB || | ||
Math.min(actualIndexA, actualIndexB) < 0 || | ||
Math.max(actualIndexA, actualIndexB) >= iterable.length | ||
) | ||
return iterable; | ||
if (typeof iterable === 'string') { | ||
return ( | ||
iterable.slice(0, actualIndexA) + | ||
iterable[actualIndexB] + | ||
iterable.slice(actualIndexA + 1, actualIndexB) + | ||
iterable[actualIndexA] + | ||
iterable.slice(actualIndexB + 1) | ||
); | ||
} | ||
const clone = iterable.slice(); | ||
const temp = clone[actualIndexA]; | ||
clone[actualIndexA] = clone[actualIndexB]; | ||
clone[actualIndexB] = temp; | ||
return clone; | ||
} | ||
export function swapFn(indexA, indexB, iterable) { | ||
if (isArray(iterable) || typeof iterable === 'string') | ||
return swapArrayOrString(indexA, indexB, iterable); | ||
|
||
const aVal = iterable[indexA]; | ||
const bVal = iterable[indexB]; | ||
if (aVal === undefined || bVal === undefined) return iterable; | ||
return { | ||
...iterable, | ||
[indexA]: iterable[indexB], | ||
[indexB]: iterable[indexA], | ||
}; | ||
} | ||
|
||
export const swap = curry(swapFn); |
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,40 @@ | ||
import { swap } from './swap'; | ||
const list = ['a', 'b', 'c', 'd', 'e', 'f']; | ||
|
||
it('swaps an element from one index to the other', () => { | ||
expect(swap(0, 1, list)).toEqual(['b', 'a', 'c', 'd', 'e', 'f']); | ||
expect(swap(2, 1, list)).toEqual(['a', 'c', 'b', 'd', 'e', 'f']); | ||
expect(swap(-1, 0, list)).toEqual(['f', 'b', 'c', 'd', 'e', 'a']); | ||
expect(swap(4, 1, list)).toEqual(['a', 'e', 'c', 'd', 'b', 'f']); | ||
}); | ||
|
||
it('does nothing when indexes are outside the list boundaries', () => { | ||
expect(swap(-20, 2, list)).toEqual(list); | ||
expect(swap(20, 2, list)).toEqual(list); | ||
expect(swap(2, 20, list)).toEqual(list); | ||
expect(swap(2, -20, list)).toEqual(list); | ||
expect(swap(20, 20, list)).toEqual(list); | ||
expect(swap(-20, -20, list)).toEqual(list); | ||
}); | ||
|
||
it('does nothing when indexes are equal', () => { | ||
expect(swap(0, 0, list)).toEqual(list); | ||
}); | ||
|
||
it('should be the same when swapping index order', () => { | ||
expect(swap(0, 1, list)).toEqual(swap(1, 0, list)); | ||
}); | ||
|
||
it('swaps property values from one property to another', () => { | ||
expect(swap('a', 'b', { a: 1, b: 2 })).toEqual({ a: 2, b: 1 }); | ||
expect(swap('b', 'a', { a: 1, b: 2 })).toEqual({ a: 2, b: 1 }); | ||
}); | ||
|
||
it('does nothing when property names are not defined', () => { | ||
expect(swap('a', 'b', { a: 1 })).toEqual({ a: 1 }); | ||
expect(swap('a', 'b', { b: 2 })).toEqual({ b: 2 }); | ||
}); | ||
|
||
it('swaps characters in string from one index to another', () => { | ||
expect(swap(0, 2, 'foo')).toEqual('oof'); | ||
}); |