refactor(core): and markdown compiler

This commit is contained in:
qingwei.li 2017-02-18 14:09:17 +08:00 committed by cinwell.li
commit fe88c154b0
12 changed files with 194 additions and 232 deletions

View file

@ -1,6 +1,7 @@
import { cached } from '../util/core'
const decode = decodeURIComponent
const encode = encodeURIComponent
export const parseQuery = cached(query => {
const res = {}
@ -11,35 +12,35 @@ export const parseQuery = cached(query => {
return res
}
// Simple parse
query.split('&').forEach(function (param) {
const parts = param.replace(/\+/g, ' ').split('=')
const key = decode(parts.shift())
const val = parts.length > 0
? decode(parts.join('='))
: null
if (res[key] === undefined) {
res[key] = val
} else if (Array.isArray(res[key])) {
res[key].push(val)
} else {
res[key] = [res[key], val]
}
res[parts[0]] = decode(parts[1])
})
return res
})
export function stringifyQuery (obj) {
const qs = []
for (const key in obj) {
qs.push(`${encode(key)}=${encode(obj[key])}`)
}
return qs.length ? `?${qs.join('&')}` : ''
}
export const getBasePath = cached(base => {
return /^(\/|https?:)/g.test(base)
? base
: cleanPath(window.location.pathname + '/' + base)
})
export const getRoot = cached(path => {
return /\/$/g.test(path) ? path : path.match(/(\S*\/)[^\/]+$/)[1]
})
export function cleanPath (path) {
return path.replace(/\/+/g, '/')
}
export function getBasePath (base) {
return /^(\/|https?:)/g.test(base)
? base
: cleanPath(window.location.pathname + '/' + base)
}
export function getCurrentRoot (path) {
return /\/$/g.test(path) ? path : path.match(/(\S*\/)[^\/]+$/)[1]
}