diff --git a/src/functions/deep-includes.spec.ts b/src/functions/deep-includes.spec.ts new file mode 100644 index 0000000..58d83d4 --- /dev/null +++ b/src/functions/deep-includes.spec.ts @@ -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) + }) +}) diff --git a/src/functions/deep-includes.ts b/src/functions/deep-includes.ts new file mode 100644 index 0000000..c93e597 --- /dev/null +++ b/src/functions/deep-includes.ts @@ -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 = ( + array: T[], + compare: T +): boolean => { + return array.reduce((includes, value) => { + if (includes) { + return true + } + + return value.reduce((match, v, i) => { + if (!match) { + return false + } + + return v === compare[i] + }, true) + }, false) +} diff --git a/src/index.ts b/src/index.ts index bd87f37..fdbfb32 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'