@phensley/messageformat
Advanced tools
Comparing version 1.2.13 to 1.2.14
@@ -7,3 +7,3 @@ /** | ||
*/ | ||
export declare type MessageArg = any; | ||
export type MessageArg = any; | ||
/** | ||
@@ -15,3 +15,3 @@ * Named args where each argument is given an explicit associative index or name. | ||
*/ | ||
export declare type MessageNamedArgs = { | ||
export type MessageNamedArgs = { | ||
[s: string]: MessageArg; | ||
@@ -25,5 +25,5 @@ [n: number]: MessageArg; | ||
*/ | ||
export declare type MessageArgs = { | ||
export type MessageArgs = { | ||
positional: MessageArg[]; | ||
named: MessageNamedArgs; | ||
}; |
@@ -0,1 +1,2 @@ | ||
export {}; | ||
//# sourceMappingURL=args.js.map |
@@ -10,3 +10,3 @@ import { PluralRules } from '@phensley/plurals'; | ||
*/ | ||
export declare type MessageFormatFunc = (args: MessageArg[], options: string[]) => string; | ||
export type MessageFormatFunc = (args: MessageArg[], options: string[]) => string; | ||
/** | ||
@@ -17,3 +17,3 @@ * A map of user-defined formatter names to their implementations. | ||
*/ | ||
export declare type MessageFormatFuncMap = { | ||
export type MessageFormatFuncMap = { | ||
[name: string]: MessageFormatFunc; | ||
@@ -20,0 +20,0 @@ }; |
@@ -34,6 +34,6 @@ import { DecimalConstants } from '@phensley/decimal'; | ||
switch (code[0]) { | ||
case 0 /* TEXT */: | ||
case 0 /* MessageOpType.TEXT */: | ||
this.buf += code[1]; | ||
break; | ||
case 4 /* BLOCK */: | ||
case 4 /* MessageOpType.BLOCK */: | ||
for (var _i = 0, _a = code[1]; _i < _a.length; _i++) { | ||
@@ -44,3 +44,3 @@ var n = _a[_i]; | ||
break; | ||
case 1 /* ARG */: { | ||
case 1 /* MessageOpType.ARG */: { | ||
var arg = get(code[1], args); | ||
@@ -50,7 +50,7 @@ this.buf += this.converter.asString(arg); | ||
} | ||
case 7 /* ARGSUB */: { | ||
case 7 /* MessageOpType.ARGSUB */: { | ||
this.buf += this.converter.asString(argsub); | ||
break; | ||
} | ||
case 2 /* PLURAL */: { | ||
case 2 /* MessageOpType.PLURAL */: { | ||
var arg = get(code[1][0], args); | ||
@@ -60,3 +60,3 @@ var offset = code[2]; | ||
argsub = offset ? num.subtract(offset) : num; | ||
var category = code[3] === 0 /* CARDINAL */ ? this.plurals.cardinal(argsub) : this.plurals.ordinal(argsub); | ||
var category = code[3] === 0 /* PluralNumberType.CARDINAL */ ? this.plurals.cardinal(argsub) : this.plurals.ordinal(argsub); | ||
var other = void 0; | ||
@@ -67,3 +67,3 @@ var found = 0; | ||
switch (c[0]) { | ||
case 0 /* EXACT */: | ||
case 0 /* PluralChoiceType.EXACT */: | ||
var v = DECIMAL_EXACT[c[1]]; | ||
@@ -79,3 +79,3 @@ if (v === undefined) { | ||
break; | ||
case 1 /* CATEGORY */: | ||
case 1 /* PluralChoiceType.CATEGORY */: | ||
if (c[1] === category) { | ||
@@ -99,3 +99,3 @@ this._evaluate(c[2], args, argsub); | ||
} | ||
case 3 /* SELECT */: { | ||
case 3 /* MessageOpType.SELECT */: { | ||
var arg = get(code[1][0], args); | ||
@@ -123,3 +123,3 @@ var str = this.converter.asString(arg); | ||
} | ||
case 6 /* SIMPLE */: { | ||
case 6 /* MessageOpType.SIMPLE */: { | ||
// One or more arguments and zero or more options | ||
@@ -126,0 +126,0 @@ var name_1 = code[1]; |
@@ -0,3 +1,4 @@ | ||
export * from './args'; | ||
export * from './converter'; | ||
export * from './evaluation'; | ||
//# sourceMappingURL=index.js.map |
@@ -36,2 +36,6 @@ import { PluralRules } from '@phensley/plurals'; | ||
cacheSize?: number; | ||
/** | ||
* Disables the escaping of text between matching apostrophes. | ||
*/ | ||
disableEscapes?: boolean; | ||
} | ||
@@ -38,0 +42,0 @@ /** |
@@ -14,4 +14,4 @@ import { Cache } from '@phensley/cldr-utils'; | ||
function MessageFormatter(options) { | ||
if (options === void 0) { options = {}; } | ||
var _this = this; | ||
if (options === void 0) { options = {}; } | ||
this.formatters = options.formatters || {}; | ||
@@ -22,3 +22,3 @@ this.converter = options.converter || new DefaultMessageArgConverter(); | ||
this.matcher = buildMessageMatcher(Object.keys(this.formatters)); | ||
this.cache = new Cache(function (s) { return parseMessagePattern(s, _this.matcher); }, size); | ||
this.cache = new Cache(function (s) { return parseMessagePattern(s, _this.matcher, options.disableEscapes); }, size); | ||
} | ||
@@ -37,3 +37,3 @@ /** | ||
MessageFormatter.prototype.toString = function () { | ||
return "MessageFormatter(formatters=" + JSON.stringify(Object.keys(this.formatters)) + " cached=" + this.cache.size() + ")"; | ||
return "MessageFormatter(formatters=".concat(JSON.stringify(Object.keys(this.formatters)), " cached=").concat(this.cache.size(), ")"); | ||
}; | ||
@@ -40,0 +40,0 @@ return MessageFormatter; |
@@ -111,3 +111,3 @@ /** | ||
*/ | ||
export declare type regexpFunc = (pattern: string) => RegExp; | ||
export type regexpFunc = (pattern: string) => RegExp; | ||
/** | ||
@@ -114,0 +114,0 @@ * Construct a regular expression for use in a StickyMatcher. |
@@ -32,3 +32,3 @@ import { __extends } from "tslib"; | ||
this._space = compile('[,\\s]+'); | ||
this._arg = compile("(0[1..9]+|\\d+|" + patterns.identifier + ")"); | ||
this._arg = compile("(0[1..9]+|\\d+|".concat(patterns.identifier, ")")); | ||
this._ident = compile(patterns.identifier); | ||
@@ -38,3 +38,3 @@ this._option = compile(patterns.option); | ||
formatters = BUILTINS.concat(formatters).sort(function (a, b) { return cmp(b.length, a.length); }); | ||
this._fmt = compile("(" + formatters.join('|') + ")"); | ||
this._fmt = compile("(".concat(formatters.join('|'), ")")); | ||
this._offset = compile(/offset:-?\d+/.source); | ||
@@ -41,0 +41,0 @@ this._choice = compile(patterns.pluralChoice); |
@@ -9,2 +9,2 @@ import { MessageCode } from './types'; | ||
*/ | ||
export declare const parseMessagePattern: (raw: string, matcher: MessageMatcher) => MessageCode; | ||
export declare const parseMessagePattern: (raw: string, matcher: MessageMatcher, disableEscapes?: boolean) => MessageCode; |
@@ -7,4 +7,4 @@ /** | ||
*/ | ||
export var parseMessagePattern = function (raw, matcher) { | ||
return new MessagePatternParser(raw, matcher).parse(); | ||
export var parseMessagePattern = function (raw, matcher, disableEscapes) { | ||
return new MessagePatternParser(raw, matcher, disableEscapes).parse(); | ||
}; | ||
@@ -38,5 +38,6 @@ /** | ||
var MessagePatternParser = /** @class */ (function () { | ||
function MessagePatternParser(raw, matcher) { | ||
function MessagePatternParser(raw, matcher, disableEscapes) { | ||
this.raw = raw; | ||
this.matcher = matcher; | ||
this.disableEscapes = disableEscapes; | ||
} | ||
@@ -59,3 +60,3 @@ MessagePatternParser.prototype.parse = function () { | ||
switch (c) { | ||
case "{" /* LEFT */: { | ||
case "{" /* Chars.LEFT */: { | ||
// Push non-empty buffer | ||
@@ -66,3 +67,3 @@ if (buf) { | ||
} | ||
var hidden = str[r.s + 1] === "-" /* MINUS */; | ||
var hidden = str[r.s + 1] === "-" /* Chars.MINUS */; | ||
var k = this.seek(r.s, r.e); | ||
@@ -75,3 +76,3 @@ if (k === -1) { | ||
// Tag is hidden from processor, emit as text | ||
n.push(text("{" /* LEFT */ + str.substring(r.s + 2, k + 1))); | ||
n.push(text("{" /* Chars.LEFT */ + str.substring(r.s + 2, k + 1))); | ||
// Skip over hidden tag | ||
@@ -97,20 +98,25 @@ r.s = k; | ||
} | ||
case "'" /* APOS */: { | ||
if (c === str[r.s + 1]) { | ||
// Convert double apostrophe to single | ||
case "'" /* Chars.APOS */: { | ||
if (this.disableEscapes) { | ||
buf += c; | ||
r.s++; | ||
} | ||
else { | ||
// Skip over apostrophe | ||
r.s++; | ||
// Capture string wrapped in apostrophes | ||
var k = str.indexOf(c, r.s); | ||
if (k === -1) { | ||
k = r.e; | ||
if (c === str[r.s + 1]) { | ||
// Convert double apostrophe to single | ||
buf += c; | ||
r.s++; | ||
} | ||
// Since this is escaped text, push text node without substituting '#' | ||
buf += str.substring(r.s, k); | ||
// Skip over escaped text | ||
r.s = k; | ||
else { | ||
// Skip over apostrophe | ||
r.s++; | ||
// Capture string wrapped in apostrophes | ||
var k = str.indexOf(c, r.s); | ||
if (k === -1) { | ||
k = r.e; | ||
} | ||
// Since this is escaped text, push text node without substituting '#' | ||
buf += str.substring(r.s, k); | ||
// Skip over escaped text | ||
r.s = k; | ||
} | ||
} | ||
@@ -147,3 +153,3 @@ break; | ||
// We have a simple argument. | ||
return [1 /* ARG */, args[0]]; | ||
return [1 /* MessageOpType.ARG */, args[0]]; | ||
} | ||
@@ -160,3 +166,3 @@ // See if any of our known formatters are present | ||
case 'selectordinal': | ||
var type = name === 'plural' ? 0 /* CARDINAL */ : 1 /* ORDINAL */; | ||
var type = name === 'plural' ? 0 /* PluralNumberType.CARDINAL */ : 1 /* PluralNumberType.ORDINAL */; | ||
return this.plural(args, type, m, r); | ||
@@ -180,6 +186,6 @@ case 'select': | ||
// Ensure we see a tag start next | ||
if (m.char(r) !== "{" /* LEFT */) { | ||
if (m.char(r) !== "{" /* Chars.LEFT */) { | ||
return undefined; | ||
} | ||
var hidden = this.raw[r.s + 1] === "-" /* MINUS */; | ||
var hidden = this.raw[r.s + 1] === "-" /* Chars.MINUS */; | ||
// Find matching end delimter | ||
@@ -194,3 +200,3 @@ var k = this.seek(r.s, r.e); | ||
var node = hidden | ||
? text("{" /* LEFT */ + this.raw.substring(r.s + 2, k + 1)) | ||
? text("{" /* Chars.LEFT */ + this.raw.substring(r.s + 2, k + 1)) | ||
: this.outer({ t: r.t, s: r.s + 1, e: k }, argsub); | ||
@@ -224,7 +230,7 @@ r.s = k + 1; | ||
var num = choice.substring(1); | ||
node = [0 /* EXACT */, num, block]; | ||
node = [0 /* PluralChoiceType.EXACT */, num, block]; | ||
} | ||
else { | ||
// Plural category match | ||
node = [1 /* CATEGORY */, choice, block]; | ||
node = [1 /* PluralChoiceType.CATEGORY */, choice, block]; | ||
} | ||
@@ -236,3 +242,3 @@ // Append and skip spaces | ||
// If we parsed no choices, emit a no-op | ||
return choices.length ? [2 /* PLURAL */, args, offset, type, choices] : NOOP; | ||
return choices.length ? [2 /* MessageOpType.PLURAL */, args, offset, type, choices] : NOOP; | ||
}; | ||
@@ -260,3 +266,3 @@ /** | ||
// If we parsed no choices, just emit a no-op | ||
return choices.length ? [3 /* SELECT */, args, choices] : NOOP; | ||
return choices.length ? [3 /* MessageOpType.SELECT */, args, choices] : NOOP; | ||
}; | ||
@@ -268,3 +274,3 @@ /** | ||
var options = m.options(r); | ||
return [6 /* SIMPLE */, name, args, options]; | ||
return [6 /* MessageOpType.SIMPLE */, name, args, options]; | ||
}; | ||
@@ -284,7 +290,7 @@ /** | ||
switch (c) { | ||
case "{" /* LEFT */: | ||
case "{" /* Chars.LEFT */: | ||
// Increase depth | ||
d++; | ||
break; | ||
case "}" /* RIGHT */: | ||
case "}" /* Chars.RIGHT */: | ||
// Reduce depth | ||
@@ -297,3 +303,3 @@ d--; | ||
break; | ||
case "'" /* APOS */: | ||
case "'" /* Chars.APOS */: | ||
if (c === r[i + 1]) { | ||
@@ -323,5 +329,5 @@ // Skip single escaped apostrophe | ||
var flatten = function (n) { | ||
return !n.length ? NOOP : n.length === 1 ? n[0] : [4 /* BLOCK */, n]; | ||
return !n.length ? NOOP : n.length === 1 ? n[0] : [4 /* MessageOpType.BLOCK */, n]; | ||
}; | ||
var text = function (s) { return [0 /* TEXT */, s]; }; | ||
var text = function (s) { return [0 /* MessageOpType.TEXT */, s]; }; | ||
/** | ||
@@ -339,3 +345,3 @@ * Emit a text node, performing argument substitution for all occurrences of | ||
// If no '#' character is found, return plain text | ||
j = s.indexOf("#" /* POUND */); | ||
j = s.indexOf("#" /* Chars.POUND */); | ||
if (j === -1) { | ||
@@ -356,6 +362,6 @@ return text(s); | ||
// Add a substitution op | ||
n.push([7 /* ARGSUB */]); | ||
n.push([7 /* MessageOpType.ARGSUB */]); | ||
// Skip over '#' and search for next occurrence | ||
i = j + 1; | ||
j = s.indexOf("#" /* POUND */, i); | ||
j = s.indexOf("#" /* Chars.POUND */, i); | ||
} | ||
@@ -368,3 +374,3 @@ // Push trailing text | ||
}; | ||
var NOOP = [5 /* NOOP */]; | ||
var NOOP = [5 /* MessageOpType.NOOP */]; | ||
//# sourceMappingURL=parser.js.map |
@@ -6,3 +6,3 @@ /** | ||
*/ | ||
export declare type Argument = string | number; | ||
export type Argument = string | number; | ||
/** | ||
@@ -121,3 +121,3 @@ * Type of a message instruction. | ||
*/ | ||
export declare type PluralChoice = PluralExactChoice | PluralCategoryChoice; | ||
export type PluralChoice = PluralExactChoice | PluralCategoryChoice; | ||
/** | ||
@@ -161,2 +161,2 @@ * Instruction representing a select formatter. | ||
*/ | ||
export declare type MessageCode = MessageArgCode | MessageTextCode | MessagePluralCode | MessageBlockCode | MessageSelectCode | MessageSimpleCode | MessageArgSubCode | MessageNoopCode; | ||
export type MessageCode = MessageArgCode | MessageTextCode | MessagePluralCode | MessageBlockCode | MessageSelectCode | MessageSimpleCode | MessageArgSubCode | MessageNoopCode; |
@@ -0,1 +1,2 @@ | ||
export {}; | ||
//# sourceMappingURL=types.js.map |
@@ -7,3 +7,3 @@ /** | ||
*/ | ||
export declare type MessageArg = any; | ||
export type MessageArg = any; | ||
/** | ||
@@ -15,3 +15,3 @@ * Named args where each argument is given an explicit associative index or name. | ||
*/ | ||
export declare type MessageNamedArgs = { | ||
export type MessageNamedArgs = { | ||
[s: string]: MessageArg; | ||
@@ -25,5 +25,5 @@ [n: number]: MessageArg; | ||
*/ | ||
export declare type MessageArgs = { | ||
export type MessageArgs = { | ||
positional: MessageArg[]; | ||
named: MessageNamedArgs; | ||
}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.DefaultMessageArgConverter = void 0; | ||
var decimal_1 = require("@phensley/decimal"); | ||
@@ -4,0 +5,0 @@ /** |
@@ -10,3 +10,3 @@ import { PluralRules } from '@phensley/plurals'; | ||
*/ | ||
export declare type MessageFormatFunc = (args: MessageArg[], options: string[]) => string; | ||
export type MessageFormatFunc = (args: MessageArg[], options: string[]) => string; | ||
/** | ||
@@ -17,3 +17,3 @@ * A map of user-defined formatter names to their implementations. | ||
*/ | ||
export declare type MessageFormatFuncMap = { | ||
export type MessageFormatFuncMap = { | ||
[name: string]: MessageFormatFunc; | ||
@@ -20,0 +20,0 @@ }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.MessageEngine = void 0; | ||
var decimal_1 = require("@phensley/decimal"); | ||
@@ -36,6 +37,6 @@ var get = function (key, args) { | ||
switch (code[0]) { | ||
case 0 /* TEXT */: | ||
case 0 /* MessageOpType.TEXT */: | ||
this.buf += code[1]; | ||
break; | ||
case 4 /* BLOCK */: | ||
case 4 /* MessageOpType.BLOCK */: | ||
for (var _i = 0, _a = code[1]; _i < _a.length; _i++) { | ||
@@ -46,3 +47,3 @@ var n = _a[_i]; | ||
break; | ||
case 1 /* ARG */: { | ||
case 1 /* MessageOpType.ARG */: { | ||
var arg = get(code[1], args); | ||
@@ -52,7 +53,7 @@ this.buf += this.converter.asString(arg); | ||
} | ||
case 7 /* ARGSUB */: { | ||
case 7 /* MessageOpType.ARGSUB */: { | ||
this.buf += this.converter.asString(argsub); | ||
break; | ||
} | ||
case 2 /* PLURAL */: { | ||
case 2 /* MessageOpType.PLURAL */: { | ||
var arg = get(code[1][0], args); | ||
@@ -62,3 +63,3 @@ var offset = code[2]; | ||
argsub = offset ? num.subtract(offset) : num; | ||
var category = code[3] === 0 /* CARDINAL */ ? this.plurals.cardinal(argsub) : this.plurals.ordinal(argsub); | ||
var category = code[3] === 0 /* PluralNumberType.CARDINAL */ ? this.plurals.cardinal(argsub) : this.plurals.ordinal(argsub); | ||
var other = void 0; | ||
@@ -69,3 +70,3 @@ var found = 0; | ||
switch (c[0]) { | ||
case 0 /* EXACT */: | ||
case 0 /* PluralChoiceType.EXACT */: | ||
var v = DECIMAL_EXACT[c[1]]; | ||
@@ -81,3 +82,3 @@ if (v === undefined) { | ||
break; | ||
case 1 /* CATEGORY */: | ||
case 1 /* PluralChoiceType.CATEGORY */: | ||
if (c[1] === category) { | ||
@@ -101,3 +102,3 @@ this._evaluate(c[2], args, argsub); | ||
} | ||
case 3 /* SELECT */: { | ||
case 3 /* MessageOpType.SELECT */: { | ||
var arg = get(code[1][0], args); | ||
@@ -125,3 +126,3 @@ var str = this.converter.asString(arg); | ||
} | ||
case 6 /* SIMPLE */: { | ||
case 6 /* MessageOpType.SIMPLE */: { | ||
// One or more arguments and zero or more options | ||
@@ -128,0 +129,0 @@ var name_1 = code[1]; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./args"), exports); | ||
tslib_1.__exportStar(require("./converter"), exports); | ||
tslib_1.__exportStar(require("./evaluation"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -36,2 +36,6 @@ import { PluralRules } from '@phensley/plurals'; | ||
cacheSize?: number; | ||
/** | ||
* Disables the escaping of text between matching apostrophes. | ||
*/ | ||
disableEscapes?: boolean; | ||
} | ||
@@ -38,0 +42,0 @@ /** |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.MessageFormatter = void 0; | ||
var cldr_utils_1 = require("@phensley/cldr-utils"); | ||
@@ -16,4 +17,4 @@ var plurals_1 = require("@phensley/plurals"); | ||
function MessageFormatter(options) { | ||
if (options === void 0) { options = {}; } | ||
var _this = this; | ||
if (options === void 0) { options = {}; } | ||
this.formatters = options.formatters || {}; | ||
@@ -23,4 +24,4 @@ this.converter = options.converter || new converter_1.DefaultMessageArgConverter(); | ||
var size = options.cacheSize || DEFAULT_CACHE_SIZE; | ||
this.matcher = parser_1.buildMessageMatcher(Object.keys(this.formatters)); | ||
this.cache = new cldr_utils_1.Cache(function (s) { return parser_1.parseMessagePattern(s, _this.matcher); }, size); | ||
this.matcher = (0, parser_1.buildMessageMatcher)(Object.keys(this.formatters)); | ||
this.cache = new cldr_utils_1.Cache(function (s) { return (0, parser_1.parseMessagePattern)(s, _this.matcher, options.disableEscapes); }, size); | ||
} | ||
@@ -39,3 +40,3 @@ /** | ||
MessageFormatter.prototype.toString = function () { | ||
return "MessageFormatter(formatters=" + JSON.stringify(Object.keys(this.formatters)) + " cached=" + this.cache.size() + ")"; | ||
return "MessageFormatter(formatters=".concat(JSON.stringify(Object.keys(this.formatters)), " cached=").concat(this.cache.size(), ")"); | ||
}; | ||
@@ -42,0 +43,0 @@ return MessageFormatter; |
@@ -111,3 +111,3 @@ /** | ||
*/ | ||
export declare type regexpFunc = (pattern: string) => RegExp; | ||
export type regexpFunc = (pattern: string) => RegExp; | ||
/** | ||
@@ -114,0 +114,0 @@ * Construct a regular expression for use in a StickyMatcher. |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.buildMessageMatcher = exports.substringRegexp = exports.stickyRegexp = exports.SubstringMatcher = exports.StickyMatcher = void 0; | ||
var tslib_1 = require("tslib"); | ||
@@ -34,3 +35,3 @@ var patterns = { | ||
this._space = compile('[,\\s]+'); | ||
this._arg = compile("(0[1..9]+|\\d+|" + patterns.identifier + ")"); | ||
this._arg = compile("(0[1..9]+|\\d+|".concat(patterns.identifier, ")")); | ||
this._ident = compile(patterns.identifier); | ||
@@ -40,3 +41,3 @@ this._option = compile(patterns.option); | ||
formatters = BUILTINS.concat(formatters).sort(function (a, b) { return cmp(b.length, a.length); }); | ||
this._fmt = compile("(" + formatters.join('|') + ")"); | ||
this._fmt = compile("(".concat(formatters.join('|'), ")")); | ||
this._offset = compile(/offset:-?\d+/.source); | ||
@@ -172,3 +173,4 @@ this._choice = compile(patterns.pluralChoice); | ||
*/ | ||
exports.stickyRegexp = function (pattern) { return new RegExp(pattern, 'y'); }; | ||
var stickyRegexp = function (pattern) { return new RegExp(pattern, 'y'); }; | ||
exports.stickyRegexp = stickyRegexp; | ||
/** | ||
@@ -179,3 +181,4 @@ * Construct a regular expression for use in a SubstringMatcher. | ||
*/ | ||
exports.substringRegexp = function (pattern) { return new RegExp('^' + pattern, 'g'); }; | ||
var substringRegexp = function (pattern) { return new RegExp('^' + pattern, 'g'); }; | ||
exports.substringRegexp = substringRegexp; | ||
/** | ||
@@ -188,6 +191,7 @@ * Constructs the right instance of matcher based on the runtime environment's | ||
*/ | ||
exports.buildMessageMatcher = function (names, sticky) { | ||
var buildMessageMatcher = function (names, sticky) { | ||
if (sticky === void 0) { sticky = hasStickyRegexp; } | ||
return new (sticky ? StickyMatcher : SubstringMatcher)(names, sticky ? exports.stickyRegexp : exports.substringRegexp); | ||
}; | ||
exports.buildMessageMatcher = buildMessageMatcher; | ||
//# sourceMappingURL=matcher.js.map |
@@ -9,2 +9,2 @@ import { MessageCode } from './types'; | ||
*/ | ||
export declare const parseMessagePattern: (raw: string, matcher: MessageMatcher) => MessageCode; | ||
export declare const parseMessagePattern: (raw: string, matcher: MessageMatcher, disableEscapes?: boolean) => MessageCode; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.parseMessagePattern = void 0; | ||
/** | ||
@@ -9,5 +10,6 @@ * Parses a string into message code. The result can be used to evaluate the | ||
*/ | ||
exports.parseMessagePattern = function (raw, matcher) { | ||
return new MessagePatternParser(raw, matcher).parse(); | ||
var parseMessagePattern = function (raw, matcher, disableEscapes) { | ||
return new MessagePatternParser(raw, matcher, disableEscapes).parse(); | ||
}; | ||
exports.parseMessagePattern = parseMessagePattern; | ||
/** | ||
@@ -40,5 +42,6 @@ * Hand-implemented parser for ICU message format. Designed to be compact and | ||
var MessagePatternParser = /** @class */ (function () { | ||
function MessagePatternParser(raw, matcher) { | ||
function MessagePatternParser(raw, matcher, disableEscapes) { | ||
this.raw = raw; | ||
this.matcher = matcher; | ||
this.disableEscapes = disableEscapes; | ||
} | ||
@@ -61,3 +64,3 @@ MessagePatternParser.prototype.parse = function () { | ||
switch (c) { | ||
case "{" /* LEFT */: { | ||
case "{" /* Chars.LEFT */: { | ||
// Push non-empty buffer | ||
@@ -68,3 +71,3 @@ if (buf) { | ||
} | ||
var hidden = str[r.s + 1] === "-" /* MINUS */; | ||
var hidden = str[r.s + 1] === "-" /* Chars.MINUS */; | ||
var k = this.seek(r.s, r.e); | ||
@@ -77,3 +80,3 @@ if (k === -1) { | ||
// Tag is hidden from processor, emit as text | ||
n.push(text("{" /* LEFT */ + str.substring(r.s + 2, k + 1))); | ||
n.push(text("{" /* Chars.LEFT */ + str.substring(r.s + 2, k + 1))); | ||
// Skip over hidden tag | ||
@@ -99,20 +102,25 @@ r.s = k; | ||
} | ||
case "'" /* APOS */: { | ||
if (c === str[r.s + 1]) { | ||
// Convert double apostrophe to single | ||
case "'" /* Chars.APOS */: { | ||
if (this.disableEscapes) { | ||
buf += c; | ||
r.s++; | ||
} | ||
else { | ||
// Skip over apostrophe | ||
r.s++; | ||
// Capture string wrapped in apostrophes | ||
var k = str.indexOf(c, r.s); | ||
if (k === -1) { | ||
k = r.e; | ||
if (c === str[r.s + 1]) { | ||
// Convert double apostrophe to single | ||
buf += c; | ||
r.s++; | ||
} | ||
// Since this is escaped text, push text node without substituting '#' | ||
buf += str.substring(r.s, k); | ||
// Skip over escaped text | ||
r.s = k; | ||
else { | ||
// Skip over apostrophe | ||
r.s++; | ||
// Capture string wrapped in apostrophes | ||
var k = str.indexOf(c, r.s); | ||
if (k === -1) { | ||
k = r.e; | ||
} | ||
// Since this is escaped text, push text node without substituting '#' | ||
buf += str.substring(r.s, k); | ||
// Skip over escaped text | ||
r.s = k; | ||
} | ||
} | ||
@@ -149,3 +157,3 @@ break; | ||
// We have a simple argument. | ||
return [1 /* ARG */, args[0]]; | ||
return [1 /* MessageOpType.ARG */, args[0]]; | ||
} | ||
@@ -162,3 +170,3 @@ // See if any of our known formatters are present | ||
case 'selectordinal': | ||
var type = name === 'plural' ? 0 /* CARDINAL */ : 1 /* ORDINAL */; | ||
var type = name === 'plural' ? 0 /* PluralNumberType.CARDINAL */ : 1 /* PluralNumberType.ORDINAL */; | ||
return this.plural(args, type, m, r); | ||
@@ -182,6 +190,6 @@ case 'select': | ||
// Ensure we see a tag start next | ||
if (m.char(r) !== "{" /* LEFT */) { | ||
if (m.char(r) !== "{" /* Chars.LEFT */) { | ||
return undefined; | ||
} | ||
var hidden = this.raw[r.s + 1] === "-" /* MINUS */; | ||
var hidden = this.raw[r.s + 1] === "-" /* Chars.MINUS */; | ||
// Find matching end delimter | ||
@@ -196,3 +204,3 @@ var k = this.seek(r.s, r.e); | ||
var node = hidden | ||
? text("{" /* LEFT */ + this.raw.substring(r.s + 2, k + 1)) | ||
? text("{" /* Chars.LEFT */ + this.raw.substring(r.s + 2, k + 1)) | ||
: this.outer({ t: r.t, s: r.s + 1, e: k }, argsub); | ||
@@ -226,7 +234,7 @@ r.s = k + 1; | ||
var num = choice.substring(1); | ||
node = [0 /* EXACT */, num, block]; | ||
node = [0 /* PluralChoiceType.EXACT */, num, block]; | ||
} | ||
else { | ||
// Plural category match | ||
node = [1 /* CATEGORY */, choice, block]; | ||
node = [1 /* PluralChoiceType.CATEGORY */, choice, block]; | ||
} | ||
@@ -238,3 +246,3 @@ // Append and skip spaces | ||
// If we parsed no choices, emit a no-op | ||
return choices.length ? [2 /* PLURAL */, args, offset, type, choices] : NOOP; | ||
return choices.length ? [2 /* MessageOpType.PLURAL */, args, offset, type, choices] : NOOP; | ||
}; | ||
@@ -262,3 +270,3 @@ /** | ||
// If we parsed no choices, just emit a no-op | ||
return choices.length ? [3 /* SELECT */, args, choices] : NOOP; | ||
return choices.length ? [3 /* MessageOpType.SELECT */, args, choices] : NOOP; | ||
}; | ||
@@ -270,3 +278,3 @@ /** | ||
var options = m.options(r); | ||
return [6 /* SIMPLE */, name, args, options]; | ||
return [6 /* MessageOpType.SIMPLE */, name, args, options]; | ||
}; | ||
@@ -286,7 +294,7 @@ /** | ||
switch (c) { | ||
case "{" /* LEFT */: | ||
case "{" /* Chars.LEFT */: | ||
// Increase depth | ||
d++; | ||
break; | ||
case "}" /* RIGHT */: | ||
case "}" /* Chars.RIGHT */: | ||
// Reduce depth | ||
@@ -299,3 +307,3 @@ d--; | ||
break; | ||
case "'" /* APOS */: | ||
case "'" /* Chars.APOS */: | ||
if (c === r[i + 1]) { | ||
@@ -325,5 +333,5 @@ // Skip single escaped apostrophe | ||
var flatten = function (n) { | ||
return !n.length ? NOOP : n.length === 1 ? n[0] : [4 /* BLOCK */, n]; | ||
return !n.length ? NOOP : n.length === 1 ? n[0] : [4 /* MessageOpType.BLOCK */, n]; | ||
}; | ||
var text = function (s) { return [0 /* TEXT */, s]; }; | ||
var text = function (s) { return [0 /* MessageOpType.TEXT */, s]; }; | ||
/** | ||
@@ -341,3 +349,3 @@ * Emit a text node, performing argument substitution for all occurrences of | ||
// If no '#' character is found, return plain text | ||
j = s.indexOf("#" /* POUND */); | ||
j = s.indexOf("#" /* Chars.POUND */); | ||
if (j === -1) { | ||
@@ -358,6 +366,6 @@ return text(s); | ||
// Add a substitution op | ||
n.push([7 /* ARGSUB */]); | ||
n.push([7 /* MessageOpType.ARGSUB */]); | ||
// Skip over '#' and search for next occurrence | ||
i = j + 1; | ||
j = s.indexOf("#" /* POUND */, i); | ||
j = s.indexOf("#" /* Chars.POUND */, i); | ||
} | ||
@@ -370,3 +378,3 @@ // Push trailing text | ||
}; | ||
var NOOP = [5 /* NOOP */]; | ||
var NOOP = [5 /* MessageOpType.NOOP */]; | ||
//# sourceMappingURL=parser.js.map |
@@ -6,3 +6,3 @@ /** | ||
*/ | ||
export declare type Argument = string | number; | ||
export type Argument = string | number; | ||
/** | ||
@@ -121,3 +121,3 @@ * Type of a message instruction. | ||
*/ | ||
export declare type PluralChoice = PluralExactChoice | PluralCategoryChoice; | ||
export type PluralChoice = PluralExactChoice | PluralCategoryChoice; | ||
/** | ||
@@ -161,2 +161,2 @@ * Instruction representing a select formatter. | ||
*/ | ||
export declare type MessageCode = MessageArgCode | MessageTextCode | MessagePluralCode | MessageBlockCode | MessageSelectCode | MessageSimpleCode | MessageArgSubCode | MessageNoopCode; | ||
export type MessageCode = MessageArgCode | MessageTextCode | MessagePluralCode | MessageBlockCode | MessageSelectCode | MessageSimpleCode | MessageArgSubCode | MessageNoopCode; |
{ | ||
"name": "@phensley/messageformat", | ||
"version": "1.2.13", | ||
"version": "1.2.14", | ||
"description": "Extensible ICU message formatter", | ||
@@ -39,6 +39,6 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@phensley/cldr-utils": "~1.2.13", | ||
"@phensley/decimal": "~1.2.13", | ||
"@phensley/plurals": "~1.2.13", | ||
"tslib": "^1.13.0" | ||
"@phensley/cldr-utils": "~1.2.14", | ||
"@phensley/decimal": "~1.2.14", | ||
"@phensley/plurals": "~1.2.14", | ||
"tslib": "^2.3.1" | ||
}, | ||
@@ -49,15 +49,15 @@ "devDependencies": { | ||
"@types/node": "^14.0.1", | ||
"@typescript-eslint/eslint-plugin": "^2.33.0", | ||
"@typescript-eslint/parser": "^2.33.0", | ||
"@typescript-eslint/eslint-plugin": "5.55.0", | ||
"@typescript-eslint/parser": "5.55.0", | ||
"beautify-benchmark": "^0.2.4", | ||
"benchmark": "^2.1.4", | ||
"chalk": "^4.0.0", | ||
"eslint": "^7.0.0", | ||
"eslint": "^8.32.0", | ||
"eslint-config-prettier": "^6.11.0", | ||
"eslint-plugin-prettier": "^3.1.3", | ||
"jest": "^26.0.1", | ||
"jest": "25.5.4", | ||
"prettier": "^2.0.5", | ||
"rimraf": "^3.0.2", | ||
"ts-jest": "^25.5.1", | ||
"typescript": "~3.8.3" | ||
"ts-jest": "25.5.1", | ||
"typescript": "4.9.4" | ||
}, | ||
@@ -77,3 +77,3 @@ "jest": { | ||
}, | ||
"gitHead": "1649a9047668bc4519d33526f50174ba3f2e2d32" | ||
"gitHead": "bea64f8ab05fb48af97cfa88b1293e0b17d2ed0d" | ||
} |
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
208977
2498
- Removedtslib@1.14.1(transitive)
Updated@phensley/cldr-utils@~1.2.14
Updated@phensley/decimal@~1.2.14
Updated@phensley/plurals@~1.2.14
Updatedtslib@^2.3.1