UNPKG

ractive-ez-table

Version:
80 lines (64 loc) 1.75 kB
const utils = { hasOwnProperty(obj, key) { return obj != null && {}.hasOwnProperty.call(obj, key); }, prop1(path) { return obj => utils.hasOwnProperty(obj, path) ? obj[path] : null; }, prop(path) { const chain = path.split("."); const proto = Object.prototype; if (chain.length == 1) return utils.prop1(path); return obj => { for (let i = 0; i < chain.length; i++) { const key = chain[i]; if (!utils.hasOwnProperty(obj, key)) return null; obj = obj[key]; } return obj; }; }, indexOf(items, path, value) { const prop = utils.prop(path); for (let i = 0; i < items.length; i++) { if (prop(items[i]) == value) return i; } return -1; }, find(items, path, value) { const index = utils.indexOf(items, path, value); return index == -1 ? null : items[index]; }, compare(a, b) { if (a == b) return 0; if (a < b) return -1; return 1; }, setImmediate(callback) { window.setImmediate ? window.setImmediate(callback) : setTimeout(callback, 0); }, createScrollHandler(callback, ms = 100) { let didScroll = false; let timer = null; return event => { didScroll = true; if (timer == null) { timer = setTimeout(() => { if (didScroll) { didScroll = false; callback(); } clearTimeout(timer); timer = null; }, ms); } }; } }; export default utils;