我想根据用户在Node.js中提供的查询字符串来过滤对象数组。有诸如rqlorql之类的软件包可以实现此目的,但是它们似乎对包含冒号的字符串(例如时间戳)有问题。使用rql的代码:

var rql = require("rql/js-array");
var query1 = "time=lt=2018-08-06";
var query2 = "time=lt=2018-08-06T13:45:36.000Z"

var products = [
 {price:14.99, rating: 5, time: "2018-08-04T13:45:36.000Z"},
 {price:5.99, rating: 3, time: "2018-08-05T13:45:36.000Z"},
 {price: 16.5, rating:2, time: "2018-08-06T13:45:36.000Z"}];

console.log(rql.query(query1, {}, products)); // works
console.log(rql.query(query2, {}, products)); // gives error


错误信息:

URIError: Unknown converter 2018-08-06T13
at stringToValue (/home/runner/node_modules/rql/parser.js:186:10)
at /home/runner/node_modules/rql/parser.js:99:20
at String.replace (<anonymous>)
at parse (/home/runner/node_modules/rql/parser.js:71:33)
at Object.query (/home/runner/node_modules/rql/js-array.js:358:10)


在orql中类似:

var orql = require('orql');

var query1 = "time=lt=2018-08-06";
var query2 = "time=lt=2018-08-06T13:45:36.000Z"

var products = [
  {price:14.99, rating: 5, time: "2018-08-04T13:45:36.000Z"},
  {price:5.99, rating: 3, time: "2018-08-05T13:45:36.000Z"},
  {price: 16.5, rating:2, time: "2018-08-06T13:45:36.000Z"}];

console.log(orql(products, query1)); // works
console.log(orql(products, query2)); // gives error


错误信息:

TypeError: Cannot read property 'name' of null
at rqlNodeToFunc (/home/runner/node_modules/orql/index.js:351:20)
at Function.rql.compile (/home/runner/node_modules/orql/index.js:65:14)
at rql (/home/runner/node_modules/orql/index.js:48:14)


任何想法如何对带有时间戳的对象数组进行查询?

最佳答案

似乎是rql解析器中的错误,因为the documentation说它支持“不带冒号编码的ISO UTC格式”:


  查询中的值可以是字符串(使用URL编码),数字,布尔值,null,未定义和日期(采用不带冒号编码的ISO UTC格式)。


您可以在日期前加上string一词来解决它。

var query2 = "time=lt=string:2018-08-06T13:45:36.000Z"


或也编码冒号(用%3A代替):

var query2 = "time=lt=2018-08-06T13%3A45%3A36.000Z";

08-28 04:12