Skip to content

Instantly share code, notes, and snippets.

@sourcegr
Last active January 21, 2021 08:36
Show Gist options
  • Save sourcegr/9cf944414c8d30a4004e39e2ee0a5605 to your computer and use it in GitHub Desktop.
Save sourcegr/9cf944414c8d30a4004e39e2ee0a5605 to your computer and use it in GitHub Desktop.
// include ramda library
var users = [
{id:1, name: 'papas', is_published:true},
{id:2, name: 'nick', is_published:false},
{id:3, name: 'Jopahn', is_published:true}
];
var $select = function(key){
var _collection, cls = [];
function get_ramda_eq(str){
return {
endsWith : R.endsWith,
contains : R.contains,
equals : R.equals,
startsWith : R.startsWith
}[str];
}
return {
from: function(collection){
_collection = collection;
return this;
},
where: function(){
var _col = arguments[0];
var _comp = get_ramda_eq(arguments[1]);
var _val = arguments[2];
cls.push(x => _comp(_val, x[_col]));
return this;
},
run:function(){
return R.compose(
R.pluck(key),
R.filter(R.allPass(cls))
)(_collection);
}
}
}
// example
$select('name').
from(users).
where('is_published', 'equals', true).
run();
// where('name', 'startsWith', 'pa').
// where('name', 'endsWith', 'as').
// where('name', 'contains', 'ap').
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment