42 lines
964 B
JavaScript
42 lines
964 B
JavaScript
// bufferish-array.js
|
|
|
|
var Bufferish = require("./bufferish");
|
|
|
|
var exports = module.exports = alloc(0);
|
|
|
|
exports.alloc = alloc;
|
|
exports.concat = Bufferish.concat;
|
|
exports.from = from;
|
|
|
|
/**
|
|
* @param size {Number}
|
|
* @returns {Buffer|Uint8Array|Array}
|
|
*/
|
|
|
|
function alloc(size) {
|
|
return new Array(size);
|
|
}
|
|
|
|
/**
|
|
* @param value {Array|ArrayBuffer|Buffer|String}
|
|
* @returns {Array}
|
|
*/
|
|
|
|
function from(value) {
|
|
if (!Bufferish.isBuffer(value) && Bufferish.isView(value)) {
|
|
// TypedArray to Uint8Array
|
|
value = Bufferish.Uint8Array.from(value);
|
|
} else if (Bufferish.isArrayBuffer(value)) {
|
|
// ArrayBuffer to Uint8Array
|
|
value = new Uint8Array(value);
|
|
} else if (typeof value === "string") {
|
|
// String to Array
|
|
return Bufferish.from.call(exports, value);
|
|
} else if (typeof value === "number") {
|
|
throw new TypeError('"value" argument must not be a number');
|
|
}
|
|
|
|
// Array-like to Array
|
|
return Array.prototype.slice.call(value);
|
|
}
|