Created
March 19, 2021 08:53
-
-
Save bryanltobing/c5d527ad0fcd9df0eaa7e0bf05f78c49 to your computer and use it in GitHub Desktop.
This file contains 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
const filterFunction = (list, options) => { | |
switch (options.option) { | |
case 'age': | |
return filerByAgeRange(list, options.payload); | |
case 'name': | |
return filerByNames(list, options.payload); | |
case 'location': | |
return filterByLocation(list, options.payload); | |
} | |
}; | |
const filerByAgeRange = (list, payload) => { | |
return list.filter((data) => { | |
return data.age > payload.min && data.age < payload.max; | |
}); | |
}; | |
const filerByNames = (list, payload) => { | |
return list.filter((data) => { | |
return data.name.startsWith(payload); | |
}); | |
}; | |
const filterByLocation = (list, payload) => { | |
return list.filter((data) => { | |
return data.location === payload; | |
}); | |
}; | |
module.exports = filterFunction; | |
/* ======= How to use ======= */ | |
// Call the filterFunction from your main file | |
// filter(list, options) | |
// * List of an array of object | |
// * Options, option and payload | |
/* ======= Example ======= */ | |
// 1. Filter by age range | |
// filterFunction(personsLkist, { option: 'age', payload: { min: 16, max: 19 } }) | |
// 2. Filter by startName | |
// filterFunction(persons, { option: 'name', payload: 'D' }) | |
// 3. Filter by location information | |
// filterFunction(persons, { option: 'location', payload: 'california' }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment