UNPKG

preferred

Version:

Utility for working with preferred number sequences

2 lines (1 loc) 1.49 kB
!function(n,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(n=n||self).preferred=r()}(this,function(){"use strict";function n(n){for(var r=arguments.length,t=new Array(r>1?r-1:0),o=1;o<r;o++)t[o-1]=arguments[o];var e=null,u=!1;return function(){return u||(e=n.apply(void 0,t),u=!0),e}}var r=n(t,[1]);function t(n){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:10,t=n.map(function(n){return{num:n,logNum:Math.log(n)}}),o=Math.log(r);function e(n,e,u,l){var a=null,f=null,i=!0,c=!1,h=void 0;try{for(var d,v=t[Symbol.iterator]();!(i=(d=v.next()).done);i=!0){var p=d.value,m=p.num,s=l((e-p.logNum)/o),M=m*Math.pow(r,s),g=Math.abs(n-M);(null===a||g<f)&&(a=M,f=g)}}catch(n){c=!0,h=n}finally{try{i||null==v.return||v.return()}finally{if(c)throw h}}return a}function u(n){return t.map(function(r){var t=r.num,e=r.logNum;return{num:t,targetExp:(n-e)/o}})}function l(n,r){var t=Math.log(n);return e(n,t,u(t),r)}return{floor:function(n){return l(n,Math.floor)},ceil:function(n){return l(n,Math.ceil)},bounds:function(n){var r=Math.log(n);u(r);return{floor:e(n,r,0,Math.floor),ceil:e(n,r,0,Math.ceil)}},round:function(n){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,t=this.bounds(n),o=t.floor,e=t.ceil,u=null===r?o:r(o),l=null===r?e:r(e),a=null===r?n:r(n),f=Math.abs(a-u),i=Math.abs(a-l);return i<=f?e:o}}}return{sequence:t,powerOfTwo:n(t,[1],2),powerOfTen:r,oneTwoFive:n(t,[1,2,5])}});