11import { readFileSync } from 'fs' ;
22
3+ /**
4+ * Import a file.
5+ * @param {string } path The path to the file.
6+ * @param {string } base The base path from which the import should be resolved.
7+ * @returns {string }
8+ */
9+ export function importFile ( path , base ) {
10+ return readFileSync ( new URL ( path , base ) ) . toString ( ) ;
11+ }
12+
313/**
414 * Import a JSON file.
515 *
@@ -8,7 +18,35 @@ import { readFileSync } from 'fs';
818 * @return {Object }
919 */
1020export function importJson ( path , base ) {
11- return JSON . parse ( readFileSync ( new URL ( path , base ) ) . toString ( ) ) ;
21+ return JSON . parse ( importFile ( path , base ) ) ;
22+ }
23+
24+ /**
25+ * Parse a CSV file.
26+ * @param {string } csv
27+ * @returns {Array<any> }
28+ * @param {{ delimiter?: string|RegExp } } [options]
29+ * @returns {Array<{ from: string, to: string }> }
30+ */
31+ export function csvToJson ( csv , { delimiter = ' ' } = { } ) {
32+ return csv . split ( '\n' ) . filter ( Boolean ) . map ( ( line ) => {
33+ const [ from , to ] = line . split ( delimiter ) ;
34+ return {
35+ from : from ?? '' ,
36+ to : to ?? '' ,
37+ } ;
38+ } ) ;
39+ }
40+
41+ /**
42+ * Import a CSV file as a config object.
43+ * @param {string } path
44+ * @param {string } base
45+ * @param {{ delimiter?: string|RegExp } } [options]
46+ * @returns {Array<{ from: string, to: string }> }
47+ */
48+ export function importCsv ( path , base , options ) {
49+ return csvToJson ( importFile ( path , base ) , options ) ;
1250}
1351
1452/**
@@ -18,7 +56,7 @@ export function importJson(path, base) {
1856 * @return {Promise<number> }
1957 */
2058export async function wait ( delay = 0 ) {
21- return new Promise ( ( resolve ) => {
22- setTimeout ( ( ) => resolve ( delay ) , delay ) ;
23- } ) ;
24- }
59+ return new Promise ( ( resolve ) => {
60+ setTimeout ( ( ) => resolve ( delay ) , delay ) ;
61+ } ) ;
62+ }
0 commit comments