Create a gist now

Instantly share code, notes, and snippets.

Helpers to filter collections
<script>
// an component
import { orderBy, isEmpty } from 'lodash';
import filterByName from '../helpers/filters/by-name';
// omit
computed: {
listOrdened() {
const { orderBy as by, order } = this.configs;
return orderBy(this.repos, by, order);
},
list() {
const filter = this.configs.filter;
if (isEmpty(filter)) {
return this.listOrdened;
}
return filterByName(this.listOrdened, filter);
},
},
</script>
// helpers/by-name.js
import { makeFilterByProp } from './by-prop';
export default makeFilterByProp('name');
// helpers/filters/by-prop.js
import { filter } from 'lodash';
import { contains } from '../strings';
export const makeFilterByProp = key => (list, st) => filter(list, row => contains(row[key], st));
export default (key, list, st) => filter(list, row => contains(row[key], st));
// helpers/strings.js
import { deburr, isEmpty } from 'lodash';
export const sanitize = deburr;
export const sanitizeAndLower = value => sanitize(value).toLowerCase();
export const contains = (st, value) => sanitizeAndLower(st).indexOf(sanitizeAndLower(value)) > -1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment