Skip to content

Commit

Permalink
feat: add deepArrayIncludes
Browse files Browse the repository at this point in the history
  • Loading branch information
Arcath committed May 14, 2021
1 parent f6fbf2e commit aa2b140
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/functions/deep-includes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {deepIncludesArray} from '../index'

describe('Deep Includes', () => {
test('it should return the correct value', () => {
const array = [
[1, 'two', 3],
[4, 'five', 6],
[7, 'eight', 9],
[10, 'eleven', 12]
]

expect(deepIncludesArray(array, [1, 'two', 3])).toBe(true)
expect(deepIncludesArray(array, [1, 2, 3])).toBe(false)
})
})
25 changes: 25 additions & 0 deletions src/functions/deep-includes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Check if an array exists inside an array
*
* @param array The arraay of arrays to check
* @param compare The array to check for existance of
* @returns `true` id the exact array matches, `false` if not.
*/
export const deepIncludesArray = <T extends any[]>(
array: T[],
compare: T
): boolean => {
return array.reduce<boolean>((includes, value) => {
if (includes) {
return true
}

return value.reduce<boolean>((match, v, i) => {
if (!match) {
return false
}

return v === compare[i]
}, true)
}, false)
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export {
numberToVh,
numberToVw
} from './functions/css'
export {deepIncludesArray} from './functions/deep-includes'
export {defaults} from './functions/defaults'
export {diffArray} from './functions/diff-array'
export {diffObject} from './functions/diff-object'
Expand Down

0 comments on commit aa2b140

Please sign in to comment.