UNPKG

map-number

Version:

processing/p5.js map like function, including floating point numbers support

51 lines (41 loc) 1.59 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.mapNum = {})); })(this, (function (exports) { 'use strict'; function map(num, inMin, inMax, outMin, outMax) { return (num - inMin) * (outMax - outMin) / (inMax - inMin) + outMin; } function transformed(map, transform) { return (num, inMin, inMax, outMin, outMax) => transform(map(num, inMin, inMax, outMin, outMax)); } const ceil = transformed(map, Math.ceil); function compile(map, inMin, inMax, outMin, outMax) { return value => map(value, inMin, inMax, outMin, outMax); } const floor = transformed(map, Math.floor); function limit(num, inMin, inMax, outMin, outMax) { const result = map(num, inMin, inMax, outMin, outMax); let boundMin = outMin; let boundMax = outMax; if (boundMax < boundMin) { const temp = boundMin; boundMin = boundMax; boundMax = temp; } return result > boundMax ? boundMax : result < boundMin ? boundMin : result; } const round = transformed(map, Math.round); exports.ceil = ceil; exports.clamp = limit; exports.compile = compile; exports.create = compile; exports.floor = floor; exports.limit = limit; exports.map = map; exports.round = round; exports.transform = transformed; exports.transformed = transformed; exports.wrap = compile; })); //# sourceMappingURL=map.js.map