2022-02-04 00:13:04 +00:00
|
|
|
export function filterDefined<T>(array: (T | null | undefined)[]) {
|
2022-03-24 17:03:08 +00:00
|
|
|
return array.filter((item) => item !== null && item !== undefined) as T[]
|
2022-02-04 00:13:04 +00:00
|
|
|
}
|
2022-08-10 17:17:33 +00:00
|
|
|
|
|
|
|
export function buildArray<T>(
|
|
|
|
...params: (T | T[] | false | undefined | null)[]
|
|
|
|
) {
|
|
|
|
const array: T[] = []
|
|
|
|
|
|
|
|
for (const el of params) {
|
|
|
|
if (Array.isArray(el)) {
|
|
|
|
array.push(...el)
|
|
|
|
} else if (el) {
|
|
|
|
array.push(el)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array
|
|
|
|
}
|
2022-08-13 03:42:58 +00:00
|
|
|
|
|
|
|
export function groupConsecutive<T, U>(xs: T[], key: (x: T) => U) {
|
|
|
|
if (!xs.length) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
const result = []
|
|
|
|
let curr = { key: key(xs[0]), items: [xs[0]] }
|
|
|
|
for (const x of xs.slice(1)) {
|
|
|
|
const k = key(x)
|
|
|
|
if (k !== curr.key) {
|
|
|
|
result.push(curr)
|
|
|
|
curr = { key: k, items: [x] }
|
|
|
|
} else {
|
|
|
|
curr.items.push(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.push(curr)
|
|
|
|
return result
|
|
|
|
}
|