2018-02-17 23:26:45 +08:00

89 lines
1.6 KiB
JavaScript

import uuid from './uuid'
/* eslint no-useless-escape: "off" */
const SPECIAL_CHARS_REGEXP = /([\:\-\_]+(.))/g
const MOZ_HACK_REGEXP = /^moz([A-Z])/
const trim = function (string) {
return (string || '').replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g, '')
}
/**
* stamp string
* @param obj
* @returns {*}
*/
const stamp = function (obj) {
let key = '_event_id_'
obj[key] = obj[key] || (uuid())
return obj[key]
}
/**
* check case
* @param name
* @returns {string}
*/
const camelCase = function (name) {
return name.replace(SPECIAL_CHARS_REGEXP, function (_, separator, letter, offset) {
return offset ? letter.toUpperCase() : letter
}).replace(MOZ_HACK_REGEXP, 'Moz$1')
}
/**
* 判断是否为对象
* @param value
* @returns {boolean}
*/
const isObject = value => {
const type = typeof value
return value !== null && (type === 'object' || type === 'function')
}
/**
* 判断是否为合法字符串
* @param value
* @returns {boolean}
*/
const isString = (value) => {
if (value == null) {
return false
}
return typeof value === 'string' || (value.constructor !== null && value.constructor === String)
}
/**
* check is number
* @param val
* @returns {boolean}
*/
const isNumber = (val) => {
return typeof val === 'number' && !isNaN(val);
}
/**
* merge
* @param a
* @param b
* @returns {*}
*/
const merge = (a, b) => {
for (const key in b) {
if (isObject(b[key]) && isObject(a[key])) {
merge(a[key], b[key])
} else {
a[key] = b[key]
}
}
return a
}
export {
merge,
trim,
stamp,
isString,
isObject,
camelCase,
isNumber
}