UNPKG

maycur-business

Version:

maycur business react components of web

74 lines (60 loc) 2.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = interpolator; var _isdate = _interopRequireDefault(require("./isdate")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } // Functor to create a function that runs the percentage between two values; // Usage: interpolator(0, 100)(0.5) == 50 // Credit: https://github.com/sveltejs/svelte/blob/master/src/motion/tweened.ts function interpolator(a, b) { if (a === b || a !== a) return function () { return a; }; var type = _typeof(a); if (type !== _typeof(b) || Array.isArray(a) !== Array.isArray(b)) { throw new Error("Cannot interpolate values of different type"); } if (Array.isArray(a)) { var arr = b.map(function (bi, i) { return interpolator(a[i], bi); }); return function (t) { return arr.map(function (fn) { return fn(t); }); }; } if (type === "object") { if (!a || !b) throw new Error("Object cannot be null"); if ((0, _isdate["default"])(a) && (0, _isdate["default"])(b)) { a = a.getTime(); b = b.getTime(); var delta = b - a; return function (t) { return new Date(a + t * delta); }; } var keys = Object.keys(b); var interpolators = {}; keys.forEach(function (key) { interpolators[key] = interpolator(a[key], b[key]); }); return function (t) { var result = {}; keys.forEach(function (key) { result[key] = interpolators[key](t); }); return result; }; } if (type === "number") { var _delta = b - a; return function (t) { return a + t * _delta; }; } throw new Error("Cannot interpolate ".concat(type, " values")); }