Files
vuepress/packages/@vuepress/core/lib/util/shared.js
T
2018-08-10 12:01:29 +08:00

68 lines
1.7 KiB
JavaScript

const chalk = require('chalk')
/**
* Build functional pipeline.
*/
exports.compose = (...processors) => {
if (processors.length === 0) return input => input
if (processors.length === 1) return processors[0]
return processors.reduce((prev, next) => {
return (...args) => next(prev(...args))
})
}
exports.isObject = obj => obj !== null && typeof obj === 'object'
/**
* Get the raw type string of a value e.g. [object Object]
*/
const _toString = Object.prototype.toString
exports.toRawType = value => _toString.call(value).slice(8, -1)
exports.getType = function (fn) {
const match = fn && fn.toString().match(/^\s*function (\w+)/)
return match ? match[1] : ''
}
/**
* Transform multi-types to natural language. e.g.
* ['Function'] => 'Function'
* ['Function', 'Object'] => 'Function or Object'
* ['Function', 'Object', 'Number'] => 'Function, Object or Number'
*/
function toNaturalMultiTypesLanguage (types) {
const len = types.length
if (len === 1) {
return types.join('')
}
const rest = types.slice(0, len - 1)
const last = types[len - 1]
return rest.join(', ') + ' or ' + last
}
exports.assertTypes = function (value, types) {
let valid
let warnMsg
let actualType = exports.toRawType(value)
const expectedTypes = []
if (actualType === 'AsyncFunction') {
actualType = 'Function'
}
for (const type of types) {
const expectedType = exports.getType(type)
expectedTypes.push(expectedType)
valid = actualType === expectedType
if (valid) break
}
if (!valid) {
warnMsg =
`expected a ${chalk.green(toNaturalMultiTypesLanguage(expectedTypes))} ` +
`but got ${chalk.yellow(actualType)}.`
}
return { valid, warnMsg }
}