Skip to content

Instantly share code, notes, and snippets.

@Voltra
Created January 5, 2022 12:38
Show Gist options
  • Select an option

  • Save Voltra/0a611263b1c4ade1a277e3166aa9fad5 to your computer and use it in GitHub Desktop.

Select an option

Save Voltra/0a611263b1c4ade1a277e3166aa9fad5 to your computer and use it in GitHub Desktop.
Magic lambdas in JS
const DeepProxy = require("proxy-deep")
const _ = new DeepProxy({}, {
_pushOp(op) {
},
get(target, path, receiver){
return this.nest(function() {});
},
apply(target, thisArg, [obj]){
return this.path.reduce((currentObj, prop) => {
if(!(currentObj && prop in currentObj)) {
return null;
}
return currentObj[prop];
}, obj);
},
});
const objects = [
{
entity: {
valid: true,
meta: {
name: "name",
description: "description",
},
},
},
{
entity: {
valid: false,
meta: {
name: "my Name",
description: "My description",
},
},
},
];
objects
.map(_.entity)
.filter(_.valid)
.map(_.meta.name)
.forEach(x => console.log(x));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment