Options
All
  • Public
  • Public/Protected
  • All
Menu

Bulk Data Tools

Index

Functions

delimitedHeaderFromArray

  • delimitedHeaderFromArray(array: IAnyObject[] | IterableIterator<IAnyObject>, options?: object): string[]
  • Loops over an array of objects or arrays (rows) and builds a header that matches the structure of the rows.

    Parameters

    • array: IAnyObject[] | IterableIterator<IAnyObject>

      The array of row objects or arrays

    • Default value options: object = {}

    Returns string[]

    The header as an array of strings

equals

  • equals(value: any): function
  • Returns a function that will return true if called with argument equal to @value.

    Parameters

    • value: any

      The value to compare with.

    Returns function

      • (x: any): boolean
      • Parameters

        • x: any

        Returns boolean

escapeDelimitedValue

  • escapeDelimitedValue(value: any, delimiter?: string): string
  • Escapes a value a value for use in delimited formats like CSV or TSV

    • If it contains a double quote, new line or delimiter (typically a comma), the value is quoted.
    • any contained quotes are escaped with another quote
    • undefined is converted to empty string.
    • everything else is converted to string (but is not quoted)

    Parameters

    • value: any

      The value to escape

    • Default value delimiter: string = ","

    Returns string

    The escaped value

filterFiles

  • filterFiles(dir: string, filter?: BulkDataTools.FileFilter): IterableIterator<string>
  • Walk a directory recursively and find files that match the @filter if its a RegExp, or for which @filter returns true if its a function.

    Parameters

    • dir: string

      Path to directory

    • Optional filter: BulkDataTools.FileFilter

    Returns IterableIterator<string>

flatObjectKeys

  • flatObjectKeys(obj: IAnyObject, _prefix?: string): string[]
  • Returns a flattened array of the structure of an object or array. For example:

    {a:1, b:{c:2,d:3}, e:4} -> ["a", "b.c", "b.d", "e"]
    {a:1, b:[ 2, 3 ], e: 4} -> ["a", "b.0", "b.1", "e"]
    [1, {a: 3, b: 4}, 2, 3] -> ["0", "1.a", "1.b", "2", "3"]

    Parameters

    • obj: IAnyObject

      The object to inspect

    • Optional _prefix: string

    Returns string[]

floatVal

  • floatVal(x: BulkDataTools.Numeric, defaultValue?: BulkDataTools.Numeric): number
  • Returns the float representation of the first argument or the "defaultValue" if the int conversion is not possible.

    Parameters

    • x: BulkDataTools.Numeric

      The argument to convert

    • Optional defaultValue: BulkDataTools.Numeric

    Returns number

    The resulting floating point number.

getPath

  • getPath(obj: IAnyObject, path?: string): any
  • Walks thru an object (ar array) and returns the value found at the provided path. This function is very simple so it intentionally does not support any argument polymorphism, meaning that the path can only be a dot-separated string. If the path is invalid returns undefined.

    Parameters

    • obj: IAnyObject

      The object (or Array) to walk through

    • Default value path: string = ""

    Returns any

    Whatever is found in the path or undefined

intVal

  • intVal(x: BulkDataTools.Numeric, defaultValue?: BulkDataTools.Numeric): number
  • Returns the int representation of the first argument or the "defaultValue" if the int conversion is not possible.

    Parameters

    • x: BulkDataTools.Numeric

      The argument to convert

    • Optional defaultValue: BulkDataTools.Numeric

      The fall-back return value. This is going to be converted to integer too.

    Returns number

    The resulting integer.

isFunction

  • isFunction(x: any): boolean
  • Tests if the given argument is a function

    Parameters

    • x: any

      The value to test

    Returns boolean

isObject

  • isObject(x: any): boolean
  • Tests if the given argument is an object

    Parameters

    • x: any

      The value to test

    Returns boolean

jsonEntries

  • jsonEntries(dir: string): IterableIterator<IAnyObject>
  • Walks a directory recursively in a synchronous fashion and yields JSON objects. Only .json and .ndjson files are parsed. Yields ane JSON object for each line of an NDJSON file and one object for each JSON file. Other files are ignored.

    Parameters

    • dir: string

      A path to a directory

    Returns IterableIterator<IAnyObject>

mergeStrict

  • mergeStrict(obj1: IAnyObject, obj2: IAnyObject): IAnyObject
  • Merges the second argument into the first one but also throws if an object property is about to be overridden with scalar (or the opposite).

    throws

    {Error} If a path in one object points to scalar value and the same path in the other object points to an object (or the opposite).

    Parameters

    • obj1: IAnyObject

      The object which should be extended

    • obj2: IAnyObject

      The object which should be merged into the other one

    Returns IAnyObject

    Returns the extended first argument

parseDelimitedLine

  • parseDelimitedLine(line: string, delimiter?: string): string[]
  • Splits the line into cells using the provided delimiter (or by comma by default) and returns the cells array. supports quoted strings and escape sequences.

    Parameters

    • line: string

      The line to parse

    • Default value delimiter: string = ","

      The delimiter to use (defaults to ",")

    Returns string[]

    The cells as array of strings

readLine

  • readLine(filePath: string): IterableIterator<string>
  • Reads a file line by line in a synchronous fashion. This will read the file line by line without having to store more than one line in the memory (so the file size does not really matter). This is much easier than an equivalent readable stream implementation. It is also easier to debug and should produce reliable stack traces in case of error.

    todo

    Add ability to customize the EOL or use a RegExp to match them all.

    Parameters

    • filePath: string

      The path to the file to read (preferably an absolute path)

    Returns IterableIterator<string>

readableFileSize

  • readableFileSize(bytes: number, options?: IReadableFileSizeOptions): string
  • Obtains a human-readable file size string (1024 based).

    Parameters

    • bytes: number

      The file size in bytes

    • Optional options: IReadableFileSizeOptions

    Returns string

roundToPrecision

  • roundToPrecision(n: BulkDataTools.Numeric, precision?: number, fixed?: number): BulkDataTools.Numeric
  • Rounds the given number @n using the specified precision.

    Parameters

    • n: BulkDataTools.Numeric
    • Optional precision: number
    • Optional fixed: number

    Returns BulkDataTools.Numeric

    Returns a number, unless a fixed precision is used

setPath

  • setPath(obj: IAnyObject, path: string | string[] | number, value: any): void
  • Finds a path in the given object and sets its value

    Parameters

    • obj: IAnyObject
    • path: string | string[] | number
    • value: any

    Returns void

strPad

  • strPad(str: string, length?: number): string
  • Adds whitespace to the end of the string until it reaches the specified length

    Parameters

    • str: string

      The input string

    • Default value length: number = 0

      The target length of the result string

    Returns string

uFloat

  • uFloat(x: BulkDataTools.Numeric, defaultValue?: BulkDataTools.Numeric): number
  • Converts the input argument @x to unsigned (positive) float. Negative numbers are converted to their absolute value.

    Parameters

    • x: BulkDataTools.Numeric

      The value to convert. Should be a number or numeric string.

    • Optional defaultValue: BulkDataTools.Numeric

      The default value that is returned in case the conversion is not possible. Note that this will also be converted to unsigned float. Defaults to 0.

    Returns number

uInt

  • uInt(x: BulkDataTools.Numeric, defaultValue?: BulkDataTools.Numeric): number
  • Converts the input argument @x to unsigned (positive) integer. Negative numbers are converted to their absolute value.

    Parameters

    • x: BulkDataTools.Numeric

      The value to convert. Should be a number or numeric string.

    • Optional defaultValue: BulkDataTools.Numeric

      The default value that is returned in case the conversion is not possible. Note that this will also be converted to unsigned integer. Defaults to 0.

    Returns number

walkSync

  • walkSync(dir: string): IterableIterator<string>
  • List all files in a directory recursively in a synchronous fashion.

    Parameters

    • dir: string

    Returns IterableIterator<string>

Generated using TypeDoc