Browse Source

chore: improve "where" filtering

e22m4u 2 years ago
parent
commit
bbbbca2c5e
2 changed files with 47 additions and 29 deletions
  1. 6 6
      src/filter/where-clause-tool.js
  2. 41 23
      src/filter/where-clause-tool.spec.js

+ 6 - 6
src/filter/where-clause-tool.js

@@ -41,11 +41,11 @@ export class WhereClauseTool extends Service {
   filter(entities, where = undefined) {
     if (!Array.isArray(entities))
       throw new InvalidArgumentError(
-        'A first argument of WhereUtils.filter ' +
-          'should be an Array of Objects, but %v given.',
+        'The first argument of WhereClauseTool.filter should be ' +
+          'an Array of Object, but %v given.',
         entities,
       );
-    if (!where) return entities;
+    if (where == null) return entities;
     return entities.filter(this._createFilter(where));
   }
 
@@ -57,7 +57,7 @@ export class WhereClauseTool extends Service {
    */
   _createFilter(whereClause) {
     if (typeof whereClause === 'function') return whereClause;
-    if (typeof whereClause !== 'object')
+    if (typeof whereClause !== 'object' || Array.isArray(whereClause))
       throw new InvalidArgumentError(
         'The provided option "where" should be an Object, but %v given.',
         whereClause,
@@ -66,8 +66,8 @@ export class WhereClauseTool extends Service {
     return data => {
       if (typeof data !== 'object')
         throw new InvalidArgumentError(
-          'A first argument of WhereUtils.filter ' +
-            'should be an Array of Objects, but %v given.',
+          'The first argument of WhereClauseTool.filter should be ' +
+            'an Array of Object, but %v given.',
           data,
         );
       return keys.every(key => {

+ 41 - 23
src/filter/where-clause-tool.spec.js

@@ -44,6 +44,47 @@ const OBJECTS = [
 
 describe('WhereClauseTool', function () {
   describe('filter', function () {
+    it('requires the first argument to be an array of objects', function () {
+      const throwable = v => () => S.filter(v, {});
+      const error = v =>
+        format(
+          'The first argument of WhereClauseTool.filter should be ' +
+            'an Array of Object, but %s given.',
+          v,
+        );
+      expect(throwable('str')).to.throw(error('"str"'));
+      expect(throwable('')).to.throw(error('""'));
+      expect(throwable(10)).to.throw(error('10'));
+      expect(throwable(0)).to.throw(error('0'));
+      expect(throwable(true)).to.throw(error('true'));
+      expect(throwable(false)).to.throw(error('false'));
+      expect(throwable({})).to.throw(error('Object'));
+      expect(throwable(undefined)).to.throw(error('undefined'));
+      expect(throwable(null)).to.throw(error('null'));
+      expect(throwable([{foo: 'bar'}])()).to.be.eql([{foo: 'bar'}]);
+      expect(throwable([])()).to.be.eql([]);
+    });
+
+    it('requires the second argument to be an object', function () {
+      const input = [{foo: 'a1'}, {foo: 'a2'}, {foo: 'a3'}];
+      const throwable = v => () => S.filter(input, v);
+      const error = v =>
+        format(
+          'The provided option "where" should be an Object, but %s given.',
+          v,
+        );
+      expect(throwable('str')).to.throw(error('"str"'));
+      expect(throwable('')).to.throw(error('""'));
+      expect(throwable(10)).to.throw(error('10'));
+      expect(throwable(0)).to.throw(error('0'));
+      expect(throwable(true)).to.throw(error('true'));
+      expect(throwable(false)).to.throw(error('false'));
+      expect(throwable([])).to.throw(error('Array'));
+      expect(throwable({})()).to.be.eql(input);
+      expect(throwable(undefined)()).to.be.eql(input);
+      expect(throwable(null)()).to.be.eql(input);
+    });
+
     it('returns the same array if no given condition', function () {
       const result = S.filter(OBJECTS);
       expect(result).to.be.eql(OBJECTS);
@@ -233,29 +274,6 @@ describe('WhereClauseTool', function () {
       expect(result).to.have.length(1);
       expect(result[0]).to.be.eql(OBJECTS[1]);
     });
-
-    it('throws an error if a first argument is not an Array', function () {
-      const throwable = () => S.filter(10, {});
-      expect(throwable).to.throw(
-        'A first argument of WhereUtils.filter ' +
-          'should be an Array of Objects, but 10 given.',
-      );
-    });
-
-    it('throws an error if elements of a first argument is not an Object', function () {
-      const throwable = () => S.filter([10], {});
-      expect(throwable).to.throw(
-        'A first argument of WhereUtils.filter ' +
-          'should be an Array of Objects, but 10 given.',
-      );
-    });
-
-    it('throws an error if a provided second argument is not an Object', function () {
-      const throwable = () => S.filter([], 10);
-      expect(throwable).to.throw(
-        'The provided option "where" should be an Object, but 10 given.',
-      );
-    });
   });
 
   describe('validateWhereClause', function () {