bigbluebutton-Github/bigbluebutton-html5/imports/utils/array-utils.js
2023-03-01 16:48:08 -03:00

40 lines
945 B
JavaScript

import { isObject } from 'radash';
export const range = (start, end) => {
const length = end - start;
return Array.from({ length }, (_, i) => start + i);
};
export const partition = (arr, criteria) => [
arr.filter((item) => criteria(item)),
arr.filter((item) => !criteria(item)),
];
export const indexOf = (arr, value) => (arr ? arr.findIndex((item) => item === value) : -1);
export const without = (arr, value) => arr.filter((item) => item !== value);
export const defaultsDeep = (override, initial) => {
if (!initial || !override) return initial ?? override ?? {};
return Object.entries({ ...initial, ...override }).reduce(
(acc, [key, value]) => ({
...acc,
[key]: (() => {
if (isObject(initial[key])) {
return defaultsDeep(value, initial[key]);
}
return value;
})(),
}), {},
);
};
export default {
range,
partition,
indexOf,
without,
defaultsDeep,
};