Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
array-fp-utils
Advanced tools
Array utilities for functional programming in TypeScript.
npm install array-fp-utils
import { pipe } from 'fp-ts/lib/function';
import { groupBy } from 'array-fp-utils';
const arr = [
{
key: 2,
score: 2,
},
{
key: 1,
score: 0.5,
},
{
key: 1,
score: 1,
},
];
// group by key, while maintaining array item order
const groupedArr = pipe(
arr,
groupBy((item) => item.key)
);
// groupedArr now contains the following
// [
// [
// {
// "key": 2,
// "score": 2,
// },
// ],
// [
// {
// "key": 1,
// "score": 0.5,
// },
// {
// "key": 1,
// "score": 1,
// },
// ],
// ]
import { pipe } from 'fp-ts/lib/function';
import { intersect } from 'array-fp-utils';
const arr = ['a', 'b', 'c', 'd', 'e'];
const otherArr = ['b', 'c', 'f'];
// calculates the intersection of two arrays
const intersection = pipe(arr, intersect(otherArr)); // returns ['b', 'c']
import { pipe } from 'fp-ts/lib/function';
import { intersectWith } from 'array-fp-utils';
const arr = [
{ foo: 'a' },
{ foo: 'b' },
{ foo: 'c' },
{ foo: 'd' },
{ foo: 'e' },
];
const otherArr = [{ bar: 'b' }, { bar: 'c' }, { bar: 'f' }];
// calculate the intersection of two arrays using comparator function
const arr = pipe(
mockData,
intersectWith(otherArr, (item, otherItem) => item.foo === otherItem.bar)
);
// arr = [{ foo: 'b' }, { foo: 'c' }]
import { pipe } from 'fp-ts/lib/function';
import { isDistinctArray } from 'array-fp-utils';
pipe([1, 2, 3], isDistinctArray); // returns true
pipe([1, 1, 2, 3], isDistinctArray); // returns false
Indicates whether an array contains the same set of items with another array, irrelevant of position (sorting).
Note: only accepts arrays of primitive values.
import { pipe } from 'fp-ts/lib/function';
import { isSameValueSet } from 'array-fp-utils';
pipe([1, 2, 3], isSameValueSet([3, 2, 1])); // returns true
pipe([1, 2], isSameValueSet([1, 2, 3])); // returns false
pipe([1, 2, 3], isSameValueSet([1, 2, 4])); // returns false
Indicates whether an array contains the same set of items with another array, irrelevant of position (sorting), using a comparator function.
import { pipe } from 'fp-ts/lib/function';
import { isSameValueSetWith } from 'array-fp-utils';
pipe(
[{ key: 1 }, { key: 2 }, { key: 3 }],
isSameValueSetWith([3, 2, 1], (value, otherValue) => value.key === otherValue)
); // returns true
import { pipe } from 'fp-ts/lib/function';
import { isSubsetOf } from 'array-fp-utils';
const arr = ['a', 'b', 'c', 'd'];
const isSubset = pipe(['b', 'd'], isSubsetOf(arr)); // returns true
const isSubset = pipe(['a', 'e'], isSubsetOf(arr)); // returns false
import { pipe } from 'fp-ts/lib/function';
import { isSubsetOfWith } from 'array-fp-utils';
const arr = [
{ foo: 'a' },
{ foo: 'b' },
{ foo: 'c' },
{ foo: 'd' },
{ foo: 'e' },
];
const otherArr = [{ bar: 'b' }, { bar: 'c' }, { bar: 'f' }];
const isSubset = pipe(
arr,
isSubsetOfWith(otherArr, (item, otherItem) => item.foo === otherItem.bar)
); // returns false since "f" does not exist in arr
import { pipe } from 'fp-ts/lib/function';
import { unique } from 'array-fp-utils';
const countries = [
'Greece',
'Greece',
'Greece',
'United States',
'United Kingdom'
'United States',
];
// returns ['Greece', 'United Kingdom', 'United State']
const uniqueArray = pipe(countries, unique);
import { pipe } from 'fp-ts/lib/function';
import { uniqueBy } from 'array-fp-utils';
const users = [
{
id: 1,
name: 'John'
},
{
id: 2,
name: 'Michael'
}
{
id: 3,
name: 'Michael'
}
]
// removes duplicate values from array in FiFo
// returns [{id:1, name: 'John'}, {id:2, name: 'Michael'}]
const uniqueArray = pipe(
users,
uniqueBy((user) => user.name)
)
This library is a collection of array utils, written with FP principles. They focus on performance instead of compatibility.
The alternative would be to use a library like lodash
. However, lodash
in an attempt to be backwards compatible with older browsers, falls behind on performance. In addition, lodash/groupBy
changes the initial order of the array items.
Source code contributions are most welcome. Please open a PR, ensure the linter is satisfied and all tests pass.
Causaly is building the world's largest biomedical knowledge platform, using technologies such as TypeScript, React and Node.js. Find out more about our openings at https://apply.workable.com/causaly/.
MIT
0.6.1
FAQs
Array utilities for functional programming in TypeScript.
The npm package array-fp-utils receives a total of 248 weekly downloads. As such, array-fp-utils popularity was classified as not popular.
We found that array-fp-utils demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.