1.1.1 • Published 6 years ago
map-to-parent v1.1.1
#map-to-parent
Easily map track and modify multiple child arrays to a parent array in a functional way,
convenient for tracking table data types.
It indexes and stores once when the child arrays are declared and doesn't re-index until necessary.
that way you are not re-indexing every time you wish to know the index of something relative to the parent.
###how to use
let map_to_parent = require("map-to-parent")
//this will be the parent array.
let columns = ["col_0","col_1", "col_2", "col_3", "col_4", "col_5","col_6"]//parent
//child arrays below consisting ONLY of items consisted in parent array
let content_editable = ["col_2", "col_3"]//child array
let booleans = ["col_1","col_5"]//child array
let float_numbers = ["col_2", "col_3", "col_4"]//child array
//initiate map_to_parent with columns as parent
let c_maps = map_to_parent(columns) //columns is defined as the parent array
//map child arrays to parent array
c_maps.child("booleans", booleans) //map array booleans to parent array
c_maps.child("content_editable", content_editable)//map array content_editable to parent array
c_maps.child("float_numbers", float_numbers)//map array float_numbers to parent array
//now the arrays can be accessed and modified in the following ways
console.log(c_maps.child("booleans")); //[ 'col_1', 'col_5']
console.log(c_maps.index("booleans")); //[ 1, 5]
console.log(c_maps.index("booleans", "col_5"));//5
c_maps.child_replace("booleans", "col_1","col_0");//replace child array item with another child array item
console.log(c_maps.index("booleans")); //['col_0', 'col_5']
console.log(c_maps.index("float_numbers"));//[ 2, 3, 4]
c_maps.child_splice("booleans", "col_0"); //remove index item from child array
console.log(c_maps.index("booleans")); //[ 5 ]
c_maps.child_push("booleans", "col_6"); //push "col_6" to booleans array
console.log(c_maps.index("booleans")); //[ 5, 5, 6]
console.log(c_maps.contains("booleans", "col_5")); //true
console.log(c_maps.contains("booleans", "COL_")); //false
Generally the point of this program was not to make the children highly modifiable, but rather to have efficient indexing and easy mapping of the parent's sub groups. ####more complete sample below
let map_to_parent = require("map-to-parent")
let columns = ["col_0","col_1", "col_2", "col_3"]
let data_rows = [
[ "row_0", 9.999996, "01-01-2014", 3.999997],
[ "row_1", 2.999996, "01-02-2014", 7.999997],
[ "row_2", 5.999996, "01-03-2014", 8.999997],
[ "row_3", 6.999996, "01-04-2014", 4.999997],
]
let column_maps = map_to_parent(columns)
column_maps.child("strings", ["col_0"])
column_maps.child("dates", ["col_2"])
column_maps.child("integers", ["col_1", "col_3"])
function str_UPPER(str){return str.toUpperCase()}
function date_format(date_str){let d = new Date(date_str); return d.toDateString()}
function parse_int(num){return parseInt(num)}//i know this is needless repetition but for demonstration's sake
function prep_format_cell(c_maps){
return function format_cell(cell, cell_i){
switch(true){
case c_maps.contains("strings", c_maps.main()[cell_i]): return str_UPPER(cell);
case c_maps.contains("dates", c_maps.main()[cell_i]): return date_format(cell);
case c_maps.contains("integers", c_maps.main()[cell_i]): return parse_int(cell);
default: return cell;
}
}
}
let format_cell = prep_format_cell(column_maps)
function format_row(row){
return row.map(format_cell)
}
let data_rows_F = data_rows.map(format_row)
console.log("original data")
console.log(data_rows)
// returns
// [ "row_0", 9.999996, "01-01-2014", 3.999997],
// [ "row_1", 2.999996, "01-02-2014", 7.999997],
// [ "row_2", 5.999996, "01-03-2014", 8.999997],
// [ "row_3", 6.999996, "01-04-2014", 4.999997],
console.log("formatted_data")
console.log(data_rows_F)
// returns
// [ "ROW_0", 9, "Wed Jan 01 2014", 3],
// [ "ROW_1", 2, "Thu Jan 02 2014", 7],
// [ "ROW_2", 5, "Fri Jan 03 2014", 8],
// [ "ROW_3", 6, "Sat Jan 04 2014", 4],
I really don't miss loops :D