Nuxt/lib/utils.js

57 lines
1.2 KiB
JavaScript
Raw Normal View History

2016-11-07 01:34:58 +00:00
'use strict'
exports.encodeHtml = (str) => str.replace(/</g, '&lt;').replace(/>/g, '&gt;')
exports.getContext = function (req, res) {
return {
req: req,
res: res
}
}
exports.setAnsiColors = function (ansiHTML) {
ansiHTML.setColors({
reset: ['efefef', 'a6004c'],
darkgrey: '5a012b',
yellow: 'ffab07',
green: 'aeefba',
magenta: 'ff84bf',
blue: '3505a0',
cyan: '56eaec',
red: '4e053a'
})
}
exports.waitFor = function * (ms) {
return new Promise(function (resolve) {
setTimeout(resolve, (ms || 0))
})
}
2016-11-10 18:34:59 +00:00
exports.urlJoin = function () {
return [].slice.call(arguments).join('/').replace(/\/+/g, '/')
}
2016-11-24 00:47:11 +00:00
exports.promisifyRouteParams = function (fn) {
// If routeParams[route] is an array
if (Array.isArray(fn)) {
return Promise.resolve(fn)
}
// If routeParams[route] is a function expecting a callback
if (fn.length === 1) {
2016-12-10 11:39:11 +00:00
return new Promise((resolve, reject) => {
2016-11-24 00:47:11 +00:00
fn(function (err, routeParams) {
if (err) {
reject(err)
}
resolve(routeParams)
})
})
}
2016-12-10 11:39:11 +00:00
let promise = fn()
2016-11-24 00:47:11 +00:00
if (!(promise instanceof Promise)) {
promise = Promise.resolve(promise)
}
return promise
}