@aeinbu/groupby
Advanced tools
Comparing version 1.1.0 to 1.2.0-beta-10
{ | ||
"name": "@aeinbu/groupby", | ||
"version": "1.1.0", | ||
"version": "1.2.0-beta-10", | ||
"description": "Lightweight and non-intrusive functions for doing \"group by\" type transformations on collections using reducers", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"lib/**/*" | ||
"dist/**/*", | ||
"src/**/*" | ||
], | ||
@@ -30,3 +31,9 @@ "directories": { | ||
"dictionary", | ||
"map" | ||
"map", | ||
"minof", | ||
"min of", | ||
"maxof", | ||
"max of", | ||
"sumof", | ||
"sum of" | ||
], | ||
@@ -41,10 +48,2 @@ "author": "Arjan Einbu <arjan@einbu.no>", | ||
"@types/jest": "^27.0.1", | ||
"@typescript-eslint/eslint-plugin": "^4.31.1", | ||
"@typescript-eslint/parser": "^4.31.1", | ||
"eslint": "^7.32.0", | ||
"eslint-config-standard": "^16.0.3", | ||
"eslint-plugin-import": "^2.24.2", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-promise": "^5.1.0", | ||
"eslint-plugin-standard": "^5.0.0", | ||
"jest": "^27.2.0", | ||
@@ -51,0 +50,0 @@ "ts-jest": "^27.0.5", |
@@ -26,5 +26,5 @@ # @aeinbu/groupby | ||
## How to use | ||
## Quickstart | ||
```javascript | ||
import { groupBy, toDictionary } from "@aeinbu/groupby" | ||
import { groupBy, toMap } from "@aeinbu/groupby" | ||
@@ -40,42 +40,6 @@ const people = [ | ||
const resultsAsArray = people.reduce( | ||
groupBy( | ||
x => x.residence, // First lambda is used to extraxt the key values, so this will group by the residence | ||
x => x.name // Second lambda to determine object values to put in the groups, so in this example the group will contain all names for people in a residence city | ||
// This paramter is optional, and if it is skipped, the whole item is selected as the value | ||
), | ||
[] | ||
) | ||
// Result (Note how the output is a new array): | ||
// [ | ||
// {"key": "Rome", values: ["Tony", "Mary", "Peter"]}, | ||
// {"key: "London", values: ["Peter", "Elisabeth"]}, | ||
// {"key: "Paris", values: ["Francois"]} | ||
// ] | ||
const results = people | ||
.reduce(groupBy(x => x.name, x => x.residence), []) | ||
.reduce(toMap(x => x.name), new Map()) | ||
const resultsAsDictionary = resultsAsArray.reduce( | ||
toDictionary( | ||
x => x.name, // First lambda to determine the property name | ||
x => x.values // Second lambda to determine where to find the value to set that property to | ||
// This paramter is optional, an if omitted it will default to `x => x.values´ which would match the default output of `groupBy` above | ||
), | ||
{} | ||
) | ||
// Result (See how the array is transformed to an object with properties for each key): | ||
// { | ||
// "Rome": ["Tony", "Mary", "Peter"], | ||
// "London": ["Peter", "Elisabeth"], | ||
// "Paris": ["Francois"] | ||
// } | ||
``` | ||
Since the reduction from `groupBy` is an array, the above two transforms can be chained and shortened (using a default parameter in toDictionary), like this: | ||
```javascript | ||
import { groupBy, toDictionary } from "@aeinbu/groupby" | ||
//... | ||
const chainedResults = people | ||
.reduce(groupBy(x => x.name, x => x.residence), []) | ||
.reduce(toDictionary(x => x.name), {}) | ||
// With the same results as above: | ||
@@ -89,22 +53,12 @@ // { | ||
## Documentation | ||
For more documentation, follow these links: | ||
- [groupBy](./docs/groupBy.md) | ||
- [distinctBy](./docs/distinctBy.md) and [distinct](./docs/distinct.md) | ||
- [toMap](./docs/toMap.md) | ||
- [toDictionary](./docs/toDictionary.md) | ||
- [isDeepEqual](./docs/isDeepEqual.md) | ||
There is also a `toMap` reducer, so you can create a ES2015 `Map` instead of an ordinary object: | ||
```javascript | ||
import { groupBy, toMap } from "@aeinbu/groupby" | ||
Also, look in the `tests` directory for more examples. The tests demonstrate at least another dozen different ways to use this library | ||
//... | ||
const chainedResults = people | ||
.reduce(groupBy(x => x.name, x => x.residence), []) | ||
.reduce(toMap(x => x.name), new Map()) | ||
// With the same results as above: | ||
// { | ||
// "Rome": ["Tony", "Mary", "Peter"], | ||
// "London": ["Peter", "Elisabeth"], | ||
// "Paris": ["Francois"] | ||
// } | ||
``` | ||
Also, look in the `tests` directory for more examples. The tests demonstrate at least another dosen different ways to use this library | ||
## Semantic versioning | ||
@@ -111,0 +65,0 @@ This package follows semantic versioning (See [semver.org](https://semver.org) for more info) |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
20225
4
23
274
1
66
1