Remove padLeft & move zeroFillBytes to Utils

+ `padLeft was changed to use `Utils.padLeft`
+ Moved `zeroFillBytes` to `Utils.padBytesRight`
This commit is contained in:
toby 2017-02-09 13:07:46 -05:00
parent 037540c9a8
commit bbc93af2ae
2 changed files with 45 additions and 36 deletions

View file

@ -93,6 +93,34 @@ var Utils = {
}, },
/**
* Adds trailing bytes to a byteArray.
*
* @param {byteArray} arr - byteArray to add trailing bytes to.
* @param {number} numBytes - Maximum width of the array.
* @param {Integer} [padByte=0] - The byte to pad with.
* @returns {byteArray}
*
* @example
* // returns "['a', 0, 0, 0]"
* Utils.padBytesRight("a", 4);
*
* // returns "['a', 1, 1, 1]"
* Utils.padBytesRight("a", 4, 1);
*/
padBytesRight: function(arr, numBytes, padByte) {
padByte = padByte || 0;
var paddedBytes = new Array(numBytes);
paddedBytes.fill(padByte);
Array.prototype.map.call(arr, function(b, i) {
paddedBytes[i] = b;
});
return paddedBytes;
},
/** /**
* @alias Utils.padLeft * @alias Utils.padLeft
*/ */

View file

@ -365,25 +365,6 @@ var Compress = {
* @returns {byteArray} * @returns {byteArray}
*/ */
runTar: function(input, args) { runTar: function(input, args) {
var zeroFillBytes = function(string, bytes) {
var paddedString = new Array(bytes);
paddedString.fill(0);
string = string.toString().slice(0, bytes);
Array.prototype.map.call(string, function(chr, i) {
paddedString[i] = chr;
});
return paddedString;
};
var padLeft = function(string, length, padChar) {
while (string.length < length) {
string = padChar + string;
}
return string;
};
var Tarball = function() { var Tarball = function() {
this.bytes = new Array(512); this.bytes = new Array(512);
this.position = 0; this.position = 0;
@ -419,27 +400,27 @@ var Compress = {
} }
}; };
var fileSize = padLeft(input.length.toString(8), 11, "0"); var fileSize = Utils.padLeft(input.length.toString(8), 11, "0");
var currentUnixTimestamp = Math.floor(Date.now() / 1000); var currentUnixTimestamp = Math.floor(Date.now() / 1000);
var lastModTime = padLeft(currentUnixTimestamp.toString(8), 11, "0"); var lastModTime = Utils.padLeft(currentUnixTimestamp.toString(8), 11, "0");
var file = { var file = {
fileName: zeroFillBytes(args[0], 100), fileName: Utils.padBytesRight(args[0], 100),
fileMode: zeroFillBytes("0000664", 8), fileMode: Utils.padBytesRight("0000664", 8),
ownerUID: zeroFillBytes("0", 8), ownerUID: Utils.padBytesRight("0", 8),
ownerGID: zeroFillBytes("0", 8), ownerGID: Utils.padBytesRight("0", 8),
size: zeroFillBytes(fileSize, 12), size: Utils.padBytesRight(fileSize, 12),
lastModTime: zeroFillBytes(lastModTime, 12), lastModTime: Utils.padBytesRight(lastModTime, 12),
checksum: " ", checksum: " ",
type: "0", type: "0",
linkedFileName: zeroFillBytes("", 100), linkedFileName: Utils.padBytesRight("", 100),
USTARFormat: zeroFillBytes("ustar", 6), USTARFormat: Utils.padBytesRight("ustar", 6),
version: "00", version: "00",
ownerUserName: zeroFillBytes("", 32), ownerUserName: Utils.padBytesRight("", 32),
ownerGroupName: zeroFillBytes("", 32), ownerGroupName: Utils.padBytesRight("", 32),
deviceMajor: zeroFillBytes("", 8), deviceMajor: Utils.padBytesRight("", 8),
deviceMinor: zeroFillBytes("", 8), deviceMinor: Utils.padBytesRight("", 8),
fileNamePrefix: zeroFillBytes("", 155), fileNamePrefix: Utils.padBytesRight("", 155),
}; };
var checksum = 0; var checksum = 0;
@ -453,7 +434,7 @@ var Compress = {
} }
}); });
} }
checksum = zeroFillBytes(padLeft(checksum.toString(8), 7, "0"), 8); checksum = Utils.padBytesRight(Utils.padLeft(checksum.toString(8), 7, "0"), 8);
file.checksum = checksum; file.checksum = checksum;
var tarball = new Tarball(); var tarball = new Tarball();
@ -473,7 +454,7 @@ var Compress = {
tarball.writeBytes(file.deviceMajor); tarball.writeBytes(file.deviceMajor);
tarball.writeBytes(file.deviceMinor); tarball.writeBytes(file.deviceMinor);
tarball.writeBytes(file.fileNamePrefix); tarball.writeBytes(file.fileNamePrefix);
tarball.writeBytes(zeroFillBytes("", 12)); tarball.writeBytes(Utils.padBytesRight("", 12));
tarball.writeBytes(input); tarball.writeBytes(input);
tarball.writeEndBlocks(); tarball.writeEndBlocks();