Skip to content

Commit 2f70f79

Browse files
committed
Attempt to fix sort and limit bug
#9
1 parent c338839 commit 2f70f79

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/create_next_fn.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ const merge = require('deepmerge');
22

33
const { hashify, getIDBError } = require('./util.js'),
44
filter = require('./filter.js'),
5-
sort = require('./sort.js');
5+
sort = require('./sort.js'),
6+
limit = require('./limit.js');
67

78
const {
89
build,
@@ -77,13 +78,15 @@ const buildPredicates = (pipeline) => {
7778
const initPredAndSortSpec = (config) => {
7879
const { pipeline } = config,
7980
preds = [],
80-
sort_specs = [];
81+
sort_specs = [],
82+
limits = [];
8183

8284
let i = 0;
8385

8486
for (let [fn, arg] of pipeline) {
8587
if (fn === sort) { sort_specs.push(arg); }
8688
else if (fn === filter) { preds.push(arg); }
89+
else if (fn === limit) { limits.push(arg); }
8790
else { break; }
8891

8992
i++;
@@ -92,10 +95,14 @@ const initPredAndSortSpec = (config) => {
9295
pipeline.splice(0, i);
9396

9497
config.pred = joinPredicates(preds);
95-
98+
9699
if (sort_specs.length) {
97100
config.sort_spec = sort_specs.reduce(merge, {});
98101
}
102+
103+
if (limits.length) {
104+
config.limit_num = limits.reduce((a, b) => a + b);
105+
}
99106
};
100107

101108
const getClauses = (col, pred) => {
@@ -165,6 +172,14 @@ const initSort = (config) => {
165172
}
166173
};
167174

175+
const initLimit = (config) => {
176+
if (config.limit_num === undefined) { return; }
177+
178+
const { limit_num, pipeline } = config;
179+
180+
pipeline.push([limit, limit_num]);
181+
};
182+
168183
const createGetIDBReqFn = ({ pred, clauses, pipeline }) => {
169184
let getIDBReq;
170185

@@ -325,6 +340,7 @@ module.exports = (cur) => {
325340
initClauses(config);
326341
initHint(config);
327342
initSort(config);
343+
initLimit(config);
328344

329345
next = createNextFn(config);
330346
}

0 commit comments

Comments
 (0)