2021-03-10 11:48:41 +00:00
|
|
|
import { hasProtocol, joinURL } from 'ufo'
|
2021-02-09 16:01:37 +00:00
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
export const encodeHtml = function encodeHtml (str) {
|
2018-12-22 21:05:13 +00:00
|
|
|
return str.replace(/</g, '<').replace(/>/g, '>')
|
|
|
|
}
|
|
|
|
|
|
|
|
export const isString = obj => typeof obj === 'string' || obj instanceof String
|
|
|
|
|
2019-01-29 19:23:42 +00:00
|
|
|
export const isNonEmptyString = obj => Boolean(obj && isString(obj))
|
2018-12-22 21:05:13 +00:00
|
|
|
|
2019-05-10 13:03:07 +00:00
|
|
|
export const isPureObject = obj => !Array.isArray(obj) && typeof obj === 'object'
|
2018-12-22 21:05:13 +00:00
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
export const isUrl = function isUrl (url) {
|
2021-03-10 11:48:41 +00:00
|
|
|
return hasProtocol(url, true)
|
2018-12-22 21:05:13 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 16:01:37 +00:00
|
|
|
export const urlJoin = joinURL
|
2018-12-22 21:05:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Wraps value in array if it is not already an array
|
|
|
|
*
|
|
|
|
* @param {any} value
|
|
|
|
* @return {array}
|
|
|
|
*/
|
|
|
|
export const wrapArray = value => Array.isArray(value) ? value : [value]
|
|
|
|
|
|
|
|
const WHITESPACE_REPLACEMENTS = [
|
|
|
|
[/[ \t\f\r]+\n/g, '\n'], // strip empty indents
|
|
|
|
[/{\n{2,}/g, '{\n'], // strip start padding from blocks
|
|
|
|
[/\n{2,}([ \t\f\r]*})/g, '\n$1'], // strip end padding from blocks
|
|
|
|
[/\n{3,}/g, '\n\n'], // strip multiple blank lines (1 allowed)
|
2021-08-13 11:22:16 +00:00
|
|
|
[/^\n+/, ''], // strip blank lines at the beginning of a string
|
|
|
|
[/\n{2,}$/, '\n'] // strip blank lines at the end of a string
|
2018-12-22 21:05:13 +00:00
|
|
|
]
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
export const stripWhitespace = function stripWhitespace (string) {
|
2018-12-22 21:05:13 +00:00
|
|
|
WHITESPACE_REPLACEMENTS.forEach(([regex, newSubstr]) => {
|
|
|
|
string = string.replace(regex, newSubstr)
|
|
|
|
})
|
|
|
|
return string
|
|
|
|
}
|