-
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.
Merge pull request #3 from vardario/sahin/utils
chore: structure clean-up
- Loading branch information
Showing
8 changed files
with
85 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { parse } from 'csv-parse/sync'; | ||
import { stringify } from 'csv-stringify/sync'; | ||
import _ from 'lodash'; | ||
|
||
const CSV_OPTIONS = { | ||
delimiter: ';', | ||
trim: true, | ||
bom: true | ||
}; | ||
|
||
export function parseCsv(csv: string): Record<string, string> { | ||
const items = parse(csv, { | ||
delimiter: ';', | ||
trim: true, | ||
bom: true | ||
}) as string[][]; | ||
|
||
return items.reduce((acc, item) => { | ||
if (item.length === 2) { | ||
acc[item[0]] = item[1]; | ||
} | ||
|
||
return acc; | ||
}, {} as Record<string, string>); | ||
} | ||
|
||
export function recordsToCsv(items: Record<string, string>) { | ||
const keys = _.keys(items); | ||
const values = _.values(items); | ||
const csv = keys.reduce((acc, key, index) => { | ||
acc.push([key, values[index]]); | ||
return acc; | ||
}, [] as string[][]); | ||
|
||
return stringify( | ||
csv.sort((a, b) => a[0].localeCompare(b[0])), | ||
CSV_OPTIONS | ||
); | ||
} | ||
|
||
export function csvToI18Next(csv: string) { | ||
const object = {}; | ||
|
||
const items = parse(csv, CSV_OPTIONS) as string[]; | ||
|
||
for (const item of items) { | ||
const [path, value] = item; | ||
_.set(object, path, value); | ||
} | ||
return object; | ||
} |
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,27 @@ | ||
import path from 'node:path'; | ||
|
||
export function stripScriptTag(code: string) { | ||
const scriptTagRegEx = /(<script[\s\S]*?[\s\S\]*?>[\s\S]*?<\/script>)/gi; | ||
const scriptTags: string[] = []; | ||
|
||
for (const match of code.matchAll(scriptTagRegEx)) { | ||
scriptTags.push(match[0]); | ||
} | ||
|
||
return { code: code.replace(scriptTagRegEx, ''), scriptTags }; | ||
} | ||
|
||
export function extractKeyPathFromFile(filename: string) { | ||
filename = filename.split('.').slice(0, -1).join('.'); | ||
const regEx = /(components|routes)/; | ||
const match = regEx.exec(filename); | ||
if (match === null) { | ||
throw new Error(`${filename} is not valid.`); | ||
} | ||
|
||
const pathParts = filename.split(path.sep); | ||
const index = pathParts.findIndex(pathPart => regEx.exec(pathPart)); | ||
|
||
const result = pathParts.slice(index).join('.'); | ||
return result.replace('.svelte', '').replace('+', ''); | ||
} |
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