From b9b45ece39ed61b9709dd0b8d043f6e3dbfaa51c Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Sat, 12 May 2018 20:42:13 +0530 Subject: [PATCH] [refactor] --- index.js | 2 +- lib/errorsHelper.js | 14 ++++++++++++++ lib/slideHelper.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/slider.js | 17 +++++++++++++++++ package.json | 1 + structures/slider.js | 46 ---------------------------------------------- test.js | 7 +++++++ 7 files changed, 92 insertions(+), 47 deletions(-) create mode 100644 lib/errorsHelper.js create mode 100644 lib/slideHelper.js create mode 100644 lib/slider.js delete mode 100644 structures/slider.js create mode 100644 test.js diff --git a/index.js b/index.js index f4903ee..a9b07f6 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ 'use strict'; -const Slider = require('./structures/slider'); +const Slider = require('./lib/slider'); module.exports = Slider; diff --git a/lib/errorsHelper.js b/lib/errorsHelper.js new file mode 100644 index 0000000..be12516 --- /dev/null +++ b/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; \ No newline at end of file diff --git a/lib/slideHelper.js b/lib/slideHelper.js new file mode 100644 index 0000000..272b8aa --- /dev/null +++ b/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; \ No newline at end of file diff --git a/lib/slider.js b/lib/slider.js new file mode 100644 index 0000000..172d11c --- /dev/null +++ b/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; diff --git a/package.json b/package.json index 0b06367..85c7f90 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ }, "homepage": "https://github.com/codefeathers/sliding-window#readme", "dependencies": { + "@codefeathers/fuse": "^0.11.0", "@codefeathers/iseq": "^1.1.0" } } diff --git a/structures/slider.js b/structures/slider.js deleted file mode 100644 index 8b1ece3..0000000 --- a/structures/slider.js +++ /dev/null @@ -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; diff --git a/test.js b/test.js new file mode 100644 index 0000000..2d21dda --- /dev/null +++ b/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)); \ No newline at end of file