Last active
February 2, 2019 21:01
-
-
Save thisredone/be5e42ce33dbe63138835a95c532935b to your computer and use it in GitHub Desktop.
Getting props in CoffeeScript/ES6 with _ (_it) Proxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
global._ = global._it = new Proxy {}, | |
get: (_, prop) -> | |
retrieve = (props...) -> | |
fn = (obj) -> | |
props.reduce ((obj, prop) -> obj[prop]), obj | |
if props.last() in ['is', 'isnt'] | |
prop = props.pop() | |
switch prop | |
when 'is' then (val) -> (obj) -> fn(obj) is val | |
when 'isnt' then (val) -> (obj) -> fn(obj) isnt val | |
else | |
new Proxy fn, | |
get: (_, prop) -> retrieve(props..., prop) | |
retrieve(prop) | |
objs = [ | |
{ dn: 1, sec: { low: 12 } } | |
{ dn: 5 , sec: { low: 41 } } | |
] | |
console.log objs.map(_.dn) | |
console.log objs.map(_.sec.low) | |
console.log objs.find _.sec.low.is 12 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment