Comparing version 0.0.9 to 0.0.10
@@ -1,1 +0,1 @@ | ||
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory():typeof define==="function"&&define.amd?define(factory):(global=typeof globalThis!=="undefined"?globalThis:global||self,global.DataSet=factory())})(this,function(){"use strict";class DataSet{constructor(entries){this.entries=[];if(entries)this.insert(entries)}insert(inData){if(Array.isArray(inData)){inData.forEach(v=>{this.entries.push(v)})}else{throw new TypeError("DataSet: Parameter to insert must be an array.")}}drop(fieldName){this.entries.forEach(e=>delete e[fieldName]);return this}join(oDs,target,conditionCb){this.entries.forEach(localObj=>{const matches=[];oDs.entries.forEach(remoteObj=>{if(conditionCb(localObj,remoteObj)){matches.push(remoteObj)}});localObj[target]=matches});return this}joinFirst(oDs,target,conditionCb){this.entries.forEach(localObj=>{oDs.entries.forEach(remoteObj=>{if(conditionCb(localObj,remoteObj)){localObj[target]=remoteObj}})});return this}copy(){return new DataSet(this.entries)}reduce(conditionCb){this.entries=this.filter(conditionCb);return this}filter(conditionCb){return conditionCb?this.entries.filter(conditionCb):this.entries}first(conditionCb){const result=this.filter(conditionCb);return result.length>=1?result[0]:undefined}groupBy(groupByFieldsInput,outputFieldName){let groupByFields=Array.isArray(groupByFieldsInput)?groupByFieldsInput:[groupByFieldsInput],outputMap={},output=[];this.entries.forEach(localObj=>{let currentOutputObj=outputMap;groupByFields.forEach(f=>{let fieldValue=localObj[f]!==undefined?localObj[f]:"undefined";if(currentOutputObj[fieldValue]===undefined){currentOutputObj[fieldValue]={field:f,value:fieldValue,children:{}}}currentOutputObj=currentOutputObj[fieldValue].children});if(Array.isArray(currentOutputObj[outputFieldName])){currentOutputObj[outputFieldName].push(localObj)}else{currentOutputObj[outputFieldName]=[localObj]}});const rebuildResults=(currentLevel,trail)=>{trail=Object.assign({},trail)||{};Object.values(currentLevel).forEach(currentItem=>{if(Array.isArray(currentItem)){trail[outputFieldName]=currentItem;output.push(trail)}else{trail[currentItem.field]=currentItem.value;rebuildResults(currentItem.children,trail)}})};rebuildResults(outputMap);return new DataSet(output)}total(outputFieldName){let newEntry={};newEntry[outputFieldName]=this.entries;return new DataSet([newEntry])}avg(field,outputFieldName,mapCb){this.entries.forEach(e=>{const data=e[field].map(mapCb);e[outputFieldName]=data.reduce((a,b)=>a+b,0)/data.length});return this}sum(field,outputFieldName,mapCb){this.entries.forEach(e=>{e[outputFieldName]=e[field].map(mapCb).reduce((a,b)=>a+b,0)});return this}min(field,outputFieldName,mapCb){this.entries.forEach(e=>{e[outputFieldName]=Math.min(...e[field].map(mapCb))});return this}max(field,outputFieldName,mapCb){this.entries.forEach(e=>{e[outputFieldName]=Math.max(...e[field].map(mapCb))});return this}countd(field,outputFieldName,mapCb){this.entries.forEach(e=>{const data=e[field].map(mapCb);e[outputFieldName]=new Set(data).size});return this}count(field,outputFieldName,mapCb){this.entries.forEach(e=>{e[outputFieldName]=e[field].map(mapCb).length});return this}calc(outputFieldName,calcCb){this.entries.forEach(e=>{e[outputFieldName]=calcCb(e)});return this}autonumber(outputFieldName){let current=0;this.entries.forEach(e=>{e[outputFieldName]=current++});return this}}DataSet.DataSet=DataSet;return DataSet}); | ||
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory():typeof define==="function"&&define.amd?define(factory):(global=typeof globalThis!=="undefined"?globalThis:global||self,global.DataSet=factory())})(this,function(){"use strict";class DataSet{constructor(entries){this.entries=[];if(entries)this.insert(entries)}insert(inData){if(Array.isArray(inData)){inData.forEach(v=>{this.entries.push(v)})}else{throw new TypeError("DataSet: Parameter to insert must be an array.")}}drop(fieldName){this.entries.forEach(e=>delete e[fieldName]);return this}join(oDs,target,conditionCb){this.entries.forEach(localObj=>{const matches=[];oDs.entries.forEach(remoteObj=>{if(conditionCb(localObj,remoteObj)){matches.push(remoteObj)}});localObj[target]=matches});return this}joinFirst(oDs,target,conditionCb){this.entries.forEach(localObj=>{oDs.entries.forEach(remoteObj=>{if(conditionCb(localObj,remoteObj)){localObj[target]=remoteObj}})});return this}copy(){return new DataSet(this.entries)}reduce(conditionCb){this.entries=this.filter(conditionCb);return this}toArray(conditionCb){return conditionCb?this.entries.filter(conditionCb):this.entries}first(conditionCb){const result=this.filter(conditionCb);return result.length>=1?result[0]:undefined}groupBy(groupByFieldsInput,outputFieldName){let groupByFields=Array.isArray(groupByFieldsInput)?groupByFieldsInput:[groupByFieldsInput],outputMap={},output=[];this.entries.forEach(localObj=>{let currentOutputObj=outputMap;groupByFields.forEach(f=>{let fieldValue=localObj[f]!==undefined?localObj[f]:"undefined";if(currentOutputObj[fieldValue]===undefined){currentOutputObj[fieldValue]={field:f,value:fieldValue,children:{}}}currentOutputObj=currentOutputObj[fieldValue].children});if(Array.isArray(currentOutputObj[outputFieldName])){currentOutputObj[outputFieldName].push(localObj)}else{currentOutputObj[outputFieldName]=[localObj]}});const rebuildResults=(currentLevel,trail)=>{trail=Object.assign({},trail)||{};Object.values(currentLevel).forEach(currentItem=>{if(Array.isArray(currentItem)){trail[outputFieldName]=currentItem;output.push(trail)}else{trail[currentItem.field]=currentItem.value;rebuildResults(currentItem.children,trail)}})};rebuildResults(outputMap);return new DataSet(output)}total(outputFieldName){let newEntry={};newEntry[outputFieldName]=this.entries;return new DataSet([newEntry])}avg(field,outputFieldName,mapCb){this.entries.forEach(e=>{const data=e[field].map(mapCb);e[outputFieldName]=data.reduce((a,b)=>a+b,0)/data.length});return this}sum(field,outputFieldName,mapCb){this.entries.forEach(e=>{e[outputFieldName]=e[field].map(mapCb).reduce((a,b)=>a+b,0)});return this}min(field,outputFieldName,mapCb){this.entries.forEach(e=>{e[outputFieldName]=Math.min(...e[field].map(mapCb))});return this}max(field,outputFieldName,mapCb){this.entries.forEach(e=>{e[outputFieldName]=Math.max(...e[field].map(mapCb))});return this}countd(field,outputFieldName,mapCb){this.entries.forEach(e=>{const data=e[field].map(mapCb);e[outputFieldName]=new Set(data).size});return this}count(field,outputFieldName,mapCb){this.entries.forEach(e=>{e[outputFieldName]=e[field].map(mapCb).length});return this}calc(outputFieldName,calcCb){this.entries.forEach(e=>{e[outputFieldName]=calcCb(e)});return this}autonumber(outputFieldName){let current=0;this.entries.forEach(e=>{e[outputFieldName]=current++});return this}}DataSet.DataSet=DataSet;return DataSet}); |
{ | ||
"name": "datacraft", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"description": "LINQ-like JavaScript object query library. No dependencies. Works in all environments.", | ||
@@ -5,0 +5,0 @@ "author": "Hexagon <github.com/hexagon>", |
@@ -5,3 +5,5 @@ # Datacraft | ||
![Node.js CI](https://github.com/Hexagon/datacraft/workflows/Node.js%20CI/badge.svg?branch=master) [![npm version](https://badge.fury.io/js/datacraft.svg)](https://badge.fury.io/js/datacraft) | ||
*Work in progress*, stay tuned for the upcoming `1.0`. | ||
![Node.js CI](https://github.com/Hexagon/datacraft/workflows/Node.js%20CI/badge.svg?branch=main) [![npm version](https://badge.fury.io/js/datacraft.svg)](https://badge.fury.io/js/datacraft) | ||
[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/Hexagon/datacraft/blob/main/LICENSE) [![NPM Downloads](https://img.shields.io/npm/dw/datacraft.svg)](https://www.npmjs.org/package/datacraft) | ||
@@ -54,3 +56,3 @@ ![No dependencies](https://img.shields.io/badge/dependencies-none-brightgreen) | ||
```javascript | ||
import DataSet from "https://cdn.jsdelivr.net/gh/hexagon/datacraft@4/src/datacraft.js"; | ||
import DataSet from "https://cdn.jsdelivr.net/gh/hexagon/datacraft@0/src/datacraft.js"; | ||
@@ -63,3 +65,3 @@ const data = new DataSet(); | ||
```typescript | ||
import { DataSet } from "https://cdn.jsdelivr.net/gh/hexagon/datacraft@1/src/datacraft.js"; | ||
import { DataSet } from "https://cdn.jsdelivr.net/gh/hexagon/datacraft@0/src/datacraft.js"; | ||
@@ -73,3 +75,3 @@ const _data : DataSet = new DataSet(); | ||
* Download latest [zipball](https://github.com/Hexagon/datacraft/archive/refs/heads/master.zip) | ||
* Download latest [zipball](https://github.com/Hexagon/datacraft/archive/refs/heads/main.zip) | ||
* Unpack | ||
@@ -83,3 +85,3 @@ * Grab ```datacraft.min.js``` (UMD and standalone) or ```datacraft.min.mjs``` (ES-module) from the [dist/](/dist) folder | ||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/datacraft@1/dist/datacraft.min.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/datacraft@0/dist/datacraft.min.js"></script> | ||
``` | ||
@@ -91,3 +93,3 @@ | ||
<script type="module"> | ||
import DataSet from "https://cdn.jsdelivr.net/npm/datacraft@1/dist/datacraft.min.mjs"; | ||
import DataSet from "https://cdn.jsdelivr.net/npm/datacraft@0/dist/datacraft.min.mjs"; | ||
@@ -103,6 +105,78 @@ // ... see usage section ... | ||
### Signature | ||
### Examples | ||
## Examples | ||
```javascript | ||
import { DataSet } from "datacraft"; | ||
// Set up data | ||
let persons = new DataSet([ | ||
{name: "Curt", group: 14, age: 34}, | ||
{name: "Lewis", group: 14, age: 38}, | ||
{name: "Stewie", group: 15, age: 31} | ||
]); | ||
let groups = new DataSet([ | ||
{group: 14, name: "Western"}, | ||
{group: 15, name: "North"} | ||
]); | ||
// Set up relations | ||
persons.joinFirst(groups, "groups", (p, g) => p.group == g.group); | ||
groups.join(persons, "persons", (g, p) => g.group == p.group); | ||
// Aggregate data | ||
groups | ||
.avg("persons", "averageAge", p => p.age) | ||
.min("persons", "maxAge", p => p.age) | ||
.max("persons", "minAge", p => p.age) | ||
.count("persons", "personCount", p => p.name); | ||
// Print data | ||
console.log(persons.toArray()); | ||
console.log(groups.toArray()); | ||
``` | ||
### All methods | ||
All methods of `DataSet`. | ||
#### Dataset operations | ||
| Method | Description | | ||
| ------ | ----------- | | ||
| insert([obj1, obj2]) | Insert new objects into data set | | ||
| 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 | ||
@@ -109,0 +183,0 @@ |
@@ -111,3 +111,3 @@ /* ------------------------------------------------------------------------------------ | ||
} | ||
filter(conditionCb) { | ||
toArray(conditionCb) { | ||
return conditionCb ? this.entries.filter(conditionCb) : this.entries; | ||
@@ -114,0 +114,0 @@ } |
@@ -22,3 +22,3 @@ export default DataSet; | ||
reduce(conditionCb: any): DataSet; | ||
filter(conditionCb: any): any[]; | ||
toArray(conditionCb: any): any[]; | ||
first(conditionCb: any): any; | ||
@@ -25,0 +25,0 @@ groupBy(groupByFieldsInput: any, outputFieldName: any): DataSet; |
export default DataSet; | ||
import DataSet from "./datacraft.js"; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35675
182