split helpers and utilities

This commit is contained in:
Philipp Kühn
2020-11-30 09:42:53 +01:00
parent 8d38459289
commit f486ddf80a
56 changed files with 57 additions and 60 deletions

View File

@@ -0,0 +1,17 @@
/**
* Optionally calls `value` as a function.
* Otherwise it is returned directly.
* @param value Function or any value.
* @param context Optional context to bind to function.
*/
export default function callOrReturn(value: any, context?: any) {
if (typeof value === 'function') {
if (context) {
return value.bind(context)()
}
return value()
}
return value
}

View File

@@ -0,0 +1,8 @@
export default function createStyleTag(style: string): HTMLStyleElement {
const styleNode = document.createElement('style')
styleNode.innerHTML = style
document.getElementsByTagName('head')[0].appendChild(styleNode)
return styleNode
}

View File

@@ -0,0 +1,20 @@
/**
* Remove a property or an array of properties from an object
* @param obj Object
* @param key Key to remove
*/
export default function deleteProps(obj: { [key: string ]: any }, propOrProps: string | string[]) {
const props = typeof propOrProps === 'string'
? [propOrProps]
: propOrProps
return Object
.keys(obj)
.reduce((newObj: { [key: string ]: any }, prop) => {
if (!props.includes(prop)) {
newObj[prop] = obj[prop]
}
return newObj
}, {})
}

View File

@@ -0,0 +1,7 @@
export default function elementFromString(value: string): HTMLElement {
const htmlString = `<div>${value}</div>`
const parser = new window.DOMParser()
const element = parser.parseFromString(htmlString, 'text/html').body
return element
}

View File

@@ -0,0 +1,19 @@
export default function fromString(value: any) {
if (typeof value !== 'string') {
return value
}
if (value.match(/^\d*(\.\d+)?$/)) {
return Number(value)
}
if (value === 'true') {
return true
}
if (value === 'false') {
return false
}
return value
}

View File

@@ -0,0 +1,4 @@
export default function getAllMethodNames(obj: Object): string[] {
return Reflect.ownKeys(Reflect.getPrototypeOf(obj))
.map(name => name.toString())
}

View File

@@ -0,0 +1,3 @@
export default function isEmptyObject(object = {}) {
return Object.keys(object).length === 0 && object.constructor === Object
}

View File

@@ -0,0 +1,34 @@
export default function magicMethods(Clazz: any) {
const classHandler = Object.create(null)
classHandler.construct = (_: any, args: any) => {
const instance = new Clazz(...args)
const instanceHandler = Object.create(null)
const get = Object.getOwnPropertyDescriptor(Clazz.prototype, '__get')
if (get) {
instanceHandler.get = (target: any, name: any) => {
if (typeof name !== 'string') {
return
}
const exists = name in target
|| name.startsWith('_')
|| ['then', 'catch'].includes(name)
if (exists) {
return target[name]
}
return get.value.call(target, name)
}
}
instance.proxy = new Proxy(instance, instanceHandler)
instance.emit('createdProxy')
return instance.proxy
}
return new Proxy(Clazz, classHandler)
}

View File

@@ -0,0 +1,28 @@
import { AnyObject } from '../types'
export default function mergeAttributes(...objects: AnyObject[]) {
return objects
.filter(item => !!item)
.reduce((items, item) => {
const mergedAttributes = { ...items }
Object.entries(item).forEach(([key, value]) => {
const exists = mergedAttributes[key]
if (!exists) {
mergedAttributes[key] = value
return
}
if (key === 'class') {
mergedAttributes[key] = [mergedAttributes[key], value].join(' ')
} else if (key === 'style') {
mergedAttributes[key] = [mergedAttributes[key], value].join('; ')
} else {
mergedAttributes[key] = value
}
})
return mergedAttributes
}, {})
}

View File

@@ -0,0 +1,3 @@
export default function minMax(value = 0, min = 0, max = 0): number {
return Math.min(Math.max(value, min), max)
}

View File

@@ -0,0 +1,11 @@
/**
* Check if object1 includes object2
* @param object1 Object
* @param object2 Object
*/
export default function objectIncludes(object1: { [key: string ]: any }, object2: { [key: string ]: any }): boolean {
return !!Object
.keys(object2)
.filter(key => object2[key] === object1[key])
.length
}

View File

@@ -0,0 +1,5 @@
export default function removeElement(element: HTMLElement) {
if (element && element.parentNode) {
element.parentNode.removeChild(element)
}
}