New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@phensley/messageformat

Package Overview
Dependencies
Maintainers
1
Versions
97
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@phensley/messageformat - npm Package Compare versions

Comparing version 0.26.0 to 1.0.0

6

lib-es/evaluation/args.d.ts
/**
* An argument value can have any type. We cast at the time of evaluation.
* This allows us to accept rich arguments for user-defined custom formatters.
*
* @public
*/

@@ -9,2 +11,4 @@ export declare type MessageArg = any;

* These can override positional arguments.
*
* @public
*/

@@ -17,2 +21,4 @@ export declare type MessageNamedArgs = {

* Combined positional and named arguments.
*
* @public
*/

@@ -19,0 +25,0 @@ export declare type MessageArgs = {

import { Decimal } from '@phensley/decimal';
import { MessageArg } from './args';
/**
* Interface for argument converters.
*
* @public
*/
export interface MessageArgConverter {

@@ -7,2 +12,7 @@ asString(arg: MessageArg | undefined): string;

}
/**
* Converts arguments to string or Decimal.
*
* @public
*/
export declare class DefaultMessageArgConverter implements MessageArgConverter {

@@ -9,0 +19,0 @@ /**

import { Decimal, DecimalConstants } from '@phensley/decimal';
/**
* Converts arguments to string or Decimal.
*
* @public
*/
var DefaultMessageArgConverter = /** @class */ (function () {

@@ -3,0 +8,0 @@ function DefaultMessageArgConverter() {

@@ -5,3 +5,13 @@ import { PluralRules } from '@phensley/plurals';

import { MessageCode } from '../parser';
/**
* User-defined message formatter function.
*
* @public
*/
export declare type MessageFormatFunc = (args: MessageArg[], options: string[]) => string;
/**
* A map of user-defined formatter names to their implementations.
*
* @public
*/
export declare type MessageFormatFuncMap = {

@@ -12,2 +22,4 @@ [name: string]: MessageFormatFunc;

* Evaluates a message format against a set of arguments, producing a string.
*
* @public
*/

@@ -21,4 +33,7 @@ export declare class MessageEngine {

constructor(plurals: PluralRules, converter: MessageArgConverter, formatters: MessageFormatFuncMap, code: MessageCode);
/**
* Evaluate the message code against the given arguments.
*/
evaluate(positional: MessageArg[], named?: MessageNamedArgs): string;
private _evaluate;
}

@@ -14,2 +14,4 @@ import { DecimalConstants } from '@phensley/decimal';

* Evaluates a message format against a set of arguments, producing a string.
*
* @public
*/

@@ -24,2 +26,5 @@ var MessageEngine = /** @class */ (function () {

}
/**
* Evaluate the message code against the given arguments.
*/
MessageEngine.prototype.evaluate = function (positional, named) {

@@ -26,0 +31,0 @@ if (named === void 0) { named = {}; }

import { PluralRules } from '@phensley/plurals';
import { MessageArg, MessageFormatFuncMap, MessageNamedArgs } from './evaluation';
import { MessageArgConverter } from './evaluation/converter';
/**
* Configures a MessageFormatter instance.
*
* @public
*/
export interface MessageFormatterOptions {

@@ -34,2 +39,4 @@ /**

* Convenience class that caches parsed messages.
*
* @public
*/

@@ -43,4 +50,11 @@ export declare class MessageFormatter {

constructor(options?: MessageFormatterOptions);
/**
* Parse and evaluate the message against the given argument. Internally caches parsed
* messages for reuse.
*/
format(message: string, positional: MessageArg[], named: MessageNamedArgs): string;
/**
* String representation of this formatter.
*/
toString(): string;
}

@@ -9,2 +9,4 @@ import { Cache } from '@phensley/cldr-utils';

* Convenience class that caches parsed messages.
*
* @public
*/

@@ -22,2 +24,6 @@ var MessageFormatter = /** @class */ (function () {

}
/**
* Parse and evaluate the message against the given argument. Internally caches parsed
* messages for reuse.
*/
MessageFormatter.prototype.format = function (message, positional, named) {

@@ -28,2 +34,5 @@ var code = this.cache.get(message);

};
/**
* String representation of this formatter.
*/
MessageFormatter.prototype.toString = function () {

@@ -30,0 +39,0 @@ return "MessageFormatter(formatters=" + JSON.stringify(Object.keys(this.formatters)) + " cached=" + this.cache.size() + ")";

@@ -0,1 +1,8 @@

/**
* A string and range of characters [s, e) (from 's' up to but not including 'e'),
* representing the current state of the parse of a given scope inside a
* message string.
*
* @public
*/
export interface MessageState {

@@ -6,11 +13,43 @@ t: string;

}
/**
* Matches message syntax fragments.
*
* @public
*/
export interface MessageMatcher {
/**
* Return the next character.
*/
char(r: MessageState): string;
/**
* Indicate if we've reached the end of input.
*/
complete(r: MessageState): boolean;
/**
* Match one or more spaces.
*/
spaces(r: MessageState): boolean;
/**
* Match a list of arguments.
*/
arguments(r: MessageState): (number | string)[] | undefined;
/**
* Match variable identifier.
*/
identifier(r: MessageState): string | undefined;
/**
* Match a list of options.
*/
options(r: MessageState): string[];
/**
* Match a formatter name.
*/
formatter(r: MessageState): string | undefined;
/**
* Match a plural offset.
*/
pluralOffset(r: MessageState): number;
/**
* Match a plural choice (exact or category).
*/
pluralChoice(r: MessageState): string | undefined;

@@ -28,3 +67,6 @@ }

*
* ```
* "{gender, select, female {guests plural one {her guest} other {her guests}}"
* ```
* @public
*/

@@ -60,2 +102,4 @@ export declare class StickyMatcher implements MessageMatcher {

* a substring [start, end].
*
* @public
*/

@@ -65,4 +109,19 @@ export declare class SubstringMatcher extends StickyMatcher {

}
/**
* Function that compiles a regular expression.
*
* @public
*/
export declare type regexpFunc = (pattern: string) => RegExp;
/**
* Construct a regular expression for use in a StickyMatcher.
*
* @public
*/
export declare const stickyRegexp: (pattern: string) => RegExp;
/**
* Construct a regular expression for use in a SubstringMatcher.
*
* @public
*/
export declare const substringRegexp: (pattern: string) => RegExp;

@@ -73,3 +132,5 @@ /**

* testing.
*
* @public
*/
export declare const buildMessageMatcher: (names: string[], sticky?: boolean) => StickyMatcher;

@@ -21,3 +21,6 @@ import { __extends } from "tslib";

*
* ```
* "{gender, select, female {guests plural one {her guest} other {her guests}}"
* ```
* @public
*/

@@ -123,2 +126,4 @@ var StickyMatcher = /** @class */ (function () {

* a substring [start, end].
*
* @public
*/

@@ -157,3 +162,13 @@ var SubstringMatcher = /** @class */ (function (_super) {

})();
/**
* Construct a regular expression for use in a StickyMatcher.
*
* @public
*/
export var stickyRegexp = function (pattern) { return new RegExp(pattern, 'y'); };
/**
* Construct a regular expression for use in a SubstringMatcher.
*
* @public
*/
export var substringRegexp = function (pattern) { return new RegExp('^' + pattern, 'g'); };

@@ -164,2 +179,4 @@ /**

* testing.
*
* @public
*/

@@ -166,0 +183,0 @@ export var buildMessageMatcher = function (names, sticky) {

import { MessageCode } from './types';
import { MessageMatcher } from './matcher';
/**
* Parses a string into message code. The result can be used to evaluate the
* message or serialized to JSON for later evaluation.
*
* @public
*/
export declare const parseMessagePattern: (raw: string, matcher: MessageMatcher) => MessageCode;

@@ -0,1 +1,7 @@

/**
* Parses a string into message code. The result can be used to evaluate the
* message or serialized to JSON for later evaluation.
*
* @public
*/
export var parseMessagePattern = function (raw, matcher) {

@@ -2,0 +8,0 @@ return new MessagePatternParser(raw, matcher).parse();

@@ -0,2 +1,12 @@

/**
* Argument reference inside a message instruction.
*
* @public
*/
export declare type Argument = string | number;
/**
* Type of a message instruction.
*
* @public
*/
export declare const enum MessageOpType {

@@ -12,5 +22,15 @@ TEXT = 0,

}
/**
* Instruction representing "no operation".
*
* @public
*/
export interface MessageNoopCode {
[0]: MessageOpType.NOOP;
}
/**
* Instruction representing an argument.
*
* @public
*/
export interface MessageArgCode {

@@ -20,5 +40,15 @@ [0]: MessageOpType.ARG;

}
/**
* Instruction representing an argument substitution.
*
* @public
*/
export interface MessageArgSubCode {
[0]: MessageOpType.ARGSUB;
}
/**
* Instruction representing static text.
*
* @public
*/
export interface MessageTextCode {

@@ -28,2 +58,7 @@ [0]: MessageOpType.TEXT;

}
/**
* Instruction representing a block of instructions.
*
* @public
*/
export interface MessageBlockCode {

@@ -33,2 +68,7 @@ [0]: MessageOpType.BLOCK;

}
/**
* Type of a plural instruction.
*
* @public
*/
export declare const enum PluralNumberType {

@@ -38,2 +78,7 @@ CARDINAL = 0,

}
/**
* Instruction representing a plural formatter.
*
* @public
*/
export interface MessagePluralCode {

@@ -46,2 +91,7 @@ [0]: MessageOpType.PLURAL;

}
/**
* Type of plural choice match.
*
* @public
*/
export declare const enum PluralChoiceType {

@@ -51,2 +101,7 @@ EXACT = 0,

}
/**
* Instruction representing a plural exact choice.
*
* @public
*/
export interface PluralExactChoice {

@@ -57,2 +112,7 @@ [0]: PluralChoiceType.EXACT;

}
/**
* Instruction representing a plural category choice.
*
* @public
*/
export interface PluralCategoryChoice {

@@ -63,3 +123,13 @@ [0]: PluralChoiceType.CATEGORY;

}
/**
* Type of plural choice instruction.
*
* @public
*/
export declare type PluralChoice = PluralExactChoice | PluralCategoryChoice;
/**
* Instruction representing a select formatter.
*
* @public
*/
export interface MessageSelectCode {

@@ -70,2 +140,7 @@ [0]: MessageOpType.SELECT;

}
/**
* Choice in a select formatter.
*
* @public
*/
export interface SelectChoice {

@@ -75,2 +150,10 @@ [0]: string;

}
/**
* Instruction representing a custom user-defined formatter.
*
* User can plugin formatters that accept 1 or more arguments
* and zero or more options.
*
* @public
*/
export interface MessageSimpleCode {

@@ -82,2 +165,7 @@ [0]: MessageOpType.SIMPLE;

}
/**
* Type of a message instruction.
*
* @public
*/
export declare type MessageCode = MessageArgCode | MessageTextCode | MessagePluralCode | MessageBlockCode | MessageSelectCode | MessageSimpleCode | MessageArgSubCode | MessageNoopCode;
/**
* An argument value can have any type. We cast at the time of evaluation.
* This allows us to accept rich arguments for user-defined custom formatters.
*
* @public
*/

@@ -9,2 +11,4 @@ export declare type MessageArg = any;

* These can override positional arguments.
*
* @public
*/

@@ -17,2 +21,4 @@ export declare type MessageNamedArgs = {

* Combined positional and named arguments.
*
* @public
*/

@@ -19,0 +25,0 @@ export declare type MessageArgs = {

import { Decimal } from '@phensley/decimal';
import { MessageArg } from './args';
/**
* Interface for argument converters.
*
* @public
*/
export interface MessageArgConverter {

@@ -7,2 +12,7 @@ asString(arg: MessageArg | undefined): string;

}
/**
* Converts arguments to string or Decimal.
*
* @public
*/
export declare class DefaultMessageArgConverter implements MessageArgConverter {

@@ -9,0 +19,0 @@ /**

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var decimal_1 = require("@phensley/decimal");
/**
* Converts arguments to string or Decimal.
*
* @public
*/
var DefaultMessageArgConverter = /** @class */ (function () {

@@ -5,0 +10,0 @@ function DefaultMessageArgConverter() {

@@ -5,3 +5,13 @@ import { PluralRules } from '@phensley/plurals';

import { MessageCode } from '../parser';
/**
* User-defined message formatter function.
*
* @public
*/
export declare type MessageFormatFunc = (args: MessageArg[], options: string[]) => string;
/**
* A map of user-defined formatter names to their implementations.
*
* @public
*/
export declare type MessageFormatFuncMap = {

@@ -12,2 +22,4 @@ [name: string]: MessageFormatFunc;

* Evaluates a message format against a set of arguments, producing a string.
*
* @public
*/

@@ -21,4 +33,7 @@ export declare class MessageEngine {

constructor(plurals: PluralRules, converter: MessageArgConverter, formatters: MessageFormatFuncMap, code: MessageCode);
/**
* Evaluate the message code against the given arguments.
*/
evaluate(positional: MessageArg[], named?: MessageNamedArgs): string;
private _evaluate;
}

@@ -16,2 +16,4 @@ "use strict";

* Evaluates a message format against a set of arguments, producing a string.
*
* @public
*/

@@ -26,2 +28,5 @@ var MessageEngine = /** @class */ (function () {

}
/**
* Evaluate the message code against the given arguments.
*/
MessageEngine.prototype.evaluate = function (positional, named) {

@@ -28,0 +33,0 @@ if (named === void 0) { named = {}; }

import { PluralRules } from '@phensley/plurals';
import { MessageArg, MessageFormatFuncMap, MessageNamedArgs } from './evaluation';
import { MessageArgConverter } from './evaluation/converter';
/**
* Configures a MessageFormatter instance.
*
* @public
*/
export interface MessageFormatterOptions {

@@ -34,2 +39,4 @@ /**

* Convenience class that caches parsed messages.
*
* @public
*/

@@ -43,4 +50,11 @@ export declare class MessageFormatter {

constructor(options?: MessageFormatterOptions);
/**
* Parse and evaluate the message against the given argument. Internally caches parsed
* messages for reuse.
*/
format(message: string, positional: MessageArg[], named: MessageNamedArgs): string;
/**
* String representation of this formatter.
*/
toString(): string;
}

@@ -11,2 +11,4 @@ "use strict";

* Convenience class that caches parsed messages.
*
* @public
*/

@@ -24,2 +26,6 @@ var MessageFormatter = /** @class */ (function () {

}
/**
* Parse and evaluate the message against the given argument. Internally caches parsed
* messages for reuse.
*/
MessageFormatter.prototype.format = function (message, positional, named) {

@@ -30,2 +36,5 @@ var code = this.cache.get(message);

};
/**
* String representation of this formatter.
*/
MessageFormatter.prototype.toString = function () {

@@ -32,0 +41,0 @@ return "MessageFormatter(formatters=" + JSON.stringify(Object.keys(this.formatters)) + " cached=" + this.cache.size() + ")";

@@ -0,1 +1,8 @@

/**
* A string and range of characters [s, e) (from 's' up to but not including 'e'),
* representing the current state of the parse of a given scope inside a
* message string.
*
* @public
*/
export interface MessageState {

@@ -6,11 +13,43 @@ t: string;

}
/**
* Matches message syntax fragments.
*
* @public
*/
export interface MessageMatcher {
/**
* Return the next character.
*/
char(r: MessageState): string;
/**
* Indicate if we've reached the end of input.
*/
complete(r: MessageState): boolean;
/**
* Match one or more spaces.
*/
spaces(r: MessageState): boolean;
/**
* Match a list of arguments.
*/
arguments(r: MessageState): (number | string)[] | undefined;
/**
* Match variable identifier.
*/
identifier(r: MessageState): string | undefined;
/**
* Match a list of options.
*/
options(r: MessageState): string[];
/**
* Match a formatter name.
*/
formatter(r: MessageState): string | undefined;
/**
* Match a plural offset.
*/
pluralOffset(r: MessageState): number;
/**
* Match a plural choice (exact or category).
*/
pluralChoice(r: MessageState): string | undefined;

@@ -28,3 +67,6 @@ }

*
* ```
* "{gender, select, female {guests plural one {her guest} other {her guests}}"
* ```
* @public
*/

@@ -60,2 +102,4 @@ export declare class StickyMatcher implements MessageMatcher {

* a substring [start, end].
*
* @public
*/

@@ -65,4 +109,19 @@ export declare class SubstringMatcher extends StickyMatcher {

}
/**
* Function that compiles a regular expression.
*
* @public
*/
export declare type regexpFunc = (pattern: string) => RegExp;
/**
* Construct a regular expression for use in a StickyMatcher.
*
* @public
*/
export declare const stickyRegexp: (pattern: string) => RegExp;
/**
* Construct a regular expression for use in a SubstringMatcher.
*
* @public
*/
export declare const substringRegexp: (pattern: string) => RegExp;

@@ -73,3 +132,5 @@ /**

* testing.
*
* @public
*/
export declare const buildMessageMatcher: (names: string[], sticky?: boolean) => StickyMatcher;

@@ -23,3 +23,6 @@ "use strict";

*
* ```
* "{gender, select, female {guests plural one {her guest} other {her guests}}"
* ```
* @public
*/

@@ -125,2 +128,4 @@ var StickyMatcher = /** @class */ (function () {

* a substring [start, end].
*
* @public
*/

@@ -159,3 +164,13 @@ var SubstringMatcher = /** @class */ (function (_super) {

})();
/**
* Construct a regular expression for use in a StickyMatcher.
*
* @public
*/
exports.stickyRegexp = function (pattern) { return new RegExp(pattern, 'y'); };
/**
* Construct a regular expression for use in a SubstringMatcher.
*
* @public
*/
exports.substringRegexp = function (pattern) { return new RegExp('^' + pattern, 'g'); };

@@ -166,2 +181,4 @@ /**

* testing.
*
* @public
*/

@@ -168,0 +185,0 @@ exports.buildMessageMatcher = function (names, sticky) {

import { MessageCode } from './types';
import { MessageMatcher } from './matcher';
/**
* Parses a string into message code. The result can be used to evaluate the
* message or serialized to JSON for later evaluation.
*
* @public
*/
export declare const parseMessagePattern: (raw: string, matcher: MessageMatcher) => MessageCode;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Parses a string into message code. The result can be used to evaluate the
* message or serialized to JSON for later evaluation.
*
* @public
*/
exports.parseMessagePattern = function (raw, matcher) {

@@ -4,0 +10,0 @@ return new MessagePatternParser(raw, matcher).parse();

@@ -0,2 +1,12 @@

/**
* Argument reference inside a message instruction.
*
* @public
*/
export declare type Argument = string | number;
/**
* Type of a message instruction.
*
* @public
*/
export declare const enum MessageOpType {

@@ -12,5 +22,15 @@ TEXT = 0,

}
/**
* Instruction representing "no operation".
*
* @public
*/
export interface MessageNoopCode {
[0]: MessageOpType.NOOP;
}
/**
* Instruction representing an argument.
*
* @public
*/
export interface MessageArgCode {

@@ -20,5 +40,15 @@ [0]: MessageOpType.ARG;

}
/**
* Instruction representing an argument substitution.
*
* @public
*/
export interface MessageArgSubCode {
[0]: MessageOpType.ARGSUB;
}
/**
* Instruction representing static text.
*
* @public
*/
export interface MessageTextCode {

@@ -28,2 +58,7 @@ [0]: MessageOpType.TEXT;

}
/**
* Instruction representing a block of instructions.
*
* @public
*/
export interface MessageBlockCode {

@@ -33,2 +68,7 @@ [0]: MessageOpType.BLOCK;

}
/**
* Type of a plural instruction.
*
* @public
*/
export declare const enum PluralNumberType {

@@ -38,2 +78,7 @@ CARDINAL = 0,

}
/**
* Instruction representing a plural formatter.
*
* @public
*/
export interface MessagePluralCode {

@@ -46,2 +91,7 @@ [0]: MessageOpType.PLURAL;

}
/**
* Type of plural choice match.
*
* @public
*/
export declare const enum PluralChoiceType {

@@ -51,2 +101,7 @@ EXACT = 0,

}
/**
* Instruction representing a plural exact choice.
*
* @public
*/
export interface PluralExactChoice {

@@ -57,2 +112,7 @@ [0]: PluralChoiceType.EXACT;

}
/**
* Instruction representing a plural category choice.
*
* @public
*/
export interface PluralCategoryChoice {

@@ -63,3 +123,13 @@ [0]: PluralChoiceType.CATEGORY;

}
/**
* Type of plural choice instruction.
*
* @public
*/
export declare type PluralChoice = PluralExactChoice | PluralCategoryChoice;
/**
* Instruction representing a select formatter.
*
* @public
*/
export interface MessageSelectCode {

@@ -70,2 +140,7 @@ [0]: MessageOpType.SELECT;

}
/**
* Choice in a select formatter.
*
* @public
*/
export interface SelectChoice {

@@ -75,2 +150,10 @@ [0]: string;

}
/**
* Instruction representing a custom user-defined formatter.
*
* User can plugin formatters that accept 1 or more arguments
* and zero or more options.
*
* @public
*/
export interface MessageSimpleCode {

@@ -82,2 +165,7 @@ [0]: MessageOpType.SIMPLE;

}
/**
* Type of a message instruction.
*
* @public
*/
export declare type MessageCode = MessageArgCode | MessageTextCode | MessagePluralCode | MessageBlockCode | MessageSelectCode | MessageSimpleCode | MessageArgSubCode | MessageNoopCode;

23

package.json
{
"name": "@phensley/messageformat",
"version": "0.26.0",
"version": "1.0.0",
"description": "Extensible ICU message formatter",

@@ -19,2 +19,4 @@ "main": "lib/index.js",

"scripts": {
"api": "api-extractor run -c ./etc/api-extractor.json --verbose",
"api:local": "yarn api -l",
"bench:off": "ts-node __benchmarks__/",

@@ -38,9 +40,10 @@ "build": "yarn build:cj && yarn build:es",

"dependencies": {
"@phensley/cldr-utils": "0.26.0",
"@phensley/decimal": "0.26.0",
"@phensley/plurals": "0.26.0",
"tslib": "1.10.x"
"@phensley/cldr-utils": "1.0.0",
"@phensley/decimal": "1.0.0",
"@phensley/plurals": "1.0.0",
"tslib": "1.11.1"
},
"devDependencies": {
"@types/jest": "24.0.22",
"@microsoft/api-extractor": "^7.7.8",
"@types/jest": "25.1.3",
"@types/node": "^10.14.10",

@@ -50,8 +53,8 @@ "beautify-benchmark": "^0.2.4",

"chalk": "^2.3.2",
"jest": "24.9.0",
"jest": "25.1.0",
"rimraf": "^3.0.0",
"ts-jest": "^24.0.2",
"ts-jest": "25.2.1",
"tslint": "^5.18.0",
"tslint-no-circular-imports": "^0.7.0",
"typescript": "3.7.x"
"typescript": "3.8.x"
},

@@ -71,3 +74,3 @@ "jest": {

},
"gitHead": "94045abc4df86296281f3eefc7d5e4bd48636cbe"
"gitHead": "84adc62f4b277203b9252f5c7aef98130d2baa3a"
}

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc