Commit f68639e4 authored by S Anand's avatar S Anand
Browse files

BUG: g1.datafilter() type-casts inputs. Fixes #66 @tejesh.p

parent 9928515e
Pipeline #52751 passed with stage
in 3 minutes and 6 seconds
import { namespace } from './namespace_util.js'
import { types } from './types.js'
function isEqual(value, compare_with, criteria_satisfied) {
// to handle: ( ...Shape!&... ) or ( ...&Shape&... )
if (!value) {
return criteria_satisfied ? (compare_with == null) : (compare_with != null)
}
return value.indexOf(compare_with) != -1 ? !criteria_satisfied : criteria_satisfied
return value === compare_with ? !criteria_satisfied : criteria_satisfied
}
function greater_than(value, compare_with, include_equals) {
if ((isNaN(compare_with) && Date.parse(compare_with))) {
compare_with = Date.parse(compare_with)
value = Date.parse(value)
}
return include_equals ? (compare_with >= value) : (compare_with > value)
return include_equals ? (typeof compare_with == typeof value && compare_with >= value) : (typeof compare_with == typeof value && compare_with > value)
}
var operators = {
'=': function (value, compare_with) {
......@@ -35,10 +32,10 @@ var operators = {
return greater_than(compare_with, value, true)
},
'~': function (value, compare_with) {
return isEqual(compare_with, value[0], false)
return compare_with.indexOf(value) >= 0 ? true : false
},
'!~': function (value, compare_with) {
return isEqual(compare_with, value[0], true)
return compare_with.indexOf(value) >= 0 ? false : true
}
}
......@@ -80,17 +77,28 @@ export function datafilter(data, filters, dataset_name) {
// url namespace sanitize
filters = namespace(filters, dataset_name)
var data_types = types(data)
// apply WHERE clause
for (var key in filters) {
if (key[0] == '_') continue
var operator_index = (key.match(/(!|>|>~|<|<~|~|!~)$/)) ? key.match(/(!|>|>~|<|<~|~|!~)$/).index : key.length
operator = (key.slice(operator_index) != '') ? key.slice(operator_index) : '='
value = (filters[key][0] != "") ? filters[key] : null
value = (filters[key][0] != "") ? filters[key] : [null]
var col = key.slice(0, operator_index)
if (data_types[col] == 'number') {
value = value.map(function(val) { return parseFloat(val)})
} else if (data_types[col] == 'boolean') {
value = value.map(function(val){ return String(val) == 'true' ? true : false})
} else if (data_types[col] == 'date') {
value = value.map(function(val) { return Date.parse(val) })
}
result = result.filter(function (row) {
return (typeof row[col] != 'undefined') ? operators[operator](value, row[col]) : true
return value.some(function(val) {
return (typeof row[col] != 'undefined') ? operators[operator](val, row[col]) : true
})
})
}
......
......@@ -6,12 +6,12 @@ const sales_data = require('./sales-edit.json')
const sales_data_with_date = require('./sales.json')
const product_sales = [
{"ID": "1", "product": "Fan", "sales": "100", "date": "06-10-2018", "city": "NY"},
{"ID": "2", "product": "Fan", "sales": "80", "date": "06-05-2018", "city": "London"},
{"ID": "3", "product": "Fan", "sales": "120", "date": "06-04-2018", "city": "NJ"},
{"ID": "4", "product": "Fan", "sales": "130", "date": "05-15-2018", "city": "London"},
{"ID": "5", "product": "Light", "sales": "500", "date": "06-07-2016", "city": "NY"},
{"ID": "5", "product": "Light", "sales": "100", "date": "06-10-2019", "city": "London"}
{"ID": 1, "product": "Fan", "sales": true, "date": Date.parse("06-10-2018"), "city": "NY"},
{"ID": 2, "product": "Fan", "sales": false, "date": Date.parse("06-05-2018"), "city": "London"},
{"ID": 3, "product": "Fan", "sales": true, "date": Date.parse("06-04-2018"), "city": "NJ"},
{"ID": 4, "product": "Fan", "sales": false, "date": Date.parse("05-15-2018"), "city": "London"},
{"ID": 5, "product": "Light", "sales": true, "date": Date.parse("06-07-2016"), "city": "NY"},
{"ID": 5, "product": "Light", "sales": false, "date": Date.parse("06-10-2019"), "city": "London"}
]
function clone_pluck(source, include_keys, exclude_keys) {
......@@ -42,6 +42,34 @@ test('g1.datafilter test', function(t) {
t.end()
})
t.test('g1.datafilter([data], filter) with one conditions (=) works for numbers', function (t) {
var result = g1.datafilter(continent_data, { "c1": ["50"] })
var length_of_valid_data = 0
for (var row in continent_data) {
if (continent_data[row]["c1"] === 50) length_of_valid_data++
}
t.equals(result.length, length_of_valid_data)
t.ok(result.every(function (val) {
return val["c1"] === 50
}))
t.end()
})
t.test('g1.datafilter([data], filter) with one conditions (=) works for boolean types', function (t) {
var result = g1.datafilter(product_sales, { "sales": ["true"] })
var length_of_valid_data = 0
for (var row in product_sales) {
if (product_sales[row]["sales"] == true) length_of_valid_data++
}
t.equals(result.length, length_of_valid_data)
t.ok(result.every(function (val) {
return val["sales"] == true
}))
t.end()
})
t.test('g1.datafilter([data], filter) with one conditions (!) works', function (t) {
var result = g1.datafilter(continent_data, { "ID!": ["AND"] })
var length_of_valid_data = 0
......@@ -56,14 +84,14 @@ test('g1.datafilter test', function(t) {
})
t.test('g1.datafilter([data], filter) with one conditions (<) works', function (t) {
var result = g1.datafilter(continent_data, { "c1<": ["30"] })
var result = g1.datafilter(continent_data, { "c1<": ["30", "20"]})
var length_of_valid_data = 0
for (var row in continent_data) {
if(continent_data[row]["c1"] < 30) length_of_valid_data++
if (continent_data[row]["c1"] < 30 || continent_data[row]["c1"] < 20) length_of_valid_data++
}
t.equals(result.length, length_of_valid_data)
t.ok(result.every(function (val) {
return val["c1"] < 30
return val["c1"] < 30 || val["c1"] < 20
}))
t.end()
})
......@@ -107,30 +135,16 @@ test('g1.datafilter test', function(t) {
t.end()
})
t.test('g1.datafilter([data], filter) with one conditions (>= for date value) works', function (t) {
var result = g1.datafilter(sales_data_with_date, { "date>~": ["1-10-2018"] })
t.test('g1.datafilter([data], filter) with more than one = condition works', function (t) {
var result = g1.datafilter(continent_data, { "c1": ["24", "28", "35"] })
var length_of_valid_data = 0
for (var row in sales_data_with_date) {
if(Date.parse(sales_data_with_date[row]["date"]) >= Date.parse("1-10-2018")) length_of_valid_data++
}
t.equals(result.length, length_of_valid_data)
t.ok(result.every(function (val) {
return Date.parse(val["date"]) >= Date.parse("1-10-2018")
}))
t.end()
})
t.test('g1.datafilter([data], filter) with one conditions (> for date value) works', function (t) {
var result = g1.datafilter(sales_data_with_date, { "date>": ["1-10-2018"] })
var length_of_valid_data = 0
for (var row in sales_data_with_date) {
if(Date.parse(sales_data_with_date[row]["date"]) > Date.parse("1-10-2018")) length_of_valid_data++
for (var row in continent_data) {
var val = continent_data[row]["c1"]
if (val == 24 || val == 28 || val == 35) length_of_valid_data++
}
t.equals(result.length, length_of_valid_data)
t.ok(result.every(function (val) {
return Date.parse(val["date"]) > Date.parse("1-10-2018")
return (val["c1"] == 24 || val["c1"] == 28 || val["c1"] == 35)
}))
t.end()
})
......@@ -178,7 +192,7 @@ test('g1.datafilter test', function(t) {
var result = g1.datafilter(continent_data, { "Name~": ["United"] })
var length_of_valid_data = 0
for (var row in continent_data) {
if(continent_data[row]["Name"].indexOf("United") != -1) length_of_valid_data++
if (continent_data[row]["Name"].indexOf("United") != -1) length_of_valid_data++
}
t.equals(result.length, length_of_valid_data)
t.ok(result.every(function (val) {
......@@ -191,7 +205,7 @@ test('g1.datafilter test', function(t) {
var result = g1.datafilter(continent_data, { "Name!~": ["United"] })
var length_of_valid_data = 0
for (var row in continent_data) {
if(continent_data[row]["Name"].indexOf("United") == -1) length_of_valid_data++
if (continent_data[row]["Name"].indexOf("United") == -1) length_of_valid_data++
}
t.equals(result.length, length_of_valid_data)
t.ok(result.every(function (val) {
......@@ -231,7 +245,7 @@ test('g1.datafilter test', function(t) {
})
t.test('g1.datafilter([data], filter) with offset when the offset values are out of bound works', function (t) {
var result = g1.datafilter(continent_data, {"Continent": "Asia", "_offset": ["50"] })
var result = g1.datafilter(continent_data, {"Continent": ["Asia"], "_offset": ["50"] })
t.equals(result.length, 0)
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