@parlajs/compiler 0.0.0-stage → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +21 -2
- package/build/build.d.ts +18 -0
- package/build/build.d.ts.map +1 -0
- package/build/build.js +10 -0
- package/build/cli.d.ts +3 -0
- package/build/cli.d.ts.map +1 -0
- package/build/cli.js +8 -0
- package/build/command.d.ts +13 -0
- package/build/command.d.ts.map +1 -0
- package/build/command.js +156 -0
- package/build/compile/code.d.ts +13 -0
- package/build/compile/code.d.ts.map +1 -0
- package/build/compile/code.js +42 -0
- package/build/compile/compile.d.ts +11 -0
- package/build/compile/compile.d.ts.map +1 -0
- package/build/compile/compile.js +244 -0
- package/build/compile/error.d.ts +4 -0
- package/build/compile/error.d.ts.map +1 -0
- package/build/compile/error.js +3 -0
- package/build/compile/info.d.ts +49 -0
- package/build/compile/info.d.ts.map +1 -0
- package/build/compile/info.js +22 -0
- package/build/compile/scope.d.ts +7 -0
- package/build/compile/scope.d.ts.map +1 -0
- package/build/compile/scope.js +14 -0
- package/build/compile/single.d.ts +11 -0
- package/build/compile/single.d.ts.map +1 -0
- package/build/compile/single.js +36 -0
- package/build/compile/variants.d.ts +16 -0
- package/build/compile/variants.d.ts.map +1 -0
- package/build/compile/variants.js +48 -0
- package/build/compile.d.ts +61 -0
- package/build/compile.d.ts.map +1 -0
- package/build/compile.js +385 -0
- package/build/config/checks.d.ts +21 -0
- package/build/config/checks.d.ts.map +1 -0
- package/build/config/checks.js +97 -0
- package/build/config/config.d.ts +71 -0
- package/build/config/config.d.ts.map +1 -0
- package/build/config/config.js +7 -0
- package/build/config/fallbacks.d.ts +6 -0
- package/build/config/fallbacks.d.ts.map +1 -0
- package/build/config/fallbacks.js +13 -0
- package/build/config/import.d.ts +31 -0
- package/build/config/import.d.ts.map +1 -0
- package/build/config/import.js +115 -0
- package/build/config/load.d.ts +13 -0
- package/build/config/load.d.ts.map +1 -0
- package/build/config/load.js +59 -0
- package/build/config.d.ts +77 -0
- package/build/config.d.ts.map +1 -0
- package/build/config.js +223 -0
- package/build/diagnostics.d.ts +31 -0
- package/build/diagnostics.d.ts.map +1 -0
- package/build/diagnostics.js +41 -0
- package/build/generate/checks.d.ts +9 -0
- package/build/generate/checks.d.ts.map +1 -0
- package/build/generate/checks.js +93 -0
- package/build/generate/chunk.d.ts +11 -0
- package/build/generate/chunk.d.ts.map +1 -0
- package/build/generate/chunk.js +57 -0
- package/build/generate/digits.d.ts +8 -0
- package/build/generate/digits.d.ts.map +1 -0
- package/build/generate/digits.js +57 -0
- package/build/generate/files.d.ts +7 -0
- package/build/generate/files.d.ts.map +1 -0
- package/build/generate/files.js +59 -0
- package/build/generate/locale.d.ts +21 -0
- package/build/generate/locale.d.ts.map +1 -0
- package/build/generate/locale.js +108 -0
- package/build/generate/modules.d.ts +27 -0
- package/build/generate/modules.d.ts.map +1 -0
- package/build/generate/modules.js +61 -0
- package/build/generate/namespace.d.ts +19 -0
- package/build/generate/namespace.d.ts.map +1 -0
- package/build/generate/namespace.js +65 -0
- package/build/generate/namespaces.d.ts +5 -0
- package/build/generate/namespaces.d.ts.map +1 -0
- package/build/generate/namespaces.js +41 -0
- package/build/generate/read.d.ts +21 -0
- package/build/generate/read.d.ts.map +1 -0
- package/build/generate/read.js +115 -0
- package/build/index.d.ts +9 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +7 -0
- package/build/init/detect.d.ts +43 -0
- package/build/init/detect.d.ts.map +1 -0
- package/build/init/detect.js +69 -0
- package/build/init/init.d.ts +12 -0
- package/build/init/init.d.ts.map +1 -0
- package/build/init/init.js +126 -0
- package/build/options/date.d.ts +3 -0
- package/build/options/date.d.ts.map +1 -0
- package/build/options/date.js +77 -0
- package/build/options/literal.d.ts +10 -0
- package/build/options/literal.d.ts.map +1 -0
- package/build/options/literal.js +11 -0
- package/build/options/names.d.ts +5 -0
- package/build/options/names.d.ts.map +1 -0
- package/build/options/names.js +56 -0
- package/build/options/number.d.ts +3 -0
- package/build/options/number.d.ts.map +1 -0
- package/build/options/number.js +17 -0
- package/build/options/options.d.ts +7 -0
- package/build/options/options.d.ts.map +1 -0
- package/build/options/options.js +66 -0
- package/build/options.d.ts +9 -0
- package/build/options.d.ts.map +1 -0
- package/build/options.js +214 -0
- package/build/plurals.d.ts +6 -0
- package/build/plurals.d.ts.map +1 -0
- package/build/plurals.js +50 -0
- package/build/types/names.d.ts +9 -0
- package/build/types/names.d.ts.map +1 -0
- package/build/types/names.js +109 -0
- package/build/types/namespace.d.ts +17 -0
- package/build/types/namespace.d.ts.map +1 -0
- package/build/types/namespace.js +113 -0
- package/build/types/signature.d.ts +8 -0
- package/build/types/signature.d.ts.map +1 -0
- package/build/types/signature.js +87 -0
- package/build/types.d.ts +25 -0
- package/build/types.d.ts.map +1 -0
- package/build/types.js +250 -0
- package/build/vite.d.ts +9 -0
- package/build/vite.d.ts.map +1 -0
- package/build/vite.js +125 -0
- package/build/watch.d.ts +12 -0
- package/build/watch.d.ts.map +1 -0
- package/build/watch.js +93 -0
- package/package.json +84 -3
- package/src/build.ts +31 -0
- package/src/cli.ts +9 -0
- package/src/command.ts +195 -0
- package/src/compile/code.ts +51 -0
- package/src/compile/compile.ts +289 -0
- package/src/compile/error.ts +3 -0
- package/src/compile/info.ts +63 -0
- package/src/compile/scope.ts +14 -0
- package/src/compile/single.ts +46 -0
- package/src/compile/variants.ts +68 -0
- package/src/config/checks.ts +140 -0
- package/src/config/config.ts +83 -0
- package/src/config/fallbacks.ts +18 -0
- package/src/config/import.ts +146 -0
- package/src/config/load.ts +75 -0
- package/src/diagnostics.ts +98 -0
- package/src/generate/checks.ts +103 -0
- package/src/generate/chunk.ts +73 -0
- package/src/generate/digits.ts +74 -0
- package/src/generate/files.ts +83 -0
- package/src/generate/locale.ts +145 -0
- package/src/generate/modules.ts +94 -0
- package/src/generate/namespace.ts +119 -0
- package/src/generate/namespaces.ts +59 -0
- package/src/generate/read.ts +159 -0
- package/src/index.ts +27 -0
- package/src/init/detect.ts +103 -0
- package/src/init/init.ts +158 -0
- package/src/options/date.ts +87 -0
- package/src/options/literal.ts +21 -0
- package/src/options/names.ts +64 -0
- package/src/options/number.ts +20 -0
- package/src/options/options.ts +75 -0
- package/src/plurals.ts +68 -0
- package/src/types/names.ts +115 -0
- package/src/types/namespace.ts +148 -0
- package/src/types/signature.ts +111 -0
- package/src/vite.ts +143 -0
- package/src/watch.ts +115 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { checkLocales } from '../config/checks.js';
|
|
4
|
+
import { ConfigError } from '../config/config.js';
|
|
5
|
+
import { CONFIG_FILES } from '../config/load.js';
|
|
6
|
+
import { detectProject } from './detect.js';
|
|
7
|
+
const FORMATS = {
|
|
8
|
+
mf2: { package: '@parlajs/format-mf2', hello: 'Hello, {$name}!' },
|
|
9
|
+
icu: { package: '@parlajs/format-icu', hello: 'Hello, {name}!' },
|
|
10
|
+
};
|
|
11
|
+
const OUT_DIR = 'src/parla';
|
|
12
|
+
const IGNORED = /^(?:\*\*\/|\/)?src\/parla\/?$/;
|
|
13
|
+
/** The options, checked before anything is written. */
|
|
14
|
+
function choose({ format = 'mf2', locales = 'en', catalogs = 'locales' }, config) {
|
|
15
|
+
if (!Object.hasOwn(FORMATS, format)) {
|
|
16
|
+
throw new ConfigError(`--format is mf2 or icu, not "${format}"`);
|
|
17
|
+
}
|
|
18
|
+
const list = locales.split(',').map((l) => l.trim());
|
|
19
|
+
checkLocales({ locales: list, sourceLocale: list[0] ?? '', fallbacks: {} });
|
|
20
|
+
const dir = catalogs.replace(/\/+$/, '');
|
|
21
|
+
// The path lands in a string literal of the config.
|
|
22
|
+
if (!/^[\w.-][\w./-]*$/.test(dir) || dir.split('/').includes('..')) {
|
|
23
|
+
throw new ConfigError(`--catalogs takes a relative folder of letters, digits, ".", "_" or "-"`);
|
|
24
|
+
}
|
|
25
|
+
const inside = path.posix.relative(OUT_DIR, path.posix.normalize(dir));
|
|
26
|
+
if (inside !== '..' && !inside.startsWith('../')) {
|
|
27
|
+
throw new ConfigError(`--catalogs cannot be in ${OUT_DIR}, which holds generated files only`);
|
|
28
|
+
}
|
|
29
|
+
return { config, format, locales: list, catalogs: dir };
|
|
30
|
+
}
|
|
31
|
+
const configModule = ({ format, locales, catalogs }) => `import { defineConfig } from '@parlajs/compiler';
|
|
32
|
+
import { ${format} } from '${FORMATS[format]?.package}';
|
|
33
|
+
|
|
34
|
+
export default defineConfig({
|
|
35
|
+
locales: [${locales.map((l) => `'${l}'`).join(', ')}],
|
|
36
|
+
sourceLocale: '${locales[0]}',
|
|
37
|
+
outDir: '${OUT_DIR}',
|
|
38
|
+
formats: { ${format} },
|
|
39
|
+
namespaces: { common: { format: '${format}', path: '${catalogs}/{locale}/common.json' } },
|
|
40
|
+
});
|
|
41
|
+
`;
|
|
42
|
+
/** Adds `src/parla/` to `.gitignore`, unless it is there. */
|
|
43
|
+
function ignore(cwd) {
|
|
44
|
+
const file = path.join(cwd, '.gitignore');
|
|
45
|
+
const text = existsSync(file) ? readFileSync(file, 'utf8') : undefined;
|
|
46
|
+
if (text?.split(/\r?\n/).some((line) => IGNORED.test(line.trim()))) {
|
|
47
|
+
return '';
|
|
48
|
+
}
|
|
49
|
+
const before = text === undefined || text === '' || text.endsWith('\n') ? '' : '\n';
|
|
50
|
+
writeFileSync(file, `${text ?? ''}${before}${OUT_DIR}/\n`);
|
|
51
|
+
return ` ${text === undefined ? 'created' : 'updated'} .gitignore: ${OUT_DIR}/`;
|
|
52
|
+
}
|
|
53
|
+
/** Writes the config, a catalog per locale and the ignore line; never one that exists. */
|
|
54
|
+
function write(cwd, choices) {
|
|
55
|
+
writeFileSync(path.join(cwd, choices.config), configModule(choices));
|
|
56
|
+
const lines = [` created ${choices.config}`];
|
|
57
|
+
const hello = `${JSON.stringify({ hello: FORMATS[choices.format]?.hello }, null, 2)}\n`;
|
|
58
|
+
for (const locale of choices.locales) {
|
|
59
|
+
const file = `${choices.catalogs}/${locale}/common.json`;
|
|
60
|
+
const target = path.join(cwd, file);
|
|
61
|
+
if (existsSync(target)) {
|
|
62
|
+
lines.push(` kept ${file}`);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
mkdirSync(path.dirname(target), { recursive: true });
|
|
66
|
+
writeFileSync(target, hello);
|
|
67
|
+
lines.push(` created ${file}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return [...lines, ignore(cwd)].filter(Boolean);
|
|
71
|
+
}
|
|
72
|
+
/** What is left to the user: installing, the Vite plugin, the first call. */
|
|
73
|
+
function next(project, { format }) {
|
|
74
|
+
const { framework, add, dependencies } = project;
|
|
75
|
+
const missing = (list) => list.filter((name) => !dependencies.has(name)).join(' ');
|
|
76
|
+
const runtime = missing([...new Set([framework.adapter, '@parlajs/core'])]);
|
|
77
|
+
const dev = missing(['@parlajs/compiler', FORMATS[format]?.package ?? '']);
|
|
78
|
+
const plugins = framework.plugin ? `parla(), ${framework.plugin}` : 'parla()';
|
|
79
|
+
return [
|
|
80
|
+
'',
|
|
81
|
+
'Next',
|
|
82
|
+
...(runtime ? [` ${add.add} ${runtime}`] : []),
|
|
83
|
+
...(dev ? [` ${add.dev} ${dev}`] : []),
|
|
84
|
+
'',
|
|
85
|
+
...(project.vite
|
|
86
|
+
? [
|
|
87
|
+
' vite.config.ts',
|
|
88
|
+
" import { parla } from '@parlajs/compiler/vite';",
|
|
89
|
+
` plugins: [${plugins}]`,
|
|
90
|
+
]
|
|
91
|
+
: [
|
|
92
|
+
' parla build before the bundler',
|
|
93
|
+
' parla build --watch beside its dev server',
|
|
94
|
+
]),
|
|
95
|
+
...(framework.name === 'Svelte'
|
|
96
|
+
? [
|
|
97
|
+
' svelte.config.js, for useParla',
|
|
98
|
+
' compilerOptions: { experimental: { async: true } }',
|
|
99
|
+
]
|
|
100
|
+
: []),
|
|
101
|
+
'',
|
|
102
|
+
" import { common } from './parla/namespaces/common.js';",
|
|
103
|
+
` ${framework.load} → t.common.hello({ name: 'Ada' })`,
|
|
104
|
+
` ${framework.docs}`,
|
|
105
|
+
];
|
|
106
|
+
}
|
|
107
|
+
/** Runs `parla init` in `cwd`: what it wrote and what is left to do. */
|
|
108
|
+
export function init(cwd, options) {
|
|
109
|
+
const project = detectProject(cwd);
|
|
110
|
+
if (!project) {
|
|
111
|
+
throw new ConfigError(`No package.json in ${cwd}: run parla init in the app's folder`);
|
|
112
|
+
}
|
|
113
|
+
// Node reads a `.ts` as CommonJS unless the package says `"type": "module"`.
|
|
114
|
+
const choices = choose(options, CONFIG_FILES[project.esm ? 0 : 1]);
|
|
115
|
+
const existing = CONFIG_FILES.find((name) => existsSync(path.join(cwd, name)));
|
|
116
|
+
if (existing) {
|
|
117
|
+
throw new ConfigError(`${existing} exists: edit it instead`);
|
|
118
|
+
}
|
|
119
|
+
const { framework, vite, manager } = project;
|
|
120
|
+
const header = [framework.name, vite ? 'Vite' : 'no Vite', manager, choices.format];
|
|
121
|
+
return [
|
|
122
|
+
`parla init: ${[...header, choices.locales.join(', ')].join(' · ')}`,
|
|
123
|
+
...write(cwd, choices),
|
|
124
|
+
...next(project, choices),
|
|
125
|
+
];
|
|
126
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"date.d.ts","sourceRoot":"","sources":["../../src/options/date.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAwD5C,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CA6BnE"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { chosen, literal } from './literal.js';
|
|
2
|
+
import { FIELDS, SETTINGS } from './names.js';
|
|
3
|
+
const DATE_FIELDS = new Set([
|
|
4
|
+
'weekday',
|
|
5
|
+
'day-weekday',
|
|
6
|
+
'month-day',
|
|
7
|
+
'month-day-weekday',
|
|
8
|
+
'year-month-day',
|
|
9
|
+
'year-month-day-weekday',
|
|
10
|
+
]);
|
|
11
|
+
const LENGTHS = new Set(['long', 'medium', 'short']);
|
|
12
|
+
const PRECISIONS = ['hour', 'minute', 'second'];
|
|
13
|
+
/** MF2's fields, length and precision, as `Intl`'s fields. */
|
|
14
|
+
function semanticFields(name, options) {
|
|
15
|
+
const { fields, dateFields, length, dateLength, precision, timePrecision, timeZoneStyle } = options;
|
|
16
|
+
const out = {};
|
|
17
|
+
if (name !== 'time') {
|
|
18
|
+
const size = length ?? dateLength;
|
|
19
|
+
const isLong = size ? chosen('length', { value: size, allowed: LENGTHS }) : 'medium';
|
|
20
|
+
const asked = fields ?? dateFields;
|
|
21
|
+
const set = asked ? chosen('fields', { value: asked, allowed: DATE_FIELDS }) : 'year-month-day';
|
|
22
|
+
const months = { long: 'long', medium: 'short', short: 'numeric' };
|
|
23
|
+
for (const field of set.split('-')) {
|
|
24
|
+
let value = 'numeric';
|
|
25
|
+
if (field === 'month') {
|
|
26
|
+
value = months[isLong] ?? value;
|
|
27
|
+
}
|
|
28
|
+
else if (field === 'weekday') {
|
|
29
|
+
value = isLong === 'long' ? 'long' : 'short';
|
|
30
|
+
}
|
|
31
|
+
out[field] = literal(value);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (name !== 'date') {
|
|
35
|
+
const asked = precision ?? timePrecision;
|
|
36
|
+
const until = asked
|
|
37
|
+
? chosen('precision', { value: asked, allowed: new Set(PRECISIONS) })
|
|
38
|
+
: 'minute';
|
|
39
|
+
for (const field of PRECISIONS.slice(0, PRECISIONS.indexOf(until) + 1)) {
|
|
40
|
+
out[field] = literal('numeric');
|
|
41
|
+
}
|
|
42
|
+
if (timeZoneStyle) {
|
|
43
|
+
out['timeZoneName'] = timeZoneStyle;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
const STYLES = ['style', 'dateStyle', 'timeStyle'];
|
|
49
|
+
const SEMANTIC = ['fields', 'dateFields', 'precision', 'timePrecision', 'timeZoneStyle'];
|
|
50
|
+
export function dateOptions(name, options) {
|
|
51
|
+
const out = Object.fromEntries(Object.entries(options).filter(([k]) => SETTINGS.includes(k) || FIELDS.includes(k)));
|
|
52
|
+
if (SEMANTIC.some((k) => options[k])) {
|
|
53
|
+
const style = STYLES.find((k) => options[k]);
|
|
54
|
+
if (style) {
|
|
55
|
+
throw new Error(`${style} cannot be combined with fields or a precision: use length`);
|
|
56
|
+
}
|
|
57
|
+
return { ...out, ...semanticFields(name, options) };
|
|
58
|
+
}
|
|
59
|
+
const { style, length, dateLength, dateStyle, timeStyle } = options;
|
|
60
|
+
if (style || length || dateLength || dateStyle || timeStyle) {
|
|
61
|
+
const size = style ?? length;
|
|
62
|
+
if (name === 'time') {
|
|
63
|
+
return { ...out, timeStyle: size ?? literal('short') };
|
|
64
|
+
}
|
|
65
|
+
out['dateStyle'] = dateStyle ?? dateLength ?? size ?? literal('medium');
|
|
66
|
+
return name === 'date' ? out : { ...out, timeStyle: timeStyle ?? literal('short') };
|
|
67
|
+
}
|
|
68
|
+
if (FIELDS.some((k) => options[k])) {
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
// Settings alone: the runtime adds the default style only to a call without options.
|
|
72
|
+
return {
|
|
73
|
+
...out,
|
|
74
|
+
...(name === 'time' ? {} : { dateStyle: literal('medium') }),
|
|
75
|
+
...(name === 'date' ? {} : { timeStyle: literal('short') }),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Model } from 'messageformat';
|
|
2
|
+
export type Value = Model.Literal | Model.VariableRef;
|
|
3
|
+
export type Options = Record<string, Value>;
|
|
4
|
+
export declare const literal: (value: string) => Model.Literal;
|
|
5
|
+
/** A literal's value, which must be one of `allowed`; a variable cannot name fields at build time. */
|
|
6
|
+
export declare function chosen(name: string, { value, allowed }: {
|
|
7
|
+
value: Value;
|
|
8
|
+
allowed: ReadonlySet<string>;
|
|
9
|
+
}): string;
|
|
10
|
+
//# sourceMappingURL=literal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"literal.d.ts","sourceRoot":"","sources":["../../src/options/literal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAE3C,MAAM,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC,WAAW,CAAC;AAEtD,MAAM,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;AAE5C,eAAO,MAAM,OAAO,UAAW,MAAM,KAAG,KAAK,CAAC,OAAuC,CAAC;AAEtF,sGAAsG;AACtG,wBAAgB,MAAM,CACpB,IAAI,EAAE,MAAM,EACZ,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,OAAO,EAAE,WAAW,CAAC,MAAM,CAAC,CAAA;CAAE,GACjE,MAAM,CAQR"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export const literal = (value) => ({ type: 'literal', value });
|
|
2
|
+
/** A literal's value, which must be one of `allowed`; a variable cannot name fields at build time. */
|
|
3
|
+
export function chosen(name, { value, allowed }) {
|
|
4
|
+
if (value.type !== 'literal') {
|
|
5
|
+
throw new Error(`${name} must be a literal`);
|
|
6
|
+
}
|
|
7
|
+
if (!allowed.has(value.value)) {
|
|
8
|
+
throw new Error(`${name} must be one of ${[...allowed].join(', ')}, not "${value.value}"`);
|
|
9
|
+
}
|
|
10
|
+
return value.value;
|
|
11
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const SETTINGS: string[];
|
|
2
|
+
export declare const FIELDS: string[];
|
|
3
|
+
export declare const NUMBERS: Readonly<Record<string, ReadonlySet<string>>>;
|
|
4
|
+
export declare const DATES: Readonly<Record<string, ReadonlySet<string>>>;
|
|
5
|
+
//# sourceMappingURL=names.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"names.d.ts","sourceRoot":"","sources":["../../src/options/names.ts"],"names":[],"mappings":"AAkBA,eAAO,MAAM,QAAQ,UAAqE,CAAC;AAE3F,eAAO,MAAM,MAAM,UAYlB,CAAC;AAEF,eAAO,MAAM,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC,CAajE,CAAC;AAEF,eAAO,MAAM,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC,CAc/D,CAAC"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
const DIGITS = [
|
|
2
|
+
'minimumIntegerDigits',
|
|
3
|
+
'minimumSignificantDigits',
|
|
4
|
+
'maximumSignificantDigits',
|
|
5
|
+
'roundingIncrement',
|
|
6
|
+
'roundingMode',
|
|
7
|
+
'roundingPriority',
|
|
8
|
+
'trailingZeroDisplay',
|
|
9
|
+
];
|
|
10
|
+
const COMMON = ['signDisplay', 'useGrouping', 'numberingSystem', 'select', 'offset', 'scale'];
|
|
11
|
+
const FRACTION = ['minimumFractionDigits', 'maximumFractionDigits'];
|
|
12
|
+
const COMPACT = ['notation', 'compactDisplay'];
|
|
13
|
+
const FORMAT = [...COMMON, ...DIGITS, ...FRACTION, ...COMPACT];
|
|
14
|
+
export const SETTINGS = ['calendar', 'numberingSystem', 'timeZone', 'hour12', 'hourCycle'];
|
|
15
|
+
export const FIELDS = [
|
|
16
|
+
'weekday',
|
|
17
|
+
'era',
|
|
18
|
+
'year',
|
|
19
|
+
'month',
|
|
20
|
+
'day',
|
|
21
|
+
'dayPeriod',
|
|
22
|
+
'hour',
|
|
23
|
+
'minute',
|
|
24
|
+
'second',
|
|
25
|
+
'fractionalSecondDigits',
|
|
26
|
+
'timeZoneName',
|
|
27
|
+
];
|
|
28
|
+
export const NUMBERS = {
|
|
29
|
+
number: new Set([...FORMAT, 'style']),
|
|
30
|
+
integer: new Set([...COMMON, 'minimumIntegerDigits', 'maximumSignificantDigits', ...COMPACT]),
|
|
31
|
+
currency: new Set([
|
|
32
|
+
...FORMAT,
|
|
33
|
+
'fractionDigits',
|
|
34
|
+
'currency',
|
|
35
|
+
'currencyDisplay',
|
|
36
|
+
'currencySign',
|
|
37
|
+
'unitDisplay',
|
|
38
|
+
]),
|
|
39
|
+
percent: new Set([...FORMAT, 'currencySign']),
|
|
40
|
+
unit: new Set([...FORMAT, 'unit', 'unitDisplay', 'currencyDisplay']),
|
|
41
|
+
};
|
|
42
|
+
export const DATES = {
|
|
43
|
+
date: new Set([...SETTINGS, ...FIELDS, 'style', 'length', 'fields']),
|
|
44
|
+
time: new Set([...SETTINGS, ...FIELDS, 'style', 'length', 'precision', 'timeZoneStyle']),
|
|
45
|
+
datetime: new Set([
|
|
46
|
+
...SETTINGS,
|
|
47
|
+
...FIELDS,
|
|
48
|
+
'style',
|
|
49
|
+
'dateStyle',
|
|
50
|
+
'timeStyle',
|
|
51
|
+
'dateFields',
|
|
52
|
+
'dateLength',
|
|
53
|
+
'timePrecision',
|
|
54
|
+
'timeZoneStyle',
|
|
55
|
+
]),
|
|
56
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"number.d.ts","sourceRoot":"","sources":["../../src/options/number.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAI5C,wBAAgB,aAAa,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAcvD"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { chosen, literal } from './literal.js';
|
|
2
|
+
const SELECTS = new Set(['plural', 'cardinal', 'ordinal', 'exact']);
|
|
3
|
+
export function numberOptions(options) {
|
|
4
|
+
const { fractionDigits, ...out } = options;
|
|
5
|
+
if (fractionDigits && !(fractionDigits.type === 'literal' && fractionDigits.value === 'auto')) {
|
|
6
|
+
out['minimumFractionDigits'] = fractionDigits;
|
|
7
|
+
out['maximumFractionDigits'] = fractionDigits;
|
|
8
|
+
}
|
|
9
|
+
const { select, useGrouping } = out;
|
|
10
|
+
if (select?.type === 'literal') {
|
|
11
|
+
chosen('select', { value: select, allowed: SELECTS });
|
|
12
|
+
}
|
|
13
|
+
if (useGrouping?.type === 'literal' && useGrouping.value === 'never') {
|
|
14
|
+
out['useGrouping'] = literal('false');
|
|
15
|
+
}
|
|
16
|
+
return out;
|
|
17
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Model } from 'messageformat';
|
|
2
|
+
import type { Options } from './literal.ts';
|
|
3
|
+
/** Which of a variable's options a function applied to it takes over. */
|
|
4
|
+
export declare function inheritedOptions(from: Model.FunctionRef, to: string): Options;
|
|
5
|
+
/** A built-in function's options as `Intl` takes them; throws for one it cannot honour. */
|
|
6
|
+
export declare function intlOptions({ name, options }: Model.FunctionRef): Options;
|
|
7
|
+
//# sourceMappingURL=options.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.d.ts","sourceRoot":"","sources":["../../src/options/options.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAG3C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AA+B5C,yEAAyE;AACzE,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,KAAK,CAAC,WAAW,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAiB7E;AAED,2FAA2F;AAC3F,wBAAgB,WAAW,CAAC,EAAE,IAAI,EAAE,OAAY,EAAE,EAAE,KAAK,CAAC,WAAW,GAAG,OAAO,CAmB9E"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { dateOptions } from './date.js';
|
|
2
|
+
import { DATES, NUMBERS } from './names.js';
|
|
3
|
+
import { numberOptions } from './number.js';
|
|
4
|
+
// An `Intl` object per message made a build 7 times slower.
|
|
5
|
+
const checked = new Set();
|
|
6
|
+
/** Throws what `Intl` would at run time, where every option is a literal. */
|
|
7
|
+
function check(name, options) {
|
|
8
|
+
const key = name + JSON.stringify(options);
|
|
9
|
+
if (checked.has(key)) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
const given = {};
|
|
13
|
+
for (const [k, v] of Object.entries(options)) {
|
|
14
|
+
if (v.type !== 'literal') {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
given[k] = v.value === 'false' || v.value === 'true' ? v.value === 'true' : v.value;
|
|
18
|
+
}
|
|
19
|
+
if (Object.hasOwn(NUMBERS, name)) {
|
|
20
|
+
const style = name === 'number' || name === 'integer' ? {} : { style: name };
|
|
21
|
+
// oxlint-disable-next-line no-new -- only its errors matter
|
|
22
|
+
new Intl.NumberFormat('en', { ...given, ...style });
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
// oxlint-disable-next-line no-new -- only its errors matter
|
|
26
|
+
new Intl.DateTimeFormat('en', given);
|
|
27
|
+
}
|
|
28
|
+
checked.add(key);
|
|
29
|
+
}
|
|
30
|
+
/** Which of a variable's options a function applied to it takes over. */
|
|
31
|
+
export function inheritedOptions(from, to) {
|
|
32
|
+
const own = NUMBERS[to];
|
|
33
|
+
const entries = Object.entries(from.options ?? {});
|
|
34
|
+
if (own && Object.hasOwn(NUMBERS, from.name)) {
|
|
35
|
+
// `offset` is a plural's: only its own `#` subtracts it.
|
|
36
|
+
return Object.fromEntries(entries.filter(([k]) => own.has(k) && k !== 'style' && k !== 'offset'));
|
|
37
|
+
}
|
|
38
|
+
if (from.name === to) {
|
|
39
|
+
return Object.fromEntries(entries);
|
|
40
|
+
}
|
|
41
|
+
return Object.hasOwn(DATES, to) && Object.hasOwn(DATES, from.name)
|
|
42
|
+
? Object.fromEntries(entries.filter(([k]) => k === 'calendar' || k === 'timeZone' || k === 'hour12'))
|
|
43
|
+
: {};
|
|
44
|
+
}
|
|
45
|
+
/** A built-in function's options as `Intl` takes them; throws for one it cannot honour. */
|
|
46
|
+
export function intlOptions({ name, options = {} }) {
|
|
47
|
+
const known = NUMBERS[name] ?? DATES[name] ?? new Set();
|
|
48
|
+
const entries = Object.entries(options).filter(([k]) => k !== 'u:id');
|
|
49
|
+
for (const [key] of entries) {
|
|
50
|
+
if (!known.has(key)) {
|
|
51
|
+
throw new Error(`:${name} has no option "${key}"`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (entries.length === 0 && name !== 'currency' && name !== 'unit') {
|
|
55
|
+
return {};
|
|
56
|
+
}
|
|
57
|
+
const given = Object.fromEntries(entries);
|
|
58
|
+
try {
|
|
59
|
+
const out = Object.hasOwn(NUMBERS, name) ? numberOptions(given) : dateOptions(name, given);
|
|
60
|
+
check(name, out);
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
throw new Error(`:${name}: ${error.message}`, { cause: error });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Model } from 'messageformat';
|
|
2
|
+
type Value = Model.Literal | Model.VariableRef;
|
|
3
|
+
type Options = Record<string, Value>;
|
|
4
|
+
/** Which of a variable's options a function applied to it takes over. */
|
|
5
|
+
export declare function inheritedOptions(from: Model.FunctionRef, to: string): Options;
|
|
6
|
+
/** A built-in function's options as `Intl` takes them; throws for one it cannot honour. */
|
|
7
|
+
export declare function intlOptions({ name, options }: Model.FunctionRef): Options;
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=options.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.d.ts","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAE3C,KAAK,KAAK,GAAG,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC,WAAW,CAAC;AAC/C,KAAK,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;AA+LrC,yEAAyE;AACzE,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,KAAK,CAAC,WAAW,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAiB7E;AAED,2FAA2F;AAC3F,wBAAgB,WAAW,CAAC,EAAE,IAAI,EAAE,OAAY,EAAE,EAAE,KAAK,CAAC,WAAW,GAAG,OAAO,CAmB9E"}
|
package/build/options.js
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
const DIGITS = [
|
|
2
|
+
'minimumIntegerDigits',
|
|
3
|
+
'minimumSignificantDigits',
|
|
4
|
+
'maximumSignificantDigits',
|
|
5
|
+
'roundingIncrement',
|
|
6
|
+
'roundingMode',
|
|
7
|
+
'roundingPriority',
|
|
8
|
+
'trailingZeroDisplay',
|
|
9
|
+
];
|
|
10
|
+
const COMMON = ['signDisplay', 'useGrouping', 'numberingSystem', 'select', 'offset', 'scale'];
|
|
11
|
+
const FRACTION = ['minimumFractionDigits', 'maximumFractionDigits'];
|
|
12
|
+
const COMPACT = ['notation', 'compactDisplay'];
|
|
13
|
+
const FORMAT = [...COMMON, ...DIGITS, ...FRACTION, ...COMPACT];
|
|
14
|
+
const SETTINGS = ['calendar', 'numberingSystem', 'timeZone', 'hour12', 'hourCycle'];
|
|
15
|
+
const FIELDS = [
|
|
16
|
+
'weekday',
|
|
17
|
+
'era',
|
|
18
|
+
'year',
|
|
19
|
+
'month',
|
|
20
|
+
'day',
|
|
21
|
+
'dayPeriod',
|
|
22
|
+
'hour',
|
|
23
|
+
'minute',
|
|
24
|
+
'second',
|
|
25
|
+
'fractionalSecondDigits',
|
|
26
|
+
'timeZoneName',
|
|
27
|
+
];
|
|
28
|
+
const NUMBERS = {
|
|
29
|
+
number: new Set([...FORMAT, 'style']),
|
|
30
|
+
integer: new Set([...COMMON, 'minimumIntegerDigits', 'maximumSignificantDigits', ...COMPACT]),
|
|
31
|
+
currency: new Set([
|
|
32
|
+
...FORMAT,
|
|
33
|
+
'fractionDigits',
|
|
34
|
+
'currency',
|
|
35
|
+
'currencyDisplay',
|
|
36
|
+
'currencySign',
|
|
37
|
+
'unitDisplay',
|
|
38
|
+
]),
|
|
39
|
+
percent: new Set([...FORMAT, 'currencySign']),
|
|
40
|
+
unit: new Set([...FORMAT, 'unit', 'unitDisplay', 'currencyDisplay']),
|
|
41
|
+
};
|
|
42
|
+
const DATES = {
|
|
43
|
+
date: new Set([...SETTINGS, ...FIELDS, 'style', 'length', 'fields']),
|
|
44
|
+
time: new Set([...SETTINGS, ...FIELDS, 'style', 'length', 'precision', 'timeZoneStyle']),
|
|
45
|
+
datetime: new Set([
|
|
46
|
+
...SETTINGS,
|
|
47
|
+
...FIELDS,
|
|
48
|
+
'style',
|
|
49
|
+
'dateStyle',
|
|
50
|
+
'timeStyle',
|
|
51
|
+
'dateFields',
|
|
52
|
+
'dateLength',
|
|
53
|
+
'timePrecision',
|
|
54
|
+
'timeZoneStyle',
|
|
55
|
+
]),
|
|
56
|
+
};
|
|
57
|
+
const DATE_FIELDS = new Set([
|
|
58
|
+
'weekday',
|
|
59
|
+
'day-weekday',
|
|
60
|
+
'month-day',
|
|
61
|
+
'month-day-weekday',
|
|
62
|
+
'year-month-day',
|
|
63
|
+
'year-month-day-weekday',
|
|
64
|
+
]);
|
|
65
|
+
const LENGTHS = new Set(['long', 'medium', 'short']);
|
|
66
|
+
const PRECISIONS = ['hour', 'minute', 'second'];
|
|
67
|
+
const SELECTS = new Set(['plural', 'cardinal', 'ordinal', 'exact']);
|
|
68
|
+
const literal = (value) => ({ type: 'literal', value });
|
|
69
|
+
/** A literal's value, which must be one of `allowed`; a variable cannot name fields at build time. */
|
|
70
|
+
function chosen(name, value, allowed) {
|
|
71
|
+
if (value.type !== 'literal') {
|
|
72
|
+
throw new Error(`${name} must be a literal`);
|
|
73
|
+
}
|
|
74
|
+
if (!allowed.has(value.value)) {
|
|
75
|
+
throw new Error(`${name} must be one of ${[...allowed].join(', ')}, not "${value.value}"`);
|
|
76
|
+
}
|
|
77
|
+
return value.value;
|
|
78
|
+
}
|
|
79
|
+
function numberOptions(options) {
|
|
80
|
+
const { fractionDigits, ...out } = options;
|
|
81
|
+
if (fractionDigits && !(fractionDigits.type === 'literal' && fractionDigits.value === 'auto')) {
|
|
82
|
+
out['minimumFractionDigits'] = fractionDigits;
|
|
83
|
+
out['maximumFractionDigits'] = fractionDigits;
|
|
84
|
+
}
|
|
85
|
+
const { select, useGrouping } = out;
|
|
86
|
+
if (select?.type === 'literal') {
|
|
87
|
+
chosen('select', select, SELECTS);
|
|
88
|
+
}
|
|
89
|
+
if (useGrouping?.type === 'literal' && useGrouping.value === 'never') {
|
|
90
|
+
out['useGrouping'] = literal('false');
|
|
91
|
+
}
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
/** MF2's fields, length and precision, as `Intl`'s fields. */
|
|
95
|
+
function semanticFields(name, options) {
|
|
96
|
+
const { fields, dateFields, length, dateLength, precision, timePrecision, timeZoneStyle } = options;
|
|
97
|
+
const out = {};
|
|
98
|
+
if (name !== 'time') {
|
|
99
|
+
const size = length ?? dateLength;
|
|
100
|
+
const isLong = size ? chosen('length', size, LENGTHS) : 'medium';
|
|
101
|
+
const asked = fields ?? dateFields;
|
|
102
|
+
const set = asked ? chosen('fields', asked, DATE_FIELDS) : 'year-month-day';
|
|
103
|
+
const months = { long: 'long', medium: 'short', short: 'numeric' };
|
|
104
|
+
for (const field of set.split('-')) {
|
|
105
|
+
let value = 'numeric';
|
|
106
|
+
if (field === 'month') {
|
|
107
|
+
value = months[isLong] ?? value;
|
|
108
|
+
}
|
|
109
|
+
else if (field === 'weekday') {
|
|
110
|
+
value = isLong === 'long' ? 'long' : 'short';
|
|
111
|
+
}
|
|
112
|
+
out[field] = literal(value);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (name !== 'date') {
|
|
116
|
+
const asked = precision ?? timePrecision;
|
|
117
|
+
const until = asked ? chosen('precision', asked, new Set(PRECISIONS)) : 'minute';
|
|
118
|
+
for (const field of PRECISIONS.slice(0, PRECISIONS.indexOf(until) + 1)) {
|
|
119
|
+
out[field] = literal('numeric');
|
|
120
|
+
}
|
|
121
|
+
if (timeZoneStyle) {
|
|
122
|
+
out['timeZoneName'] = timeZoneStyle;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return out;
|
|
126
|
+
}
|
|
127
|
+
const SEMANTIC = ['fields', 'dateFields', 'precision', 'timePrecision', 'timeZoneStyle'];
|
|
128
|
+
function dateOptions(name, options) {
|
|
129
|
+
const out = Object.fromEntries(Object.entries(options).filter(([k]) => SETTINGS.includes(k) || FIELDS.includes(k)));
|
|
130
|
+
if (SEMANTIC.some((k) => options[k])) {
|
|
131
|
+
return { ...out, ...semanticFields(name, options) };
|
|
132
|
+
}
|
|
133
|
+
const { style, length, dateLength, dateStyle, timeStyle } = options;
|
|
134
|
+
if (style || length || dateLength || dateStyle || timeStyle) {
|
|
135
|
+
const size = style ?? length;
|
|
136
|
+
if (name === 'time') {
|
|
137
|
+
return { ...out, timeStyle: size ?? literal('short') };
|
|
138
|
+
}
|
|
139
|
+
out['dateStyle'] = dateStyle ?? dateLength ?? size ?? literal('medium');
|
|
140
|
+
return name === 'date' ? out : { ...out, timeStyle: timeStyle ?? literal('short') };
|
|
141
|
+
}
|
|
142
|
+
if (FIELDS.some((k) => options[k])) {
|
|
143
|
+
return out;
|
|
144
|
+
}
|
|
145
|
+
// Settings alone: the runtime adds the default style only to a call without options.
|
|
146
|
+
return {
|
|
147
|
+
...out,
|
|
148
|
+
...(name === 'time' ? {} : { dateStyle: literal('medium') }),
|
|
149
|
+
...(name === 'date' ? {} : { timeStyle: literal('short') }),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
// An `Intl` object per message made a build 7 times slower.
|
|
153
|
+
const checked = new Set();
|
|
154
|
+
/** Throws what `Intl` would at run time, where every option is a literal. */
|
|
155
|
+
function check(name, options) {
|
|
156
|
+
const key = name + JSON.stringify(options);
|
|
157
|
+
if (checked.has(key)) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const given = {};
|
|
161
|
+
for (const [k, v] of Object.entries(options)) {
|
|
162
|
+
if (v.type !== 'literal') {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
given[k] = v.value === 'false' || v.value === 'true' ? v.value === 'true' : v.value;
|
|
166
|
+
}
|
|
167
|
+
if (Object.hasOwn(NUMBERS, name)) {
|
|
168
|
+
const style = name === 'number' || name === 'integer' ? {} : { style: name };
|
|
169
|
+
// oxlint-disable-next-line no-new -- only its errors matter
|
|
170
|
+
new Intl.NumberFormat('en', { ...given, ...style });
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
// oxlint-disable-next-line no-new -- only its errors matter
|
|
174
|
+
new Intl.DateTimeFormat('en', given);
|
|
175
|
+
}
|
|
176
|
+
checked.add(key);
|
|
177
|
+
}
|
|
178
|
+
/** Which of a variable's options a function applied to it takes over. */
|
|
179
|
+
export function inheritedOptions(from, to) {
|
|
180
|
+
const own = NUMBERS[to];
|
|
181
|
+
const entries = Object.entries(from.options ?? {});
|
|
182
|
+
if (own && Object.hasOwn(NUMBERS, from.name)) {
|
|
183
|
+
// `offset` is a plural's: only its own `#` subtracts it.
|
|
184
|
+
return Object.fromEntries(entries.filter(([k]) => own.has(k) && k !== 'style' && k !== 'offset'));
|
|
185
|
+
}
|
|
186
|
+
if (from.name === to) {
|
|
187
|
+
return Object.fromEntries(entries);
|
|
188
|
+
}
|
|
189
|
+
return Object.hasOwn(DATES, to) && Object.hasOwn(DATES, from.name)
|
|
190
|
+
? Object.fromEntries(entries.filter(([k]) => k === 'calendar' || k === 'timeZone' || k === 'hour12'))
|
|
191
|
+
: {};
|
|
192
|
+
}
|
|
193
|
+
/** A built-in function's options as `Intl` takes them; throws for one it cannot honour. */
|
|
194
|
+
export function intlOptions({ name, options = {} }) {
|
|
195
|
+
const known = NUMBERS[name] ?? DATES[name] ?? new Set();
|
|
196
|
+
const entries = Object.entries(options).filter(([k]) => k !== 'u:id');
|
|
197
|
+
for (const [key] of entries) {
|
|
198
|
+
if (!known.has(key)) {
|
|
199
|
+
throw new Error(`:${name} has no option "${key}"`);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (entries.length === 0 && name !== 'currency' && name !== 'unit') {
|
|
203
|
+
return {};
|
|
204
|
+
}
|
|
205
|
+
const given = Object.fromEntries(entries);
|
|
206
|
+
try {
|
|
207
|
+
const out = Object.hasOwn(NUMBERS, name) ? numberOptions(given) : dateOptions(name, given);
|
|
208
|
+
check(name, out);
|
|
209
|
+
return out;
|
|
210
|
+
}
|
|
211
|
+
catch (error) {
|
|
212
|
+
throw new Error(`:${name}: ${error.message}`, { cause: error });
|
|
213
|
+
}
|
|
214
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { MessageInfo } from './compile/info.ts';
|
|
2
|
+
/** Whether a message's plurals read the same under both locales' rules. */
|
|
3
|
+
export declare function samePlurals(a: string, b: string): boolean;
|
|
4
|
+
/** The categories `locale` uses for everyday numbers that a plural leaves to its catch-all. */
|
|
5
|
+
export declare function missingPlurals(locale: string, { plurals }: MessageInfo): Array<string>;
|
|
6
|
+
//# sourceMappingURL=plurals.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plurals.d.ts","sourceRoot":"","sources":["../src/plurals.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAkCrD,2EAA2E;AAC3E,wBAAgB,WAAW,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO,CAezD;AAKD,+FAA+F;AAC/F,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,OAAO,EAAE,EAAE,WAAW,GAAG,KAAK,CAAC,MAAM,CAAC,CAWtF"}
|