Datacraft
LINQ-like JavaScript object query library. No dependencies. Works in all environments.
Work in progress, stay tuned for the upcoming 1.0
.
- Works in Node.js >=4.0 (both require and import).
- Works in Deno >=1.16.
- Works in browsers as standalone, UMD or ES-module.
- Includes TypeScript typings.
Quick examples:
More examples...
Installation
Node.js
npm install datacraft --save
JavaScript
import DataSet from "datacraft";
const DataSet = require("datacraft");
TypeScript
Notes for TypeScript:
import DataSet from "datacraft";
const data : DataSet = new DataSet();
Deno
JavaScript
import DataSet from "https://deno.land/x/datacraft/src/datacraft.js";
const data = new DataSet();
TypeScript
import { DataSet } from "https://deno.land/x/datacraft/src/datacraft.js";
const _data : DataSet = new DataSet();
Browser
Manual
- Download latest zipball
- Unpack
- Grab
datacraft.min.js
(UMD and standalone) or datacraft.min.mjs
(ES-module) from the dist/ folder
CDN
To use as a UMD-module (stand alone, RequireJS etc.)
<script src="https://cdn.jsdelivr.net/npm/datacraft@0/dist/datacraft.min.js"></script>
To use as a ES-module
<script type="module">
import DataSet from "https://cdn.jsdelivr.net/npm/datacraft@0/dist/datacraft.min.mjs";
</script>
Documentation
Full documentation available at hexagon.github.io/datacraft.
The short version:
Examples
import DataSet from "https://deno.land/x/datacraft/src/datacraft.js";
let persons = new DataSet().insert([
{name: "Curt", group: 14, age: 34},
{name: "Lewis", group: 14, age: 38},
{name: "Stewie", group: 15, age: 31}
]);
let groups = new DataSet().insert([
{group: 14, name: "Western"},
{group: 15, name: "North"}
]);
let personsWithFirstGroup = persons.copy().joinFirst(groups, "groups", (p, g) => p.group == g.group);
let groupsWithAllPersons = groups.copy().join(persons, "persons", (g, p) => g.group == p.group);
groupsWithAllPersons
.avg("persons", "averageAge", p => p.age)
.min("persons", "maxAge", p => p.age)
.max("persons", "minAge", p => p.age)
.count("persons", "personCount", p => p.name);
console.log(personsWithFirstGroup.toArray());
console.log(groupsWithAllPersons.toArray());
All methods
All methods of DataSet
.
Dataset operations
Method | Description |
---|
insert([obj1, obj2]) | Insert new objects into data set |
update(obj, conditionCb) | Update all entries matching conditionCb with properties of obj |
drop(fieldName) | Remove a field from all objects in data set |
copy() | Make a shallow copy of current data set |
calc(outputFieldName, calcCb) | Creates a new field, containing the return value of calcCb |
autonumber(outputFieldName) | Creates a new field, contaning a number that increment for each entry |
Relations
Method | Description |
---|
join(otherDataSet, newFieldName, conditionCb) | Join all objects in data set with objects of another data set where conditionsCb return true |
joinFirst(otherDataSet, newFieldName, conditionCb) | Join first object from otherDataSet with objects in current data set |
Filtering
Method | Description |
---|
filter(conditionCb) | Keep only objects matching conditionCb |
toArray(conditionCb) | Returns an array of all entries in current data set, optionally filtered by conditionCb |
first(conditionCb) | Returns the first entry if current data set, optionally filtered by conditionCb |
Aggregation
Method | Description |
---|
groupBy(groupByFieldsInput, outputFieldName) | Group current dataset by field(s) specified by groupByFieldsInput |
total(outputFieldName) | Group current dataset, placing all entries in a new field named by parameter outputFieldName |
avg(field, outputFieldName, mapCb) | Average a value from entries in field specified by field , in a new field named by outputFieldName . mapCb points out which subfield should be averaged |
sum(field, outputFieldName, mapCb) | Like above |
min(field, outputFieldName, mapCb) | Like above |
max(field, outputFieldName, mapCb) | Like above |
countd(field, outputFieldName, mapCb) | Like above |
count(field, outputFieldName, mapCb) | Like above |
Contributing
See Contribution Guide
License
MIT