split helpers and utilities
This commit is contained in:
17
packages/core/src/utilities/callOrReturn.ts
Normal file
17
packages/core/src/utilities/callOrReturn.ts
Normal 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
|
||||
}
|
||||
8
packages/core/src/utilities/createStyleTag.ts
Normal file
8
packages/core/src/utilities/createStyleTag.ts
Normal 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
|
||||
}
|
||||
20
packages/core/src/utilities/deleteProps.ts
Normal file
20
packages/core/src/utilities/deleteProps.ts
Normal 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
|
||||
}, {})
|
||||
}
|
||||
7
packages/core/src/utilities/elementFromString.ts
Normal file
7
packages/core/src/utilities/elementFromString.ts
Normal 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
|
||||
}
|
||||
19
packages/core/src/utilities/fromString.ts
Normal file
19
packages/core/src/utilities/fromString.ts
Normal 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
|
||||
}
|
||||
4
packages/core/src/utilities/getAllMethodNames.ts
Normal file
4
packages/core/src/utilities/getAllMethodNames.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export default function getAllMethodNames(obj: Object): string[] {
|
||||
return Reflect.ownKeys(Reflect.getPrototypeOf(obj))
|
||||
.map(name => name.toString())
|
||||
}
|
||||
3
packages/core/src/utilities/isEmptyObject.ts
Normal file
3
packages/core/src/utilities/isEmptyObject.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export default function isEmptyObject(object = {}) {
|
||||
return Object.keys(object).length === 0 && object.constructor === Object
|
||||
}
|
||||
34
packages/core/src/utilities/magicMethods.ts
Normal file
34
packages/core/src/utilities/magicMethods.ts
Normal 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)
|
||||
}
|
||||
28
packages/core/src/utilities/mergeAttributes.ts
Normal file
28
packages/core/src/utilities/mergeAttributes.ts
Normal 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
|
||||
}, {})
|
||||
}
|
||||
3
packages/core/src/utilities/minMax.ts
Normal file
3
packages/core/src/utilities/minMax.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export default function minMax(value = 0, min = 0, max = 0): number {
|
||||
return Math.min(Math.max(value, min), max)
|
||||
}
|
||||
11
packages/core/src/utilities/objectIncludes.ts
Normal file
11
packages/core/src/utilities/objectIncludes.ts
Normal 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
|
||||
}
|
||||
5
packages/core/src/utilities/removeElement.ts
Normal file
5
packages/core/src/utilities/removeElement.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export default function removeElement(element: HTMLElement) {
|
||||
if (element && element.parentNode) {
|
||||
element.parentNode.removeChild(element)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user