Created
October 1, 2016 18:41
-
-
Save vinicius73/c10297ca3c14dae7b8095cbad5747da7 to your computer and use it in GitHub Desktop.
Helpers to filter collections
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
<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> |
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
// helpers/by-name.js | |
import { makeFilterByProp } from './by-prop'; | |
export default makeFilterByProp('name'); |
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
// 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)); |
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
// 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