Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Commit

Permalink
Define a filter lib for more consistent filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
tjerman committed Feb 3, 2022
1 parent ed65fd9 commit 03a6f7c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/components/field/FieldPicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
<script>
import draggable from 'vuedraggable'
import FieldItem from './FieldItem.vue'
import { filter } from '@cortezaproject/corteza-vue'
export default {
components: {
Expand Down Expand Up @@ -267,8 +268,7 @@ export default {
methods: {
filterFields (fields) {
return fields
.filter(f => f.name.toLowerCase().indexOf(this.query.toLowerCase()) > -1 || f.label.toLowerCase().indexOf(this.query.toLowerCase()) > -1)
return fields.filter(f => filter.Assert(f, this.query, 'name', 'label'))
},
selectField (field) {
if (this.selectedFields.some(selectedField => selectedField.label === field.label)) return
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as corredor from './corredor'
import * as filters from './filters'
import * as store from './store'
import * as url from './libs/url'
import * as filter from './libs/filter'
import * as websocket from './libs/websocket'
import i18n from './i18n'

Expand All @@ -16,6 +17,7 @@ export {
filters,
store,
url,
filter,
websocket,
i18n,
}
16 changes: 16 additions & 0 deletions src/libs/filter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect } from 'chai'
import { Assert } from './filter'

describe(__filename, () => {
it('regular query', () => {
expect(Assert({ k: 'csz' }, 'cs', 'k')).to.be.true
})

it('query with diacritics', () => {
expect(Assert({ k: 'čšž' }, 'čš', 'k')).to.be.true
})

it('query without diacritics', () => {
expect(Assert({ k: 'čšž' }, 'cs', 'k')).to.be.true
})
})
15 changes: 15 additions & 0 deletions src/libs/filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript/37511463#37511463
function toNFD (s: string): string {
return s.normalize('NFD').replace(/[\u0300-\u036f]/g, '')
}

export function Assert (obj: any, query: string, field: string, ...fields: Array<string>): boolean {
if (!obj) {
return false
}

query = toNFD(query.trim().toLowerCase())

return fields.concat([field])
.some(f => toNFD(obj[f].trim().toLowerCase()).includes(query))
}

0 comments on commit 03a6f7c

Please sign in to comment.