Skip to content

Instantly share code, notes, and snippets.

@ivomarsan
Created November 28, 2017 04:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivomarsan/4764462f82c49857d5b5be1367ba2982 to your computer and use it in GitHub Desktop.
Save ivomarsan/4764462f82c49857d5b5be1367ba2982 to your computer and use it in GitHub Desktop.
const findByFieldRange = field => (list, range) =>
list.filter(obj => obj[field] >= range[0] && obj[field] <= range[1]);
const getAllByField = field => list => list.filter(obj => obj[field]);
const findIn = list => ({
by: field => ({
between: (start, end = start) =>
findByFieldRange(field)(list, [start, end]),
getAll: () => getAllByField(field)(list),
}),
});
const list = [
{ id: 1, name: 'Suissa' },
{ id: 2, name: 'Jean' },
{ id: 3, name: 'Carlo' },
{ id: 4, name: 'Nascimento' },
{ name: 'Debochado :P' },
];
const between = findIn(list)
.by('id')
.between(1);
const getAll = findIn(list)
.by('id')
.getAll();
console.log('between', between);
console.log('getAll', getAll);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment