From ff0fcc849296158cea8d75282aeb58ab8e0cc613 Mon Sep 17 00:00:00 2001 From: Xc Date: Mon, 9 Jun 2025 15:42:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=B0=E5=AF=8C=E4=BA=86=EF=BC=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=9B=B4=E5=A4=9A=E8=AF=AD=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- es/store.js | 9 +++++++-- lib/store.js | 9 +++++++-- src/store.ts | 9 +++++++-- test/test.ts | 22 +++++++++++++++++++++- 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/es/store.js b/es/store.js index 05009bb..d9e2523 100644 --- a/es/store.js +++ b/es/store.js @@ -569,11 +569,16 @@ export default class TreeStore extends CascadeStore { case '$length': { // json中的数组长度查询 const length = value; - assert(typeof length === 'number'); return (row) => { const data = path ? get(row, path) : row; assert(data instanceof Array, '$length operator can only used on array attribute'); - return data.length === length; + if (typeof length === 'number') { + return data.length === length; + } + else { + const op = Object.keys(length)[0]; + return this.translatePredicate(entity, length, op, length[op], option)(data); + } }; } default: { diff --git a/lib/store.js b/lib/store.js index 7addd51..eac590c 100644 --- a/lib/store.js +++ b/lib/store.js @@ -571,11 +571,16 @@ class TreeStore extends CascadeStore_1.CascadeStore { case '$length': { // json中的数组长度查询 const length = value; - (0, assert_1.assert)(typeof length === 'number'); return (row) => { const data = path ? (0, lodash_1.get)(row, path) : row; (0, assert_1.assert)(data instanceof Array, '$length operator can only used on array attribute'); - return data.length === length; + if (typeof length === 'number') { + return data.length === length; + } + else { + const op = Object.keys(length)[0]; + return this.translatePredicate(entity, length, op, length[op], option)(data); + } }; } default: { diff --git a/src/store.ts b/src/store.ts index 62dfdab..8a42809 100644 --- a/src/store.ts +++ b/src/store.ts @@ -710,11 +710,16 @@ export default class TreeStore extends C case '$length': { // json中的数组长度查询 const length = value; - assert (typeof length === 'number'); return (row) => { const data = path ? get(row, path) : row; assert(data instanceof Array, '$length operator can only used on array attribute'); - return data.length === length; + if (typeof length === 'number') { + return data.length === length; + } + else { + const op = Object.keys(length)[0]; + return this.translatePredicate(entity, length, op, length[op], option)(data); + } } } default: { diff --git a/test/test.ts b/test/test.ts index f1f7eb2..5bae2b5 100644 --- a/test/test.ts +++ b/test/test.ts @@ -1317,7 +1317,6 @@ describe('基础测试', function () { } }, context, {}); - const row12 = store.select('oper', { data: { id: 1, @@ -1335,6 +1334,26 @@ describe('基础测试', function () { } } }, context, {}); + + const row13 = store.select('oper', { + data: { + id: 1, + data: { + name: 1, + price: 1, + }, + }, + filter: { + id, + data: { + price: { + $length: { + $gt: 3, + }, + }, + } + } + }, context, {}); context.commit(); assert(row.length === 1); assert(row2.length === 0); @@ -1348,6 +1367,7 @@ describe('基础测试', function () { assert(row10.length === 1); assert(row11.length === 0); assert(row12.length === 1); + assert(row13.length === 0); // console.log(JSON.stringify(row7)); });