Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

list-fns

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

list-fns - npm Package Compare versions

Comparing version 1.3.0 to 1.3.1

12

CHANGELOG.md
# Changelog
## 1.3.1
- Updates readme
- Improves performance of
- `duplicates`
- `duplicatesBy`
- `duplicatesByProperty`
- `groupBy`
- `groupByMany`
- `groupByProperty`
- `partition`
## 1.3.0

@@ -4,0 +16,0 @@

6

dist/index.d.ts

@@ -364,3 +364,3 @@ /** Use with: `filter`

*
* Given a key-returning function, returns an object of lists of elements. 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.
* Given a key-returning function, returns the elements grouped in an object according to the returned keys. 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 may be required.
```ts

@@ -376,3 +376,3 @@ [{ age: 10 }, { age: 80 }].reduce(

*
* Given a function `func` that returns a list of keys, returns an object of lists of elements. Works like `groupBy` except that `func` should return a list of keys. Good for grouping objects by properties that are arrays. An empty object must be passed as the second argument to `reduce`
* Given a function `func` that returns a list of keys, returns an object containing the elements grouped by the returned keys. Unlike the `groupBy` function, elements can appear several times in this object. Good for grouping objects by properties that are arrays. An empty object must be passed as the second argument to `reduce`
```ts

@@ -389,3 +389,3 @@ const b1: B = { items: ["a", "b"] };

*
* 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.
* Given a property name, returns an object containing the 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 may be required.
```ts

@@ -392,0 +392,0 @@ [{ name: "Jane" }, { name: "John" }].reduce(

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.countBy = exports.partition = exports.groupByProperty = exports.groupByMany = 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.has = 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) {
var isDefined = function (x) {
return typeof x !== "undefined";
};
exports.or = function (fallback) { return function (x) {
exports.isDefined = isDefined;
var or = function (fallback) { return function (x) {
return exports.isDefined(x) ? x : fallback;
}; };
exports.or = or;
var findIndex = function (list, pred) {

@@ -17,27 +19,38 @@ for (var i = 0; i < list.length; i++) {

};
var numberOfOccurencesBy = function (list, el, map) {
var duplicatesBy = function (func) { return function (el, _, list) {
var n = 0;
for (var i = 0; i < list.length; i++) {
if (map(list[i]) === map(el))
if (n >= 2)
return true;
if (func(list[i]) === func(el))
n++;
}
return n;
};
exports.duplicatesBy = function (func) { return function (el, _, list) { return numberOfOccurencesBy(list, el, func) > 1; }; };
return false;
}; };
exports.duplicatesBy = duplicatesBy;
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); }); }; };
var duplicatesByProperty = function (key) { return exports.duplicatesBy(get(key)); };
exports.duplicatesByProperty = duplicatesByProperty;
var uniqueBy = function (func) { return function (el, index, list) { return index === findIndex(list, function (t) { return func(t) === func(el); }); }; };
exports.uniqueBy = uniqueBy;
exports.unique = exports.uniqueBy(function (el) { return el; });
exports.uniqueByProperty = function (key) { return exports.uniqueBy(get(key)); };
exports.is = function (value) { return function (el) { return el === value; }; };
exports.isBy = function (func, value) { return function (el) {
var uniqueByProperty = function (key) { return exports.uniqueBy(get(key)); };
exports.uniqueByProperty = uniqueByProperty;
var is = function (value) { return function (el) { return el === value; }; };
exports.is = is;
var isBy = function (func, value) { return function (el) {
return func(el) === value;
}; };
exports.propertyIs = function (key, value) { return exports.isBy(get(key), value); };
exports.isnt = function (value) { return function (el) { return el !== value; }; };
exports.isntBy = function (func, value) { return function (el) {
exports.isBy = isBy;
var propertyIs = function (key, value) { return exports.isBy(get(key), value); };
exports.propertyIs = propertyIs;
var isnt = function (value) { return function (el) { return el !== value; }; };
exports.isnt = isnt;
var isntBy = function (func, value) { return function (el) {
return func(el) !== value;
}; };
exports.propertyIsnt = function (key, value) { return exports.isntBy(get(key), value); };
exports.has = function () {
exports.isntBy = isntBy;
var propertyIsnt = function (key, value) { return exports.isntBy(get(key), value); };
exports.propertyIsnt = propertyIsnt;
var has = function () {
var keys = [];

@@ -51,25 +64,36 @@ for (var _i = 0; _i < arguments.length; _i++) {

};
exports.intersection = function (list) { return function (el) {
exports.has = has;
var intersection = function (list) { return function (el) {
return findIndex(list, function (a) { return a === el; }) !== -1;
}; };
exports.intersectionBy = function (func, list) { return function (el) { return findIndex(list, function (a) { return func(a) === func(el); }) !== -1; }; };
exports.intersectionByProperty = function (key, list) { return exports.intersectionBy(get(key), list); };
exports.intersection = intersection;
var intersectionBy = function (func, list) { return function (el) { return findIndex(list, function (a) { return func(a) === func(el); }) !== -1; }; };
exports.intersectionBy = intersectionBy;
var intersectionByProperty = function (key, list) { return exports.intersectionBy(get(key), list); };
exports.intersectionByProperty = intersectionByProperty;
exports.isOneOf = exports.intersection;
exports.isOneOfBy = function (func, list) { return function (el) {
var isOneOfBy = function (func, list) { return function (el) {
return findIndex(list, function (a) { return a === func(el); }) !== -1;
}; };
exports.propertyIsOneOf = function (key, list) { return exports.isOneOfBy(get(key), list); };
exports.exclude = function (list) { return function (el) {
exports.isOneOfBy = isOneOfBy;
var propertyIsOneOf = function (key, list) { return exports.isOneOfBy(get(key), list); };
exports.propertyIsOneOf = propertyIsOneOf;
var exclude = function (list) { return function (el) {
return findIndex(list, function (a) { return a === el; }) === -1;
}; };
exports.excludeBy = function (func, list) { return function (el) {
exports.exclude = exclude;
var excludeBy = function (func, list) { return function (el) {
return findIndex(list, function (a) { return func(a) === func(el); }) === -1;
}; };
exports.excludeByProperty = function (key, list) { return exports.excludeBy(get(key), list); };
exports.excludeBy = excludeBy;
var excludeByProperty = function (key, list) { return exports.excludeBy(get(key), list); };
exports.excludeByProperty = excludeByProperty;
exports.isntOneOf = exports.exclude;
exports.isntOneOfBy = function (func, list) { return function (el) {
var isntOneOfBy = function (func, list) { return function (el) {
return findIndex(list, function (a) { return a === func(el); }) === -1;
}; };
exports.propertyIsntOneOf = function (key, list) { return exports.isntOneOfBy(get(key), list); };
exports.by = function (func) { return function (a, b) {
exports.isntOneOfBy = isntOneOfBy;
var propertyIsntOneOf = function (key, list) { return exports.isntOneOfBy(get(key), list); };
exports.propertyIsntOneOf = propertyIsntOneOf;
var by = function (func) { return function (a, b) {
var A = func(a), B = func(b);

@@ -80,4 +104,7 @@ if (typeof A === "boolean")

}; };
exports.byValue = function (a, b) { return (a < b ? -1 : a > b ? 1 : 0); };
exports.byProperty = function (key) { return exports.by(get(key)); };
exports.by = by;
var byValue = function (a, b) { return (a < b ? -1 : a > b ? 1 : 0); };
exports.byValue = byValue;
var byProperty = function (key) { return exports.by(get(key)); };
exports.byProperty = byProperty;
function get(key1, key2, key3) {

@@ -93,3 +120,4 @@ return function (obj) {

exports.get = get;
exports.sum = function (acc, element) { return acc + element; };
var sum = function (acc, element) { return acc + element; };
exports.sum = sum;
function sumBy(func) {

@@ -101,42 +129,62 @@ return function (acc, el) {

exports.sumBy = sumBy;
exports.sumByProperty = function (key) { return function (acc, el) { return acc + el[key]; }; };
exports.max = function (acc, el) { return Math.max(acc, el); };
exports.maxBy = function (func) { return function (acc, el) {
var sumByProperty = function (key) { return function (acc, el) { return acc + el[key]; }; };
exports.sumByProperty = sumByProperty;
var max = function (acc, el) { return Math.max(acc, el); };
exports.max = max;
var maxBy = function (func) { return function (acc, el) {
return func(el) > func(acc) ? el : acc;
}; };
exports.maxByProperty = function (key) { return function (acc, el) { return (el[key] > acc[key] ? el : acc); }; };
exports.min = function (acc, el) { return Math.min(acc, el); };
exports.minBy = function (func) { return function (acc, el) {
exports.maxBy = maxBy;
var maxByProperty = function (key) { return function (acc, el) { return (el[key] > acc[key] ? el : acc); }; };
exports.maxByProperty = maxByProperty;
var min = function (acc, el) { return Math.min(acc, el); };
exports.min = min;
var minBy = function (func) { return function (acc, el) {
return func(el) < func(acc) ? el : acc;
}; };
exports.minByProperty = function (key) { return function (acc, el) { return (el[key] < acc[key] ? el : acc); }; };
exports.groupBy = function (func) { return function (acc, el) {
var _a;
exports.minBy = minBy;
var minByProperty = function (key) { return function (acc, el) { return (el[key] < acc[key] ? el : acc); }; };
exports.minByProperty = minByProperty;
var groupBy = function (func) { return function (acc, el) {
var groupName = func(el);
if (!groupName)
return acc;
var group = acc[groupName] || [];
return Object.assign({}, acc, (_a = {}, _a[groupName] = group.concat(el), _a));
if (!acc[groupName])
acc[groupName] = [];
acc[groupName].push(el);
return acc;
}; };
exports.groupByMany = function (func) { return function (acc, el) {
exports.groupBy = groupBy;
var groupByMany = function (func) { return function (acc, el) {
var groupNames = func(el) || [];
groupNames.forEach(function (key) {
acc[key] = (acc[key] || []).concat(el);
if (!acc[key])
acc[key] = [];
acc[key].push(el);
});
return acc;
}; };
exports.groupByProperty = function (key) { return function (acc, el) {
var _a;
exports.groupByMany = groupByMany;
var groupByProperty = function (key) { return function (acc, el) {
var groupName = el[key];
if (!groupName)
return acc;
var group = acc[groupName] || [];
return Object.assign({}, acc, (_a = {}, _a[groupName] = group.concat(el), _a));
if (!acc[groupName])
acc[groupName] = [];
acc[groupName].push(el);
return acc;
}; };
exports.partition = function (func) { return function (acc, el) {
exports.groupByProperty = groupByProperty;
var partition = function (func) { return function (acc, el) {
var a0 = acc[0] || [], a1 = acc[1] || [];
return func(el) ? [a0.concat(el), a1] : [a0, a1.concat(el)];
if (func(el))
a0.push(el);
else
a1.push(el);
return [a0, a1];
}; };
exports.countBy = function (func) { return function (acc, el) {
exports.partition = partition;
var countBy = function (func) { return function (acc, el) {
return acc + (func(el) ? 1 : 0);
}; };
exports.countBy = countBy;
{
"name": "list-fns",
"version": "1.3.0",
"version": "1.3.1",
"description": "A collection of utility functions to be used with .map, .filter, .sort and .reduce",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -7,4 +7,6 @@ # list-fns

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.
This library contains higher order functions that simplify common list operations, similar to what you'd find in `lodash` or `ramda`. Unlike these libraries, `list-fns` is designed specifically to be used with the native array methods.
These functions have not been rigorously tested for performance so they are currently not recommended for use with large datasets.
**Example**

@@ -21,2 +23,3 @@

// Inline implementation:
people

@@ -29,2 +32,3 @@ .filter(

// With list functions:
people

@@ -42,3 +46,3 @@ .filter(uniqueByProperty("name"))

## Disclaimer about sorting
## A note about sorting

@@ -289,3 +293,10 @@ This library contains functions to be used with `[].sort()`. Always be mindful of the fact that `.sort()` and `.reverse()` will mutate the original list. If `.sort()` is the first method you're calling on a list you should probably clone it first in order to avoid unexpected behavior:

list: T[]
) => numberOfOccurencesBy(list, el, func) > 1
) => {
let n = 0;
for (let i = 0; i < list.length; i++) {
if (n >= 2) return true;
if (func(list[i]) === func(el)) n++;
}
return false;
}
```

@@ -485,3 +496,3 @@

Given a key-returning function, returns an object of lists of elements. 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.
Given a key-returning function, returns the elements grouped in an object according to the returned keys. 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 may be required.

@@ -507,4 +518,5 @@ ```ts

if (!groupName) return acc;
const group: V[] = acc[groupName] || [];
return Object.assign({}, acc, { [groupName]: group.concat(el) });
if (!acc[groupName]) acc[groupName] = [];
acc[groupName].push(el);
return acc;
}

@@ -526,3 +538,3 @@ ```

Given a function `func` that returns a list of keys, returns an object of lists of elements. Works like `groupBy` except that `func` should return a list of keys. Good for grouping objects by properties that are arrays. An empty object must be passed as the second argument to `reduce`
Given a function `func` that returns a list of keys, returns an object containing the elements grouped by the returned keys. Unlike the `groupBy` function, elements can appear several times in this object. Good for grouping objects by properties that are arrays. An empty object must be passed as the second argument to `reduce`

@@ -549,3 +561,4 @@ ```ts

groupNames.forEach(key => {
acc[key] = (acc[key] || []).concat(el);
if (!acc[key]) acc[key] = [];
acc[key].push(el);
});

@@ -569,3 +582,3 @@ return acc;

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.
Given a property name, returns an object containing the 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 may be required.

@@ -594,4 +607,5 @@ ```ts

if (!groupName) return acc;
const group: V[] = acc[groupName] || [];
return Object.assign({}, acc, { [groupName]: group.concat(el) });
if (!acc[groupName]) acc[groupName] = [];
acc[groupName].push(el);
return acc;
}

@@ -1261,3 +1275,5 @@ ```

a1 = acc[1] || [];
return func(el) ? [a0.concat(el), a1] : [a0, a1.concat(el)];
if (func(el)) a0.push(el);
else a1.push(el);
return [a0, a1];
}

@@ -1264,0 +1280,0 @@ ```

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc