Parser

Parser

Source:
To Do:
  • 1. implement error string property
  • 2. wrap all util functions in parser

Example

Usage

parser = require('./parser.js')
util = require('./util.js')
...
let myParser = new parser.Parser(myCsvString);
myParser.setMeta({"title":"myData","author":"me"}); //optional
myParser.setHeaders(0,0); //set the header rows and columns - required
myParser.clear(/"/g)  //clear quotes from data array - optional. Be careful clearing before setting headers, some header values may not may properly to objects if not in string format, such as "15-10"
myParser.setProps("row"); //set row headers to be the property map source - required
myParser.propsToArray(/!!/); //splits header values of the selected prop header to arrays for mapping to properties: POPULATION!!15 AND OVER in header becomes [POPULATION, 15 AND OVER] which will become columnheader.POPULATION["15 AND OVER"] = intersecting_value
let json = myParser.mapProps();  

Members

data :Array.<Array>

The data array operated on by this Parser. May be referenced for cleaning.
Source:
See:
Type:
  • Array.<Array>

metadata

Contains metadata of properties that will be added to the top level JSON object.
Source:

Methods

chop(find, regindexopt)

Chops a row from the data array in parser. Also removes those indices from the RowHeaders.
Source:
Parameters:
Name Type Attributes Default Description
find number | Array.<number> | RegExp The row index to remove, an array of row indexes to remove, or a regular expression. If a regular expression is passed, all rows that match the regex will be removed. Numbers may be negative to operate from the end.
regindex number | string <optional>
0 The **column** index to search when using regular expressions. Defaults to the first column, index 0. If a string, such as "HEADER" is passed, the row header array will be searched instead.

chopColumn(find, regIndexopt)

Chops a column from an array
Source:
Parameters:
Name Type Attributes Default Description
find number | Array.<number> | RegExp The column index to remove, an array of column indexes to remove, or a regular expression. If a regular expression is passed _all_ columns that match on the RegIndex row will be removed. Negative numbers will operate from the last column backwards.
regIndex number | string <optional>
0 The **row** index to search when using regular expressions as the find parameter. Defaults to first row, index 0. Can also pass in the string "HEADER" to search the header row.

clear(findopt, numerifyopt, rowindopt, colindopt)

Clears unwanted characters from data array or a portion of that array
Source:
Parameters:
Name Type Attributes Default Description
find string | Array.<string> | RegExp <optional>
'"' The string, array of strings, or regular expression to remove.
numerify boolean <optional>
true Calls this.numerify() to also convert any strings that look like numbers into numbers.
rowind number <optional>
-1 The row to operate on. If -1 (default), it will operate on the entire 2D array.
colind number <optional>
-1 The column to operate on. If -1 (default), it will operate on the entire 2D array.

mapProps()

Maps data to an object. The object will contain a property for each value not in the selected props array. For each array in the selected props array, a chain of sub-properties will be created. Metadata, if extant, will be added as properties to the mother object. So if you have this data:
"headers""head1""head2"
"prop1::prop2"35"val2"
"prop1::anotherprop""val3"5
After setting the props array to "Row" and processing to an array, i.e.
[ ["prop1","prop2"], ["prop1","anotherprop"] ]
using setProps and propsToArray You will have a JSON like the example below.
Source:
Example

Output JSON

{
 "head1": {
         "prop1": {
                "prop2":35,
                "anotherprop":"val3"     
                  }
         },
 "head2": {
         "prop1": {
                "prop2":"val2",
                "anotherprop":5     
                }
         }
}

mergeToHeader(index)

Merges another row or column into the header column so that header column becomes an array, which causes the higher indexed elements of the array to nest inside the lower indexed elements of the array in the final output json object.
Source:
Parameters:
Name Type Description
index number The index to merge to the header(not counting the header itself in the index). If headers set to row, merges that index row. If set to col, merges that column

numerify()

Goes through array and all sub-arrays and converts strings into numbers where it is possible to do so.
Source:

propsToArray(regexopt)

Splits the header column selected by setProps() to an array for mapping to the other column.
Source:
Parameters:
Name Type Attributes Default Description
regex RegExp <optional>
!! The regex to split the rows on

setHeaders(rowIndexopt, columnIndexopt)

Sets the internal header arrays and removes those rows and columns from the data. The intersecting element will be removed.
Source:
To Do:
  • implement error string property to log errors in index sizes
Parameters:
Name Type Attributes Default Description
rowIndex integer <optional>
0 The row index of the column headers. Negative will operate from end.
columnIndex integer <optional>
0 The column index of the row headers. Negative will operate from end.

setProps(str)

Sets either the row headers or the column headers as the one to be mapped - the other column will be the parent object name.
Source:
Parameters:
Name Type Description
str string A string of either 'ROW' or 'COL' - Parser initializes with this set to ROW. A string other than these will do nothing.