bigbluebutton-Github/bigbluebutton-html5/imports/utils/array-utils.js

40 lines
945 B
JavaScript
Raw Normal View History

2023-02-24 01:38:48 +08:00
import { isObject } from 'radash';
export const range = (start, end) => {
const length = end - start;
return Array.from({ length }, (_, i) => start + i);
};
2023-03-02 03:48:08 +08:00
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);
2023-02-23 20:45:30 +08:00
2023-03-02 03:48:08 +08:00
export const without = (arr, value) => arr.filter((item) => item !== value);
2023-02-23 20:45:30 +08:00
2023-02-24 01:38:48 +08:00
export const defaultsDeep = (override, initial) => {
2023-03-02 03:48:08 +08:00
if (!initial || !override) return initial ?? override ?? {};
2023-02-24 01:38:48 +08:00
return Object.entries({ ...initial, ...override }).reduce(
2023-03-02 03:48:08 +08:00
(acc, [key, value]) => ({
...acc,
[key]: (() => {
if (isObject(initial[key])) {
return defaultsDeep(value, initial[key]);
}
return value;
})(),
}), {},
);
};
2023-02-24 01:38:48 +08:00
export default {
range,
partition,
2023-02-23 20:45:30 +08:00
indexOf,
without,
2023-02-24 01:38:48 +08:00
defaultsDeep,
};