A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/jsonquerylang/jsonquery/commit/3019075fb6edb9e3414c30084c91a01151ecad38 below:

functions `filter` and `if` wrongly filtering on empty strings · jsonquerylang/jsonquery@3019075 · GitHub

File tree Expand file treeCollapse file tree 2 files changed

+12

-3

lines changed

Filter options

Expand file treeCollapse file tree 2 files changed

+12

-3

lines changed Original file line number Diff line number Diff line change

@@ -81,7 +81,7 @@ export const functions: FunctionBuildersMap = {

81 81

filter: <T>(predicate: JSONQuery[]) => {

82 82

const _predicate = compile(predicate)

83 83 84 -

return (data: T[]) => data.filter(_predicate)

84 +

return (data: T[]) => data.filter((item) => truthy(_predicate(item)))

85 85

},

86 86 87 87

sort: <T>(path: JSONQueryProperty = ['get'], direction?: 'asc' | 'desc') => {

@@ -209,7 +209,7 @@ export const functions: FunctionBuildersMap = {

209 209

const _valueIfTrue = compile(valueIfTrue)

210 210

const _valueIfFalse = compile(valueIfFalse)

211 211 212 -

return (data: unknown) => (_condition(data) ? _valueIfTrue(data) : _valueIfFalse(data))

212 +

return (data: unknown) => (truthy(_condition(data)) ? _valueIfTrue(data) : _valueIfFalse(data))

213 213

},

214 214

in: (path: string, values: JSONQuery) => {

215 215

const getter = compile(path)

@@ -248,3 +248,5 @@ export const functions: FunctionBuildersMap = {

248 248

return Number(`${num}e${-digits}`)

249 249

})

250 250

}

251 + 252 +

const truthy = (x: unknown) => x !== null && x !== 0 && x !== false

Original file line number Diff line number Diff line change

@@ -228,6 +228,13 @@

228 228

"query": ["filter", ["get"]],

229 229

"output": [-1, 1, 2, 3]

230 230

},

231 +

{

232 +

"category": "filter",

233 +

"description": "should filter an array with strings",

234 +

"input": ["hello", "", "world", " "],

235 +

"query": ["filter", ["get"]],

236 +

"output": ["hello", "", "world", " "]

237 +

},

231 238 232 239

{

233 240

"category": "sort",

@@ -983,7 +990,7 @@

983 990

"description": "should calculate if (7)",

984 991

"input": null,

985 992

"query": ["if", "", true, false],

986 -

"output": false

993 +

"output": true

987 994

},

988 995

{

989 996

"category": "if",

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4