Module: datafile

Data file handler functions.

Methods

(static) findFilesSync(baseDir, pattern, recurse, splitBaseDir) → {Array}

Find files by name pattern.

Recursively finds the files under the baseDir directory, that have name which matches the pattern.

Parameters:
Name Type Description
baseDir String

The path to the base directory to start searching.

pattern RegExp

The regular expression pattern to match the filenames.

recurse Boolean

Find files in subdirectories as well if true. Default: true.

splitBaseDir Booleand

If true, removes the baseDir from the paths of the files found, if false then returns with the full path. Default: 'false'.

Returns:
  • The list of file paths found
Type
Array

(static) listFilesSync(baseDir, recurse) → {Array}

List files of a directory recursively or top level only

Recursively lists the files under the baseDir directory. Skips directory names from the result list. Only files will be listed.

Parameters:
Name Type Description
baseDir String

The path to the base directory to start searching.

recurse Boolean

true tells to recursively list the subdirectories as well. false means, only the baseDir content will be listed. Default: true.

Returns:
  • The list of file paths found.
Type
Array

(static) loadCsvFileSync(fileName, options, raiseErrors)

Load records from a CSV format file

Parameters:
Name Type Description
fileName String

The full path of the input file

options Object

The options of the yaml.dump() function. See the docs of js-yaml package.

raiseErrors Boolean

If true then exit with process errorCode: 1 in case of error otherwise does nothing. Default: true.

(static) loadData()

Load the listed data files and merge them into a single object.

It is an alias of the mergeJsonFilesSync function.

Deprecated:
  • Yes
See:
  • mergeJsonFilesSync

(static) loadJsonFileSync(fileName, raiseErrors) → {Object}

Load JSON/YAML datafile

The data file can be either a JSON or a YAML format file.

Parameters:
Name Type Description
fileName String

The full path of the file to load.

raiseErrors Boolean

If true then exit with process errorCode: 1 in case of error otherwise does nothing. Default: true.

Returns:
  • The data loaded as a JSON object. If the file does not exists and raiseErrors is false, then returns with an empty object: {}.
Type
Object

(static) loadJsonWithRefs(fileName, raiseErrors) → {Object}

Asynchronous load of JSON/YAML datafile with references

The data file can be either a JSON or a YAML format file, references are loaded and resolved too.

Parameters:
Name Type Description
fileName String

The full path of the file to load.

raiseErrors Boolean

If true then exit with process errorCode: 1 in case of error otherwise does nothing. Default: true.

Returns:
  • A Promise that resolves to an object with two properties: { refs, resolved}. The resolved holds the data loaded as a JSON object, and the refs that holds the references.
Type
Object

(static) loadTextFileSync(fileName, raiseErrors) → {String}

Load text content from a file

Parameters:
Name Type Description
fileName String

The full path of the input file

raiseErrors Boolean

If true then exit with process errorCode: 1 in case of error otherwise does nothing. Default: true.

Returns:
  • The loaded content. If the file does not exists and raiseErrors is false, then returns with null.
Type
String

(static) mergeJsonFilesByFileNameSync(listOfJsonFiles, acc) → {Object}

Load objects from files and merge them into one object using the file names as property names.

Loads each data files, and merges them into one object.

The merging begins with a copy of the acc accumulator object, and the objects loaded from the data files extend this initial object in the order they were listed in the array.

The original acc accumulator object will not be modified, but a new copy will be returned, which is extended with the new property that holds the loaded text.

Each object is added to the result as a property. The name of the property will be the name of the data file to be merged.

Parameters:
Name Type Description
listOfJsonFiles Array

The list of paths to the data files to be loaded

acc String

The initial accumulator object to merge with

Returns:
  • The resulted data object
Type
Object

(static) mergeJsonFilesByKeySync(listOfJsonFiles, keyProp) → {Object}

Load objects from files and merge them into one object using one of their properties as a key.

Loads each data files, and merges them into one object.

The merging begins with an empty object, and the objects loaded from the data files extend this initial object in the order they were listed in the array.

Each object is added to the result as a property. The name of the property is determined by the object to merge. The keyProp parameter identifies the property in this object to merge that value that will be used as a property name in the new object. This value should be unique among the objects to merge.

For example each file to merge contains a document of a bigger collection, and has an id field, which holds an uuid unique ID value of the document. The keyProp should be "id", and the result of merging will be an object, which has as many properties as the number of merged files, and each property holds the complete loaded object, and the name of the property is the uuid value.

Parameters:
Name Type Description
listOfJsonFiles Array

The list of paths to the data files to be loaded

keyProp String

The name of the property that's value is used as a key

Returns:
  • The resulted data object
Type
Object

(static) mergeJsonFilesSync(listOfJsonFiles) → {Object}

Load the listed data files and merge them into a single object.

Loads each data files, and merges them into one object. The merging begins with an empty object, and the objects loaded from the data files extend this initial object in order of listing in the array.

Parameters:
Name Type Description
listOfJsonFiles Array

The list of paths to the data files to be loaded

Returns:
  • The resulted data object
Type
Object
Example
// earth.yml:
 planets:
     Earth:
         moons:
             Moon: {}

 // moons.yml:
 planets:
     Earth:
         numOfMoons: 1

 mergeJsonFilesSync([
     'src/fixtures/merge/earth.yml',
     'src/fixtures/merge/moons.yml'
 ]

 // =>
 planets:
     Earth:
         numOfMoons: 1
         moons:
             Moon: {}

(static) mergeTextFilesByFileNameSync(listOfTextFiles, acc) → {Object}

Load plain textual content from files, and build up an object which holds the accumulated contents as properties of the object, using the filename as the property name.

Parameters:
Name Type Description
listOfTextFiles Array

The list of paths to the data files to be loaded

acc Object

The initial content of the accumulator object. Default: {}.

Returns:
  • The resulted data object
Type
Object

(static) saveCsvFileSync(fileName, content, options, raiseErrors)

Save content into a CSV format file

Parameters:
Name Type Description
fileName String

The full path of the output file

content String

The content to dump in CSV format

options Object

The options of the stringify() function. See the docs of csv-stringify package.

raiseErrors Boolean

If true then exit with process errorCode: 1 in case of error otherwise does nothing. Default: true.

(static) saveTextFileSync(fileName, content, raiseErrors)

Save content into a text file

Parameters:
Name Type Description
fileName String

The full path of the output file

content String

The content to save

raiseErrors Boolean

If true then exit with process errorCode: 1 in case of error otherwise does nothing. Default: true.

(static) saveYamlFileSync(fileName, content, options, raiseErrors)

Save content into a YAML format file

Parameters:
Name Type Description
fileName String

The full path of the output file

content String

The content to dump in YAML format

options Object

The options of the yaml.dump() function. See the docs of js-yaml package.

raiseErrors Boolean

If true then exit with process errorCode: 1 in case of error otherwise does nothing. Default: true.

(static) stringifyToCsv()

Dump an object into a CSV format string

It is an alias of the stringify() function of the csv-stringify package.

(static) stringifyToYaml()

Dump an object into a YAML format string

It is an alias to the yaml.dump() function of the js-yaml package.

(inner) mergeJsonFileByFileNameSync(acc, textFileName) → {Object}

Load and add one JSON data file into an accumulator object, as a property named as the filename.

The original acc accumulator object will not be modified, but a new copy will be returned, which is extended with the new property that holds the loaded text.

Parameters:
Name Type Description
acc Object

The accumulator object to merge with.

textFileName String

The name of the text file to load.

Returns:
  • The new accumulator object, including the loaded text.
Type
Object

(inner) mergeJsonFileByKeySync(keyProp) → {function}

Creates a function that merge a single file into an accumulator object, using a unique value as a key.

Parameters:
Name Type Description
keyProp String

The name of the property that's value will be used as a key to add the loaded data to the accumator to merge.

Returns:
  • The merge function with the following signature: function(acc: Object, dataFileName : String), where the dataFileName is the path to the data file to merge, and the acc the accumulator object, to merge the data into.
Type
function

(inner) mergeJsonFileSync(acc, dataFileName) → {Object}

Load and merge one data file into the accumulator object.

Get an accumulator object, then load the data from the file identified by the dataFileName argument, then extend the accumulator object with the loaded data.

Note: The function returns with a new object, and the original object will not be modified.

Parameters:
Name Type Description
acc Object

The original object, to extend with the data to be loaded

dataFileName String

The name of the data file to be loaded

Returns:
  • The result object of merging
Type
Object

(inner) mergeTextFileByFileNameSync(acc, textFileName) → {Object}

Load and add one text file into an accumulator object, as a property named as the filename.

The original acc accumulator object will not be modified, but a new copy will be returned, which is extended with the new property that holds the loaded text.

Parameters:
Name Type Description
acc Object

The accumulator object to merge with.

textFileName String

The name of the text file to load.

Returns:
  • The new accumulator object, including the loaded text.
Type
Object