Skip to content

Commit

Permalink
add "split" to pointer API
Browse files Browse the repository at this point in the history
  • Loading branch information
MZanggl committed Feb 23, 2023
1 parent 74f7328 commit d2ee379
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,14 @@ Removes the current index and returns a new array.
given.array(['music', 'tech']).point(1).remove() // ['music']
```

#### split

Splits the array at the current index

```javascript
given.array(['a', 'is', 'c']).point(1).split() // [['a'], ['c']]
```

#### value

<!-- array.point.value -->
Expand Down
9 changes: 9 additions & 0 deletions src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ export function point<T>(value: T[], indexOrFn: number | ((item: T) => boolean))
value() {
return array[index]
},

/**
* Splits the array at the current index
*/
split() {
const left = array.slice(0, index)
const right = array.slice(index + 1)
return [left, right]
},
/**
* Steps forward or backward given the number of steps.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/objects/Arrayable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ class Arrayable<T> extends Array<T> {
set(callback: (item: T) => T) {
return nativePointer.set(callback)
},

/**
* Splits the array at the current index
*/
split() {
return nativePointer.split()
},

/**
* Appends given value to array in between the currently pointed item and its next item and returns a new array.
*/
Expand Down
9 changes: 9 additions & 0 deletions test/arrayable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,15 @@ test.group('Arrayable', () => {
assert.deepEqual(updated, ['a', 'c'])
assert.deepEqual(original, ['a', 'b', 'c'])
})

test('can split the array at specific pointer', (assert) => {
const original = given.array(['a', 'is', 'c'])
const [left, right] = original.point(1).split()
isArr(assert, left, original)
isArr(assert, right, original)
assert.deepEqual(left, ['a'])
assert.deepEqual(right, ['c'])
})

test('can read value from current position', assert => {
const value = given.array(['a', 'b', 'c']).point(-1).value()
Expand Down

0 comments on commit d2ee379

Please sign in to comment.