-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
110 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
**/*.js | ||
examples/ | ||
.eslintrc.cjs |
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,3 @@ | ||
# Examples | ||
|
||
Few example use cases to try out changes, use tests if you want to properly test things. |
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,61 @@ | ||
import { type LabelData } from "./types"; | ||
|
||
/** | ||
* Update existing label cache based on given data and source labels | ||
*/ | ||
export function updateLabelCache({ | ||
cache, | ||
source, | ||
data, | ||
}: { | ||
cache: LabelData; | ||
source: LabelData; | ||
data: Record<string, Set<string>>; | ||
}) { | ||
for (const [key, values] of Object.entries(data)) { | ||
// Next-intl uses dot notation for nested objects | ||
const keys = key.split("."); | ||
let currentCache = cache; | ||
|
||
// Set up the namespace in the cache | ||
for (let i = 0; i < keys.length; i++) { | ||
const currentKey = keys[i]; | ||
currentCache[currentKey] = currentCache[currentKey] || {}; | ||
currentCache = currentCache[currentKey] as LabelData; | ||
} | ||
|
||
// Add values for each label, try the existing source first | ||
// or use the namespace with name as a value | ||
for (const value of values) { | ||
currentCache[value] = | ||
getLabelFromData(source, [...keys, value]) || `${key}.${value}`; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Traverse through label data and find existing label or return undefined | ||
* @param path Array of keys to traverse through | ||
* @param source Label data object to get label from | ||
* @returns String value if available or undefined if not | ||
*/ | ||
function getLabelFromData( | ||
source: LabelData, | ||
path: Array<string> | ||
): string | undefined { | ||
let current: LabelData | string = source; | ||
for (const key of path) { | ||
if ( | ||
typeof current !== "object" || | ||
current[key] === undefined || | ||
!(key in current) | ||
) { | ||
return undefined; | ||
} | ||
|
||
current = current[key]; | ||
} | ||
|
||
// Only return label if it's actually a string | ||
return typeof current === "string" ? current : undefined; | ||
} |
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
File renamed without changes.
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 @@ | ||
export { extractLabels, extractLabelsFromFile } from "./extract"; |
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,6 @@ | ||
/** | ||
* Recursive type for labels which can be nested objects containing strings | ||
*/ | ||
export type LabelData = { | ||
[key: string]: string | LabelData; | ||
}; |
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 |
---|---|---|
@@ -1,23 +1,28 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/tsconfig", | ||
"display": "Backend", | ||
"compilerOptions": { | ||
// Enable latest features | ||
"lib": ["ESNext", "DOM"], | ||
"target": "ESNext", | ||
"module": "ESNext", | ||
"moduleDetection": "force", | ||
"jsx": "react-jsx", | ||
"allowJs": true, | ||
"composite": true, | ||
"esModuleInterop": true, | ||
"experimentalDecorators": true, | ||
"isolatedModules": true, | ||
"module": "ES2022", | ||
|
||
// Bundler mode | ||
"moduleResolution": "bundler", | ||
"allowImportingTsExtensions": true, | ||
"verbatimModuleSyntax": true, | ||
"noEmit": true, | ||
"outDir": "dist", | ||
"preserveWatchOutput": true, | ||
"resolveJsonModule": true, | ||
"sourceMap": true, | ||
"skipLibCheck": true, | ||
|
||
// Best practices | ||
"strict": true, | ||
"strictPropertyInitialization": false, | ||
"target": "ES2022" | ||
"skipLibCheck": true, | ||
"noFallthroughCasesInSwitch": true, | ||
|
||
// Some stricter flags (disabled by default) | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"noPropertyAccessFromIndexSignature": true | ||
}, | ||
"exclude": ["node_modules/*", "**/node_modules/*"] | ||
"include": ["./src/**/*"] | ||
} |
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