spirit
Version:
extensible web library for building applications & frameworks
56 lines (48 loc) • 1.52 kB
JavaScript
;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
/**
* the size of in bytes of a string or buffer
* utf-8 assumed
*
* @param {string|Buffer} v - a string or buffer to check
* @return {number|undefined} size in bytes
*/
var size_of = function size_of(v) {
if (typeof v === "string") {
if (v === "") return 0;
return Buffer.byteLength(v);
}
if (Buffer.isBuffer(v)) return v.length;
return undefined;
};
/**
* function to get better type information than typeof
*
* As with common primitive types:
* undefined, string, number, symbol, function, boolean
*
* It will also identify correctly:
* null, array, buffer, stream, file-stream
*
* And of course, "object" when all else fails
*
* @param {*} v - value to extract type information from
* @return {string} type represented as string
*/
var type_of = function type_of(v) {
var t = typeof v === "undefined" ? "undefined" : _typeof(v);
if (t === "object") {
if (v === null) return "null";
if (Buffer.isBuffer(v)) return "buffer";
if (typeof v.pipe === "function") {
if (typeof v.path === "string") return "file-stream";
return "stream";
}
if (Array.isArray(v)) return "array";
}
return t;
};
module.exports = {
type_of: type_of,
size_of: size_of
};