Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Shades is a lodash inspired lens-like library.
(Psst! Don't want to learn about lenses? Start with the collection functions to see how you can clean up your Iterable code, or check out the magic of into
).
A lens is a path into an object, which can be used to extract its values, or even "modify" them in place (by creating a new object with the value changed).
When writing immutable code, we very commonly end up with deeply nested data stores, e.g.:
const store = {
users: [
{
name: 'Jack Sparrow',
posts: [
{
title: 'Why is the rum always gone? An analysis of Carribean trade surplus'
}
],
...
},
...
]
}
And updating a deeply nested structure will require heavy usage of the spread operator (or Object.assign
). E.g. To capitalize the title of the first post of the first user, you would write:
const userIdx = 0;
const postIdx = 0;
const capitalize = (string) => {...}
{...store,
users: store.users.map((user, idx) => (
idx === userIdx
? {...user,
posts: user.posts.map((post, idx) =>
idx === postIdx
? {
...post,
title: capitalize(post.title)
}
: post)
}
: user
))
}
This is an enormous amount of obfuscating boiler plate code for a very simple update.
With lenses, we could write this update much more declaratively:
mod('users' userIdx, 'posts', postIdx, 'title')(capitalize)(store);
If you're using TypeScript, you'll benefit from very robust type-checking. For example if we had typed the above as:
mod('users' userIdx, 'pots', postIdx, 'title')(capitalize)(store)
TS will error on store
because it doesn't have an attribute pots
. Similarly,
mod('users' userIdx, 'posts', postIdx, 'title')((x: number) => x + 1)(store);
will error because the type of title
is string
and not number
shades contains a little node playground that you can use to follow along with the guide or generally mess around with it.
You can run it with npx
(which you already have if you're running npm@^5.2.x
):
npx shades
Or the old fashioned way
npm install --global shades
shades
For reference, we will use the following objects:
const jack = {
name: 'Jack Sparrow',
goldMember: false,
posts: [
{
title:
'Why is the rum always gone? An analysis of Carribean trade surplus',
likes: 5
},
{
title: 'Sea Turtles - The Tortoise and the Hair',
likes: 70
}
]
};
const liz = {
name: 'Elizabeth Swan',
goldMember: true,
posts: [
{
title: 'Bloody Pirates - My Life Aboard the Black Pearl',
likes: 10000
},
{
title:
'Guidelines - When YOU need to be disinclined to acquiesce to their request',
likes: 5000
}
]
};
const bill = {
name: 'Bill Turner',
goldMember: false,
posts: [
{
title: 'Bootstraps Bootstraps - UEFI, GRUB and the Linux Kernel',
likes: 3000
}
]
};
const store = {
users: [jack, liz, bill],
byName: {
jack,
liz,
bill
}
};
Conceptually, a lens is something that represents a path through an object.
The simplest lens is a string or number path like 'name'
or 0
. Strings represent object properties and numbers represent Array or Object indexes.
get
is the simplest lens consumer. It takes a lens into an object and produces a function that will take an object and produce the focus of that lens (focus = final value referenced by the lens, i.e. name
or streetName
). Using the examples from above:
> const getName = get('name')
> getName(jack)
'Jack Sparrow'
or more succinctly:
> get('name')(jack)
'Jack Sparrow'
Multiple lenses can be passed in to get
and they will be composed left-to-right:
> get('users', 0, 'name')(store)
'Jack Sparrow'
This is all well and good, but that 0
is unrealistic. We rarely know which index of an array we need to edit. Thus we need a way to focus on multiple points in an array (or object)
This is where stuff starts to get interesting.
Traversals split the focus of lenses into multiple focus points. These can be particularly helpful when working with arrays.
The simplest traversal is all
. all
focuses on every element of an array (or every value in an object).
> get('users', all, 'posts')(store)
[
[ { title: 'Why is the rum always gone? An analysis of Carribean trade surplus', likes: 5} ],
[ { title: 'Bloody Pirates - My Life Aboard the Black Pearl', likes: 10000 } ]
]
Note: if you are using the TypeScript bindings, you MUST call all
as a function, e.g. get('users', all(), 'posts')
. It behaves exactly the same way.
Traversals can be used anywhere a lens is used. However, as you can see above, when all
appears in a composition, everything after is applied to every element of a collection, instead of on a single object. In this way, traversals act like prisms:
Multiple traversals can be composed into a single lens. Each traversal in the lens will result to a further level of nesting in the output
> get('users', all, 'posts', all, 'likes')(store)
[[5], [100000]]
Above, we focused on the users
key of the store, then for every user in the users
array we focused on the posts array, and then for every post in THAT array we focused on the likes
key.
all
will always produce an array in the output, and so we got an array for when we traversed over users
, and another nested array when we traversed over posts
. Pretty neat, huh?
get
ting data is all well and good, but where shades really shines is performing immutable updates. The good news is everything we have learned up until now translates seamlessly.
Meet mod
. mod
is a lot like get
: it accepts lenses and produces a function. The difference is, before we pass mod
an object to act on, we pass it a function that transforms the focus of the lens. Then we pass it an object, and instead of producing the focus of the object (like get
) it will produce a copy of the entire object, with the focus of the lens transformed by your function.
> const transformer = mod('users', 0, 'posts', 0, 'likes')(likes => likes + 1)
> transformer(store)
{
users: [
{
name: 'Jack Sparrow',
goldMember: false,
posts: [
{
title: 'Why is the rum always gone? An analysis of Carribean trade surplus',
likes: 6, // <---- Incremented!!
}
]
},
{ ... },
{ ... }
]
}
This transform was done immutably, so our original store
is unmodified.
mod
also works with traversals:
> mod('users', all, 'posts', all, 'likes')(likes => likes + 1)(store)
{
users: [
{
name: 'Jack Sparrow',
goldMember: false,
posts: [
{
title: 'Why is the rum always gone? An analysis of Carribean trade surplus',
likes: 6, // <---- Incremented!!
}
]
},
{
name: 'Elizabeth Swan',
goldMember: true,
posts: [
{
title: 'Bloody Pirates - My Life Aboard the Black Pearl',
likes: 10001, // <---- Also Incremented!! Wow!
}
]
},
{ ... }
]
}
Now you're ready to start cooking with gas! If you wanna see an even cooler traversal, check out matching
. Or just check out some of the API below, there's a
lot of really great stuff we didn't even get a chance to touch on.
It's not necessary to fully grok the type signatures when you read them, but if you do want to understand some of the custom types are, they can be found in types/utils.ts
export function into<Fn extends (...a: any[]) => any>(f: Fn): Fn;
export function into<Key extends string>(f: Key): <Obj extends HasKey<Key>>(s: Obj) => Obj[Key];
export function into<Pattern extends object>(p: Pattern): (o: HasPattern<Pattern>) => boolean;
into
is the engine of much of shades' magical goodness. It takes either a string or object
(or function) and turns it into a useful function. All of shades collection functions
will automatically pass their inputs into into
, creating a useful shorthand.
The transformation follows one of the following 3 rules:
get
has
. This one is the most interesting, and
requires some explanation.In the simplest form, a pattern of keys and values will produce a function that takes a test
value and returns true
if the given test value has at least the equivalent keys and values
of the pattern. Using the store example from above:
// Tests if an object passed to it has the key goldMember mapped to true
> const isGoldMember = into({goldMember: true})
> isGoldMember(jack)
false
// test multiple values
> into({goldMember: true, name: "Elizabeth Swan"})(liz)
true
Nested values work just as you'd expect:
> into({jack: {goldMember: false}})(store.byName)
true
Where it REALLY gets interesting is when the values in your pattern are predicate functions.
In this case, the value at that key in the test object is passed to the function, and validation
only continues if that function returns true
// Tests if the object passed to it has a title attribute that is less than 50 letters long
> const hasShortTitle = into({title: title => title.length < 50})
> hasShortTitle(jack.posts[0])
false
This pattern is especially useful with lenses and traversals
into('a')({a: 10}) // $ExpectType number
into('b')({a: 10}) // $ExpectError
into({a: 10})({a: 10}) // $ExpectType boolean
into({a: 10})({b: 10}) // $ExpectError
into((x: number) => x + 1)(10) // $ExpectType number
it('should use into to create functions', () => {
into('a')({ a: 10 }).should.equal(10);
into({ a: 10 })({ a: 10 }).should.be.true;
into(x => x + 1)(10).should.equal(11);
});
We all love Array::map
, Array::filter
, etc. but what do you do when you have an object, or a Map?
Even if you're just using arrays, defining an arrow function to just extract a property, or test if a
key has a certain value is clunky.
Enter shades. Shades provides collection functions that work polymorphically over many different object
types, and are powered by into
. (And they're pretty fast, too).
> map('name')(store.users)
['jack', 'liz', 'bill']
> map('goldMember')(store.byName)
{
jack: false,
liz: true,
bill: false
}
> filter({name: 'jack'})(store.users)
[jack]
export function filter<K extends string>(k: K): <A extends HasKey<K>, F extends Collection<A>>(f: F) => Functor<F, A, Unpack<F>>;
export function filter<A>(f: (a: A) => any): <F>(f: F) => Functor<F, A, A>;
export function filter<Pattern>(p: Pattern): <A extends HasPattern<Pattern>, F extends Collection<A>>(f: F) => Functor<F, A, Unpack<F>>;
Takes an into pattern from A => boolean
and produces a function that takes a Collection
and produces a collection of the same type, with all items that failed the test removed.
> filter(isEven)([1, 2, 3, 4])
[2, 4]
> filter((value, key) => isEven(key) && isOdd(value))({2: 1, 3: 1})
{2: 1}
> filter(isEven)(new Set([1, 2, 3, 4]))
Set({2, 4})
> filter('goldMember')(store.users)
[liz]
> filter({posts: includes({likes: lessThan(10)})})(store.users)
[jack]
filter((user: User) => user.friends.length > 0)(users); // $ExpectType User[]
filter((user: User) => user.name)(byName); // $ExpectType { [key: string]: User; }
filter('name')(users); // $ExpectType User[]
filter('name')(byName); // $ExpectType { [key: string]: User; }
filter('butts')(users); // $ExpectError
filter({ name: 'john' })(users); // $ExpectType User[]
filter({ name: 'john' })(byName); // $ExpectType { [key: string]: User; }
filter({
settings: (settings: string) => settings
})(users); // $ExpectError
filter({
settings: (settings: Settings) => settings
})(users); // $ExpectType User[]
it('should work on lists', () => {
filter(greaterThan(2))([1, 2, 3]).should.deep.equal([3]);
});
it('should work on objects', () => {
filter(greaterThan(2))({ a: 1, b: 2, c: 3 }).should.deep.equal({ c: 3 })
});
it('should work on Maps', () => {
filter('goldMember')(
new Map(Object.entries(store.byName))
).should.deep.equal(new Map([['liz', liz]]));
});
export function map<K extends string>(k: K): <F>(f: F) => KeyedFunctor<K, F>;
export function map(i: number): <F>(f: F) => IndexFunctor<F>;
export function map<A, B>(f: (a: A) => B): <F>(f: F) => Functor<F, A, B>;
export function map<Pattern>(p: Pattern): <A extends HasPattern<Pattern>, F extends Container<A>>(f: F) => Functor<F, A, boolean>;
Takes an into pattern from A => B
and produces a function that takes a Container
of A
s and produces the same type of container with B
s
> map(inc)([1, 2, 3, 4])
[2, 3, 4, 5]
> map((value, key) => `${value} was at {key}`)({a: 1, b: 2})
{a: '1 was at a', b: '2 was at b'}
> map((value, key) => `${value} was at {key}`)(new Map([['a', 1], ['b', 2]])
Map {a => '1 was at a', b => '2 was at b'}
> map('goldMember')(store.byName)
{jack: false, liz: true, bill: false}
> map({name: includes('Bill')})(store.users)
[false, false, true]
map('name')(users); // $ExpectType string[]
map('name')(byName); // $ExpectType { [key: string]: string; }
map('not-a-key')(users); // $ExpectType never
map('not-a-key')(byName); // $ExpectType never
const usersFriends = map('friends')(users); // $ExpectType User[][]
map(1)(usersFriends); // $ExpectType User[]
const usersFriendsByName = map('friends')(byName); // $ExpectType { [key: string]: User[]; }
map(2)(usersFriendsByName); // $ExpectType { [key: string]: User; }
map((x: User) => x.name)(users); // $ExpectType string[]
map({ name: 'john', settings: (settings: Settings) => !!settings })(users); // $ExpectType boolean[]
map({ name: 'john', settings: (settings: Settings) => !!settings })(byName); // $ExpectType { [key: string]: boolean; }
declare const fetchUsers: Promise<User[]>
// Nested maps require type annotations, but still provide safety
map<User[], string[]>(map('name'))(fetchUsers) // $ExpectType Promise<string[]>
// map<User[], boolean[]>(map('name'))(fetchUsers) // $ExpectError
declare const userMap: Map<string, User>
declare const userSet: Set<User>
map('name')(userMap) // $ExpectType Map<string, string>
map('name')(userSet) // $ExpectType Set<string>
it('should work on lists', () => {
map(inc)([1, 2, 3]).should.deep.equal([2, 3, 4])
});
it('should work on objects', () => {
map(inc)({ a: 1, b: 2, c: 3 }).should.deep.equal({ a: 2, b: 3, c: 4 })
})
it('should receive key as second param', () => {
map((value, key) => value + key)({a: 1}).should.deep.equal({a: '1a'})
})
it('should work on maps', () => {
const input = new Map([['a', 1], ['b', 2], ['c', 3]])
const output = new Map([['a', 2], ['b', 3], ['c', 4]])
map(inc)(input).should.deep.equal(output)
})
it('should work on sets', () => {
const input = new Set([1, 2, 3])
const output = new Set([2, 3, 4])
map(inc)(input).should.deep.equal(output)
})
it('should work on promises', () => {
const p = Promise.resolve({a: 1})
return map('a')(p).should.eventually.equal(1)
})
it('should work with shorthand', () => {
map('a')([{ a: 1 }, { a: 2 }, { a: 3 }]).should.deep.equal([1, 2, 3]);
map('a')({ d: { a: 1 }, c: { a: 2 }, e: { a: 3 } }).should.deep.equal({
d: 1,
c: 2,
e: 3
});
map({ a: 1 })([{ a: 1 }, { a: 2 }, { a: 3 }]).should.deep.equal([
true,
false,
false
]);
});
export function find<Key extends string>(f: Key): <A extends HasKey<Key>>(f: Collection<A>) => A | undefined;
export function find<A>(f: (a: A) => any): (f: Collection<A>) => A | undefined;
export function find<Pattern>(p: Pattern): <A extends HasPattern<Pattern>>(f: Collection<A>) => A | undefined;
Takes an into pattern from A => any
and produces a function that takes a
Collection
returns the first item in the collection that returns
a truthy value for the test (or undefined
if none match)
find('name')(users); // $ExpectedType User | undefined
find((user: User) => user.friends); // $ExpectedType User | undefined
find((user: User) => user.friends.length > 0)(users); // $ExpectType User | undefined
find({ name: 'barg' })(users); // $ExpectType User | undefined
find({ name: false })(users); // $ExpectError
find({ name: (s: string) => !!'barg' })(users); // $ExpectType User | undefined
find({ name: (s: Settings) => !!'barg' })(users); // $ExpectError
const a = find({
friends: find({ name: 'silent bob' })
})(users);
a; // $ExpectType User | undefined
find({ settings: { permissions: false } })(users); // $ExpectError
find({
settings: { permissions: false }
})(users); // $ExpectError
find({
settings: { permissions: (perm: string) => !!perm }
})(users); // ExpectType User | undefined
find({
settings: { permissions: (perm: boolean) => !!perm }
})(users); // $ExpectError
it('should work on lists', () => {
find(user => user.isLive)([
{ isLive: true, name: 'jack' }
]).name.should.equal('jack');
find('isLive')([{ isLive: true, name: 'jack' }]).name.should.equal(
'jack'
);
find({ name: 'jack' })([{ isLive: true, name: 'jack' }]).isLive.should
.be.true;
});
it('should work on objects', () => {
find(user => user.isLive)({
jack: { isLive: true, name: 'jack' }
}).name.should.equal('jack');
find('isLive')({
jack: { isLive: true, name: 'jack' }
}).name.should.equal('jack');
find({ name: 'jack' })({ jack: { isLive: true, name: 'jack' } }).isLive
.should.be.true;
});
it('should work on Maps', () => {
find('goldMember')(
new Map(Object.entries(store.byName))
).should.deep.equal(liz);
});
it('should work on Sets', () => {
find('goldMember')(
new Set(Object.values(store.byName))
).should.deep.equal(liz);
});
export function some<Key extends string>(f: Key): (f: Collection<HasKey<Key>>) => boolean;
export function some<A>(f: (a: A) => any): (f: Collection<A>) => boolean;
export function some<Pattern>(p: Pattern): (f: Collection<HasPattern<Pattern>>) => boolean;
Takes an into pattern and returns a function that takes a [Collection](#collection-type) and returns true if there is any member in the collection that returns
true` for the test
some('name')(users); // $ExpectedType boolean
some((user: User) => user.friends); // $ExpectedType boolean
some((user: User) => user.friends.length > 0)(users); // $ExpectType boolean
some({ name: 'barg' })(users); // $ExpectType boolean
some({ name: false })(users); // $ExpectError
some({ name: (s: string) => !!'barg' })(users); // $ExpectType boolean
some({ name: (s: boolean) => !!'barg' })(users); // $ExpectError
it('should work on lists', () => {
some(user => user.isLive)([
{ isLive: true, name: 'jack' }
]).should.be.true
some('isLive')([{ isLive: true, name: 'jack' }]).should.be.true
some({ name: 'jack' })([{ isLive: true, name: 'jack' }]).should.be.true
some({ name: 'john' })([{ isLive: true, name: 'jack' }]).should.be.false
some(user => user.isLive)([{ isLive: true, name: 'jack' }]).should.be.true
some(user => !user.isLive)([{ isLive: true, name: 'jack' }]).should.be.false
});
it('should work on objects', () => {
some(user => user.isLive)({
jack: { isLive: true, name: 'jack' }
}).should.be.true
some('isLive')({
jack: { isLive: true, name: 'jack' }
}).should.be.true
some({ name: 'jack' })({ jack: { isLive: true, name: 'jack' } }).should.be.true;
});
it('should work on Maps', () => {
some('goldMember')(
new Map(Object.entries(store.byName))
).should.be.true
});
it('should work on Sets', () => {
some('goldMember')(
new Set(store.users)
).should.be.true
some({name: s => s.includes('z')})(
new Set(store.users)
).should.be.true
some({name: s => s.includes('x')})(
new Set(store.users)
).should.be.false
});
export function cons<A>(a: A): (as: A[]) => A[]
Consumes an element x
and an array xs
and returns a new array with x
APPENDED to xs
(not prepended, which is more typical with cons
and lists. This
is to make it easier to use in pipelined scenarios)
cons(1)([1, 2, 3]); // $ExpectType number[]
cons('a')(['a', 'b', 'c']); // $ExpectType string[]
cons(1)(2); // $ExpectError
cons(1)(['a', 'b', 'c']); // $ExpectError
cons('1')([1, 2, 3]); // $ExpectError
it('should concat lists', () => {
cons(1)([1, 2, 3]).should.deep.equal([1, 2, 3, 1]);
expect(() => cons(1)(2)).to.throw(
'Invalid attempt to spread non-iterable instance'
);
});
export function first(s: string): string
export function first<A>(xs: A[]): A
Extracts the first element of a collection
first([1, 3, 4]); // $ExpectType number
first(users); // $ExpectType User
first('hi'); // $ExpectType string
first(true); // $ExpectError
it('should extract the first element', () => {
first([1, 2, 3]).should.equal(1);
first('hello').should.equal('h');
should.not.exist(first([]));
});
export function rest<A>(xs: A[]): A[]
Extracts everything from the list except for the head
rest([1, 3, 4]); // $ExpectType number[]
rest(users); // $ExpectType User[]
rest('hi'); // $ExpectError
rest(true); // $ExpectError
it('should extract the tail', () => {
rest([1, 2, 3]).should.deep.equal([2, 3]);
rest([]).should.deep.equal([]);
});
export function push<A>(a: A): (as: A[]) => A[]
Alias for cons
export function concat<A>(as: A[]): (bs: A[]) => A[]
Takes two arrays and concatenates the first on to the second.
concat([1, 2, 3])([2, 3]); // $ExpectType number[]
// [2, 3, 1, 2, 3]
concat(['hi'])(['wo']); // $ExpectType string[]
// ['wo', 'hi']
concat(['hi'])([1, 2, 3]); // $ExpectError
it('should concatenate lists in reverse order', () => {
concat([1, 2, 3])([2, 3]).should.deep.equal([2, 3, 1, 2, 3]);
})
export function append<A>(as: A[]): (bs: A[]) => A[]
Alias for concat
export function prepend<A>(as: A[]): (bs: A[]) => A[]
Takes two arrays and concatenates the second on to the first.
prepend([1, 2, 3])([2, 3]); // $ExpectType number[]
// [1, 2, 3, 2, 3]
prepend(['hi'])(['wo']); // $ExpectType string[]
// ['hi', 'wo']
prepend(['hi'])([1, 2, 3]); // $ExpectError
it('should concatenate lists in lexical order', () => {
prepend([1, 2, 3])([2, 3]).should.deep.equal([1, 2, 3, 2, 3]);
})
Reducer generators are functions that take into patterns
and produce specialized
reducer functions ((A, S) => A
):
> jack.posts.reduce(maxOf('likes'))
{
title: 'Sea Turtles - The Tortoise and the Hair',
likes: 70
}
export function maxOf<Key extends string>(k: Key): <Item extends HasKey<Key, number>>(acc: Item, current: Item) => Item
export function maxOf<A>(f: (a: A) => number): (acc: A, current: A) => A
A reducer generator that takes either a path or a getter function and producers a reducer that will find the element in the collection that has the max of that property
> [{a: 1}, {a: 3}, {a: 2}].reduce(maxOf('a'))
{ a: 3 }
> store.users.reduce(maxOf(user => user.name.length))
{ name: 'Elizabeth Swan', ...}
users[0].posts.reduce(maxOf('likes')) // $ExpectType Post
users[0].posts.reduce(maxOf('title')) // $ExpectError
users[0].posts.reduce(maxOf('farts')) // $ExpectError
users.reduce(maxOf(user => user.name.length)) // $ExpectType User
users.reduce(maxOf(user => user.name)) // $ExpectError
it('should find largest elements', () => {
store.users.reduce(maxOf(user => user.name.length)).should.be.equal(liz)
jack.posts.reduce(maxOf('likes')).likes.should.be.equal(70)
})
export function minOf<Key extends string>(k: Key): <Item extends HasKey<Key, number>>(acc: Item, current: Item) => Item
export function minOf<Item>(f: (a: Item) => number): (acc: Item, current: Item) => Item
The opposite of maxOf
.
export function findOf<Key extends string>(k: Key): <Item extends HasKey<Key>>(acc: Item, item: Item) => Item
export function findOf<Item>(f: (a: Item) => any): (acc: Item, current: Item) => Item
export function findOf<Pattern>(p: Pattern): <Item extends HasPattern<Pattern>>(acc: Item, item: Item) => Item
Takes an into pattern and produces a reducer that returns either the accumulated item or the current item if it passes the given test.
> store.users.reduce(findOf('goldMember'))
liz
> store.users.reduce(findOf({goldMember: false}))
jack
users.reduce(findOf('name')) // $ExpectType User
users.reduce(findOf({name: 'butt'})) // $ExpectType User
users.reduce(findOf({butt: 'name'})) // $ExpectError
users.reduce(findOf(user => user.name)) // $ExpectType User
users.reduce(findOf(user => user.butt)) // $ExpectError
users.map(findOf(user => user.butt)) // $ExpectError
it('finds elements given a pattern', () => {
store.users.reduce(findOf('name')).should.be.equal(store.users[0])
store.users.reduce(findOf({name: liz.name})).should.be.equal(liz)
})
export function sumOf<Key extends string>(k: Key): (acc: number, current: HasKey<Key, number>) => number
export function sumOf<A>(f: (a: A) => number): (acc: number, current: A) => number
A reducer generator that takes either a path or a getter function and producers a reducer that will sum all of the values produced by the getter
> [{a: 1}, {a: 3}, {a: 2}].reduce(sumOf('a'), 0)
6
> liz.posts.reduce(sumOf('likes'))
15000
users[0].posts.reduce(sumOf('likes'), 0) // $ExpectType number
users[0].posts.reduce(sumOf('title'), 0) // $ExpectError
users[0].posts.reduce(sumOf('farts'), 0) // $ExpectError
users.reduce(sumOf(user => user.name.length), 0) // $ExpectType number
users.reduce(sumOf(user => user.name), 0) // $ExpectError
it('should sum all elements specified by pattern', () => {
store.users.reduce(sumOf(user => user.name.length)).should.be.equal(37)
liz.posts.reduce(sumOf('likes')).should.be.equal(15000)
})
export function productOf<Key extends string>(k: Key): (acc: number, current: HasKey<Key, number>) => number
export function productOf<A>(f: (a: A) => number): (acc: number, current: A) => number
A reducer generator that takes either a path or a getter function and producers a reducer that will multiply all of the values produced by the getter
> [{a: 1}, {a: 30}, {a: 2}].reduce(productOf('a'), 1)
60
> liz.posts.reduce(productOf('likes'))
50000000
users[0].posts.reduce(productOf('likes'), 1) // $ExpectType number
users[0].posts.reduce(productOf('title'), 1) // $ExpectError
users[0].posts.reduce(productOf('farts'), 1) // $ExpectError
users.reduce(productOf(user => user.name.length), 1) // $ExpectType number
users.reduce(productOf(user => user.name), 1) // $ExpectError
it('should multiply all elements specified by pattern', () => {
store.users.reduce(productOf(user => user.name.length)).should.be.equal(1848)
liz.posts.reduce(productOf('likes')).should.be.equal(50000000)
})
export function identity<A>(a: A): A
Identity function. Not much to say about this one. You give it something, it gives it back. Nice easy no-op for higher order functions.
identity(10) // $ExpectType 10
identity("butts") // $ExpectType "butts"
it('just gives stuff back', () => {
identity(10).should.be.equal(10)
identity('hi').should.be.equal('hi')
})
export function flip<A, B, Out>(f: (a: A) => (b: B) => Out): (b: B) => (a: A) => Out
Takes a 2-curried function and flips the order of the arguments
> const lessThanEq = flip(greaterThanEq)
// Cards on the table this one does not type check with polymorphic
// functions very well. Rank-N type inference is hard to you might
// have to help it along
declare function numAndBool(a: number): (b: boolean) => boolean
flip(numAndBool) // $ExpectType (b: boolean) => (a: number) => boolean
flip<"hi", 7, "hi">(always)(7)("hi") // $ExpectType "hi"
flip<"hi", 7, 7>(always)(7)("hi") // $ExpectError
it('flips argument order', () => {
flip(lessThan)(3)(9).should.be.true
flip(sub)(1)(9).should.equal(-8)
})
export function always<A>(a: A): (b: any) => A
A constant function. This is particularly useful when you want to just produce a value, but are working with higher order functions that expect to call a function for a result.
always(10)(map) // $ExpectType number
always('10')(map) // $ExpectType string
always(10) // $ExpectType (b: any) => number
it('should be constant', () => {
const fifteen = always(15)
fifteen(20).should.be.equal(15)
fifteen('asdfasdf').should.be.equal(15)
})
export function not<Key extends string>(k: Key): (obj: HasKey<Key>) => boolean
export function not<A>(a: Fn1<A, any>): Fn1<A, boolean>;
export function not<A, B>(a: Fn2<A, B, any>): Fn2<A, B, boolean>;
export function not<A, B, C>(a: Fn3<A, B, C, any>): Fn3<A, B, C, boolean>;
export function not<A, B, C, D>(a: Fn4<A, B, C, D, any>): Fn4<A, B, C, D, boolean>;
export function not<A, B, C, D, E>(a: Fn5<A, B, C, D, E, any>): Fn5<A, B, C, D, E, boolean>;
export function not<Pattern>(p: Pattern): (obj: HasPattern<Pattern>) => boolean
A function level equivalent of the !
operator. It consumes a function or into pattern, and returns a
function that takes the same arguments, and returns the negation of the output
> const isOdd = not(isEven);
> isOdd(3)
true
> not('goldMember')(jack)
true
> not({name: "Jack Sparrow"})(liz)
true
declare function notFn1(a: number): string
declare function notFn4(a: number, b: string, c: boolean, d: number): string
not(notFn1) // $ExpectType Fn1<number, boolean>
not(notFn4) // $ExpectType Fn4<number, string, boolean, number, boolean>
not("name")(users[0]) // $ExpectType boolean
not("butt")(users[0]) // $ExpectError
it('should negate functions of various arities', () => {
const isEven = n => n % 2 == 0
const plus = (a, b) => a + b
not(isEven)(3).should.be.true
not(plus)(2, 3).should.be.false
not(plus)(2, -2).should.be.true
})
it('should handle shorthand', () => {
not('goldMember')(jack).should.be.true
not({name: 'Jack Sparrow'})(jack).should.be.false
})
export function and<A, Out>(a?: Fn1<A, Out>, b?: Fn1<A, Out>, c?: Fn1<A, Out>, d?: Fn1<A, Out>, e?: Fn1<A, Out>, f?: Fn1<A, Out>): Fn1<A, Out>
export function and<A, B, Out>(a?: Fn2<A, B, Out>, b?: Fn2<A, B, Out>, c?: Fn2<A, B, Out>, d?: Fn2<A, B, Out>, e?: Fn2<A, B, Out>, f?: Fn2<A, B, Out>): Fn2<A, B, Out>
export function and<A, B, C, Out>(a?: Fn3<A, B, C, Out>, b?: Fn3<A, B, C, Out>, c?: Fn3<A, B, C, Out>, d?: Fn3<A, B, C, Out>, e?: Fn3<A, B, C, Out>, f?: Fn3<A, B, C, Out>): Fn3<A, B, C, Out>
export function and<A, B, C, D, Out>(a?: Fn4<A, B, C, D, Out>, b?: Fn4<A, B, C, D, Out>, c?: Fn4<A, B, C, D, Out>, d?: Fn4<A, B, C, D, Out>, e?: Fn4<A, B, C, D, Out>, f?: Fn4<A, B, C, D, Out>): Fn4<A, B, C, D, Out>
export function and<A, B, C, D, E, Out>(a?: Fn5<A, B, C, D, E, Out>, b?: Fn5<A, B, C, D, E, Out>, c?: Fn5<A, B, C, D, E, Out>, d?: Fn5<A, B, C, D, E, Out>, e?: Fn5<A, B, C, D, E, Out>, f?: Fn5<A, B, C, D, E, Out>): Fn5<A, B, C, D, E, Out>
A function level equivalent of the &&
operator. It consumes an arbitrary number of
functions that take the same argument types and produce booleans, and returns a
single function that takes the same arguments, and returns a truthy value if all of
the functions are truthy (Return value mimics the behavior of &&
)
> and(isEven, greaterThan(3))(6)
true
> [42, 2, 63].filter(and(isEven, greaterThan(3)))
[42]
declare function andFn1(a: number): number
declare function andFn2(a: number, b: string): number
declare function andFn3(a: number, b: string, c: boolean): number
declare function andFn3Bad(a: number, b: string, c: boolean): boolean
and(andFn3, andFn3, andFn3) // $ExpectType Fn3<number, string, boolean, number>
and(andFn1, andFn2, andFn3) // $ExpectType Fn3<number, string, boolean, number>
and(andFn1, andFn2, identity) // $ExpectType Fn2<number, string, number>
and(andFn1) // $ExpectType Fn1<number, number>
and(andFn1, andFn2, andFn3Bad) // $ExpectError
const isEven = n => n % 2 == 0;
const isPositive = n => n > 0;
const plus = (a, b) => a + b
const lt = (a, b) => a < b
const gt = (a, b) => a > b
it('handles multiple functions', () => {
and(isEven, isPositive)(4).should.be.true;
and(isEven, isPositive)(3).should.be.false;
and(isEven, isPositive)(-1).should.be.false
})
it('handles functions with different arities', () => {
and(lt, isEven)(4, 9).should.be.true;
and(lt, isEven)(4, 9).should.be.true;
and(lt, isEven)(3, 9).should.be.false;
})
it('returns the final value or short circuits', () => {
and(isEven, plus)(4, 9).should.equal(13);
and(gt, isEven, plus)(3, 9).should.be.false;
and(lt, sub(3), isEven)(3, 9).should.equal(0);
})
it('execution stops after a false', () => {
const boomMsg = 'boom'
const boom = () => {throw new Error(boomMsg)}
and(always(false), boom)(false).should.be.false
expect(() => and(always(true), boom)(false)).throws(boomMsg)
})
export function or<A, Out>(a?: Fn1<A, Out>, b?: Fn1<A, Out>, c?: Fn1<A, Out>, d?: Fn1<A, Out>, e?: Fn1<A, Out>, f?: Fn1<A, Out>): Fn1<A, Out>
export function or<A, B, Out>(a?: Fn2<A, B, Out>, b?: Fn2<A, B, Out>, c?: Fn2<A, B, Out>, d?: Fn2<A, B, Out>, e?: Fn2<A, B, Out>, f?: Fn2<A, B, Out>): Fn2<A, B, Out>
export function or<A, B, C, Out>(a?: Fn3<A, B, C, Out>, b?: Fn3<A, B, C, Out>, c?: Fn3<A, B, C, Out>, d?: Fn3<A, B, C, Out>, e?: Fn3<A, B, C, Out>, f?: Fn3<A, B, C, Out>): Fn3<A, B, C, Out>
export function or<A, B, C, D, Out>(a?: Fn4<A, B, C, D, Out>, b?: Fn4<A, B, C, D, Out>, c?: Fn4<A, B, C, D, Out>, d?: Fn4<A, B, C, D, Out>, e?: Fn4<A, B, C, D, Out>, f?: Fn4<A, B, C, D, Out>): Fn4<A, B, C, D, Out>
export function or<A, B, C, D, E, Out>(a?: Fn5<A, B, C, D, E, Out>, b?: Fn5<A, B, C, D, E, Out>, c?: Fn5<A, B, C, D, E, Out>, d?: Fn5<A, B, C, D, E, Out>, e?: Fn5<A, B, C, D, E, Out>, f?: Fn5<A, B, C, D, E, Out>): Fn5<A, B, C, D, E, Out>
A function level equivalent of the ||
operator. It consumes an arbitrary number
of functions that take the same argument types and produce truthy values, and returns
a single function that takes the same arguments, and returns a truthy value if any of
the functions produce truthy values (Return value mimics the behavior of ||
)
> or(isEven, greaterThan(3))(5)
true
> or(isEven, greaterThan(3))(1)
false
declare function orFn1(a: number): number
declare function orFn2(a: number, b: string): number
declare function orFn3(a: number, b: string, c: boolean): number
declare function orFn3Bad(a: number, b: string, c: boolean): boolean
or(orFn3, orFn3, orFn3) // $ExpectType Fn3<number, string, boolean, number>
or(orFn1, orFn2, orFn3) // $ExpectType Fn3<number, string, boolean, number>
or(orFn1, orFn2, identity) // $ExpectType Fn2<number, string, number>
or(orFn1) // $ExpectType Fn1<number, number>
or(orFn1, orFn2, orFn3Bad) // $ExpectError
const isEven = n => n % 2 == 0;
const isPositive = n => n > 0;
const plus = (a, b) => a + b
const lt = (a, b) => a < b
const gt = (a, b) => a > b
it('handles multiple functions', () => {
or(isEven, isPositive)(4).should.be.true;
or(isEven, isPositive)(3).should.be.true;
or(isEven, isPositive)(-1).should.be.false
})
it('handles functions with different arities', () => {
or(lt, isEven)(4, 9).should.be.true;
or(lt, isEven)(4, 9).should.be.true;
or(lt, isEven)(3, 9).should.be.true;
or(lt, isEven)(3, 1).should.be.false;
})
it('returns the final value or short circuits', () => {
or(isEven, plus)(3, 9).should.equal(12);
or(gt, isEven, plus)(3, 9).should.equal(12)
or(lt, sub(3), isEven)(3, 9).should.be.true
})
it('execution stops after a true', () => {
const boomMsg = 'boom'
const boom = () => {throw new Error(boomMsg)}
or(always(true), boom)(false).should.be.true
expect(() => or(always(false), boom)(false)).throws(boomMsg)
})
export function has<Pattern>(p: Pattern): (obj: HasPattern<Pattern>) => boolean
has
takes a pattern and transforms it into a predicate function. In the simplest form, it takes a pattern of keys
and values and produces a function that takes a test value and returns true
if the given test value has at least
the equivalent keys and values of the pattern. Using the store example from above:
// Tests if an object passed to it has the key goldMember mapped to true
> const isGoldMember = has({goldMember: true})
> isGoldMember(jack)
false
// test multiple values
> has({goldMember: true, name: "Elizabeth Swan"})(liz)
true
Nested values work just as you'd expect:
> has({jack: {goldMember: false}})(store.byName)
true
Where it REALLY gets interesting is when the values in your pattern are predicate functions.
In this case, the value at that key in the test object is passed to the function, and validation
only continues if that function returns true
// Tests if the object passed to it has a title attribute that is less than 50 letters long
> const hasShortTitle = has({title: title => title.length < 50})
> hasShortTitle(jack.posts[0])
false
This pattern is especially useful with lenses and traversals
has({a: 1}) // $ExpectType (obj: HasPattern<{ a: number; }>) => boolean
has({a: false}) // $ExpectType (obj: HasPattern<{ a: boolean; }>) => boolean
has({a: 1})({a: 10}) // $ExpectType boolean
has({a: 1})({a: false}) // $ExpectError
has({a: (n: number) => n > 10})({a: 5}) // $ExpectType boolean
has({a: (n: number) => n > 10})({a: false}) // $ExpectError
it('should handle multiple patterns and nested keys', () => {
has({ a: { b: 2 }, c: 3 })({ a: { b: 2, f: 5 }, c: 3, d: 4 }).should.be.true
});
it('should return false if not true', () => {
has({ a: { b: 2 }, c: 3 })({ a: { b: 6, f: 5 }, d: 4 }).should.be.false
});
it('should handle null values', () => {
has({ a: null })({ a: null }).should.be.true
});
it('should handle scalars', () => {
has('three')('three').should.be.true;
has('three')('four').should.be.false;
has(true)(true).should.be.true;
has(false)(false).should.be.true;
has(true)(false).should.be.false;
has(undefined)(undefined).should.be.true;
has(null)(null).should.be.true;
has(undefined)(null).should.be.false;
has(3)(3).should.be.true;
has(3)(4).should.be.false;
});
it('should handle lists', () => {
has([1, 2])([1, 2]).should.be.true;
has({ a: [1, 2] })({ a: [1, 2], b: 3 }).should.be.true;
});
it('should handle predicate functions', () => {
has(_.isString)('hello').should.be.true;
has(_.isString)(5).should.be.false;
has({ a: _.isString })({ a: 'hello' }).should.be.true;
has({ a: _.isString })({ a: 5 }).should.be.false;
has({ a: n => n % 2 == 1, b: { c: _.isString } })({
a: 5,
b: { c: 'hello' }
}).should.be.true;
has({ a: n => n % 2 == 0, b: { c: _.isString } })({
a: 5,
b: { c: 'hello' }
}).should.be.false
});
it('should handle unbalanced patterns and objects', () => {
has({ a: { b: { c: 12 } } })(null).should.be.false;
has({ a: { b: { c: 12 } } })({ a: { b: null } }).should.be.false;
});
it('should handle binding', () => {
const base = {
IDTag() {
return this.tag;
}
};
const extended = {
...base,
tag: 'hi'
};
has({ IDTag: returns('hi') })(extended).should.be.true;
});
export function greaterThan(a: number): (b: number) => boolean
export function greaterThan(a: string): (b: string) => boolean
Curried function to compare greater than for two values. NOTE: All logical functions
in shades are reversed; i.e. greaterThan(a)(b) === b > a
. This might seem confusing,
but think of it as predicate factories, that take a value n
and produce a function
that tests 'Is this value greater than n
?'
greaterThan(2) // $ExpectType (b: number) => boolean
greaterThan('a') // $ExpectType (b: string) => boolean
greaterThan('a')('b') // $ExpectType boolean
greaterThan('a')(1) // $ExpectError
greaterThan({a: 1}) // $ExpectError
it('should compare greaterThan', () => {
greaterThan(2)(3).should.be.true;
greaterThan(3)(2).should.be.false;
})
it('should compare strings value', () => {
greaterThan('a')('b').should.be.true;
greaterThan('b')('a').should.be.false;
})
export function lessThan(a: number): (b: number) => boolean
export function lessThan(a: string): (b: string) => boolean
Curried function to compare less than for two values. NOTE: All logical functions
in shades are reversed; i.e. lessThan(a)(b) === b > a
. This might seem confusing,
but think of it as predicate factories, that take a value n
and produce a function
that tests 'Is this value less than n
?'
lessThan(2) // $ExpectType (b: number) => boolean
lessThan('a') // $ExpectType (b: string) => boolean
lessThan('a')('b') // $ExpectType boolean
lessThan('a')(1) // $ExpectError
lessThan({a: 1}) // $ExpectError
it('should compare lessThan', () => {
lessThan(2)(3).should.be.false;
lessThan(3)(2).should.be.true;
})
it('should compare strings value', () => {
lessThan('a')('b').should.be.false;
lessThan('b')('a').should.be.true;
})
Same as greaterThan
but >=
instead of >
Same as greaterThan
but >=
instead of >
export function toggle(b: boolean): boolean
The !
operator as a function. Takes a boolean and flips the value. Very useful as an updater function:
> mod('byName', jack, 'goldMember')(toggle)(store)
{
byName: {
jack: {
goldMember: true,
...
}
...
}
...
}
toggle(false) // $ExpectType boolean
toggle('a') // $ExpectError
it('should toggle values', () => {
toggle(true).should.be.false;
toggle(false).should.be.true;
})
export function returns<A>(a: A): (f: () => A) => boolean
A curried function that takes a value a
of type A
and a function of no arguments that
returns a value of type A
. These two values are then compared for equality.
This is very useful with has
or into
when your test value has
getter functions, and you want to see if those getters produce a certain value:
> const a = {
ID() {
return '10'
}
}
> has({a: returns(10)})(a)
true
returns(10)(() => 10) // $ExpectType boolean
returns(10)(() => 'hi') // $ExpectError
declare const getID: {
ID(): string
}
has({ID: returns('blah')})(getID) // $ExpectType boolean
has({ID: returns(10)})(getID) // $ExpectError
it('works', () => {
returns(10)(() => 10).should.be.true;
returns(7)(() => 10).should.be.false;
})
export function add(a: number): (b: number) => number
Curried +
operator
> add(5)(2)
7
> [1, 2, 3].map(add(5))
[6, 7, 8]
add(1)(3) // $ExpectType number
add(1)('s') // $ExpectError
it('works', () => {
add(5)(2).should.be.equal(7);
[1, 2, 3].map(add(5)).should.deep.equal([6, 7, 8]);
})
export function sub(a: number): (b: number) => number
Curried -
operator. NOTE: Like the logical functions, sub
is
reversed; i.e. sub(a)(b) === b - a
, so sub(3)
means "Take a number and subtract
3 from it"
> sub(5)(2)
3
> [1, 2, 3].map(sub(5))
[-4, -3, -2]
sub(1)(3) // $ExpectType number
sub(1)('s') // $ExpectError
it('works', () => {
sub(5)(2).should.be.equal(-3);
[1, 2, 3].map(sub(5)).should.deep.equal([-4, -3, -2]);
})
export function includes(snippet: string): (text: string) => boolean
Reversed version of String::includes
. Takes a snippet, and produces a function that will take a string,
and produce a boolean if that string contains the snippet. Very useful when working with into
includes('hello')('hello') // $ExpectType boolean
includes('hello')(false) // $ExpectError
it('checks for inclusion', () => {
includes('he')('hello').should.be.true
includes('hello')('he').should.be.false
})
export function all<A>(): Traversal<A>; // tslint:disable-line
all
is the simplest traversal; it simply signifies that this traversal wi
in a collection. It is the lens equivalent of the map
function.
> get('users', all(), 'name')(store)
['Jack Sparrow', 'Elizabeth Swan', 'Bootstrap Bill']
As you can see above, the 'name'
lens didn't apply directly to the array of users, and try to extract
a name
property from the array, but instead mapped it over the array.
If you're not using typescript, you'll find that you can just use the all
function itself as
the traversal, and there's no need to call it:
> set('users', all, 'name')('butt')(store)
{ users: [...] } // All users will have the name butt
get('friends', all<User>(), 'name')(user) // $ExpectType string[]
it('should act as identity with get', () => {
get(all)([1, 2, 3, 4]).should.deep.equal([1, 2, 3, 4]);
get(all)({ a: 1, b: 2, c: 3, d: 4 }).should.deep.equal({ a: 1, b: 2, c: 3, d: 4 });
});
it('should allow multifoci gets', () => {
get('a', all, 'b')({ a: [{ b: 1 }, { b: 2 }] }).should.deep.equal([ 1, 2 ]);
});
it('should allow deep multifoci gets', () => {
const store = {
users: [
{
blog: {
posts: [
{
title: 'Hi'
}
]
}
}
]
};
get('users', all, 'blog', 'posts', all, 'title')(store).should.deep.equal(
[['Hi']]
);
});
it('should allow deep multifoci mods', () => {
const store = {
users: [
{
blog: {
posts: [
{
title: 'Hi'
}
]
}
}
]
};
mod('users', all, 'blog', 'posts', all, 'title')(s => s.toLowerCase())(
store
).users[0].blog.posts[0].title.should.equal('hi');
});
it('should act as map with mod', () => {
assert.deepStrictEqual([2, 3, 4, 5], mod(all)(inc)([1, 2, 3, 4]));
assert.deepStrictEqual(
{ a: 2, b: 3, c: 4, d: 5 },
mod(all)(inc)({ a: 1, b: 2, c: 3, d: 4 })
);
});
it('should compose in the middle of a lens and act as map', () => {
assert.deepStrictEqual(
[{ n: 1, c: 5 }, { n: 2, c: 7 }],
mod(all, 'c')(inc)([{ n: 1, c: 4 }, { n: 2, c: 6 }])
);
});
it('should compose in the middle of multiple lenses', () => {
mod(all, 'c', all)(inc)([
{ n: 1, c: { d: 1, e: 7 } },
{ n: 2, c: { d: 1, e: 7 } }
]).should.deep.equal(
[{ n: 1, c: { d: 2, e: 8 } }, { n: 2, c: { d: 2, e: 8 } }]
);
});
it('should work in function form as well', () => {
Object.entries(all).should.deep.equal(Object.entries(all()))
});
export function matching<Key extends string>(k: Key): Traversal<HasKey<Key>>
export function matching<A>(f: (a: A) => any): Traversal<A>
export function matching<Pattern>(p: Pattern): Traversal<HasPattern<Pattern>>
matching
is the filter
of traversals. It takes an predicate function (or into
pattern) and produces
a lens that will apply to every item in the collection that passes the criterion.
For instance, to get
every user name that is a gold member in our store
example, we could write
> get('users', matching('goldMember'), 'name')(store)
['Elizabeth Swan']
They can be stacked together and used to modify, e.g. to find all the gold members and like only their posts with more than 10 likes (sounds complicated), all we have to write is:
> mod('users', matching('goldMember'), 'posts', matching({likes: greaterThan(10)}))(inc)(store)
{ users: [...] } // Trust me, it's updated
get(matching('goldMember'))(users) // $ExpectType User[]
get(matching('goldMember'), 'name')(users) // $ExpectType string[]
const isEven = n => n % 2 == 0;
it('should be able to get matching elements', () => {
get(matching(isEven))([1, 2, 3, 4]).should.deep.equal([2, 4]);
get(matching(isEven))({ a: 1, b: 2, c: 3, d: 4 }).should.deep.equal({ b: 2, d: 4 });
});
it('should be able to set matching elements', () => {
mod(matching(isEven))(inc)([1, 2, 3, 4]).should.deep.equal([1, 3, 3, 5])
mod(matching(isEven))(inc)({ a: 1, b: 2, c: 3, d: 4 }).should.deep.equal({ a: 1, b: 3, c: 3, d: 5 })
});
it('should compose in the middle of a lens', () => {
mod(matching(({ n }) => n % 2 === 0), 'c')(inc)([
{ n: 1, c: 4 },
{ n: 2, c: 6 }
]).should.deep.equal(
[{ n: 1, c: 4 }, { n: 2, c: 7 }]
)
});
it('should compose in the middle of a lens', () => {
mod(
matching(({ n }) => isEven(n)),
'c',
matching(({ d }) => d === 1),
'e'
)(inc)([
{ n: 1, c: 4 },
{ n: 2, c: { a: { d: 1, e: 2 }, b: { d: 5, e: 12 } } }
]).should.deep.equal(
[
{ n: 1, c: 4 },
{ n: 2, c: { a: { d: 1, e: 3 }, b: { d: 5, e: 12 } } }
])
});
it('should handle shorthands', () => {
get(matching({ n: isEven }), 'c', matching('d'), 'e')([
{ n: 1, c: 4 },
{ n: 2, c: { a: { d: true, e: 2 }, b: { d: false, e: 12 } } }
]).should.deep.equal([{ a: 2 }]);
get(matching({ n: isEven }), 'c', matching('d'), 'e')([
{ n: 1, c: 4 },
{ n: 2, c: { a: { d: true, e: 2 }, b: { d: true, e: 12 } } }
]).should.deep.equal([{ a: 2, b: 12 }]);
});
it('should set with shorthands', () => {
set(matching({ n: isEven }), 'c', matching('d'), 'e')(10)([
{ n: 1, c: 4 },
{ n: 2, c: { a: { d: true, e: 2 }, b: { d: false, e: 12 } } }
]).should.deep.equal([
{ n: 1, c: 4 },
{ n: 2, c: { a: { d: true, e: 10 }, b: { d: false, e: 12 } } }
]);
});
FAQs
Lens-like functionality with a lodash-style interface.
The npm package shades receives a total of 7,296 weekly downloads. As such, shades popularity was classified as popular.
We found that shades demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.