Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.

Commit fb0ea3d

Browse files
committed
#45 Support Reserved Characters & Spaces && accompanying tests
1 parent 0379a8d commit fb0ea3d

File tree

2 files changed

+65
-14
lines changed

2 files changed

+65
-14
lines changed

src/utils/Filters.js

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import * as Helpers from './Helpers'
2+
13
/** @module Filters **/
24
/**
35
* All exports are prefixed with an underscore to avoid collisions with reserved keywords (eg: "in")
46
*/
57

6-
7-
88
/**
99
* Finds all rows whose value on the stated columnName exactly matches the specified filterValue.
1010
* @param {string} columnName Name of the database column
@@ -153,7 +153,6 @@ export function _is(columnName, filterValue) {
153153
return `${columnName}=is.${filterValue}`
154154
}
155155

156-
157156
/**
158157
* Finds all rows whose value on the stated columnName is found on the specified filterArray.
159158
* @param {string} columnName Name of the database column
@@ -166,9 +165,21 @@ export function _is(columnName, filterValue) {
166165
* _in('name', ['China', 'France'])
167166
* //=>
168167
* 'name=in.(China,France)'
168+
*
169+
* @example
170+
* _in('capitals', ['Beijing,China', 'Paris,France'])
171+
* //=>
172+
* 'capitals=in.("Beijing,China","Paris,France")'
173+
*
174+
* @example
175+
* _in('food_supplies', ['carrot (big)', 'carrot (small)'])
176+
* //=>
177+
* 'food_supplies=in.("carrot (big)","carrot (small)")'
169178
*/
170-
export function _in (columnName, filterArray) {
171-
return `${columnName}=in.(${filterArray.join(',')})`
179+
export function _in(columnName, filterArray) {
180+
let cleanedFilterArray = Helpers.cleanFilterArray(filterArray)
181+
182+
return `${columnName}=in.(${cleanedFilterArray.join(',')})`
172183
}
173184

174185
/**
@@ -202,12 +213,21 @@ export function _neq(columnName, filterValue) {
202213
* 'countries=cs.{China,France}'
203214
*
204215
* @example
205-
* _cd('food_supplies', {fruits:1000, meat:800})
216+
* _cs('capitals', ['Beijing,China', 'Paris,France'])
206217
* //=>
207-
* 'food_supplies=cd.{"fruits":1000,"meat":800}'
218+
* 'capitals=cs.{"Beijing,China","Paris,France"}'
219+
*
220+
* @example
221+
* _cs('food_supplies', {fruits:1000, meat:800})
222+
* //=>
223+
* 'food_supplies=cs.{"fruits":1000,"meat":800}'
208224
*/
209225
export function _cs(columnName, filterObject) {
210-
if(Array.isArray(filterObject)) return `${columnName}=cs.{${filterObject.join(',')}}`
226+
if (Array.isArray(filterObject)) {
227+
let cleanedFilterArray = Helpers.cleanFilterArray(filterObject)
228+
229+
return `${columnName}=cs.{${cleanedFilterArray.join(',')}}`
230+
}
211231
return `${columnName}=cs.${JSON.stringify(filterObject)}`
212232
}
213233

@@ -225,12 +245,21 @@ export function _cs(columnName, filterObject) {
225245
* 'countries=cd.{China,France}'
226246
*
227247
* @example
248+
* _cd('capitals', ['Beijing,China', 'Paris,France'])
249+
* //=>
250+
* 'capitals=cd.{"Beijing,China","Paris,France"}'
251+
*
252+
* @example
228253
* _cd('food_supplies', {fruits:1000, meat:800})
229254
* //=>
230255
* 'food_supplies=cd.{"fruits":1000,"meat":800}'
231256
*/
232257
export function _cd(columnName, filterObject) {
233-
if(Array.isArray(filterObject)) return `${columnName}=cd.{${filterObject.join(',')}}`
258+
if (Array.isArray(filterObject)) {
259+
let cleanedFilterArray = Helpers.cleanFilterArray(filterObject)
260+
261+
return `${columnName}=cd.{${cleanedFilterArray.join(',')}}`
262+
}
234263
return `${columnName}=cd.${JSON.stringify(filterObject)}`
235264
}
236265

@@ -241,14 +270,22 @@ export function _cd(columnName, filterObject) {
241270
* @name ova
242271
* @function
243272
* @returns {string}
244-
*
273+
*
245274
* @example
246275
* _ova('allies', ['China', 'France'])
247276
* //=>
248277
* 'allies=ov.{China,France}'
278+
*
279+
* @example
280+
* _ova('capitals', ['Beijing,China', 'Paris,France'])
281+
* //=>
282+
* 'capitals=ov.{"Beijing,China","Paris,France"}'
283+
*
249284
*/
250285
export function _ova(columnName, filterArray) {
251-
return `${columnName}=ov.{${filterArray.join(',')}}`
286+
let cleanedFilterArray = Helpers.cleanFilterArray(filterArray)
287+
288+
return `${columnName}=ov.{${cleanedFilterArray.join(',')}}`
252289
}
253290

254291
/**

src/utils/Helpers.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1-
21
/**
3-
* @param {object} obj
2+
* @param {object} obj
43
* @private
54
* @returns {string}
65
*/
76
export function objectToQueryString(obj) {
87
return Object.keys(obj)
9-
.map(param => `${param}=${obj[param]}`)
8+
.map((param) => `${param}=${obj[param]}`)
109
.join('&')
1110
}
11+
12+
export function cleanFilterArray(filterArray) {
13+
let cleanedFilterArray = filterArray.map((filter) => {
14+
let cleanedFilter
15+
if (
16+
typeof filter == 'string' &&
17+
(filter.includes(',') || filter.includes('(') || filter.includes(')'))
18+
)
19+
cleanedFilter = `"${filter}"`
20+
else cleanedFilter = filter
21+
return cleanedFilter
22+
})
23+
24+
return cleanedFilterArray
25+
}

0 commit comments

Comments
 (0)