Refactor r() into utils

This commit is contained in:
Pooya Parsa 2017-05-18 13:14:31 +04:30
parent b060090fb1
commit 3f84161811

View File

@ -1,4 +1,6 @@
'use strict'
import { resolve, sep } from 'path'
import _ from 'lodash'
export function encodeHtml (str) {
return str.replace(/</g, '&lt;').replace(/>/g, '&gt;')
@ -74,3 +76,24 @@ export function chainFn (base, fn) {
fn.apply(this, arguments)
}
}
function wp (p) {
/* istanbul ignore if */
if (/^win/.test(process.platform)) {
p = p.replace(/\\/g, '\\\\')
}
return p
}
const reqSep = /\//g
const sysSep = _.escapeRegExp(sep)
const normalize = string => string.replace(reqSep, sysSep)
export function r () {
let args = Array.from(arguments)
if (_.last(args).includes('~')) {
return wp(_.last(args))
}
args = args.map(normalize)
return wp(resolve.apply(null, args))
}