Initial commit

master
Clement Bois 2019-05-23 14:17:22 +02:00
commit 6d1d65783f
4 changed files with 149 additions and 0 deletions

78
ObjectHelper.js Normal file
View File

@ -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
})
}
}

35
TimeHelper.js Normal file
View File

@ -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))
}
}
}
}

20
UniqueList.js Normal file
View File

@ -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
}
}

16
others.js Normal file
View File

@ -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()
}
}
}