@journeyapps/evaluator
Advanced tools
Comparing version 0.0.0-dev.3d82e35.28b73fb to 0.0.0-dev.3ef8fa7
@@ -9,5 +9,12 @@ import { TokenExpression } from './token-expressions/TokenExpression'; | ||
export declare class FormatString { | ||
static TYPE: string; | ||
type: string; | ||
expression: string; | ||
tokens: TokenExpression[]; | ||
constructor(expression: string); | ||
static isInstanceOf(val: any): val is FormatString; | ||
/** | ||
* Compile a format string expression into tokens. | ||
*/ | ||
static compile(format: string): TokenExpression[]; | ||
toString(): string; | ||
@@ -32,2 +39,8 @@ /** | ||
evaluate(scope: FormatStringScope): string; | ||
static parseEnclosingBraces(format: string): { | ||
length: number; | ||
}; | ||
static unescape(s: string): string; | ||
} | ||
export declare const _compile: typeof FormatString.compile; | ||
export declare const parseEnclosingBraces: typeof FormatString.parseEnclosingBraces; |
@@ -1,2 +0,1 @@ | ||
import { TokenExpression } from './token-expressions/TokenExpression'; | ||
import { FunctionTokenExpression } from './token-expressions/FunctionTokenExpression'; | ||
@@ -8,12 +7,2 @@ import { ShorthandTokenExpression } from './token-expressions/ShorthandTokenExpression'; | ||
import { LegacyFunctionTokenExpression } from './token-expressions/LegacyFunctionTokenExpression'; | ||
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 +28,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 }; |
export interface TypeInterface { | ||
name: string; | ||
options: object; | ||
isPrimitiveType: boolean; | ||
objectType?: TypeInterface; | ||
getVariable(expression: string): any; | ||
getVariableTypeAndNameWithParent(expression: string): any; | ||
getType(expression: string): TypeInterface | null; | ||
toJSON(): any; | ||
stringify(): string; | ||
format(value: any, format?: string): string; | ||
options: object; | ||
stringify(): string; | ||
} |
@@ -9,3 +9,3 @@ /** | ||
import { TypeInterface } from './TypeInterface'; | ||
export declare type VariableScope = Record<string, any> & { | ||
export type VariableScope = Record<string, any> & { | ||
type: TypeInterface; | ||
@@ -12,0 +12,0 @@ _display?(): Promise<string>; |
"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,4 +15,84 @@ * Construct a new format string expression. | ||
this.expression = expression || ''; | ||
this.tokens = tools_1.compile(this.expression); | ||
this.tokens = FormatString.compile(this.expression); | ||
this.type = FormatString.TYPE; | ||
} | ||
static isInstanceOf(val) { | ||
return (val === null || val === void 0 ? void 0 : val.type) === FormatString.TYPE; | ||
} | ||
/** | ||
* 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(FormatString.unescape(format.substring(start)), start)); | ||
break; | ||
} | ||
// normal text in the gaps between curly braces | ||
tokens.push(new ConstantTokenExpression_1.ConstantTokenExpression(FormatString.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 = FormatString.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() { | ||
@@ -40,3 +124,3 @@ return this.expression; | ||
var expression = token.expression; | ||
tools_1.extract(type, expression, result, depth); | ||
(0, tools_1.extract)(type, expression, result, depth); | ||
} | ||
@@ -86,8 +170,8 @@ } | ||
var recordings = []; | ||
for (var i = 0; i < tokens.length; i++) { | ||
var token = tokens[i]; | ||
for (let i = 0; i < tokens.length; i++) { | ||
const token = tokens[i]; | ||
if (!token.isConstant()) { | ||
var expression = token.expression; | ||
const expression = token.expression; | ||
// We are interested in the type and name of the final two variables in the expression | ||
var arrayOfVariables = scopeType.getVariableTypeAndNameWithParent(expression); | ||
const arrayOfVariables = scopeType.getVariableTypeAndNameWithParent(expression); | ||
if (arrayOfVariables[0] == null && scopeType.name != 'view') { | ||
@@ -174,3 +258,3 @@ // This can happen in, e.g., an object table where the attribute is on its own as a property | ||
const type = scope.getExpressionType(expression); | ||
const text = tools_1.formatValue(value, type, token.format); | ||
const text = (0, tools_1.formatValue)(value, type, token.format); | ||
result += text; | ||
@@ -182,4 +266,64 @@ } | ||
} | ||
static 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 = 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 }; | ||
} | ||
} | ||
// Came to end of loop without a match. Faulty, return null | ||
return null; | ||
} | ||
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; | ||
} | ||
return result; | ||
} | ||
} | ||
exports.FormatString = FormatString; | ||
FormatString.TYPE = 'format-string'; | ||
// Expose internal functions for tests | ||
exports._compile = FormatString.compile; | ||
exports.parseEnclosingBraces = FormatString.parseEnclosingBraces; | ||
//# sourceMappingURL=FormatString.js.map |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -10,3 +14,3 @@ if (k2 === undefined) k2 = k; | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
@@ -13,0 +17,0 @@ Object.defineProperty(exports, "__esModule", { value: true }); |
@@ -30,3 +30,3 @@ "use strict"; | ||
const type = scope.getExpressionType(expression); | ||
return tools_1.formatValueAsync(value, type, this.format); | ||
return (0, tools_1.formatValueAsync)(value, type, this.format); | ||
} | ||
@@ -33,0 +33,0 @@ } |
@@ -23,3 +23,3 @@ "use strict"; | ||
const type = scope.getExpressionType(expression); | ||
return tools_1.formatValueAsync(value, type, this.format); | ||
return (0, tools_1.formatValueAsync)(value, type, this.format); | ||
} | ||
@@ -26,0 +26,0 @@ } |
"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 = void 0; | ||
const FunctionTokenExpression_1 = require("./token-expressions/FunctionTokenExpression"); | ||
@@ -10,142 +9,2 @@ const ShorthandTokenExpression_1 = require("./token-expressions/ShorthandTokenExpression"); | ||
const LegacyFunctionTokenExpression_1 = require("./token-expressions/LegacyFunctionTokenExpression"); | ||
function unescape(s) { | ||
var start = 0; | ||
var result = ''; | ||
var len = s.length; | ||
while (true) { | ||
var 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; | ||
} | ||
return result; | ||
} | ||
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 +11,0 @@ var variable = parent.getAttribute(name); |
{ | ||
"name": "@journeyapps/evaluator", | ||
"version": "0.0.0-dev.3d82e35.28b73fb", | ||
"description": "Journey JS library", | ||
"version": "0.0.0-dev.3ef8fa7", | ||
"description": "Journey Evaluator library", | ||
"main": "./dist/src/index.js", | ||
"typings": "./dist/@types/src/index", | ||
"scripts": { | ||
"build": "../node_modules/.bin/tsc --build", | ||
"test": "yarn build && yarn test:node && yarn test:browser", | ||
"test:browser": "karma start karma.conf.js --single-run", | ||
"test:node": "jasmine dist/test/unit/all.js" | ||
}, | ||
"dependencies": { | ||
"@journeyapps/core-xml": "0.0.0-dev.3d82e35.28b73fb" | ||
}, | ||
"files": [ | ||
@@ -20,3 +11,14 @@ "dist/src", | ||
], | ||
"gitHead": "f044d164080528680e2caf9504974227789a2e1b" | ||
} | ||
"dependencies": { | ||
"@journeyapps/core-xml": "0.0.0-dev.3ef8fa7" | ||
}, | ||
"devDependencies": {}, | ||
"scripts": { | ||
"build": "tsc --build", | ||
"watch": "tsc -b --watch", | ||
"clean": "tsc -b --clean && rm -rf dist", | ||
"test": "pnpm build && pnpm test:node && pnpm test:browser", | ||
"test:browser": "vitest --run --config vitest.config.browser.ts ", | ||
"test:node": "vitest --run --config vitest.config.unit.ts" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
125364
1154
44
+ Added@journeyapps/core-xml@0.0.0-dev.3ef8fa7(transitive)
+ Added@journeyapps/domparser@0.4.1(transitive)
+ Addedxmldom@0.6.0(transitive)
- Removed@journeyapps/core-xml@0.0.0-dev.3d82e35.28b73fb(transitive)
- Removed@journeyapps/domparser@0.3.0(transitive)