UNPKG

@fastly/esi

Version:

ESI implementation for JavaScript, using the modern fetch and streaming APIs.

25 lines (24 loc) 762 B
/* * Copyright Fastly, Inc. * Licensed under the MIT license. See LICENSE file for details. */ export function quoteString(input) { return "'" + input.replaceAll("'", "\\'") + "'"; } export function unquoteString(input) { if (!input.startsWith("'") || !input.endsWith("'")) { throw new Error('unquoteString input should start and end with single quote'); } let str = input.slice(1, input.length - 1); let p = -1; while (true) { p = str.indexOf("'", p + 1); if (p === -1) { break; } if (str.charAt(p - 1) !== '\\') { throw new Error('unquoteString input should not contain unescaped single quotes'); } } return input.slice(1, -1).replaceAll("\\'", "'"); }