Methods
(static) readCsv(csvPath, conversionFnopt, withHeaderopt) → {promise}
- Source:
- Since:
- 0.1.0
- See:
[node environment] Return a promise that reads and then parses a csv file. You can create a conversionFn using transformValues() from @svizzle/utils
Example
$ cat source/withHeader.csv
name,amount
annie,200
joe,100
> readCsv('source/withHeader.csv', row => ({
name: row.name,
amount: Number(row.amount)
}))
.then(x => console.log(x))
.catch(err => console.error(err))
[{name: 'annie', amount: 200}, {name: 'joe', amount: 100}]
$ cat source/withNoHeader.csv
annie,200
joe,100
> readCsv('source/withNoHeader.csv', row => ({
name: row.name,
amount: Number(row.amount)
}), false)
.then(x => console.log(x))
.catch(err => console.error(err))
[{name: 'annie', amount: 200}, {name: 'joe', amount: 100}]
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
csvPath |
string | The filepath of the CSV file to read. |
||
conversionFn |
function |
<optional> |
x=>x
|
A function invoked for each row to convert columns values. |
withHeader |
boolean |
<optional> |
true
|
Does the first line contain the header? |
Returns:
- @sideEffects: fs.readFile
- Type
- promise
(static) readDir(dirPath) → {promise}
- Source:
- Since:
- 0.4.0
[node environment] Return a promise that reads the directory at the provided path.
Example
> readDir('source/dir/')
.then(x => console.log(x))
.catch(err => console.error(err))
['dir1', 'dir2', 'file1.txt', 'file2.txt', 'folder1', 'folder2']
Parameters:
Name | Type | Description |
---|---|---|
dirPath |
string |
Returns:
- @sideEffects: fs.readdir
- Type
- promise
(static) readDirFiles(dirPath, filterPathFn, parseFn) → {promise}
- Source:
- Since:
- 0.9.0
[node environment] Return a promise that reads files in the given directory with a file path satisfying the provided criteria, returning the array with their content parsed with the provided parser.
Example
$ ls -a source/dir
. 01.csv foo.txt .foo
.. 02.csv bar.json
$ cat source/dir/.foo
hidden foo
$ cat source/dir/foo.txt
foo
$ cat source/dir/bar.json
{"a": 1}
$ cat source/dir/01.csv
a,b
foo,1
bar,2
$ cat source/dir/02.csv
foo,1
bar,2
> readDirFiles('source/dir/')
.then(x => console.log(x))
.catch(err => console.error(err))
[
'a,b\nfoo,1\nbar,2',
'foo,1\nbar,2',
'foo',
'{"a": 1}',
'hidden foo'
]
> readDirFiles('source/dir/', isPathCSV)
.then(x => console.log(x))
.catch(err => console.error(err))
['a,b\nfoo,1\nbar,2', 'foo,1\nbar,2']
> readDirFiles('source/dir/', isPathCSV, d3.csvParseRows)
.then(x => console.log(x))
.catch(err => console.error(err))
[
[['a', 'b'], ['foo', '1'], ['bar', '2']],
[['foo', '1'], ['bar', '2']]
]
Parameters:
Name | Type | Description |
---|---|---|
dirPath |
string | the directory path |
filterPathFn |
function | (String -> Boolean) predicate to filter the filepaths (e.g. () |
parseFn |
function | (String -> Any) target file parser |
Returns:
- @sideEffects: fs.readdir, fs.readFile
- Type
- promise
(static) readDirFilesIndexed(dirPath, filterPathFn, parseFn, withFilepathAsKeyopt) → {promise}
- Source:
- Since:
- 0.9.0
[node environment]
Return a promise that reads files in the given directory with a file path satisfying the provided criteria, returning their content parsed with the provided parser indexed by file name.
From v0.10.0, withFilepathAsKey
drives what to get as keys.
Example
$ ls -a test_assets/
. .foo ab.csv foo.txt
.. .bar rows.csv bar.json
$ cat test_assets/ab.csv
a,b
foo,1
bar,2
$ cat test_assets/rows.csv
foo,1
bar,2
> readDirFilesIndexed('source/dir/', isPathCSV, d3.csvParseRows)
.then(x => console.log(x))
.catch(err => console.error(err))
{
'ab.csv': [['a', 'b'], ['foo', '1'], ['bar', '2']],
'rows.csv': [['foo', '1'], ['bar', '2']]
}
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
dirPath |
string | the directory path |
||
filterPathFn |
function | (String -> Boolean) predicate to filter the filepaths (e.g. () |
||
parseFn |
function | (String -> Any) target file parser |
||
withFilepathAsKey |
boolean |
<optional> |
false
|
defaults to |
Returns:
- @sideEffects: fs.readdir, fs.readFile
- Type
- promise
(static) readDsv(csvPath, separator, conversionFnopt, withHeaderopt) → {promise}
- Source:
- Since:
- 0.4.0
- See:
[node environment] Return a promise that reads and then parses a file. You can create a conversionFn using transformValues() from @svizzle/utils
Example
$ cat source/withHeader.txt
name;amount
annie;200
joe;100
> readDsv('source/withHeader.txt', ({name, amount}) => ({
name,
amount: Number(amount)
}), ';')
.then(x => console.log(x))
.catch(err => console.error(err))
[{name: 'annie', amount: 200}, {name: 'joe', amount: 100}]
$ cat source/withNoHeader.txt
annie|200
joe|100
> readDsv('source/withNoHeader.txt', ([name, amount]) => ({
name,
amount: Number(amount)
}), '|', false)
.then(x => console.log(x))
.catch(err => console.error(err))
[{name: 'annie', amount: 200}, {name: 'joe', amount: 100}]
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
csvPath |
string | The filepath of the CSV file to read. |
||
separator |
string | Separator string. |
||
conversionFn |
function |
<optional> |
x=>x
|
A function invoked for each row to convert columns values. |
withHeader |
boolean |
<optional> |
true
|
Does the first line contain the header? |
Returns:
- @sideEffects: fs.readFile
- Type
- promise
(static) readExportedJson(jsFilePath) → {promise}
- Source:
- Since:
- 0.14.0
[node environment] Return a promise that reads the file at the provided js path and returns the exported value.
Example
$ cat source/object.js
export default {"a": 1}
> readExportedJson('source/object.js')
.then(x => console.log(x))
.catch(err => console.error(err))
{a: 1}
$ cat source/array.js
export default [1, 2, 3];
> readExportedJson('source/array.js')
.then(x => console.log(x))
.catch(err => console.error(err))
[1, 2, 3]
Parameters:
Name | Type | Description |
---|---|---|
jsFilePath |
string |
Returns:
- @sideEffects: fs.readFile
- Type
- promise
(static) readFile(filePath, encodingopt) → {promise}
- Source:
- Since:
- 0.4.0
[node environment] Return a promise that reads the file at the provided path.
Example
> readFile('source/path.txt')
.then(x => console.log(x))
.catch(err => console.error(err))
<Buffer 49 27 6d ...0a>
> readFile('source/path.txt', 'utf-8')
.then(x => console.log(x))
.catch(err => console.error(err))
'the file content'
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
filePath |
string | |||
encoding |
string |
<optional> |
null
|
Encoding supported by Node Buffer |
Returns:
- @sideEffects: fs.readFile
- Type
- promise
(static) readJson(jsonPath) → {promise}
- Source:
- Since:
- 0.1.0
[node environment] Return a promise that reads and then parses a json file.
Example
> readJson('source/filepath.json')
.then(x => console.log(x))
.catch(err => console.error(err))
[{name: 'annie', amount: 200}, {name: 'joe', amount: 100}]
Parameters:
Name | Type | Description |
---|---|---|
jsonPath |
string | The filepath of the JSON file to read. |
Returns:
- @sideEffects: fs.readFile
- Type
- promise
(static) readJsonDir(dirPath) → {promise}
- Source:
- Since:
- 0.1.0
[node environment] Return a promise returning an array of objects of the JSON files of a directory, not recursively.
Example
> readJsonDir('source/path/')
.then(x => console.log(x))
.catch(err => console.error(err))
[{'a': 1}, '{'a': 2}', '{'a': 2}']
Parameters:
Name | Type | Description |
---|---|---|
dirPath |
string | The path of the directory containing the JSON files to read. |
Returns:
- @sideEffects: fs.readdir, fs.readFile
- Type
- promise
(static) readTsv(tsvPath, conversionFnopt, withHeaderopt) → {promise}
- Source:
- Since:
- 0.3.0
- See:
[node environment] Return a promise that reads and then parses a tsv file. You can create a conversionFn using transformValues() from @svizzle/utils
Example
$ cat source/withHeader.txt
name\tamount
annie\t200
joe\t100
> readTsv('source/withHeader.txt', ({name, amount}) => ({
name,
amount: Number(amount)
}))
.then(x => console.log(x))
.catch(err => console.error(err))
[{name: 'annie', amount: 200}, {name: 'joe', amount: 100}]
$ cat source/withNoHeader.txt
annie\t200
joe\t100
> readTsv('source/withNoHeader.txt', ([name, amount]) => ({
name,
amount: Number(amount)
}), false)
.then(x => console.log(x))
.catch(err => console.error(err))
[{name: 'annie', amount: 200}, {name: 'joe', amount: 100}]
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
tsvPath |
string | The filepath of the TSV file to read. |
||
conversionFn |
function |
<optional> |
x=>x
|
A function invoked for each row to convert columns values, |
withHeader |
boolean |
<optional> |
true
|
Does the first line contain the header? |
Returns:
- @sideEffects: fs.readFile
- Type
- promise
(static) readYaml(yamlPath) → {promise}
- Source:
- Since:
- 0.13.0
[node environment] Return a promise that reads and then parses a YAML file.
Example
> readYaml('source/filepath.yaml')
.then(x => console.log(x))
.catch(err => console.error(err))
{
foo: [{a: 1}, {b: 2}],
bar: [1, 2, 3],
}
Parameters:
Name | Type | Description |
---|---|---|
yamlPath |
string | The filepath of the YAML file to read. |
Returns:
- @sideEffects: fs.readFile
- Type
- promise