Browse Source

Remove String.lpad and cleanup

hk-custom-address
Andreas Brekken 11 years ago
parent
commit
80f46d32b4
  1. 106
      src/convert.js

106
src/convert.js

@ -1,74 +1,102 @@
// convert to/from various values var base64map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
var base64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; exports.lpad = function lpad(str, padString, length) {
String.prototype.lpad = function(padString, length) {
var str = this;
while (str.length < length) str = padString + str; while (str.length < length) str = padString + str;
return str; return str;
} }
// Convert a byte array to a hex string /**
module.exports.bytesToHex = function(bytes) { * Convert a byte array to a hex string
return bytes.map(function(x) { return x.toString(16).lpad('0',2) }).join(''); */
exports.bytesToHex = function(bytes) {
return bytes.map(function(x) {
return exports.lpad(x.toString(16), '0', 2)
}).join('');
}; };
// Convert a hex string to a byte array /**
module.exports.hexToBytes = function(hex) { * Convert a hex string to a byte array
return hex.match(/../g).map(function(x) { return parseInt(x,16) }); */
exports.hexToBytes = function(hex) {
return hex.match(/../g).map(function(x) {
return parseInt(x,16)
});
} }
// Convert a byte array to a base-64 string /**
module.exports.bytesToBase64 = function(bytes) { * Convert a byte array to a base-64 string
*/
exports.bytesToBase64 = function(bytes) {
var base64 = []
for(var base64 = [], i = 0; i < bytes.length; i += 3) { for (var i = 0; i < bytes.length; i += 3) {
var triplet = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]; var triplet = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
for (var j = 0; j < 4; j++) { for (var j = 0; j < 4; j++) {
if (i * 8 + j * 6 <= bytes.length * 8) if (i * 8 + j * 6 <= bytes.length * 8) {
base64.push(base64map.charAt((triplet >>> 6 * (3 - j)) & 0x3F)); base64.push(base64map.charAt((triplet >>> 6 * (3 - j)) & 0x3F));
else base64.push("="); } else {
base64.push('=');
}
} }
} }
return base64.join(""); return base64.join('');
} }
/**
* Convert a base-64 string to a byte array
*/
exports.base64ToBytes = function(base64) {
// Remove non-base-64 characters
base64 = base64.replace(/[^A-Z0-9+\/]/ig, '');
// Convert a base-64 string to a byte array var bytes = []
module.exports.base64ToBytes = function(base64) { , imod4 = 0
// Remove non-base-64 characters for (var i = 0; i < base64.length; imod4 = ++i % 4) {
base64 = base64.replace(/[^A-Z0-9+\/]/ig, ""); if (!imod4) continue
for (var bytes = [], i = 0, imod4 = 0; i < base64.length; imod4 = ++i % 4) { bytes.push(
if (imod4 == 0) continue; (
bytes.push(((base64map.indexOf(base64.charAt(i - 1)) & (Math.pow(2, -2 * imod4 + 8) - 1)) << (imod4 * 2)) | (base64map.indexOf(base64.charAt(i - 1)) & (Math.pow(2, -2 * imod4 + 8) - 1)) <<
(base64map.indexOf(base64.charAt(i)) >>> (6 - imod4 * 2))); (imod4 * 2)
) |
(base64map.indexOf(base64.charAt(i)) >>> (6 - imod4 * 2))
);
} }
return bytes; return bytes;
} }
// Hex only (allowing bin would be potentially risky, as 01010101 = \x01 * 4 or 85) /**
module.exports.coerceToBytes = function(input) { * Hex only (allowing bin would be potentially risky, as 01010101 = \x01 * 4 or 85)
if (typeof input == "string") return module.exports.hexToBytes(input); */
return input; exports.coerceToBytes = function(input) {
if (typeof input != 'string') return input
return exports.hexToBytes(input);
} }
module.exports.binToBytes = function(bin) { exports.binToBytes = function(bin) {
return bin.match(/......../g).map(function(x) { return parseInt(x,2) }); return bin.match(/......../g).map(function(x) {
return parseInt(x,2)
});
} }
module.exports.bytesToBin = function(bytes) { exports.bytesToBin = function(bytes) {
return bytes.map(function(x) { return x.toString(2).lpad('0',8) }).join(''); return bytes.map(function(x) {
return exports.lpad(x.toString(2), '0', 8)
}).join('');
} }
module.exports.bytesToString = function(bytes) { exports.bytesToString = function(bytes) {
return bytes.map(function(x){ return String.fromCharCode(x) }).join(''); return bytes.map(function(x){
return String.fromCharCode(x)
}).join('');
} }
module.exports.stringToBytes = function(string) { exports.stringToBytes = function(string) {
return string.split('').map(function(x) { return x.charCodeAt(0) }); return string.split('').map(function(x) {
return x.charCodeAt(0)
});
} }
// utf8

Loading…
Cancel
Save