UNPKG

react-stockcharts-ferdibiflator

Version:

Highly customizable stock charts with ReactJS and d3

128 lines (109 loc) 3.59 kB
"use strict"; /* Taken from https://github.com/ScottLogic/d3fc/blob/master/src/indicator/algorithm/calculator/slidingWindow.js The MIT License (MIT) Copyright (c) 2014-2015 Scott Logic Ltd. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.default = function () { var undefinedValue = undefined, windowSize = 10, accumulator = _noop2.default, sourcePath = void 0, source = void 0, skipInitial = 0, misc = void 0; // eslint-disable-next-line prefer-const var slidingWindow = function slidingWindow(data) { var sourceFunction = source || (0, _index.path)(sourcePath); var size = (0, _index.functor)(windowSize).apply(this, arguments); var windowData = data.slice(skipInitial, size + skipInitial).map(sourceFunction); var accumulatorIdx = 0; var undef = (0, _index.functor)(undefinedValue); // console.log(skipInitial, size, data.length, windowData.length); return data.map(function (d, i) { // console.log(d, i); if (i < skipInitial + size - 1) { return undef(sourceFunction(d), i, misc); } if (i >= skipInitial + size) { // Treat windowData as FIFO rolling buffer windowData.shift(); windowData.push(sourceFunction(d, i)); } return accumulator(windowData, i, accumulatorIdx++, misc); }); }; slidingWindow.undefinedValue = function (x) { if (!arguments.length) { return undefinedValue; } undefinedValue = x; return slidingWindow; }; slidingWindow.windowSize = function (x) { if (!arguments.length) { return windowSize; } windowSize = x; return slidingWindow; }; slidingWindow.misc = function (x) { if (!arguments.length) { return misc; } misc = x; return slidingWindow; }; slidingWindow.accumulator = function (x) { if (!arguments.length) { return accumulator; } accumulator = x; return slidingWindow; }; slidingWindow.skipInitial = function (x) { if (!arguments.length) { return skipInitial; } skipInitial = x; return slidingWindow; }; slidingWindow.sourcePath = function (x) { if (!arguments.length) { return sourcePath; } sourcePath = x; return slidingWindow; }; slidingWindow.source = function (x) { if (!arguments.length) { return source; } source = x; return slidingWindow; }; return slidingWindow; }; var _noop = require("./noop"); var _noop2 = _interopRequireDefault(_noop); var _index = require("./index"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } //# sourceMappingURL=slidingWindow.js.map