Skip to content

Instantly share code, notes, and snippets.

@thisredone
Last active February 2, 2019 21:01
Show Gist options
  • Save thisredone/be5e42ce33dbe63138835a95c532935b to your computer and use it in GitHub Desktop.
Save thisredone/be5e42ce33dbe63138835a95c532935b to your computer and use it in GitHub Desktop.
Getting props in CoffeeScript/ES6 with _ (_it) Proxy
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