Commit 5086164d authored by Abinesh Lal's avatar Abinesh Lal
Browse files

DEV: Basic implementation of g1.datafilter


Signed-off-by: Abinesh Lal's avatarAbinesh <abinesh.lal@gramenerit.com>
parent c66db787
Pipeline #43432 passed with stage
in 1 minute and 34 seconds
...@@ -26,6 +26,8 @@ Interactions: ...@@ -26,6 +26,8 @@ Interactions:
- [$.highlight](#highlight) toggles classes on elements when clicked or hover - [$.highlight](#highlight) toggles classes on elements when clicked or hover
- [search.min.js](dist/search.min.js): search-as-you-type library - [search.min.js](dist/search.min.js): search-as-you-type library
- [$.search](#search) - [$.search](#search)
- [datafilter.min.js](dist/datafilter.min.js): filtering data library
- [$.datafilter](#datafilter) filters the data based on the options
Data components: Data components:
...@@ -168,6 +170,61 @@ Example: ...@@ -168,6 +170,61 @@ Example:
- `data-toggle="search"` - `data-toggle="search"`
## datafilter
`g1.datafiilter(data, filters)` returns the filtered data based on the filters. For example:
```js
var data = [
{"ID": "1", "product": "Fan", "sales": "100", "city": "NY"},
{"ID": "2", "product": "Fan", "sales": "80", "city": "London"},
{"ID": "3", "product": "Fan", "sales": "120", "city": "NJ"},
{"ID": "4", "product": "Fan", "sales": "130", "city": "London"},
{"ID": "5", "product": "Light", "sales": "500", "city": "NY"},
{"ID": "5", "product": "Light", "sales": "100", "city": "London"}
]
g1.datafilter(data, [{col: 'sales', op: '>', val: 100},
{col: 'city', op: 'in', val: ['London', 'NY']},
{col: 'product', val: 'Fan'}])
// Returns [{"ID": "3", "product": "Fan", "sales": "120", "city": "NJ"}, {"ID": "4", "product": "Fan", "sales": "130", "city": "London"}]
```
## datafilter options
datafilter() contains three parameters:
- data: a list of objects
- filters: a list of objects, that will contains the below keys:
- col: column to be filtered.
- op: operator to be applied for filteration. default: `=`
- val: value of the selected column
- options: a dictionary that contains the below keys:
- limit: result is limited to. default: `1000`
- offset: filtering data should start from. default: `0`
- sort: a list of objects, that will contains the below keys:
- column: column to be sorted
- order: asc or desc. default: `asc`
- columns: a list of objects, that will contains the below keys:
- allow: a list of column names to be returned in the filtered data
- not: a list of column names to be skiped in the filtered data
Rules:
- the key `op` may contains any one of the below values:
- `=`
- `!=`
- `>`
- `<`
- `>=`
- `<=`
- `~`
- `!~`
- `in`
## $.formhandler ## $.formhandler
......
export { version } from './src/package.js' export { version } from './src/package.js'
export { url } from './index-urlfilter.js' export { url } from './index-urlfilter.js'
export { datafilter } from './src/datafilter.js'
import './index-highlight.js' import './index-highlight.js'
import './index-template.js' import './index-template.js'
import './index-formhandler.js' import './index-formhandler.js'
......
var operators = {
'=': function(value, compare_with) { return value == compare_with },
'!=': function(value, compare_with) { return value != compare_with },
'>': function(value, compare_with) { return value > compare_with },
'<': function(value, compare_with) { return value < compare_with },
'>=': function(value, compare_with) { return value >= compare_with },
'<=': function(value, compare_with) { return value <= compare_with },
'in': function(value, compare_with) { return (compare_with.indexOf(value) != -1) },
'~': function(value, compare_with) { return (compare_with.indexOf(value) != -1) },
'!~': function(value, compare_with) { return (compare_with.indexOf(value) == -1) }
}
var sorting = {
'string': function(value, compare_with) {
value = value.toUpperCase()
compare_with = compare_with.toUpperCase()
if (value < compare_with) {
return -1
}
if (value > compare_with) {
return 1
}
return 0
},
'number': function(value, compare_with) { return value - compare_with }
}
export function datafilter(data, filters, options) {
filters = filters || []
options = options || {}
options.limit = options.limit || 1000
options.offset = options.offset || 0
options.sort = options.sort || []
options.columns = options.columns || {}
options.columns.allow = options.columns.allow || []
options.columns.not = options.columns.not || []
var result_count = 0
var result = []
for(var index = options.offset; index < data.length; index++) {
var criteria_statisfied = true
var row = data[index]
for(var i = 0; i < filters.length; i++) {
var col = filters[i].col || 'null'
var operator = filters[i].op || '='
var value = filters[i].val || null
if(!(col in row))
continue
if(!operators[operator](row[col], value)){
criteria_statisfied = false
break
}
}
if (criteria_statisfied) {
for(var column in row) {
if(options.columns.not.indexOf(column) != -1)
delete row[column]
else if(options.columns.allow.indexOf(column) == -1 && options.columns.allow.length != 0)
delete row[column]
}
result.push(row)
result_count++
}
if(result_count == options.limit) {
break
}
}
if(options.sort.length > 0) {
result.sort(function(a, b) {
var sort_status = false
options.sort.forEach(function(sort) {
var type = (isNaN(a[sort.column])) ? 'string' : 'number'
if(sort.order == 'asc') {
sort_status = sort_status || sorting[type](a[sort.column], b[sort.column])
} else if (sort.order == 'desc') {
sort_status = sort_status || sorting[type](b[sort.column], a[sort.column])
}
})
return sort_status
})
}
return result
}
const test = require('tape')
const g1 = require('../dist/g1')
test('datafilter test', function(t) {
t.test('datafilter([]) returns {}', function(t) {
t.deepEquals(g1.datafilter([]), [])
t.end()
})
t.test('datafilter([data], filter) with one conditions works', function (t) {
t.deepEquals(g1.datafilter([
{"ID": "1", "product": "Fan", "sales": "100", "city": "NY"},
{"ID": "2", "product": "Fan", "sales": "80", "city": "London"},
{"ID": "3", "product": "Fan", "sales": "120", "city": "NJ"},
{"ID": "4", "product": "Fan", "sales": "130", "city": "London"},
{"ID": "5", "product": "Light", "sales": "500", "city": "NY"},
{"ID": "5", "product": "Light", "sales": "100", "city": "London"}
], [
{col: 'sales', op: '>', val: 100}
]),
[
{"ID": "3", "product": "Fan", "sales": "120", "city": "NJ"},
{"ID": "4", "product": "Fan", "sales": "130", "city": "London"},
{"ID": "5", "product": "Light", "sales": "500", "city": "NY"}
])
t.end()
})
t.test('datafilter([data], filter) with more than one conditions works', function (t) {
t.deepEquals(g1.datafilter([
{"ID": "1", "product": "Fan", "sales": "100", "city": "NY"},
{"ID": "2", "product": "Fan", "sales": "80", "city": "London"},
{"ID": "3", "product": "Fan", "sales": "120", "city": "NJ"},
{"ID": "4", "product": "Fan", "sales": "130", "city": "London"},
{"ID": "5", "product": "Light", "sales": "500", "city": "NY"},
{"ID": "5", "product": "Light", "sales": "100", "city": "London"}
], [
{col: 'sales', op: '>=', val: 100},
{col: 'city', op: 'in', val: ['London', 'NY']},
{col: 'product', val: 'Fan'}
]),
[
{"ID": "1", "product": "Fan", "sales": "100", "city": "NY"},
{"ID": "4", "product": "Fan", "sales": "130", "city": "London"}
])
t.end()
})
t.test('datafilter([data], filter, options) with column name selection works', function (t) {
t.deepEquals(g1.datafilter([
{"ID": "1", "product": "Fan", "sales": "100", "city": "NY"},
{"ID": "2", "product": "Fan", "sales": "80", "city": "London"},
{"ID": "3", "product": "Fan", "sales": "120", "city": "NJ"},
{"ID": "4", "product": "Fan", "sales": "130", "city": "London"},
{"ID": "5", "product": "Light", "sales": "500", "city": "NY"},
{"ID": "5", "product": "Light", "sales": "100", "city": "London"}
], [
{col: 'sales', op: '>=', val: 100},
{col: 'city', op: 'in', val: ['London', 'NY']},
{col: 'product', val: 'Fan'}
], {
columns: {
allow: ["product", "sales"]
}
}),
[
{"product": "Fan", "sales": "100"},
{"product": "Fan", "sales": "130"}
])
t.end()
})
t.test('datafilter([data], filter, options) with column name exclusion works', function (t) {
t.deepEquals(g1.datafilter([
{"ID": "1", "product": "Fan", "sales": "100", "city": "NY"},
{"ID": "2", "product": "Fan", "sales": "80", "city": "London"},
{"ID": "3", "product": "Fan", "sales": "120", "city": "NJ"},
{"ID": "4", "product": "Fan", "sales": "130", "city": "London"},
{"ID": "5", "product": "Light", "sales": "500", "city": "NY"},
{"ID": "5", "product": "Light", "sales": "100", "city": "London"}
], [
{col: 'sales', op: '>=', val: 100},
{col: 'city', op: 'in', val: ['London', 'NY']},
{col: 'product', val: 'Fan'}
], {
columns: {
not: ["city"]
}
}),
[
{"ID": "1", "product": "Fan", "sales": "100"},
{"ID": "4", "product": "Fan", "sales": "130"}
])
t.end()
})
t.test('datafilter([data], filter, options) with column sorting works', function (t) {
t.deepEquals(g1.datafilter([
{"ID": "1", "product": "Fan", "sales": "100", "city": "NY"},
{"ID": "2", "product": "Fan", "sales": "80", "city": "London"},
{"ID": "3", "product": "Fan", "sales": "120", "city": "NJ"},
{"ID": "4", "product": "Fan", "sales": "130", "city": "London"},
{"ID": "5", "product": "Light", "sales": "500", "city": "NY"},
{"ID": "5", "product": "Light", "sales": "100", "city": "London"}
], [
{col: 'city', op: 'in', val: ['London', 'NY']},
{col: 'product', val: 'Fan'}
], {
sort: [{
'column': 'city',
'order': 'asc'
}, {
'column': 'ID',
'order': 'desc'
}]
}),
[
{"ID": "4", "product": "Fan", "sales": "130", "city": "London"},
{"ID": "2", "product": "Fan", "sales": "80", "city": "London"},
{"ID": "1", "product": "Fan", "sales": "100", "city": "NY"},
])
t.end()
})
})
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment