Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add offset facility to limit filter aka mysql limit #1699

Merged
merged 3 commits into from
Nov 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/filters/array-filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ var toArray = require('../directives/public/for')._postProcess
* Limit filter for arrays
*
* @param {Number} n
* @param {Number} offset (Decimal expected)
*/

exports.limitBy = function (arr, n) {
exports.limitBy = function (arr, n, offset) {
offset = offset ? parseInt(offset, 10) : 0
return typeof n === 'number'
? arr.slice(0, n)
? arr.slice(offset, offset + n)
: arr
}

Expand Down
7 changes: 7 additions & 0 deletions test/unit/specs/filters/filters_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ describe('Filters', function () {
assertArray(res, [1, 2, 3])
res = filter(arr, -1)
assertArray(res, [1, 2])
// with offsets, note offsets are 0 bound (as expected)
res = filter(arr, 1, 1)
assertArray(res, [2])
res = filter(arr, 2, 1)
assertArray(res, [2, 3])
res = filter(arr, 1, 2)
assertArray(res, [3])
})

it('filterBy', function () {
Expand Down