Skip to content

Commit

Permalink
feat(loki): add simplified javascript comparisons $jgt, $jgte, $jlt, …
Browse files Browse the repository at this point in the history
…$jlte, $jbetween (#87)

See techfort/LokiJS@bb011df
  • Loading branch information
Viatorus authored Mar 22, 2018
1 parent d611722 commit 32e4b1e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/loki/spec/generic/ops.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,26 @@ describe("Individual operator tests", () => {
expect(coll.chain().find({a: {$gte: "7.2"}}).find({a: {$finite: true}}).data().length).toEqual(3); // 7.2, "11", "18.1"
expect(coll.find({a: {$gt: "7.2"}}).length).toEqual(3); // "11", "18.1", "asdf"
expect(coll.find({a: {$lte: "7.2"}}).length).toEqual(7); // 7.2, "5", "4", 4, 2, 1, null
});

it("js range ops work as expected", () => {
const db = new Loki("db");
const coll = db.addCollection<{a: string | number, b:number}>("coll");

coll.insert({ a: null, b: 5});
coll.insert({ a: "11", b: 5});
coll.insert({ a: 2, b: 5});
coll.insert({ a: "1", b: 5});
coll.insert({ a: "4", b: 5});
coll.insert({ a: 7.2, b: 5});
coll.insert({ a: "5", b: 5});
coll.insert({ a: 4, b: 5});
coll.insert({ a: "18.1", b: 5});

expect(coll.find({ a: { $jgt: 5 } }).length).toEqual(3);
expect(coll.find({ a: { $jgte: 5 } }).length).toEqual(4);
expect(coll.find({ a: { $jlt: 7.2 } }).length).toEqual(6);
expect(coll.find({ a: { $jlte: 7.2 } }).length).toEqual(7);
expect(coll.find({ a: { $jbetween: [3.2, 7.8] } }).length).toEqual(4);
});
});
32 changes: 32 additions & 0 deletions packages/loki/src/result_set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,28 @@ export const LokiOps = {
return (gtHelper(a, range[0], true) && ltHelper(a, range[1], true));
},

// lightweight javascript comparisons
$jgt(a: any, b: any): boolean {
return a > b;
},

$jgte(a: any, b: any): boolean {
return a >= b;
},

$jlt(a: any, b: any): boolean {
return a < b;
},

$jlte(a: any, b: any): boolean {
return a <= b;
},

$jbetween(a: any, range: [any, any]): boolean {
if (a === undefined || a === null) return false;
return (a >= range[0] && a <= range[1]);
},

$in(a: any, b: any): boolean {
return b.indexOf(a) !== -1;
},
Expand Down Expand Up @@ -1265,6 +1287,16 @@ export namespace ResultSet {
$len?: number;
} | {
$where?: (val?: R) => boolean;
} | {
$jgt?: R;
} | {
$jgte?: R;
} | {
$jlt?: R;
} | {
$jlte?: R;
} | {
$jbetween?: [R, R];
};

export type Query<T> =
Expand Down

0 comments on commit 32e4b1e

Please sign in to comment.