@journeyapps/evaluator
Advanced tools
Comparing version 0.0.0-dev-20230621144252 to 0.0.0-dev-20230713095442
@@ -15,2 +15,6 @@ import { TokenExpression } from './token-expressions/TokenExpression'; | ||
static isInstanceOf(val: any): val is FormatString; | ||
/** | ||
* Compile a format string expression into tokens. | ||
*/ | ||
static compile(format: string): TokenExpression[]; | ||
toString(): string; | ||
@@ -36,1 +40,5 @@ /** | ||
} | ||
export declare const _compile: typeof FormatString.compile; | ||
export declare function parseEnclosingBraces(format: string): { | ||
length: number; | ||
}; |
@@ -1,2 +0,1 @@ | ||
import { TokenExpression } from './token-expressions/TokenExpression'; | ||
import { FunctionTokenExpression } from './token-expressions/FunctionTokenExpression'; | ||
@@ -9,11 +8,2 @@ import { ShorthandTokenExpression } from './token-expressions/ShorthandTokenExpression'; | ||
export declare function unescape(s: string): string; | ||
export declare function parseEnclosingBraces(format: string): { | ||
length: number; | ||
}; | ||
/** | ||
* Compile a format string expression into tokens. | ||
* @param {string} format string expression | ||
* @return {TokenExpression[]} compiled tokens | ||
*/ | ||
export declare function compile(format: string): TokenExpression[]; | ||
export declare function getObjectType(parent: any, name: string): any; | ||
@@ -39,2 +29,2 @@ export declare function deepMerge(a: any, b: any): any; | ||
export declare function formatValueAsync(value: any, type: TypeInterface, format: string): Promise<string>; | ||
export { compile as _compile, deepMerge as _deepMerge }; | ||
export { deepMerge as _deepMerge }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.FormatString = void 0; | ||
exports.parseEnclosingBraces = exports._compile = exports.FormatString = void 0; | ||
const tools_1 = require("./tools"); | ||
const FunctionTokenExpression_1 = require("./token-expressions/FunctionTokenExpression"); | ||
const ConstantTokenExpression_1 = require("./token-expressions/ConstantTokenExpression"); | ||
const ShorthandTokenExpression_1 = require("./token-expressions/ShorthandTokenExpression"); | ||
const FormatShorthandTokenExpression_1 = require("./token-expressions/FormatShorthandTokenExpression"); | ||
/** | ||
@@ -11,3 +15,3 @@ * Construct a new format string expression. | ||
this.expression = expression || ''; | ||
this.tokens = (0, tools_1.compile)(this.expression); | ||
this.tokens = FormatString.compile(this.expression); | ||
this.type = FormatString.TYPE; | ||
@@ -18,2 +22,78 @@ } | ||
} | ||
/** | ||
* Compile a format string expression into tokens. | ||
*/ | ||
static compile(format) { | ||
let start = 0; | ||
let tokens = []; | ||
let len = format.length; | ||
while (true) { | ||
const i = format.indexOf('{', start); | ||
if (i < 0 || i == len - 1) { | ||
// end of string - everything is normal text | ||
tokens.push(new ConstantTokenExpression_1.ConstantTokenExpression(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)); | ||
if (format[i + 1] == '{') { | ||
// Double left brace - escape and continue | ||
tokens.push(new ConstantTokenExpression_1.ConstantTokenExpression('{', start)); | ||
start = i + 2; | ||
continue; | ||
} | ||
const parsedBraces = parseEnclosingBraces(format.substring(i)); | ||
if (!parsedBraces) { | ||
// Brace pair faulty (no closing brace), return as a constant | ||
tokens.push(new ConstantTokenExpression_1.ConstantTokenExpression(format.substring(i), start)); | ||
break; | ||
} | ||
// Next start is at the end of the currently parsed brace pair | ||
start = i + parsedBraces.length + 1; | ||
// `spec` is everything between the curly braces "{" and "}". | ||
const spec = format.substring(i + 1, i + parsedBraces.length); | ||
// test for function token prefix | ||
if (spec.trim().indexOf(FunctionTokenExpression_1.FunctionTokenExpression.PREFIX) === 0) { | ||
// function token because the function name has "$:" as prefix (leading whitespace is ignored) | ||
tokens.push(new FunctionTokenExpression_1.FunctionTokenExpression(spec, i)); | ||
} | ||
else { | ||
// shorthand token | ||
const colon = spec.indexOf(':'); | ||
if (colon == -1) { | ||
tokens.push(new ShorthandTokenExpression_1.ShorthandTokenExpression(spec, i)); | ||
} | ||
else { | ||
tokens.push(new FormatShorthandTokenExpression_1.FormatShorthandTokenExpression(spec.substring(0, colon), spec.substring(colon + 1), i)); | ||
} | ||
} | ||
} | ||
// concatenate any neighbouring constant token expressions | ||
let result = []; | ||
let last = null; | ||
for (var j = 0; j < tokens.length; j++) { | ||
var token = tokens[j]; | ||
if (token instanceof ConstantTokenExpression_1.ConstantTokenExpression) { | ||
if (last == null) { | ||
if (token.expression.length > 0) { | ||
last = token; | ||
} | ||
} | ||
else { | ||
last = last.concat(token); | ||
} | ||
} | ||
else { | ||
if (last != null) { | ||
result.push(last); | ||
last = null; | ||
} | ||
result.push(token); | ||
} | ||
} | ||
if (last != null) { | ||
result.push(last); | ||
} | ||
return result; | ||
} | ||
toString() { | ||
@@ -187,2 +267,45 @@ return this.expression; | ||
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; | ||
} | ||
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; | ||
} | ||
k += inner.length; | ||
continue; | ||
} | ||
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; | ||
} | ||
exports.parseEnclosingBraces = parseEnclosingBraces; | ||
//# sourceMappingURL=FormatString.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports._deepMerge = exports._compile = exports.formatValueAsync = exports.formatValue = exports.actionableTokenExpression = exports.functionTokenExpression = exports.formatString = exports.extract = exports.deepMerge = exports.getObjectType = exports.compile = exports.parseEnclosingBraces = exports.unescape = void 0; | ||
const ConstantTokenExpression_1 = require("./token-expressions/ConstantTokenExpression"); | ||
exports._deepMerge = exports.formatValueAsync = exports.formatValue = exports.actionableTokenExpression = exports.functionTokenExpression = exports.formatString = exports.extract = exports.deepMerge = exports.getObjectType = exports.unescape = void 0; | ||
const FunctionTokenExpression_1 = require("./token-expressions/FunctionTokenExpression"); | ||
@@ -27,125 +26,2 @@ const ShorthandTokenExpression_1 = require("./token-expressions/ShorthandTokenExpression"); | ||
exports.unescape = unescape; | ||
function parseEnclosingBraces(format) { | ||
var 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. "{" | ||
var SPECIAL_SECTIONS = ["'", '"']; | ||
for (var k = i + 1; k < format.length; k++) { | ||
var character = format[k]; | ||
if (SPECIAL_SECTIONS.indexOf(character) != -1) { | ||
// This is the start of a string, jump to its end | ||
var endChar = format.indexOf(character, k + 1); | ||
if (endChar == -1) { | ||
// Unless the end doesn't exist. Error out. | ||
return null; | ||
} | ||
k = endChar; | ||
continue; | ||
} | ||
if (character == '{') { | ||
// Start of pair of inner braces, | ||
// recursively parse them | ||
var inner = 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 | ||
}; | ||
} | ||
} | ||
// Came to end of loop without a match. Faulty, return null | ||
return null; | ||
} | ||
exports.parseEnclosingBraces = parseEnclosingBraces; | ||
/** | ||
* Compile a format string expression into tokens. | ||
* @param {string} format string expression | ||
* @return {TokenExpression[]} compiled tokens | ||
*/ | ||
function compile(format) { | ||
let start = 0; | ||
let tokens = []; | ||
let len = format.length; | ||
while (true) { | ||
const i = format.indexOf('{', start); | ||
if (i < 0 || i == len - 1) { | ||
// end of string - everything is normal text | ||
tokens.push(new ConstantTokenExpression_1.ConstantTokenExpression(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)); | ||
if (format[i + 1] == '{') { | ||
// Double left brace - escape and continue | ||
tokens.push(new ConstantTokenExpression_1.ConstantTokenExpression('{', start)); | ||
start = i + 2; | ||
continue; | ||
} | ||
const parsedBraces = parseEnclosingBraces(format.substring(i)); | ||
if (!parsedBraces) { | ||
// Brace pair faulty (no closing brace), return as a constant | ||
tokens.push(new ConstantTokenExpression_1.ConstantTokenExpression(format.substring(i), start)); | ||
break; | ||
} | ||
// Next start is at the end of the currently parsed brace pair | ||
start = i + parsedBraces.length + 1; | ||
// `spec` is everything between the curly braces "{" and "}". | ||
const spec = format.substring(i + 1, i + parsedBraces.length); | ||
// test for function token prefix | ||
if (spec.trim().indexOf(FunctionTokenExpression_1.FunctionTokenExpression.PREFIX) === 0) { | ||
// function token because the function name has "$:" as prefix (leading whitespace is ignored) | ||
tokens.push(new FunctionTokenExpression_1.FunctionTokenExpression(spec, i)); | ||
} | ||
else { | ||
// shorthand token | ||
const colon = spec.indexOf(':'); | ||
if (colon == -1) { | ||
tokens.push(new ShorthandTokenExpression_1.ShorthandTokenExpression(spec, i)); | ||
} | ||
else { | ||
tokens.push(new FormatShorthandTokenExpression_1.FormatShorthandTokenExpression(spec.substring(0, colon), spec.substring(colon + 1), i)); | ||
} | ||
} | ||
} | ||
// concatenate any neighbouring constant token expressions | ||
let result = []; | ||
let last = null; | ||
for (var j = 0; j < tokens.length; j++) { | ||
var token = tokens[j]; | ||
if (token instanceof ConstantTokenExpression_1.ConstantTokenExpression) { | ||
if (last == null) { | ||
if (token.expression.length > 0) { | ||
last = token; | ||
} | ||
} | ||
else { | ||
last = last.concat(token); | ||
} | ||
} | ||
else { | ||
if (last != null) { | ||
result.push(last); | ||
last = null; | ||
} | ||
result.push(token); | ||
} | ||
} | ||
if (last != null) { | ||
result.push(last); | ||
} | ||
return result; | ||
} | ||
exports.compile = compile; | ||
exports._compile = compile; | ||
function getObjectType(parent, name) { | ||
@@ -152,0 +28,0 @@ var variable = parent.getAttribute(name); |
{ | ||
"name": "@journeyapps/evaluator", | ||
"version": "0.0.0-dev-20230621144252", | ||
"version": "0.0.0-dev-20230713095442", | ||
"description": "Journey Evaluator library", | ||
@@ -8,3 +8,3 @@ "main": "./dist/src/index.js", | ||
"dependencies": { | ||
"@journeyapps/core-xml": "0.0.0-dev-20230621144252" | ||
"@journeyapps/core-xml": "5.0.2" | ||
}, | ||
@@ -11,0 +11,0 @@ "files": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
126683
1186
+ Added@journeyapps/core-xml@5.0.2(transitive)
- Removed@journeyapps/core-xml@0.0.0-dev-20230621144252(transitive)
Updated@journeyapps/core-xml@5.0.2