Skip to content

Commit

Permalink
Generate TypeScript definition file
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraq committed Sep 8, 2023
1 parent 578b7f8 commit 1e4bf07
Show file tree
Hide file tree
Showing 9 changed files with 1,571 additions and 378 deletions.
1,884 changes: 1,534 additions & 350 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
],
"module": "lib/icu-message-formatter.es.js",
"main": "lib/icu-message-formatter.cjs.js",
"types": "types/IcuMessageFormatter.d.ts",
"sideEffects": false,
"scripts": {
"format": "eslint --fix \"**/*.js\"",
"lint": "eslint \"**/*.js\"",
"test": "jest",
"build": "rollup --config --bundleConfigAsCjs && rollup --config rollup.config.dist.js --bundleConfigAsCjs",
"build": "npm run build:lib && npm run build:dist && npm run build:dts",
"build:lib": "rollup --config --bundleConfigAsCjs",
"build:dist": "rollup --config rollup.config.dist.js --bundleConfigAsCjs",
"build:dts": "tsc",
"prepublishOnly": "npm run build"
},
"dependencies": {
Expand Down Expand Up @@ -50,7 +54,8 @@
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-jsdoc": "^46.5.1",
"jest": "^29.6.4",
"rollup": "^3.29.0"
"rollup": "^3.29.0",
"typescript": "^5.2.2"
},
"engines": {
"node": ">=18"
Expand Down
12 changes: 5 additions & 7 deletions source/MessageFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {memoize} from '@ultraq/function-utils';

/**
* @template T
* @callback TypeHandler<T>
* @callback TypeHandler
* @param {string} value
* @param {string} matches
* @param {string} locale
Expand All @@ -53,7 +53,7 @@ export default class MessageFormatter {
* you register.
*
* @param {string} locale
* @param {Record<string,TypeHandler<*>>} [typeHandlers={}]
* @param {Record<string,TypeHandler<any>>} typeHandlers
* Optional object where the keys are the names of the types to register,
* their values being the functions that will return a nicely formatted
* string for the data and locale they are given.
Expand All @@ -68,14 +68,12 @@ export default class MessageFormatter {
* Formats an ICU message syntax string using `values` for placeholder data
* and any currently-registered type handlers.
*
* @param {string} message
* @param {FormatValues} [values={}]
* @return {string}
* @type {FormatFunction}
*/
format = memoize((message, values = {}) => {

return flatten(this.process(message, values)).join('');
})
});

/**
* Process an ICU message syntax string using `values` for placeholder data
Expand All @@ -89,7 +87,7 @@ export default class MessageFormatter {
* string renderer.
*
* @param {string} message
* @param {FormatValues} [values={}]
* @param {FormatValues} values
* @return {Array<any>}
*/
process(message, values = {}) {
Expand Down
11 changes: 3 additions & 8 deletions source/pluralTypeHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,10 @@ function replaceNumberSign(caseBody, value) {
* See https://formatjs.io/docs/core-concepts/icu-syntax#plural-format for more
* details on how the `plural` statement works.
*
* @param {string} value
* @param {string} matches
* @param {string} locale
* @param {FormatValues} values
* @param {FormatFunction} format
* @return {string}
* @type {TypeHandler<string>}
*/
export default function pluralTypeHandler(value, matches = '', locale, values, format) {
const { args, cases } = parseCases(matches);
const {args, cases} = parseCases(matches);

let intValue = parseInt(value);

Expand Down Expand Up @@ -110,7 +105,7 @@ export default function pluralTypeHandler(value, matches = '', locale, values, f
for (let i = 0; i < keywordPossibilities.length; i++) {
const keyword = keywordPossibilities[i];
if (keyword in cases) {
const { caseBody, numberValues } = replaceNumberSign(cases[keyword], intValue);
const {caseBody, numberValues} = replaceNumberSign(cases[keyword], intValue);
return format(caseBody, {
...values,
...numberValues
Expand Down
2 changes: 1 addition & 1 deletion source/selectTypeHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const OTHER = 'other';
* @return {string}
*/
export default function selectTypeHandler(value, matches = '', locale, values, format) {
const { cases } = parseCases(matches);
const {cases} = parseCases(matches);

if (value in cases) {
return format(cases[value], values);
Expand Down
6 changes: 3 additions & 3 deletions source/selectTypeHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ describe('selectTypeHandler', function() {
let message = '{mood, select, happy {Good} sad {bad} other {Ambivalent}} morning';

test('Explicit branch', function() {
let result = formatter.format(message, { mood: 'happy' });
let result = formatter.format(message, {mood: 'happy'});
expect(result).toBe('Good morning');
});

test('Fallback branch', function() {
let result = formatter.format(message, { mood: 'angry' });
let result = formatter.format(message, {mood: 'angry'});
expect(result).toBe('Ambivalent morning');
});
});
Expand All @@ -45,7 +45,7 @@ describe('selectTypeHandler', function() {

test('No matching branch', function() {
let message = 'I am feeling {mood, select, happy {good}}';
let result = formatter.format(message, { mood: 'ambivalent' });
let result = formatter.format(message, {mood: 'ambivalent'});
expect(result).toBe('I am feeling ambivalent');
});
});
Expand Down
2 changes: 1 addition & 1 deletion source/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export function splitFormattedArgument(block) {
* @param {string} string
* @param {string} separator
* @param {number} limit
* @param {string[]} [accumulator=[]]
* @param {string[]} accumulator
* @return {string[]}
*/
function split(string, separator, limit, accumulator = []) {
Expand Down
12 changes: 6 additions & 6 deletions source/utilities.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

/* eslint-env jest */
import { parseCases } from './utilities';
import {parseCases} from './utilities';

/**
* Tests for the `parseCases` util.
Expand Down Expand Up @@ -45,13 +45,13 @@ describe('parseCases', function() {
test('multiple cases', function() {
let result = parseCases('key1 {case1} key2 {case2} key3 {case3}');
expect(result.args).toStrictEqual([]);
expect(result.cases).toStrictEqual({ key1: 'case1', key2: 'case2', key3: 'case3' });
expect(result.cases).toStrictEqual({key1: 'case1', key2: 'case2', key3: 'case3'});
});

test('multiple cases with symbols', function() {
let result = parseCases('=key1 {case1} &key2 {case2} key3 {case3}');
expect(result.args).toStrictEqual([]);
expect(result.cases).toStrictEqual({ '=key1': 'case1', '&key2': 'case2', key3: 'case3' });
expect(result.cases).toStrictEqual({'=key1': 'case1', '&key2': 'case2', key3: 'case3'});
});

test('multiple cases with inconsistent whitespace', function() {
Expand All @@ -61,13 +61,13 @@ describe('parseCases', function() {
key2 {case2}
key3 {case3}`);
expect(result.args).toStrictEqual([]);
expect(result.cases).toStrictEqual({ key1: 'case1', key2: 'case2', key3: 'case3' });
expect(result.cases).toStrictEqual({key1: 'case1', key2: 'case2', key3: 'case3'});
});

test('multiple cases with minimal whitespace', function() {
let result = parseCases(`key1{case1}key2{case2}key3{case3}`);
expect(result.args).toStrictEqual([]);
expect(result.cases).toStrictEqual({ key1: 'case1', key2: 'case2', key3: 'case3' });
expect(result.cases).toStrictEqual({key1: 'case1', key2: 'case2', key3: 'case3'});
});

test('multiple cases with complex bodies', function() {
Expand All @@ -77,7 +77,7 @@ describe('parseCases', function() {
key2 {=key1 {case1} &key2 {case2} key3 {case3}}
key3 {}`);
expect(result.args).toStrictEqual([]);
expect(result.cases).toStrictEqual({ key1: '{}{}{}{{{{}}}}', key2: '=key1 {case1} &key2 {case2} key3 {case3}', key3: '' });
expect(result.cases).toStrictEqual({key1: '{}{}{}{{{{}}}}', key2: '=key1 {case1} &key2 {case2} key3 {case3}', key3: ''});
});
});

Expand Down
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"include": ["source/**/*.js"],
"exclude": ["source/**/*.test.js"],
"compilerOptions": {
"allowJs": true,
"declaration": true,
"declarationMap": true,
"emitDeclarationOnly": true,
"outFile": "lib/icu-message-formatter.d.ts"
}
}

0 comments on commit 1e4bf07

Please sign in to comment.