Browse Source

[slider] Accepts custom predicate

master
Muthu Kumar 7 years ago
parent
commit
fbb98c2d90
  1. 8
      lib/slideHelper.js
  2. 3
      lib/slider.js

8
lib/slideHelper.js

@ -22,7 +22,9 @@ const slideNext = (host, window, hostPos, windowPos, len = 1) => {
return (len); return (len);
}; };
const startSliding = (k, host, guest, window, windowPos) => { const startSliding = (k, predicate, host, guest, window, windowPos) => {
const test = predicate ? predicate : isEq;
const firstMatch = host.findIndex(wrap(isEq, window[0])); const firstMatch = host.findIndex(wrap(isEq, window[0]));
@ -38,14 +40,14 @@ const startSliding = (k, host, guest, window, windowPos) => {
}; };
const slide = (k, host, guest) => { const slide = (k, predicate, host, guest) => {
if (k > host.length) return errors.WIN_TOO_LARGE_HOST; if (k > host.length) return errors.WIN_TOO_LARGE_HOST;
if (k > guest.length) return errors.WIN_TOO_LARGE_GUEST; if (k > guest.length) return errors.WIN_TOO_LARGE_GUEST;
// Create a new window and start sliding // Create a new window and start sliding
const window = guest.slice(0, k); const window = guest.slice(0, k);
return startSliding(k, host, guest, window, 0); return startSliding(k, predicate, host, guest, window, 0);
} }
module.exports = slide; module.exports = slide;

3
lib/slider.js

@ -5,12 +5,13 @@ const slideHelper = require('./slideHelper');
class Slider { class Slider {
constructor (options) { constructor (options) {
this.windowSize = options.size; this.windowSize = options.size;
this.predicate = options.predicate;
this.prefer = options.prefer; this.prefer = options.prefer;
this.accepting = options.accepting; this.accepting = options.accepting;
} }
slide (host, guest) { slide (host, guest) {
return slideHelper(this.windowSize, host, guest); return slideHelper(this.windowSize, this.predicate, host, guest);
} }
}; };

Loading…
Cancel
Save