Skip to content

Commit

Permalink
feat(utils): pull
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
  • Loading branch information
unicornware committed May 25, 2023
1 parent 409ca18 commit 1fb621e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/utils/__tests__/pull.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @file Unit Tests - pull
* @module tutils/utils/tests/unit/pull
*/

import testSubject from '../pull'

describe('unit:utils/pull', () => {
it('should return array without items in drop', () => {
// Arrange
const array: number[] = [0, 1, 2, 3, 4, 5]
const drop: number[] = [3, 4, 5]

// Expect
expect(testSubject(array, drop, null)).to.deep.equal([0, 1, 2])
})
})
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export { default as isUndefined } from './is-undefined'
export { default as isUppercase } from './is-uppercase'
export { default as lowercase } from './lowercase'
export { default as passthrough } from './passthrough'
export { default as pull } from './pull'
export { default as select } from './select'
export { default as split } from './split'
export { default as timeiso } from './timeiso'
Expand Down
29 changes: 29 additions & 0 deletions src/utils/pull.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @file Utilities - pull
* @module tutils/utils/pull
*/

import type { Fn, IndexSignature, Nilable, NumberString } from '#src/types'
import includes from './includes'
import select from './select'

/**
* Removes all items in `drop` from `array` without modifying `array`.
*
* @template T - Array item type
* @template K - Identity key type
*
* @param {ReadonlyArray<T>} array - Array to evaluate
* @param {ReadonlyArray<T>} drop - Items to remove
* @param {Nilable<Fn<[T], K>>} [identity] - Identity key function
* @return {T[]} New array without items in `drop`
*/
function pull<T, K extends IndexSignature = NumberString>(
array: readonly T[],
drop: readonly T[],
identity?: Nilable<Fn<[T], K>>
): T[] {
return select(array, (item: T): boolean => !includes(drop, item, identity))
}

export default pull

0 comments on commit 1fb621e

Please sign in to comment.