UNPKG

@kahi-ui/framework

Version:

Straight-forward Svelte UI for the Web

49 lines (48 loc) 1.58 kB
export function chunk(array, length) { return array.reduce((accum, date, index) => { const chunk_index = Math.floor(index / length); if (!accum[chunk_index]) accum[chunk_index] = []; accum[chunk_index].push(date); return accum; }, []); } export function debounce(func, duration = 0) { let identifier; return (...args) => { if (identifier !== undefined) { clearTimeout(identifier); identifier = undefined; } // @ts-ignore - HACK: NodeJS doesn't follow spec identifier = setTimeout(() => func(...args), duration); }; } export function defaultopt(value, default_value) { for (const key in value) { if (typeof value[key] !== "undefined") return value; } return default_value; } export function fill(generator, length) { return new Array(length).fill(null).map((_, index) => generator(index)); } export function pick(map, keys) { const lookup = new Set(keys); const entries = Array.from(map.entries()).filter(([key, value], index) => lookup.has(key)); return new Map(entries); } export function range(minimum, maximum) { return new Array(maximum - minimum + 1).fill(null).map((_, index) => minimum + index); } export function throttle(func, duration = 0) { let previous_call = Number.MIN_SAFE_INTEGER; return (...args) => { const current_call = Date.now(); if (current_call - previous_call >= duration) { func(...args); previous_call = current_call; } }; }