@darkwolf/helper
Version:
127 lines (105 loc) • 3.56 kB
JavaScript
import Constants from './constants/index.mjs'
export {
Constants
}
export default class Helper {
static toFloat = parseFloat
static toInteger = parseInt
static Constants = Constants
static getTag(value) {
return Object.prototype.toString.call(value)
}
static getType(value) {
return typeof value
}
static getPathParts(path) {
const parts = Array.isArray(path) ? path.join('.') : path
const regex = new RegExp(Constants.PATH_PARTS_PATTERN, 'g')
return parts.match(regex)
}
static get(obj, path) {
return Helper.getPathParts(path).reduce((obj, key, i, arr) => {
const index = key.length && key.startsWith('[') ?
parseInt(key.slice(1, -1)) : key
return i < arr.length && obj ? obj[
index < 0 && Number.isInteger(index) && Array.isArray(obj) ?
Math.max(0, obj.length + index) : index
] : undefined
}, obj)
}
static set(obj, path, value) {
return Helper.getPathParts(path).reduce((obj, key, i, arr) => {
const index = key.length && key.startsWith('[') ?
parseInt(key.slice(1, -1)) : key
const prop = index < 0 && Number.isInteger(index) && Array.isArray(obj) ?
Math.max(0, obj.length + index) : index
if (obj[prop] === undefined && i < arr.length - 1) {
const nextKey = arr[i + 1]
const nextIndex = nextKey.length && nextKey.startsWith('[') ?
parseInt(nextKey.slice(1, -1)) : nextKey
obj[prop] = Number.isInteger(nextIndex) ? [] : {}
return obj[prop]
}
if (i === arr.length - 1) {
obj[prop] = value
return value
}
return obj[prop]
}, obj)
}
static chunk(arr, size) {
return arr.reduce((a, b, i) => i % size ? a : [
...a,
arr.slice(i, i + size)
], [])
}
static shuffle(arr) {
return arr
.map(value => [Math.random(), value])
.sort(([a], [b]) => a - b)
.map(([random, value]) => value)
}
static unique(arr) {
return [...new Set(arr)]
}
static getWords(str) {
const regex = new RegExp(Constants.WORDS_PATTERN, 'g')
return str.match(regex) || []
}
static capitalize(str) {
return str.length ? str[0].toUpperCase() + str.slice(1).toLowerCase() : str
}
static toCamelCase(str) {
return Helper.getWords(str).reduce((prev, next, i) =>
prev + (i ? Helper.capitalize(next) : next.toLowerCase()), '')
}
static toPascalCase(str) {
return Helper.getWords(str).reduce((prev, next) => prev + Helper.capitalize(next), '')
}
static toSnakeCase(str) {
return Helper.getWords(str).join('_').toLowerCase()
}
static toUpperSnakeCase(str) {
return Helper.getWords(str).join('_').toUpperCase()
}
static toKebabCase(str) {
return Helper.getWords(str).join('-').toLowerCase()
}
static template(str, props, options = {}) {
const normalize = options.normalize === undefined || !!options.normalize
const regex = new RegExp(Constants.TEMPLATE_PROPS_PATTERN, 'g')
return str.replace(regex, (input, name) => {
let prop = Helper.get(props, name)
if (typeof prop === 'function') prop = prop()
return normalize ? Helper.normalizeString(prop) : prop
})
}
static normalizeString(value) {
return value !== undefined && value !== null ? `${value}` : ''
}
static define(name, helper) {
this[name] = helper
return this
}
define = Helper.define
}