ts-data-model
v1.0.10
Published
Library for the data model objects used in VIDA
Downloads
24
Readme
ts-data-model library
Using the library
To install the library in your project
yarn add "ts-data-model"
To use the library in your react project
import { DataModel, fromDhis2 } from "ts-data-model"
const x = DataModel({ input: data, adapter: fromDhis2 })
Library Documentation
The ts-data-model libary consists of 4 major modules:
- Data Adapters
- Data Transformations
- Data Formmaters
- Utils functions
Mapping
| Function | Function type | Functions used in implementation | | ----------------: | ------------------: | --------------------------------------: | | fromDhis2 | Data Adapter | findPosition, transpose, fromColumnDict | | fromColumnDict | Data Adapter | | | fromArrayOfArrays | Data Adapter | | | toColumnDict | Data Formatter | | | toArrayOfArrays | Data Formatter | | | apply | Data Transformation | | | map | Data Transformation | addColumns, apply | | agg | Data Transformation | | | join | Data Transformation | innerJoin, outerJoin | | filter | Data Transformation | buildFilterFn, evaluateOr, evaluateAnd | | addColumns | Data Transformation | | | fillNaN | Data Transformation | detectNaN | | dropNaN | Data Transformation | detectNaN | | selectColumns | Data Transformation | | | rename | Data Transformation | | | transpose | Utils | | | findPosition | Utils | | | innerJoin | Utils | | | outerJoin | Utils | |
Mapping diagram
Data Adapters
Description: Data adapters are functions that convert output from different sources to the standard data model structure i.e. an array of objects
fromDhis2
Description: function to convert output from a dhis2 analytics query to the standard model structure i.e. an array of objects Example usage
import { DataModel } from "ts-data-model"
const response = {
headers: [
{ name: "dx", column: "Data", valueType: "TEXT", type: "java.lang.String", hidden: false, meta: true,},
{ name: "pe", column: "Period", valueType: "TEXT", type: "java.lang.String", hidden: false, meta: true,},
{ name: "ou", column: "Organisation unit", valueType: "TEXT", type: "java.lang.String", hidden: false, meta: true,},
{name: "value", column: "Value", valueType: "NUMBER", type: "java.lang.Double", hidden: false, meta: false,},
{name: "factor", column: "Factor", valueType: "NUMBER", type: "java.lang.Double", hidden: false, meta: false,},
{name: "multiplier", column: "Multiplier", valueType: "NUMBER", type: "java.lang.Double", hidden: false, meta: false,},
{name: "denominator", column: "Denominator", valueType: "NUMBER", type: "java.lang.Double", hidden: false, meta: false,},
{name: "divisor", column: "Divisor", valueType: "NUMBER", type: "java.lang.Double", hidden: false, meta: false,},
]
rows:
[
['fbfJHSPpUQD', '202205', 'ARZ4y5i4reU', '18', '0', '0', '0', '0', '0'];
['fbfJHSPpUQD', '202201', 'YmmeuGbqOwR', '23', '0', '0', '0', '0', '0']
]
}
const data = new DataModel({ input: response, adapter: fromDhis2 })
fromColumnDict
Description: You can create the data model object from a column dictionary using the fromColumnDict adapter Example usage
import { DataModel } from "ts-data-model"
const d2 = {
"column 1": ["1", "2"],
"column 2": ["3", "4"],
}
const data = new DataModel({ input: response, adapter: fromColumnDict })
Data Transformations
Description: functions that operate on a data model object, changing its columns or rows, but returning an object that keeps the structure of the data model. Transformations are categorizd into Aggregators, Selectors, Reducers, Mutators
Aggregators
Transformations implemented: agg Example usage
import { agg } from "ts-data-model"
const aggregations = [
["age", "sum"],
["age", "mean"],
]
const result = data.agg({ groupBy: ["city", "gender"], aggregations })
Reducers
Transformations implemented: filter Example usage:
import { filter } from "ts-data-model"
const filterCriteria = [
"city",
"=",
["London", "Paris"],
"&&",
"age",
">",
30,
"&&",
"age",
"<=",
40,
]
const filteredData = data.filterData({ filterCriteria: filterCriteria })
Mutators
Transformations implemented: apply, fillNaN
apply
Example usage
const transformFn = (row) => {
const bonus = row.salary * 0.1
return { age: row.age * 2, salary: row.salary + bonus, name: row.name }
}
const transformedData = data.apply({ transformFn: transformFn })
fillNaN
Example usage
data.fillNaN()