You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
6.3 KiB
94 lines
6.3 KiB
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.AnyBase = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ |
|
'use strict'; |
|
|
|
/** |
|
* Converter |
|
* |
|
* @param {string} srcAlphabet |
|
* @param {string} dstAlphabet |
|
* @constructor |
|
*/ |
|
function Converter(srcAlphabet, dstAlphabet) { |
|
if (!srcAlphabet || !dstAlphabet || !srcAlphabet.length || !dstAlphabet.length) { |
|
throw new Error('Bad alphabet'); |
|
} |
|
this.srcAlphabet = srcAlphabet; |
|
this.dstAlphabet = dstAlphabet; |
|
} |
|
|
|
/** |
|
* Convert number from source alphabet to destonation alphabet |
|
* |
|
* @param {string} number - number represent as a string |
|
* |
|
* @returns {string} |
|
*/ |
|
Converter.prototype.convert = function(number) { |
|
var i, divide, newlen, |
|
numberMap = {}, |
|
fromBase = this.srcAlphabet.length, |
|
toBase = this.dstAlphabet.length, |
|
length = number.length, |
|
result = ''; |
|
|
|
if (this.srcAlphabet === this.dstAlphabet) { |
|
return number; |
|
} |
|
|
|
for (i = 0; i < length; i++) { |
|
numberMap[i] = this.srcAlphabet.indexOf(number[i]); |
|
} |
|
do { |
|
divide = 0; |
|
newlen = 0; |
|
for (i = 0; i < length; i++) { |
|
divide = divide * fromBase + numberMap[i]; |
|
if (divide >= toBase) { |
|
numberMap[newlen++] = parseInt(divide / toBase, 10); |
|
divide = divide % toBase; |
|
} else if (newlen > 0) { |
|
numberMap[newlen++] = 0; |
|
} |
|
} |
|
length = newlen; |
|
result = this.dstAlphabet[divide] + result; |
|
} while (newlen != 0); |
|
|
|
return result; |
|
} |
|
|
|
module.exports = Converter; |
|
},{}],2:[function(require,module,exports){ |
|
var Converter = require('./src/converter'); |
|
|
|
/** |
|
* Function get source and destination alphabet and return convert function |
|
* |
|
* @param {string} srcAlphabet |
|
* @param {string} dstAlphabet |
|
* |
|
* @returns {function(number)} |
|
*/ |
|
function anyBase(srcAlphabet, dstAlphabet) { |
|
var converter = new Converter(srcAlphabet, dstAlphabet); |
|
/** |
|
* Convert function |
|
* |
|
* @param {string} number |
|
* |
|
* @return {string} number |
|
*/ |
|
return function (number) { |
|
return converter.convert(number); |
|
} |
|
}; |
|
|
|
anyBase.BIN = '01'; |
|
anyBase.OCT = '01234567'; |
|
anyBase.DEC = '0123456789'; |
|
anyBase.HEX = '0123456789abcdef'; |
|
|
|
module.exports = anyBase; |
|
},{"./src/converter":1}]},{},[2])(2) |
|
}); |
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm5vZGVfbW9kdWxlcy9icm93c2VyLXBhY2svX3ByZWx1ZGUuanMiLCJzcmMvY29udmVydGVyLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBO0FDQUE7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7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