@common.js/schummar__icu-type-parser 1.21.5
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/README.md +5 -0
- package/dist/index.d.ts +59 -0
- package/dist/index.js +1 -0
- package/package.json +36 -0
package/README.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# @common.js/schummar__icu-type-parser
|
|
2
|
+
|
|
3
|
+
The [@schummar/icu-type-parser](https://www.npmjs.com/package/@schummar/icu-type-parser) package exported as CommonJS modules.
|
|
4
|
+
|
|
5
|
+
Exported from [@schummar/icu-type-parser@1.21.5](https://www.npmjs.com/package/@schummar/icu-type-parser/v/1.21.5) using https://github.com/etienne-martin/common.js.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
type Flatten<T> = T extends object ? {
|
|
2
|
+
[P in keyof T]: T[P];
|
|
3
|
+
} : T;
|
|
4
|
+
type EmptyObject = Record<never, never>;
|
|
5
|
+
type OtherString = string & {
|
|
6
|
+
__type: 'other';
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
interface GetICUArgsOptions {
|
|
10
|
+
ProvidedArgs?: string;
|
|
11
|
+
ICUNumberArgument?: unknown;
|
|
12
|
+
ICUDateArgument?: unknown;
|
|
13
|
+
ICUArgument?: unknown;
|
|
14
|
+
}
|
|
15
|
+
type Whitespace = ' ' | '\t' | '\n' | '\r';
|
|
16
|
+
/** Remove leading and tailing whitespace */
|
|
17
|
+
type Trim<T> = T extends `${Whitespace}${infer Rest}` ? Trim<Rest> : T extends `${infer Rest}${Whitespace}` ? Trim<Rest> : T extends string ? T : never;
|
|
18
|
+
/** Returns an array of top level blocks */
|
|
19
|
+
type FindBlocks<Text> = Text extends `${string}{${infer Right}` ? ReadBlock<'', Right, ''> extends [infer Block, infer Tail] ? [Block, ...FindBlocks<Tail>] : [EmptyObject] : [];
|
|
20
|
+
/** Find blocks for each tuple entry */
|
|
21
|
+
type TupleFindBlocks<T> = T extends readonly [infer First, ...infer Rest] ? [...FindBlocks<First>, ...TupleFindBlocks<Rest>] : [];
|
|
22
|
+
/** Read tail until the currently open block is closed. Return the block content and rest of tail */
|
|
23
|
+
type ReadBlock<Block extends string, Tail extends string, Depth extends string> = Tail extends `${infer L1}}${infer R1}` ? L1 extends `${infer L2}{${infer R2}` ? ReadBlock<`${Block}${L2}{`, `${R2}}${R1}`, `${Depth}+`> : Depth extends `+${infer Rest}` ? ReadBlock<`${Block}${L1}}`, R1, Rest> : [`${Block}${L1}`, R1] : [];
|
|
24
|
+
/** Parse block, return variables with types and recursively find nested blocks within */
|
|
25
|
+
type ParseBlock<Block, Opts extends GetICUArgsOptions> = Block extends `${infer Name},${infer Format},${infer Rest}` ? Trim<Format> extends 'select' ? SelectOptions<Trim<Name>, Trim<Rest>, Opts> : {
|
|
26
|
+
[K in Trim<Name>]: VariableType<Trim<Format>, Opts>;
|
|
27
|
+
} & TupleParseBlock<TupleFindBlocks<FindBlocks<Rest>>, Opts> : Block extends `${infer Name},${infer Format}` ? {
|
|
28
|
+
[K in Trim<Name>]: VariableType<Trim<Format>, Opts>;
|
|
29
|
+
} : {
|
|
30
|
+
[K in Trim<Block>]: Opts['ICUArgument'];
|
|
31
|
+
};
|
|
32
|
+
/** Parse block for each tuple entry */
|
|
33
|
+
type TupleParseBlock<T, C extends GetICUArgsOptions> = T extends readonly [infer First, ...infer Rest] ? ParseBlock<First, C> & TupleParseBlock<Rest, C> : EmptyObject;
|
|
34
|
+
type VariableType<T extends string, Opts extends GetICUArgsOptions> = T extends 'number' | 'plural' | 'selectordinal' ? Opts['ICUNumberArgument'] : T extends 'date' | 'time' ? Opts['ICUDateArgument'] : Opts['ICUArgument'];
|
|
35
|
+
type SelectOptions<Name extends string, Rest, Opts extends GetICUArgsOptions> = KeepAndMerge<ParseSelectBlock<Name, Rest, Opts>>;
|
|
36
|
+
type ParseSelectBlock<Name extends string, Rest, Opts extends GetICUArgsOptions> = Rest extends `${infer Left}{${infer Right}` ? ReadBlock<'', Right, ''> extends [infer Block, infer Tail] ? ({
|
|
37
|
+
[K in Name]: HandleOther<Trim<Left>>;
|
|
38
|
+
} & TupleParseBlock<FindBlocks<Block>, Opts>) | ParseSelectBlock<Name, Tail, Opts> : never : never;
|
|
39
|
+
type HandleOther<T> = 'other' extends T ? Exclude<T, 'other'> | OtherString : T;
|
|
40
|
+
type KeepAndMerge<T extends object> = T | MergeTypeUnion<T>;
|
|
41
|
+
type KeysFromUnion<T> = T extends T ? keyof T : never;
|
|
42
|
+
type SimpleTypeMerge<T, K extends keyof any> = T extends {
|
|
43
|
+
[k in K]?: any;
|
|
44
|
+
} ? (T[K] extends OtherString ? string & {} : T[K]) : never;
|
|
45
|
+
type MergeTypeUnion<T extends object> = {
|
|
46
|
+
[k in KeysFromUnion<T>]: SimpleTypeMerge<T, k>;
|
|
47
|
+
};
|
|
48
|
+
type EscapeLike = `'${'{' | '}' | '<' | '>'}`;
|
|
49
|
+
type StripEscapes<T> = T extends `${infer Left}''${infer Right}` ? `${Left}${Right}` : T extends `${infer Start}${EscapeLike}${string}'${infer End}` ? `${Start}${StripEscapes<End>}` : T extends `${infer Start}${EscapeLike}${string}` ? Start : T;
|
|
50
|
+
type TupleStripEscapes<T> = T extends readonly [infer First, ...infer Rest] ? [StripEscapes<First>, ...TupleStripEscapes<Rest>] : [];
|
|
51
|
+
type MakeProvidedOptional<T, ProvidedArgs> = {
|
|
52
|
+
[K in keyof T as K extends ProvidedArgs ? never : K]: T[K];
|
|
53
|
+
} & {
|
|
54
|
+
[K in ProvidedArgs & keyof T]?: T[K];
|
|
55
|
+
};
|
|
56
|
+
/** Calculates an object type with all variables and their types in the given ICU format string */
|
|
57
|
+
type GetICUArgs<T extends string | readonly string[], TOptions extends GetICUArgsOptions = GetICUArgsOptions> = Flatten<MakeProvidedOptional<TupleParseBlock<T extends readonly string[] ? TupleFindBlocks<TupleStripEscapes<T>> : FindBlocks<StripEscapes<T>>, TOptions>, 'ProvidedArgs' extends keyof TOptions ? TOptions['ProvidedArgs'] : never>>;
|
|
58
|
+
|
|
59
|
+
export type { GetICUArgs, GetICUArgsOptions };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@common.js/schummar__icu-type-parser",
|
|
3
|
+
"repository": "etienne-martin/common.js",
|
|
4
|
+
"version": "1.21.5",
|
|
5
|
+
"description": "@schummar/icu-type-parser package exported as CommonJS modules",
|
|
6
|
+
"type": "commonjs",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"contributors": [
|
|
9
|
+
{
|
|
10
|
+
"name": "Marco Schumacher",
|
|
11
|
+
"email": "marco@schumacher.dev"
|
|
12
|
+
}
|
|
13
|
+
],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"typesVersions": {
|
|
16
|
+
".": {
|
|
17
|
+
"*": [
|
|
18
|
+
"dist/index.d.ts"
|
|
19
|
+
]
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsup",
|
|
25
|
+
"publint": "publint",
|
|
26
|
+
"release": "release-it --ci -c ../../.release-it-package.ts"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist"
|
|
33
|
+
],
|
|
34
|
+
"homepage": "https://github.com/etienne-martin/common.js#readme",
|
|
35
|
+
"dependencies": {}
|
|
36
|
+
}
|