Comparing version 1.0.0 to 1.1.0
# Changelog | ||
## 1.1.0 | ||
- Adds `byValue` | ||
- Adds `groupByProperty` | ||
- Adds `or` | ||
- Adds support for returning `undefined` in `groupBy`. | ||
## 1.0.0 | ||
@@ -4,0 +11,0 @@ |
@@ -9,2 +9,10 @@ /** Use with: `filter` | ||
export declare const isDefined: <T>(x: T | undefined) => x is T; | ||
/** Use with: `map` | ||
* | ||
* Replaces list elements that are `undefined` with `fallback` | ||
```ts | ||
[1, undefined, 2].map(or(0)); // Returns [1, 0, 2] | ||
``` | ||
*/ | ||
export declare const or: <T>(fallback: T) => (x: T | undefined) => T; | ||
/** Use with: `filter` | ||
@@ -228,3 +236,3 @@ * | ||
export declare const propertyIsntOneOf: <TObject extends object, TKey extends keyof TObject>(key: TKey, list: TObject[TKey][]) => (el: TObject) => boolean; | ||
/**Use with: `sort` | ||
/** Use with: `sort` | ||
* | ||
@@ -239,2 +247,11 @@ * Sort the elements by `func(element)`. Supports sorting by boolean values (elements that are `true` first). | ||
* | ||
* Sort a list of numbers. This is useful because javascript sorts numbers as string, meaning that [25, 100] results in [100, 25] since "2" is greater than "1" | ||
```ts | ||
[100, 25].sort(); // Returns [100, 25] | ||
[100, 25].sort(byValue); // Returns [25, 100] | ||
``` | ||
*/ | ||
export declare const byValue: (a: number, b: number) => 1 | 0 | -1; | ||
/** Use with: `sort` | ||
* | ||
* Sort the elements by `element[key]` (can also be an array index). Supports sorting by boolean values (elements that are `true` first). | ||
@@ -345,5 +362,18 @@ ```ts | ||
*/ | ||
export declare const groupBy: <K extends string, V>(func: (el: V) => K) => (acc: Record<K, V[]>, el: V) => Record<K, V[]>; | ||
export declare const groupBy: <K extends string, V>(func: (el: V) => K | undefined) => (acc: Record<K, V[]>, el: V) => Record<K, V[]>; | ||
/** Use with: `reduce` | ||
* | ||
* Given a property name, returns an object of lists of elements, grouped by the values for that property. A second argument must be passed to `reduce`. For javascript an empty object is enough. For typescript an object with properties or a type cast is required. | ||
```ts | ||
[{ name: "Jane" }, { name: "John" }].reduce( | ||
groupByProperty("name"), | ||
{} | ||
); // Returns { Jane: [{ name: "Jane" }], John: [{ name: "John" }] } | ||
``` | ||
*/ | ||
export declare const groupByProperty: <K extends keyof V, V extends { | ||
[key: string]: any; | ||
}>(key: K) => (acc: Record<V[K], V[]>, el: V) => Record<V[K], V[]>; | ||
/** Use with: `reduce` | ||
* | ||
* Splits the input list into two lists. The first list contains elements for which the given function returned `true`, the second contains elements for which the function returned `false`. | ||
@@ -350,0 +380,0 @@ ```ts |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.countBy = exports.partition = exports.groupBy = exports.minByProperty = exports.minBy = exports.min = exports.maxByProperty = exports.maxBy = exports.max = exports.sumByProperty = exports.sumBy = exports.sum = exports.get = exports.byProperty = exports.by = exports.propertyIsntOneOf = exports.isntOneOfBy = exports.isntOneOf = exports.excludeByProperty = exports.excludeBy = exports.exclude = exports.propertyIsOneOf = exports.isOneOfBy = exports.isOneOf = exports.intersectionByProperty = exports.intersectionBy = exports.intersection = exports.propertyIsnt = exports.isntBy = exports.isnt = exports.propertyIs = exports.isBy = exports.is = exports.uniqueByProperty = exports.unique = exports.uniqueBy = exports.duplicatesByProperty = exports.duplicates = exports.duplicatesBy = exports.isDefined = void 0; | ||
exports.countBy = exports.partition = exports.groupByProperty = exports.groupBy = exports.minByProperty = exports.minBy = exports.min = exports.maxByProperty = exports.maxBy = exports.max = exports.sumByProperty = exports.sumBy = exports.sum = exports.get = exports.byProperty = exports.byValue = exports.by = exports.propertyIsntOneOf = exports.isntOneOfBy = exports.isntOneOf = exports.excludeByProperty = exports.excludeBy = exports.exclude = exports.propertyIsOneOf = exports.isOneOfBy = exports.isOneOf = exports.intersectionByProperty = exports.intersectionBy = exports.intersection = exports.propertyIsnt = exports.isntBy = exports.isnt = exports.propertyIs = exports.isBy = exports.is = exports.uniqueByProperty = exports.unique = exports.uniqueBy = exports.duplicatesByProperty = exports.duplicates = exports.duplicatesBy = exports.or = exports.isDefined = void 0; | ||
exports.isDefined = function (x) { | ||
return typeof x !== "undefined"; | ||
}; | ||
exports.or = function (fallback) { return function (x) { | ||
return exports.isDefined(x) ? x : fallback; | ||
}; }; | ||
var findIndex = function (list, pred) { | ||
@@ -66,2 +69,3 @@ for (var i = 0; i < list.length; i++) { | ||
}; }; | ||
exports.byValue = function (a, b) { return (a < b ? -1 : a > b ? 1 : 0); }; | ||
exports.byProperty = function (key) { return exports.by(get(key)); }; | ||
@@ -98,5 +102,16 @@ function get(key1, key2, key3) { | ||
var _a; | ||
var groupName = func(el), group = acc[groupName] || []; | ||
var groupName = func(el); | ||
if (!groupName) | ||
return acc; | ||
var group = acc[groupName] || []; | ||
return Object.assign({}, acc, (_a = {}, _a[groupName] = group.concat(el), _a)); | ||
}; }; | ||
exports.groupByProperty = function (key) { return function (acc, el) { | ||
var _a; | ||
var groupName = el[key]; | ||
if (!groupName) | ||
return acc; | ||
var group = acc[groupName] || []; | ||
return Object.assign({}, acc, (_a = {}, _a[groupName] = group.concat(el), _a)); | ||
}; }; | ||
exports.partition = function (func) { return function (acc, el) { | ||
@@ -103,0 +118,0 @@ var a0 = acc[0] || [], a1 = acc[1] || []; |
{ | ||
"name": "list-fns", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A collection of utility functions to be used with .map, .filter, .sort and .reduce", | ||
@@ -36,4 +36,4 @@ "main": "dist/index.js", | ||
"ts-node": "^8.10.2", | ||
"typescript": "^3.9.7" | ||
"typescript": "^4.0.5" | ||
} | ||
} |
119
README.md
@@ -56,2 +56,3 @@ # list-fns | ||
<li><a href="#byProperty">byProperty</a></li> | ||
<li><a href="#byValue">byValue</a></li> | ||
<li><a href="#countBy">countBy</a></li> | ||
@@ -66,2 +67,3 @@ <li><a href="#duplicates">duplicates</a></li> | ||
<li><a href="#groupBy">groupBy</a></li> | ||
<li><a href="#groupByProperty">groupByProperty</a></li> | ||
<li><a href="#intersection">intersection</a></li> | ||
@@ -85,2 +87,3 @@ <li><a href="#intersectionBy">intersectionBy</a></li> | ||
<li><a href="#minByProperty">minByProperty</a></li> | ||
<li><a href="#or">or</a></li> | ||
<li><a href="#partition">partition</a></li> | ||
@@ -166,2 +169,32 @@ <li><a href="#propertyIs">propertyIs</a></li> | ||
### <div id="byValue"></div> byValue | ||
```ts | ||
byValue: (a: number, b: number) => 0 | 1 | -1 | ||
``` | ||
Use with: `sort` | ||
Sort a list of numbers. This is useful because javascript sorts numbers as string, meaning that [25, 100] results in [100, 25] since "2" is greater than "1" | ||
```ts | ||
[100, 25].sort(); // Returns [100, 25] | ||
[100, 25].sort(byValue); // Returns [25, 100] | ||
``` | ||
<details> | ||
<summary>Implementation</summary> | ||
<p> | ||
```ts | ||
const byValue = (a: number, b: number) => (a < b ? -1 : a > b ? 1 : 0) | ||
``` | ||
<p> | ||
</details> | ||
### <div id="countBy"></div> countBy | ||
@@ -443,3 +476,3 @@ | ||
```ts | ||
groupBy: <K extends string, V>(func: (el: V) => K) => (acc: Record<K, V[]>, el: V) => Record<K, V[]> | ||
groupBy: <K extends string, V>(func: (el: V) => K | undefined) => (acc: Record<K, V[]>, el: V) => Record<K, V[]> | ||
``` | ||
@@ -466,8 +499,8 @@ | ||
```ts | ||
const groupBy = <K extends string, V>(func: (el: V) => K) => ( | ||
acc: Record<K, V[]>, | ||
el: V | ||
): Record<K, V[]> => { | ||
const groupName = func(el), | ||
group: V[] = acc[groupName] || []; | ||
const groupBy = <K extends string, V>( | ||
func: (el: V) => K | undefined | ||
) => (acc: Record<K, V[]>, el: V): Record<K, V[]> => { | ||
const groupName = func(el); | ||
if (!groupName) return acc; | ||
const group: V[] = acc[groupName] || []; | ||
return Object.assign({}, acc, { [groupName]: group.concat(el) }); | ||
@@ -480,2 +513,44 @@ } | ||
### <div id="groupByProperty"></div> groupByProperty | ||
```ts | ||
groupByProperty: <K extends keyof V, V extends { [key: string]: any; }>(key: K) => (acc: Record<V[K], V[]>, el: V) => Record<V[K], V[]> | ||
``` | ||
Use with: `reduce` | ||
Given a property name, returns an object of lists of elements, grouped by the values for that property. A second argument must be passed to `reduce` . For javascript an empty object is enough. For typescript an object with properties or a type cast is required. | ||
```ts | ||
[{ name: "Jane" }, { name: "John" }].reduce( | ||
groupByProperty("name"), | ||
{} | ||
); // Returns { Jane: [{ name: "Jane" }], John: [{ name: "John" }] } | ||
``` | ||
<details> | ||
<summary>Implementation</summary> | ||
<p> | ||
```ts | ||
const groupByProperty = < | ||
K extends keyof V, | ||
V extends { [key: string]: any } | ||
>( | ||
key: K | ||
) => (acc: Record<V[K], V[]>, el: V): Record<V[K], V[]> => { | ||
const groupName = el[key]; | ||
if (!groupName) return acc; | ||
const group: V[] = acc[groupName] || []; | ||
return Object.assign({}, acc, { [groupName]: group.concat(el) }); | ||
} | ||
``` | ||
<p> | ||
</details> | ||
### <div id="intersection"></div> intersection | ||
@@ -1043,2 +1118,32 @@ | ||
### <div id="or"></div> or | ||
```ts | ||
or: <T>(fallback: T) => (x: T | undefined) => T | ||
``` | ||
Use with: `map` | ||
Replaces list elements that are `undefined` with `fallback` | ||
```ts | ||
[1, undefined, 2].map(or(0)); // Returns [1, 0, 2] | ||
``` | ||
<details> | ||
<summary>Implementation</summary> | ||
<p> | ||
```ts | ||
const or = <T>(fallback: T) => (x: T | undefined): T => | ||
isDefined(x) ? x : fallback | ||
``` | ||
<p> | ||
</details> | ||
### <div id="partition"></div> partition | ||
@@ -1045,0 +1150,0 @@ |
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
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
52644
514
1517
0