Skip to content
This repository was archived by the owner on Jan 12, 2023. It is now read-only.

Commit 8d3c70a

Browse files
author
Kevin Hermawan
committed
refactor: changes swap args
1 parent b76c2c5 commit 8d3c70a

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ Exchanges the element at the specified indices of the array.
174174
```ts
175175
import { swap } from 'enhanced-array';
176176

177-
const result = swap([1, 2, 3, 4, 5], { i: 0, j: 4 });
177+
const result = swap([1, 2, 3, 4, 5], { index: 0, withIndex: 4 });
178178
console.log(result); // [5, 2, 3, 4, 1]
179179
```
180180

src/swap.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
type SwapArgs = {
2-
i: number;
3-
j: number;
2+
index: number;
3+
withIndex: number;
44
};
55

66
export function swap<T>(array: T[], args: SwapArgs) {
7-
const { i, j } = args;
7+
const { index, withIndex } = args;
88
const result = array;
99

10-
const temp = result[i];
11-
result[i] = result[j];
12-
result[j] = temp;
10+
const temp = result[index];
11+
result[index] = result[withIndex];
12+
result[withIndex] = temp;
1313

1414
return result;
1515
}

test/swap.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { swap } from '../src/swap';
44

55
describe('swap', () => {
66
it('swaps element correctly', () => {
7-
const result = swap([1, 2, 3, 4, 5], { i: 0, j: 4 });
7+
const result = swap([1, 2, 3, 4, 5], { index: 0, withIndex: 4 });
88

99
expect(result).to.eql([5, 2, 3, 4, 1]);
1010
});

0 commit comments

Comments
 (0)