Browse Source

[refactor]

master
Muthu Kumar 7 years ago
parent
commit
b9b45ece39
  1. 2
      index.js
  2. 14
      lib/errorsHelper.js
  3. 52
      lib/slideHelper.js
  4. 17
      lib/slider.js
  5. 1
      package.json
  6. 46
      structures/slider.js
  7. 7
      test.js

2
index.js

@ -1,5 +1,5 @@
'use strict'; 'use strict';
const Slider = require('./structures/slider'); const Slider = require('./lib/slider');
module.exports = Slider; module.exports = Slider;

14
lib/errorsHelper.js

@ -0,0 +1,14 @@
const returnError = (code, message) => {
return {
err: message,
code
}
}
const errors = {
'WIN_TOO_LARGE_HOST': returnError('WIN_TOO_LARGE_HOST', 'Window size is larger than host object'),
'WIN_TOO_LARGE_GUEST': returnError('WIN_TOO_LARGE_GUEST', 'Window size is larger than guest object'),
'COULD_NOT_SLIDE': returnError('COULD_NOT_SLIDE', 'Could not slide into position for given window')
};
module.exports = errors;

52
lib/slideHelper.js

@ -0,0 +1,52 @@
const isEq = require('@codefeathers/iseq');
const Fuse = require('@codefeathers/fuse');
const errors = require('./errorsHelper');
const wrap = (f, ...x) => (...y) => f(...x, ...y);
const converter = (
hostPosition,
guestPosition,
matchLength
) => ({
hostPosition,
guestPosition,
matchLength
});
const slideNext = (host, window, hostPos, windowPos, len = 1) => {
const newHostPos = hostPos + 1;
const newWindowPos = windowPos + 1;
if (isEq(window[newWindowPos], host[newHostPos]))
return slideNext(host, window, newHostPos, newWindowPos, len + 1);
return (len);
};
const startSliding = (k, host, guest, window, windowPos) => {
const firstMatch = host.findIndex(wrap(isEq, window[0]));
// If first item returns a match, then continue to slideNext()
const len = firstMatch ? slideNext(host, guest, firstMatch, windowPos) : undefined;
if (len && len > 1) return converter(firstMatch, windowPos, len);
const nextWindowPos = windowPos + 1;
const newWindow = guest.slice(nextWindowPos, nextWindowPos + k);
if (newWindow.length === k) return startSliding(newWindow, nextWindowPos);
return errors.COULD_NOT_SLIDE;
};
const slide = (k, host, guest) => {
if (k > host.length) return errors.WIN_TOO_LARGE_HOST;
if (k > guest.length) return errors.WIN_TOO_LARGE_GUEST;
// Create a new window and start sliding
const window = guest.slice(0, k);
return startSliding(k, host, guest, window, 0);
}
module.exports = slide;

17
lib/slider.js

@ -0,0 +1,17 @@
'use strict';
const slideHelper = require('./slideHelper');
class Slider {
constructor (options) {
this.windowSize = options.size;
this.prefer = options.prefer;
this.accepting = options.accepting;
}
slide (host, guest) {
return slideHelper(this.windowSize, host, guest);
}
};
module.exports = Slider;

1
package.json

@ -24,6 +24,7 @@
}, },
"homepage": "https://github.com/codefeathers/sliding-window#readme", "homepage": "https://github.com/codefeathers/sliding-window#readme",
"dependencies": { "dependencies": {
"@codefeathers/fuse": "^0.11.0",
"@codefeathers/iseq": "^1.1.0" "@codefeathers/iseq": "^1.1.0"
} }
} }

46
structures/slider.js

@ -1,46 +0,0 @@
'use strict';
const isEq = require('@codefeathers/iseq');
const wrap = (f, ...x) => (...y) => f(...x, ...y);
class Slider {
constructor (options) {
this.windowSize = options.size;
this.prefer = options.prefer;
this.accepting = options.accepting;
}
slide (host, guest) {
const k = this.windowSize;
const get = {
val: 0,
next: () => ++get.val,
}
const startSliding = (window, i) => {
const firstMatch = host.findIndex(wrap(isEq, window[0]));
if(firstMatch !== -1) {
get.val = firstMatch;
if(window.every((item, j) => {
if(j === 0) return true;
return isEq(item, host[get.next()]);
})) return [ firstMatch, i ];
}
const nextI = i + 1;
const newWindow = guest.slice(nextI, nextI + k);
if(newWindow.length === k) return startSliding(newWindow, nextI);
return [ -1, -1 ];
};
const window = guest.slice(0, k);
return startSliding(window, 0);
}
};
module.exports = Slider;

7
test.js

@ -0,0 +1,7 @@
const Slider = require('.');
const a = [ 0, 1, 2, 3, 4, 5 ];
const b = [ 2, 3, 4 ];
const w = new Slider({ size: 3 });
console.log(w.slide(a, b));
Loading…
Cancel
Save