Comparing version 0.2.0 to 0.3.0
# Changelog | ||
## 0.3.0 | ||
- Adds `duplicates`, `duplicatesBy` and `duplicatesByProperty` | ||
## 0.2.0 | ||
- Adds `countBy` |
@@ -11,2 +11,26 @@ /** Use with: `filter` | ||
* | ||
* Returns all duplicates (compared by the provided function) | ||
```ts | ||
[{ a: 1 }, { a : 1 }, { a: 2 }].filter(duplicatesBy(el => el.a)); // Returns [{ a: 1 }, { a: 1 }] | ||
``` | ||
*/ | ||
export declare const duplicatesBy: <T>(func: (el: T) => unknown) => (el: T, _: number, list: T[]) => boolean; | ||
/** Use with: `filter` | ||
* | ||
* Returns duplicates | ||
```ts | ||
[1, 1, 1, 2].filter(duplicates); // Returns [1, 1, 1] | ||
``` | ||
*/ | ||
export declare const duplicates: (el: unknown, _: number, list: unknown[]) => boolean; | ||
/** Use with: `filter` | ||
* | ||
* Returns duplicates by comparing the `key` property of the elements | ||
```ts | ||
[{ a: 1 }, { a: 1 }].filter(duplicatesByProperty('a')); // Return [{ a: 1 }, { a: 1 }] | ||
``` | ||
*/ | ||
export declare const duplicatesByProperty: <TObject extends object, TKey extends keyof TObject>(key: TKey) => (el: TObject, _: number, list: TObject[]) => boolean; | ||
/** Use with: `filter` | ||
* | ||
* Removes duplicates by comparing elements according to the provided function | ||
@@ -13,0 +37,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.excludeByProperty = exports.excludeBy = exports.exclude = exports.intersectionByProperty = exports.intersectionBy = exports.intersection = exports.uniqueByProperty = exports.unique = exports.uniqueBy = exports.isDefined = void 0; | ||
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.excludeByProperty = exports.excludeBy = exports.exclude = exports.intersectionByProperty = exports.intersectionBy = exports.intersection = exports.uniqueByProperty = exports.unique = exports.uniqueBy = exports.duplicatesByProperty = exports.duplicates = exports.duplicatesBy = exports.isDefined = void 0; | ||
exports.isDefined = function (x) { | ||
return typeof x !== "undefined"; | ||
}; | ||
var findIndex = function (list, pred) { | ||
for (var i = 0; i < list.length; i++) { | ||
if (pred(list[i])) | ||
return i; | ||
} | ||
return -1; | ||
}; | ||
var numberOfOccurencesBy = function (list, el, map) { | ||
var n = 0; | ||
for (var i = 0; i < list.length; i++) { | ||
if (map(list[i]) === map(el)) | ||
n++; | ||
} | ||
return n; | ||
}; | ||
exports.duplicatesBy = function (func) { return function (el, _, list) { return numberOfOccurencesBy(list, el, func) > 1; }; }; | ||
exports.duplicates = exports.duplicatesBy(function (el) { return el; }); | ||
exports.duplicatesByProperty = function (key) { return exports.duplicatesBy(get(key)); }; | ||
exports.uniqueBy = function (func) { return function (el, index, list) { return index === findIndex(list, function (t) { return func(t) === func(el); }); }; }; | ||
@@ -47,9 +65,2 @@ exports.unique = exports.uniqueBy(function (el) { return el; }); | ||
exports.sumByProperty = function (key) { return function (acc, el) { return acc + el[key]; }; }; | ||
var findIndex = function (list, pred) { | ||
for (var i = 0; i < list.length; i++) { | ||
if (pred(list[i])) | ||
return i; | ||
} | ||
return -1; | ||
}; | ||
exports.max = function (acc, el) { return Math.max(acc, el); }; | ||
@@ -56,0 +67,0 @@ exports.maxBy = function (func) { return function (acc, el) { |
{ | ||
"name": "list-fns", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "A collection of utility functions to be used with .map, .filter, .sort and .reduce", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
101
README.md
@@ -7,3 +7,3 @@ # list-fns | ||
This library contains higher order functions for doing common list operations to enable a more declarative style when working with lists. | ||
This library contains higher order functions for doing common list operations to enable a more declarative style when working with lists. File size is prioritized over performance (several of the functions are O(n^2)); this is not recommended for use with very large datasets. | ||
@@ -58,2 +58,5 @@ **Example** | ||
<li><a href="#countBy">countBy</a></li> | ||
<li><a href="#duplicates">duplicates</a></li> | ||
<li><a href="#duplicatesBy">duplicatesBy</a></li> | ||
<li><a href="#duplicatesByProperty">duplicatesByProperty</a></li> | ||
<li><a href="#exclude">exclude</a></li> | ||
@@ -180,2 +183,98 @@ <li><a href="#excludeBy">excludeBy</a></li> | ||
### <div id="duplicates"></div> duplicates | ||
```ts | ||
duplicates: (el: unknown, _: number, list: unknown[]) => boolean | ||
``` | ||
Use with: `filter` | ||
Returns duplicates | ||
```ts | ||
[1, 1, 1, 2].filter(duplicates); // Returns [1, 1, 1] | ||
``` | ||
<details> | ||
<summary>Implementation</summary> | ||
<p> | ||
```ts | ||
const duplicates = duplicatesBy(el => el) | ||
``` | ||
<p> | ||
</details> | ||
### <div id="duplicatesBy"></div> duplicatesBy | ||
```ts | ||
duplicatesBy: <T>(func: (el: T) => unknown) => (el: T, _: number, list: T[]) => boolean | ||
``` | ||
Use with: `filter` | ||
Returns all duplicates (compared by the provided function) | ||
```ts | ||
[{ a: 1 }, { a : 1 }, { a: 2 }].filter(duplicatesBy(el => el.a)); // Returns [{ a: 1 }, { a: 1 }] | ||
``` | ||
<details> | ||
<summary>Implementation</summary> | ||
<p> | ||
```ts | ||
const duplicatesBy = <T>(func: (el: T) => unknown) => ( | ||
el: T, | ||
_: number, | ||
list: T[] | ||
) => numberOfOccurencesBy(list, el, func) > 1 | ||
``` | ||
<p> | ||
</details> | ||
### <div id="duplicatesByProperty"></div> duplicatesByProperty | ||
```ts | ||
duplicatesByProperty: <TObject extends object, TKey extends keyof TObject>(key: TKey) => (el: TObject, _: number, list: TObject[]) => boolean | ||
``` | ||
Use with: `filter` | ||
Returns duplicates by comparing the `key` property of the elements | ||
```ts | ||
[{ a: 1 }, { a: 1 }].filter(duplicatesByProperty('a')); // Return [{ a: 1 }, { a: 1 }] | ||
``` | ||
<details> | ||
<summary>Implementation</summary> | ||
<p> | ||
```ts | ||
const duplicatesByProperty = < | ||
TObject extends object, | ||
TKey extends keyof TObject | ||
>( | ||
key: TKey | ||
) => duplicatesBy<TObject>(get(key)) | ||
``` | ||
<p> | ||
</details> | ||
### <div id="exclude"></div> exclude | ||
@@ -182,0 +281,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
35345
333
1008