-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
share csv logic & assert RTL dict for ar->ar
- Loading branch information
1 parent
baf6cf0
commit 880c77b
Showing
6 changed files
with
83 additions
and
50 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// we've found that the RTL column order in the ar_ar dict csv isn't reliable in CI | ||
// so if the key isn't in the resulting dict we either invert it or throw an error | ||
export const arToArAssertKey = "ابو الليل"; |
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,50 @@ | ||
import fs from "fs"; | ||
|
||
export const readCsv = (repoPath: string) => { | ||
const csvString = fs.readFileSync(repoPath).toString(); | ||
return csvString.split(/\r?\n/g).map((row) => row.split(",")); | ||
}; | ||
|
||
/** | ||
* read a CSV file and return an object lookup ("dict") with keys | ||
* as the first CSV column value, and values as the second CSV column | ||
* | ||
* @param repoPath relative path from root of repo | ||
* @param options optional object with assertion key to make sure key exists in resulting object | ||
* if assertKey does not exist, the dict is inverted and if the assertKey still does not exist | ||
* the method @throws | ||
*/ | ||
export const readCsvToDict = ( | ||
repoPath: string, | ||
options: { assertKey?: string; invert?: boolean } = {} | ||
): Record<string, string> => { | ||
const result = readCsv(repoPath).reduce( | ||
(dict, row) => ({ | ||
...dict, | ||
[row[options.invert ? 1 : 0]]: row[options.invert ? 0 : 1], | ||
}), | ||
{} as Record<string, string> | ||
); | ||
|
||
if (options.assertKey && !options.invert && !result[options.assertKey]) { | ||
console.log( | ||
`could not find assertKey ${options.assertKey} in resulting dict, inverting` | ||
); | ||
return readCsvToDict(repoPath, { ...options, invert: true }); | ||
} | ||
|
||
if (options.assertKey && options.invert && !result[options.assertKey]) { | ||
throw new Error( | ||
`Expected dict to include key '${options.assertKey}' but it did not exist in the initial or inverted dict` | ||
); | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
const toCsv = (rows: string[][]) => | ||
rows.map((columns) => columns.join(",")).join("\r\n"); | ||
|
||
export const writeCsv = (repoPath: string, rows: string[][]) => { | ||
fs.writeFileSync(repoPath, toCsv(rows)); | ||
}; |
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