UNPKG

oparser

Version:

A very forgiving key-value option parser

32 lines (30 loc) 666 B
const NUMBER_REGEX = /^\d+(\.\d+)?$/ // also match 1.222 const BOOLEAN_REGEX = /^(true|false)$/ const ARRAY_REGEX = /^\[.*\]$/ function ensureWrap(s = '', open, close) { const isQuote = open === '"' || open === "'" // Don't wrap numbers if (NUMBER_REGEX.test(s) && isQuote) { return s } // Don't wrap booleans if (BOOLEAN_REGEX.test(s) && isQuote) { return s } // Don't wrap arrays if (ARRAY_REGEX.test(s) && isQuote) { return s } let str = s close = close || open if (str[0] !== open) { str = open + str } if (str[str.length - 1] !== close) { str = str + close } return str } module.exports = { ensureWrap }