-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from matrix-org/t3chguy/utils
- Loading branch information
Showing
9 changed files
with
172 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
Copyright 2023 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
export * from "./utils"; | ||
export * from "./types"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
Copyright 2023 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Utility type for string dot notation for accessing nested object properties. | ||
* Based on https://stackoverflow.com/a/58436959 | ||
* @example | ||
* { | ||
* "a": { | ||
* "b": { | ||
* "c": "value" | ||
* }, | ||
* "d": "foobar" | ||
* } | ||
* } | ||
* will yield a type of `"a.b.c" | "a.d"` with Separator="." | ||
* @typeParam Target the target type to generate leaf keys for | ||
* @typeParam Separator the separator to use between key segments when accessing nested objects | ||
* @typeParam LeafType the type which leaves of this object extend, used to determine when to stop recursion | ||
* @typeParam MaxDepth the maximum depth to recurse to | ||
* @returns a union type representing all dot (Separator) string notation keys which can access a Leaf (of LeafType) | ||
*/ | ||
export type Leaves<Target, Separator extends string = ".", LeafType = string, MaxDepth extends number = 3> = [ | ||
MaxDepth, | ||
] extends [never] | ||
? never | ||
: Target extends LeafType | ||
? "" | ||
: { | ||
[K in keyof Target]-?: Join<K, Leaves<Target[K], Separator, LeafType, Prev[MaxDepth]>, Separator>; | ||
}[keyof Target]; | ||
type Prev = [never, 0, 1, 2, 3, ...0[]]; | ||
type Join<K, P, S extends string = "."> = K extends string | number | ||
? P extends string | number | ||
? `${K}${"" extends P ? "" : S}${P}` | ||
: never | ||
: never; | ||
|
||
/** | ||
* Utility type for |-separated keys indexing into a translations file | ||
* @typeParam Translations the target type to generate leaf keys for | ||
* @typeParam Separator the separator to use between key segments when accessing nested objects | ||
* @typeParam LeafType the type which leaves of this object extend, used to determine when to stop recursion | ||
* @typeParam MaxDepth the maximum depth to recurse to | ||
* @returns a union type representing all dot (Separator) string notation keys which can access a Leaf (of LeafType) | ||
*/ | ||
export type TranslationKey<T extends Translations> = Leaves<T, "|", string | { other: string }>; | ||
|
||
export type Translation = string | { | ||
one?: string; | ||
other: string; | ||
}; | ||
|
||
export interface Translations { | ||
[key: string]: Translation | Translations; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
Copyright 2017 MTRNord and Cooperative EITA | ||
Copyright 2017 Vector Creations Ltd. | ||
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com> | ||
Copyright 2019 - 2022 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Returns a language string with underscores replaced with | ||
* hyphens, and lower-cased. | ||
* | ||
* @param {string} language The language string to be normalized | ||
* @returns {string} The normalized language string | ||
*/ | ||
export function normalizeLanguageKey(language: string): string { | ||
return language.toLowerCase().replace("_", "-"); | ||
} | ||
|
||
/** | ||
* Turns a language string, normalises it, | ||
* (see normalizeLanguageKey) into an array of language strings | ||
* with fallback to generic languages | ||
* (e.g. 'pt-BR' => ['pt-br', 'pt']) | ||
* | ||
* @param language The input language string | ||
* @return a list of normalised languages | ||
*/ | ||
export function getNormalizedLanguageKeys(language: string): string[] { | ||
const languageKeys: string[] = []; | ||
const normalizedLanguage = normalizeLanguageKey(language); | ||
const languageParts = normalizedLanguage.split("-"); | ||
if (languageParts.length === 2 && languageParts[0] === languageParts[1]) { | ||
languageKeys.push(languageParts[0]); | ||
} else { | ||
languageKeys.push(normalizedLanguage); | ||
if (languageParts.length === 2) { | ||
languageKeys.push(languageParts[0]); | ||
} | ||
} | ||
return languageKeys; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es2016", | ||
"esModuleInterop": true, | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"strict": true, | ||
"noUnusedLocals": true | ||
}, | ||
"include": [ | ||
"./scripts/*.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters