@journeyapps/evaluator
Advanced tools
Comparing version 0.0.0-dev-20230713120451 to 0.0.0-dev-20230714125355
@@ -38,7 +38,8 @@ import { TokenExpression } from './token-expressions/TokenExpression'; | ||
evaluate(scope: FormatStringScope): string; | ||
static parseEnclosingBraces(format: string): { | ||
length: number; | ||
}; | ||
static unescape(s: string): string; | ||
} | ||
export declare const _compile: typeof FormatString.compile; | ||
export declare function parseEnclosingBraces(format: string): { | ||
length: number; | ||
}; | ||
export declare function unescape(s: string): string; | ||
export declare const parseEnclosingBraces: typeof FormatString.parseEnclosingBraces; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.unescape = exports.parseEnclosingBraces = exports._compile = exports.FormatString = void 0; | ||
exports.parseEnclosingBraces = exports._compile = exports.FormatString = void 0; | ||
const tools_1 = require("./tools"); | ||
@@ -32,7 +32,7 @@ const FunctionTokenExpression_1 = require("./token-expressions/FunctionTokenExpression"); | ||
// end of string - everything is normal text | ||
tokens.push(new ConstantTokenExpression_1.ConstantTokenExpression(unescape(format.substring(start)), start)); | ||
tokens.push(new ConstantTokenExpression_1.ConstantTokenExpression(FormatString.unescape(format.substring(start)), start)); | ||
break; | ||
} | ||
// normal text in the gaps between curly braces | ||
tokens.push(new ConstantTokenExpression_1.ConstantTokenExpression(unescape(format.substring(start, i)), start)); | ||
tokens.push(new ConstantTokenExpression_1.ConstantTokenExpression(FormatString.unescape(format.substring(start, i)), start)); | ||
if (format[i + 1] == '{') { | ||
@@ -44,3 +44,3 @@ // Double left brace - escape and continue | ||
} | ||
const parsedBraces = parseEnclosingBraces(format.substring(i)); | ||
const parsedBraces = FormatString.parseEnclosingBraces(format.substring(i)); | ||
if (!parsedBraces) { | ||
@@ -264,65 +264,64 @@ // Brace pair faulty (no closing brace), return as a constant | ||
} | ||
} | ||
exports.FormatString = FormatString; | ||
FormatString.TYPE = 'format-string'; | ||
// Expose internal functions for tests | ||
exports._compile = FormatString.compile; | ||
function parseEnclosingBraces(format) { | ||
const i = format.indexOf('{'); | ||
if (i === -1) { | ||
return null; | ||
} | ||
// We want to skip through these sections | ||
// i.e. do not match { in a string, e.g. "{" | ||
const SPECIAL_SECTIONS = ["'", '"']; | ||
for (let k = i + 1; k < format.length; k++) { | ||
const character = format[k]; | ||
if (SPECIAL_SECTIONS.indexOf(character) !== -1) { | ||
// This is the start of a string, jump to its end | ||
const endChar = format.indexOf(character, k + 1); | ||
if (endChar === -1) { | ||
// Unless the end doesn't exist. Error out. | ||
return null; | ||
} | ||
k = endChar; | ||
continue; | ||
static parseEnclosingBraces(format) { | ||
const i = format.indexOf('{'); | ||
if (i === -1) { | ||
return null; | ||
} | ||
if (character === '{') { | ||
// Start of a pair of inner braces, | ||
// recursively parse them | ||
const inner = parseEnclosingBraces(format.substring(k)); | ||
if (!inner) { | ||
// Faulty inner, return null | ||
return null; | ||
// We want to skip through these sections | ||
// i.e. do not match { in a string, e.g. "{" | ||
const SPECIAL_SECTIONS = ["'", '"']; | ||
for (let k = i + 1; k < format.length; k++) { | ||
const character = format[k]; | ||
if (SPECIAL_SECTIONS.indexOf(character) !== -1) { | ||
// This is the start of a string, jump to its end | ||
const endChar = format.indexOf(character, k + 1); | ||
if (endChar === -1) { | ||
// Unless the end doesn't exist. Error out. | ||
return null; | ||
} | ||
k = endChar; | ||
continue; | ||
} | ||
k += inner.length; | ||
continue; | ||
if (character === '{') { | ||
// Start of a pair of inner braces, | ||
// recursively parse them | ||
const inner = FormatString.parseEnclosingBraces(format.substring(k)); | ||
if (!inner) { | ||
// Faulty inner, return null | ||
return null; | ||
} | ||
k += inner.length; | ||
continue; | ||
} | ||
if (character === '}') { | ||
// Found closing part for current level of braces | ||
// Return the length to the caller | ||
return { length: k - i }; | ||
} | ||
} | ||
if (character === '}') { | ||
// Found closing part for current level of braces | ||
// Return the length to the caller | ||
return { length: k - i }; | ||
} | ||
// Came to end of loop without a match. Faulty, return null | ||
return null; | ||
} | ||
// Came to end of loop without a match. Faulty, return null | ||
return null; | ||
} | ||
exports.parseEnclosingBraces = parseEnclosingBraces; | ||
function unescape(s) { | ||
let start = 0; | ||
let result = ''; | ||
const len = s.length; | ||
while (true) { | ||
const i = s.indexOf('}', start); | ||
if (i == -1 || i == len - 1) { | ||
result += s.substring(start); | ||
break; | ||
static unescape(s) { | ||
let start = 0; | ||
let result = ''; | ||
const len = s.length; | ||
while (true) { | ||
const i = s.indexOf('}', start); | ||
if (i == -1 || i == len - 1) { | ||
result += s.substring(start); | ||
break; | ||
} | ||
result += s.substring(start, i + 1); | ||
// We assume that the character at i+1 is another right brace, but we don't do any checking. | ||
start = i + 2; | ||
} | ||
result += s.substring(start, i + 1); | ||
// We assume that the character at i+1 is another right brace, but we don't do any checking. | ||
start = i + 2; | ||
return result; | ||
} | ||
return result; | ||
} | ||
exports.unescape = unescape; | ||
exports.FormatString = FormatString; | ||
FormatString.TYPE = 'format-string'; | ||
// Expose internal functions for tests | ||
exports._compile = FormatString.compile; | ||
exports.parseEnclosingBraces = FormatString.parseEnclosingBraces; | ||
//# sourceMappingURL=FormatString.js.map |
{ | ||
"name": "@journeyapps/evaluator", | ||
"version": "0.0.0-dev-20230713120451", | ||
"version": "0.0.0-dev-20230714125355", | ||
"description": "Journey Evaluator library", | ||
@@ -5,0 +5,0 @@ "main": "./dist/src/index.js", |
Sorry, the diff of this file is not supported yet
127175