colur
Version:
A kool colur logger
182 lines (167 loc) • 5.86 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory(require("colors"), require("lodash.reduce"));
else if(typeof define === 'function' && define.amd)
define(["colors", "lodash.reduce"], factory);
else if(typeof exports === 'object')
exports["colur"] = factory(require("colors"), require("lodash.reduce"));
else
root["colur"] = factory(root["colors"], root["lodash.reduce"]);
})(this, function(__WEBPACK_EXTERNAL_MODULE_1__, __WEBPACK_EXTERNAL_MODULE_2__) {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
'use strict';
var colors = __webpack_require__(1),
reduce = __webpack_require__(2);
/**
* The koolest of colur loggers
* @param {str} str -> logger string
* @param {obj} option -> option object
* @return {---} -> logging magic + stylized string
*/
var colur = function colur() {
var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
_ref$color = _ref.color,
color = _ref$color === undefined ? null : _ref$color,
_ref$endColor = _ref.endColor,
endColor = _ref$endColor === undefined ? false : _ref$endColor,
_ref$end = _ref.end,
end = _ref$end === undefined ? true : _ref$end,
_ref$start = _ref.start,
start = _ref$start === undefined ? false : _ref$start,
_ref$startColor = _ref.startColor,
startColor = _ref$startColor === undefined ? false : _ref$startColor,
_ref$error = _ref.error,
error = _ref$error === undefined ? false : _ref$error,
_ref$consoleType = _ref.consoleType,
consoleType = _ref$consoleType === undefined ? 'info' : _ref$consoleType,
_ref$strReturn = _ref.strReturn,
strReturn = _ref$strReturn === undefined ? false : _ref$strReturn,
_ref$stringCaps = _ref.stringCaps,
stringCaps = _ref$stringCaps === undefined ? true : _ref$stringCaps;
//set defaults
var startColorDefault = colors.bgBlue.white.bold;
var endColorDefault = colors.bgGreen.white.bold;
var errorColor = colors.bgRed.white.bold;
var colorNotFound = false;
end = start === true || error === true ? false : end;
//check shorthand
startColor = startColor ? startColor : start ? start : false;
endColor = endColor ? endColor : end ? end : false;
/**
* Helper to apply a String to colors
* @param {str} colorStr -> query string
* @return {---} -> colors ref || undefined
*/
var getColor = function getColor(colorStr) {
return typeof colorStr === 'string' ? reduce(colorStr.split('.'), function (col, val) {
//if user messes up save them
if (colorNotFound || col === undefined) {
return startColorDefault;
}
col = col[val];
colorNotFound = col === undefined;
return col;
}, colors) : undefined;
};
//=== //if color
color = color ? getColor(color) || startColorDefault :
//if error
error ? errorColor
//if end color
: endColor ? endColorDefault
//if statrt color
: startColor ? startColor
//default
: startColorDefault;
//reverse create border'n shit
var strLen = str.length + 6;
var modLen = strLen;
do {
++modLen;
} while (modLen % 2 !== 0 || modLen % 3 !== 0);
var capStr = '';
for (var i = 0; i <= modLen + 2; i++) {
if (i % 2 === 0) {
capStr += '+';
} else {
capStr += '-';
}
}
var buf = Math.floor((modLen - str.length) / 2);
var bufStr = '';
for (var _i = 0; _i < buf; _i++) {
bufStr += ' ';
}
//create string for logger
var sCap = function sCap(val) {
return stringCaps ? val : '';
};
var msg = sCap('|>') + (str.length % 2 ? bufStr : bufStr.substring(1)) + str + bufStr + sCap('<|');
var res = '\n' + capStr + '\n' + msg + '\n' + capStr;
//short, return
if (strReturn) {
return res;
}
//show the world what you got!
console[error ? 'error' : consoleType](color(res));
return res;
};
module.exports = colur;
/***/ }),
/* 1 */
/***/ (function(module, exports) {
module.exports = require("colors");
/***/ }),
/* 2 */
/***/ (function(module, exports) {
module.exports = require("lodash.reduce");
/***/ })
/******/ ])
});
;
//# sourceMappingURL=colur.js.map