commit 6d1d65783f782930f5a305bd6b01d0019fd5a118 Author: Clement Bois Date: Thu May 23 14:17:22 2019 +0200 Initial commit diff --git a/ObjectHelper.js b/ObjectHelper.js new file mode 100644 index 0000000..bcb06cf --- /dev/null +++ b/ObjectHelper.js @@ -0,0 +1,78 @@ +/** + * Wrapper for Object functions + */ +class ObjectHelper { + static values(obj) { + return Object.values(obj) + } + + static entries(obj) { + return Object.entries(obj) + } + + static reduce(obj, fn, init) { + return this.entries(obj).reduce(fn, init || {}) + } + + static reduceTo(arr, key_fn, val_fn, init) { + return arr.reduce((acc, el, id) => ({ + ...acc, [key_fn(el, id)]: val_fn(el, id) + }), init || {}) + } + + static map(obj, fn) { + return this.entries(obj).map(fn) + } + + static flatMap(obj, fn, unique = false) { + const map = this.entries(obj).flatMap(fn) + if (unique) { + return map.filter(UniqueList.filter) + } + return map + } + + static filter(obj, predicate) { + let filtered = {} + for (const key in obj) { + if (obj.hasOwnProperty(key) && predicate(key, obj[key])) { + filtered[key] = obj[key] + } + } + return filtered + } + + /** + * Map parent: { child } to 'parent.child' for fields + */ + static flattenTree(source, fields, split = '.') { + return fields.reduce((flat, field) => { + let parts = field.split(split) + let target = source + while (parts.length > 1) { // Walk tree + target = target[parts.shift()] || {} + } + flat[field] = target[parts.shift()] + return flat + }, {}) + } + + /** + * Map 'parent.child' to parent: { child } + */ + static expandTree(source, split = '.') { + return this.reduce(source, (tree, [key, value]) => { + let parts = key.split(split) + let target = tree + while (parts.length > 1) { // Create tree + let child = parts.shift() + if (target[child] == undefined) { + target[child] = {} + } + target = target[child] + } + target[parts.shift()] = value + return tree + }) + } +} \ No newline at end of file diff --git a/TimeHelper.js b/TimeHelper.js new file mode 100644 index 0000000..f4fa8ba --- /dev/null +++ b/TimeHelper.js @@ -0,0 +1,35 @@ +class TimeHelper { + static throttle(func, limit) { + let inThrottle + return function() { + const args = arguments + const context = this + if (!inThrottle) { + func.apply(context, args) + inThrottle = true + setTimeout(() => inThrottle = false, limit) + } + } + } + + static throttleLast(func, limit) { + let lastFunc + let lastRan + return function() { + const context = this + const args = arguments + if (!lastRan) { + func.apply(context, args) + lastRan = Date.now() + } else { + clearTimeout(lastFunc) + lastFunc = setTimeout(function() { + if ((Date.now() - lastRan) >= limit) { + func.apply(context, args) + lastRan = Date.now() + } + }, limit - (Date.now() - lastRan)) + } + } + } +} \ No newline at end of file diff --git a/UniqueList.js b/UniqueList.js new file mode 100644 index 0000000..aa73c09 --- /dev/null +++ b/UniqueList.js @@ -0,0 +1,20 @@ +/** + * List of unique values (Set) + */ +class UniqueList { + constructor() { + this.list = {} + } + + add(key) { + this.list[key] = 1 + } + + toArray() { + return Object.keys(this.list) + } + + static filter(value, index, self) { + return self.indexOf(value) === index + } +} \ No newline at end of file diff --git a/others.js b/others.js new file mode 100644 index 0000000..3326964 --- /dev/null +++ b/others.js @@ -0,0 +1,16 @@ +function loadScripts(libs, callback) { + return (event) => { + if (event) { + event.target.remove() + } + const lib = libs.shift() + if (lib) { + const script = document.createElement('script') + script.src = lib + script.onload = loadScripts(libs, callback) + document.body.append(script) + } else { + callback() + } + } +} \ No newline at end of file