saven
Version:
58 lines (50 loc) • 1.59 kB
JavaScript
function shouleBeObject (target) {
if (target && typeof target === 'object') return { res: true }
return {
res: false,
msg: getParameterError({
correct: 'Object',
wrong: target
})
}
}
function getParameterError ({ name = '', para, correct, wrong }) {
const parameter = para ? `parameter.${para}` : 'parameter'
const errorType = upperCaseFirstLetter(wrong === null ? 'Null' : typeof wrong)
return `${name}:fail parameter error: ${parameter} should be ${correct} instead of ${errorType}`
}
function upperCaseFirstLetter (string) {
if (typeof string !== 'string') return string
string = string.replace(/^./, match => match.toUpperCase())
return string
}
function inlineStyle (style) {
let res = ''
for (let attr in style) res += `${attr}: ${style[attr]};`
if (res.indexOf('display: flex;') >= 0) res += 'display: -webkit-box;display: -webkit-flex;'
res = res.replace(/transform:(.+?);/g, (s, $1) => `${s}-webkit-transform:${$1};`)
res = res.replace(/flex-direction:(.+?);/g, (s, $1) => `${s}-webkit-flex-direction:${$1};`)
return res
}
function errorHandler (fail, complete) {
return function (res) {
typeof fail === 'function' && fail(res)
typeof complete === 'function' && complete(res)
return Promise.reject(res)
}
}
const enc = encodeURIComponent
function serializeParams (params) {
if (!params) {
return ''
}
return Object.keys(params)
.map(key => (`${enc(key)}=${enc(params[key])}`)).join('&')
}
export {
shouleBeObject,
getParameterError,
inlineStyle,
errorHandler,
serializeParams
}