function() { var a = new ArrayDB( undefined, 'undefined' ); expect( a.query({ strict: false }).length ).to.equal( 0 ); });
it( 'should not match regexp values', function() { var a = new ArrayDB( /foo*bar/, /foo\*bar/, /foo*bar/g ); expect( a.query({ query: /foo*bar/, strict: false }).length ).to.equal( 0 ); });
function() { var a = new ArrayDB( 1, 1, 1, 2, 12, 1, 5 ); expect( a.query( 1, 2 ).length ).to.equal( 2 ); expect( a.query({ query: 1, limit: 2 }).length ).to.equal( 2 ); });
it( 'should match NaN values if they are the same', function() { var a = new ArrayDB( NaN, 42, NaN, 'NaN', ['a'] ); expect( a.query({ query: NaN, strict: true }).length ).to.equal( 2 ); });
it( 'should match undefined values if they are the same', function() { var a = new ArrayDB( undefined, 'undefined' ); expect( a.query({ query: undefined, strict: false }).length ).to.equal( 1 ); });
it( 'should match string values if they are the same', function() { var a = new ArrayDB( 'foo', 'foo\n', 'FOO' ); expect( a.query({ query: 'FOO', strict: false }).length ).to.equal( 1 ); });
function() { var a = new ArrayDB( 1, 1, 1, 2, 12, 1, 5 ); expect( a.query( 1, 10, 1 ).length ).to.equal( 3 ); expect( a.query({ query: 1, offset: 1 }).length ).to.equal( 3 ); });
it( 'should include a `ne` function', function() { var a = new ArrayDB( 1, 2, 3, 4 ); expect( a.query({ query: ArrayDB.ne( 3 ), strict: false }).length ).to.equal( 3 ); });
it( 'should match null values if they are the same', function() { var a = new ArrayDB( null, 'null', false ); expect( a.query({ query: null, strict: false }).length ).to.equal( 1 ); });
+ 'if the former match the latter', function() { var a = new ArrayDB( 'moo', 'foo', 'fooo', 'bar' ); expect( a.query({ query: /^foo+/, strict: false }).length ).to.equal( 2 ); });
it( 'should match regexp values if they are the same', function() { var a = new ArrayDB( /foo*bar/, /foo\*bar/, /foo*bar/g, 'foo*bar', 'foobar' ); expect( a.query({ query: /foo*bar/, strict: true }).length ).to.equal( 1 ); });
it( 'should match NaN with non-numbers', function() { var a = new ArrayDB( 'foo', 42, ['a'], 1 ); expect( a.query({ query: NaN, strict: false }) ).to.deep.equal([ 'foo', ['a'] ]); });
it( 'should inherit Arrays\' properties', function() { var a = new ArrayDB(); expect( a.constructor ).to.equal( Array ); expect( a.length ).to.equal( 0 ); a.push( 2 ); expect( a.length ).to.equal( 1 ); });
function() { var a = new ArrayDB( false, 1, 0, true, '' ); expect( a.query({ query: true, strict: false }).length ).to.equal( 2 ); expect( a.query({ query: false, strict: false }).length ).to.equal( 3 ); });
+ 'if they don\'t have the same properties', function() { var o = { foo: 1, bar: 2 }, p = {}, a = new ArrayDB( o, p ); expect( a.query({ query: { moo: 2 }, strict: false }).length ).to.equal( 0 ); });
function() { var a = new ArrayDB( 1, NaN, true, 'false', [] ); expect( a.query({ query: NaN, strict: false, reverse: true }) ).to.deep.equal([ 1, true, [] ]); });
function() { var a = new ArrayDB( false, true, 1, false ); expect( a.query({ query: true, strict: true }).length ).to.deep.equal( 1 ); expect( a.query({ query: false, strict: true }).length ).to.deep.equal( 2 ); });
it( 'should include an `any` function', function() { var a = new ArrayDB( [ 1, 2, 'foo' ], [ 1, 3, 'foo' ], [ 1, 3, 'bar' ] ); expect( a.query({ query: [ 1, ArrayDB.any, 'foo' ], strict: false }).length ).to.equal( 2 ); });
it( 'should match number values if they are the same', function() { var a = new ArrayDB( 1, 2, -Infinity, Infinity, 0, 2, 0.1 ); expect( a.query({ query: 2, strict: true }).length ).to.equal( 2 ); expect( a.query({ query: Infinity, strict: true }).length ).to.equal( 1 ); expect( a.query({ query: 0.1, strict: true }).length ).to.equal( 1 ); });
it( 'should match array values if their elements match', function() { var a = [ [[ 1 ]], [ 3, 2 ], true, 'NaN' ], b = [ [ 1 ], [ 3, 2 ], true, 'NaN' ], c = [ [[ 1 ]], [ 3, 3 ], 1, 'NaN' ], d = new ArrayDB( a, b, c ); expect( d.query({ query: a.slice(), strict: false }) ).to.deep.equal([ a ]); });
it( 'should handle deep circular references', function() { var o = { foo: 42 }, q = { foo: 42 }, a; o.bar = { foo: 42, bar: o }; q.bar = q; a = new ArrayDB( o, { foo: 42 }, { foo: 43 } ); expect(a.query({ query: q }).length ).to.equal( 1 ); });
it( 'should works without Array#filter', function() { var a = new ArrayDB( 1, 2, 3 ), _filter = Array.prototype.filter; Array.prototype.filter = null; expect( a.query( 42 ) ).to.deep.equal( [] ); Array.prototype.filter = _filter; });
+ 'if their string values are the same', function() { var f = function f() { return 2+2; }, g = function g() { return 2+2; }, h = function f() { return 2+2; }, i = function f() { return 2+3; }, a = new ArrayDB( f, g, h, i ); expect( a.query({ query: f, strict: true }).length ).to.equal( 2 ); });
+ 'with a non-circular-referenced object', function() { var circ = { foo: 42 }, normal = { foo: 42, bar: { foo: 42, bar: { foo: 42 } } }, a; circ.bar = circ; a = new ArrayDB( normal, { foo: 56 }, null ); // as a pattern, `circ` doesn’t match `normal` expect(a.query({ query: circ }).length ).to.equal( 0 ); });
+ 'with a circular-referenced object', function() { var circ = { foo: 42 }, normal = { foo: 42, bar: { foo: 42, bar: { foo: 42 } } }, a; circ.bar = circ; a = new ArrayDB( circ, { foo: 56 }, null ); // as a pattern, `normal` matches `circ` expect(a.query({ query: normal }).length ).to.equal( 1 ); });
it( 'should match only elements of the same type', function() { var a = new ArrayDB( 2, '2', [2], true ); expect( a.query({ query: 2, strict: true }).length ).to.equal( 1 ); expect( a.query({ query: '2', strict: true }).length ).to.equal( 1 ); expect( a.query({ query: [2], strict: true }).length ).to.equal( 1 ); expect( a.query({ query: true, strict: true }).length ).to.equal( 1 ); });
+ 'if their mutual properties have the same values', function() { var o = { foo: [ 2 ], bar: { a: 2 }, moo: NaN }, p = { bar: { a: 2, b: 4 } }, q = { moo: NaN, barfoo: 42 }, a = new ArrayDB( o, p, q ); expect( a.query({ query: {}, strict: false }).length ).to.equal( 3 ); expect( a.query({ query: { bar: { a: 2 } }, strict: false }).length ).to.equal( 2 ); });
it( 'should add a .query method on arrays', function() { var a = []; expect( a.query ).to.be.a( 'undefined' ); ArrayDB.monkeyPatch(); expect( a.query ).to.be.a( 'function' ); });
function() { var gt2 = function( e ) { return e > 2; }, eq3 = function( e ) { return e == 3; }, truthy = function() { return 'foo'; }, a = new ArrayDB( 1, 2, 3, 4, 5, { foo: 2 }, { foo: 3 } ); expect( a.query({ query: gt2, strict: false }).length ).to.equal( 3 ); expect( a.query({ query: eq3, strict: false }).length ).to.equal( 1 ); expect( a.query({ query: truthy, strict: false }).length ).to.equal( 7 ); expect( a.query({ query: { foo: eq3 }, strict: false }).length ).to.equal( 1 ); });
it( 'should return an array', function() { var a = new ArrayDB(); expect( a.query() ).to.be.a( 'array' ); expect( a.query([]) ).to.be.a( 'array' ); expect( a.query({}) ).to.be.a( 'array' ); expect( a.query(0) ).to.be.a( 'array' ); expect( a.query(NaN) ).to.be.a( 'array' ); expect( a.query(true) ).to.be.a( 'array' ); });