@gitlab/ui
Version:
GitLab UI Components
47 lines (42 loc) • 2.17 kB
JavaScript
function _arrayLikeToArray(r, a) {
(null == a || a > r.length) && (a = r.length);
for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
return n;
}
function _arrayWithHoles(r) {
if (Array.isArray(r)) return r;
}
function _iterableToArray(r) {
if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r);
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _toArray(r) {
return _arrayWithHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableRest();
}
function _unsupportedIterableToArray(r, a) {
if (r) {
if ("string" == typeof r) return _arrayLikeToArray(r, a);
var t = {}.toString.call(r).slice(8, -1);
return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
}
}
const getRepeatingValue = index => {
const values = [100, 500, 400, 200, 100, 800, 400, 500, 600, 300, 800, 900, 110, 700, 400, 300, 500, 300, 400, 600, 700];
return index < values.length ? values[index] : values[index % values.length];
};
const generateTimeSeries = () => new Array(100).fill(0).map((el, i) => [new Date(2018, 0, i), getRepeatingValue(i)]);
// takes an element and a list and `intersperses' that element between the elements of the list.
// (',' ['a', 'b', 'c']) -> ['a', ',', 'b', ',', 'c']
const intersperse = (separator, items) => {
const _items = _toArray(items),
head = _items[0],
rest = _arrayLikeToArray(_items).slice(1);
const separatorFactory = typeof separator === 'function' ? separator : () => separator;
return [head, ...rest.flatMap(item => [separatorFactory(), item], rest)];
};
// inserts a value at a given index into an array
// (1, 2, [1, 3, 4]) -> [1, 2, 3, 4]
const insert = (index, newItem, items) => [...items.slice(0, index), newItem, ...items.slice(index)];
export { generateTimeSeries, insert, intersperse };