@technobuddha/library
Version:
A large library of useful functions
32 lines (28 loc) • 1.03 kB
text/typescript
import isString from 'lodash/isString';
import escapeRegExp from 'lodash/escapeRegExp';
import unescapeJS from '../unescapeJS';
type Options = {
/** The quote character(s) to use */
quote?: string;
/** Character sequence to replace the quote mark within the text */
escape?: (string | ((input: string) => string));
};
/**
* Remove surrounding quotes from text
*
* @param input The text to surrounded by quotes
* @param __namedParameters see {@link Options}
* @default quote double-quote (")
* @default escape unescapeJS
* @returns the unescaped text with quotes removed
*/
export function unquote(input: string, { quote = '"', escape = unescapeJS }: Options = {}): string {
if(input.startsWith(quote) && input.endsWith(quote)) {
input = input.slice(quote.length, input.length - quote.length);
if(isString(escape))
return input.replace(new RegExp(escapeRegExp(escape), 'gu'), quote);
return escape(input);
}
return input;
}
export default unquote;