Skip to content

isSubset

Static Badge

Input two arrays arr1 and arr2, and return whether arr1 is subset of arr2.

Accepts a getter, which can be a field path of getByPath or a callback function, used to provide an identifier to distinguish elements.

Added in v0.1.0

Usage

ts
import { isSubset } from 'parsnip-kit'

const arr0 = [1, 2]
const arr1 = [1, 1, 2]
const arr2 = [1, 2, 3]

isSubset(arr0, arr2) // true
isSubset(arr1, arr2) // false

const getter = (product: Product, index: number, arr: Product[]) => 
  `${product.id}-${index}`
const subset = [{ id: 'p1', name: 'Laptop', price: 1000 }]
const superset = [
  { id: 'p1', name: 'Laptop', price: 1000 },
  { id: 'p2', name: 'Mouse', price: 50 }
]
isSubset(subset, superset, getter) // true

API

Type Parameter

ArgTypeDescription
TThe type of element of input arr

Arguments

ArgTypeOptionalDefaultDescription
arr1T[]falseundefinedThe candidate subset array
arr2T[]falseundefinedThe superset array to check against
getterstring | ((item: T, index: number, arr: T[]) => any)trueundefinedProvide an identifier to distinguish the elements

Returns

Type
boolean