Methods
(static) chainMultiple(arr2d, vals, parentobjopt, mutateopt) → {Object}
Creates nested properties in an object based on an array and assigns a value to the last property in the chain.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
arr2d |
Array.<Array>
|
A 2 dimensional array aligned with the vals array. The nested arrays contain strings representing properties to chain. | ||
vals |
Array
|
A 1 dimensional array aliged with arr1d where vals[x] is the value to be chained with arr1d[x] | ||
parentobj |
Object
|
<optional> |
{} | the parent object to mutate |
mutate |
boolean
|
<optional> |
true | controls whether a semi-deep copy of parentobj is made before chaining. |
Returns:
- Type:
-
Object
- the parent object with the new properties chain, or a semi-deep clone of the parent object with the new properties chain if mutate is set to true.
(static) chainSingle(arr1d, val, objopt, mutateopt)
Chains an array into nested object/properties and sets the final property to the value given.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
arr1d |
Array.<string>
|
string
|
an array of strings to be chained into properties | ||
val |
*
|
the value to set the final object | ||
obj |
obj
|
<optional> |
{} | the parent object on which to perform the chaining |
mutate |
boolean
|
<optional> |
true | controls whether a semi-deep copy of the object is made before chaining |
Returns:
- the object with the new properties chain.
(static) chop(arr, find, regindexopt, keepFirstopt) → {Array}
Chops a row from an array, non mutating.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
arr |
Array.<Array>
|
the 2-dimensional array to operate on | ||
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
|
<optional> |
0 | The **column** index to search when using regular expressions. Defaults to the first column, index 0, as the typical use case would be to remove rows corresponding to unwanted data categories. If set to -1, chop will search the entire array for the regex, and whenever it finds a match, it will delete the entire row on which it was found. |
keepFirst |
boolean
|
<optional> |
false | If set to true, Chop will ignore the first row of the .csv, which is often the header row when doing RegEx based searches. |
Returns:
- Type:
-
Array
- A 2-dimensional array, possibly with some rows removed.
(static) chopColumn(arr, find, regIndexopt, keepFirstopt) → {Array}
Chops a column from an array, non mutating.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
arr |
*
|
A 2 dimensional array to operate on. Rows should be **equal length** | ||
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
|
<optional> |
0 | The **row** index to search when using regular expressions as the find parameter. Defaults to first row, index 0, as the typical use case would be to remove columns representing unwanted data, and column headers are usually located at row 0. If set to -1, chopColumn will search the entire array for a regex, and whenever it finds a match, it will delete the entire column on which it was found. |
keepFirst |
boolean
|
<optional> |
false | If set to true, chopColumn will ignore the first column of the .csv, which is often the header row when doing RegEx based searches. |
Returns:
- Type:
-
Array
- A 2-dimensional array with the indicated columns removed
(static) clear(arr, findopt, rowIndopt, colIndopt)
Clears quotations or another character from elements in a 2D array or portion of that array.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
arr |
Array.<Array>
|
The 2-dimensional array to operate on. | ||
find |
string
|
Array.<string>
|
RegExp
|
Array.<RegExp>
|
<optional> |
'"' | The string, array of strings, or regular expression to remove. |
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. |
(static) convertArrToCSV(arr, colDelimopt, rowDelimopt) → {string}
Converts array to a CSV style string
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
arr |
Array.<Array>
|
the array to convert to a string | ||
colDelim |
string
|
<optional> |
',' | the string to delimit columns |
rowDelim |
string
|
<optional> |
'\n' | the string to delimit rows |
Returns:
- Type:
-
string
a csv-style string
(static) csvArray(csvtext, delimopt, row_delimopt, trimopt) → {Array}
Parses csv into a 2-dimensional array. By default, also trims rows and columns left by trailing delimiters.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
csvtext |
string
|
A csv string | ||
delim |
string
|
<optional> |
'","' | The value delimiter |
row_delim |
string
|
<optional> |
'\n' | The row delimiter |
trim |
boolean
|
<optional> |
true | Trims empty trailing rows and empty trailing elements in rows |
Returns:
- Type:
-
Array
- A 2-dimensional array, with each sub-array representing rows.
(static) getColumn(arr2d, colIndex) → {Array.<Array>}
Gets a column from a 2D array as an array.
Parameters:
Name | Type | Description |
---|---|---|
arr2d |
*
|
The array to operate on |
colIndex |
*
|
The index of the column to get |
Returns:
- Type:
-
Array.<Array>
An array of the values in the column.
(static) numerify(arr) → {Array}
Goes through arrays and sub-arrays and converts any numeric strings to numbers that it finds
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array
|
An array of any dimension; numerify will recursively move through sub arrays. |
Returns:
- Type:
-
Array
An array with numeric strings converted to numbers.
(static) toArray(arr, findopt, rowIndopt, colIndopt)
Coverts an element or series of elements in a 2D array to an array by splitting it on a regular expression. Useful for pre-processing before calling chain, which will map an array to nested object properties.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
arr |
Array.<Array>
|
The 2-dimensional array to operate on. | ||
find |
string
|
Array.<string>
|
RegExp
|
Array.<RegExp>
|
<optional> |
'!!' | The string, array of strings, or regular expression to split values on. Defaults to !!, which is used by census data, such as POPULATION!!15 AND OLDER!!, which will becomes [POPULATION,15 AND OLDER]. Note: If passing multiple regexes, all flags will be ignored and they will be set to global. If matching for certain specific characters, like '.', a regex must be used. |
rowInd |
number
|
<optional> |
-1 | The row index to operate on, default -1, which operates on all. |
colInd |
number
|
<optional> |
0 | The column index to operate on, default 0. -1 operates on all. |