myy-common
Version:
Common That Helper.
157 lines (156 loc) • 5.46 kB
JavaScript
;
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NameTyp = exports.TestLog = exports.jumpBasedArray = exports.objToArr = exports.getLog = void 0;
// common, [index, interface, function, class].
var util = require("util");
/* ~ export interface */
/* export function */
function getLog(message, messageFunc, pipe) {
if (message === void 0) { message = ''; }
if (pipe === void 0) { pipe = function (x) { return x; }; }
// has messageFunc, add to message.
if (messageFunc) {
return function () {
var optionalParams = [];
for (var _i = 0; _i < arguments.length; _i++) {
optionalParams[_i] = arguments[_i];
}
var m = messageFunc.apply(void 0, optionalParams) || '';
console.log(message + m, pipe.apply(void 0, optionalParams));
};
}
// simple message
else {
return function () {
var optionalParams = [];
for (var _i = 0; _i < arguments.length; _i++) {
optionalParams[_i] = arguments[_i];
}
console.log.apply(console, __spreadArrays([message], optionalParams));
};
}
}
exports.getLog = getLog;
function objToArr(obj, isAdd) {
var arr = [];
var item;
// has second param (isAdd), filter
if (isAdd) {
// Predicate, get some by bool logic
if ("call" in isAdd) {
for (var key in obj) {
//if (obj.hasOwnProperty(key)) {
var element = obj[key];
// simple
item = new NameTyp(key, element);
if (isAdd.call(obj, item)) {
arr.push(item);
}
//}
}
}
// Array, get specific
else {
for (var index = 0; index < isAdd.length; index++) {
var key = isAdd[index];
var element = obj[key];
item = new NameTyp(key, element);
arr.push(item);
}
}
}
// no second param, get all
else {
for (var key in obj) {
var element = obj[key];
// simple
item = new NameTyp(key, element);
arr.push(item);
}
}
return arr;
}
exports.objToArr = objToArr;
function jumpBasedArray(batchAmount, batchIndex) {
// jump: amount in single batch (don't change between batchs)
// batch: 0 based, batch=0 => index=0 to jump-1
var indexStart = batchIndex * batchAmount;
var maxLength = (batchIndex + 1) * batchAmount;
return { indexStart: indexStart, maxLength: maxLength };
}
exports.jumpBasedArray = jumpBasedArray;
/* ~ export function */
/* export class */
var TestLog = /** @class */ (function () {
function TestLog(testName, isStart) {
/* TestLog.wrap('Test Name', () => {})
// ~Test Name test*/
this.testName = '';
this.styleString = TestLog.styleString;
if (testName) {
this.testName = testName;
}
if (isStart) {
this.start();
}
}
//log = console.warn.bind(console);
TestLog.prototype.log = function (message) {
var params = [];
for (var _i = 1; _i < arguments.length; _i++) {
params[_i - 1] = arguments[_i];
}
var paramsStr = '';
if (params && params.length > 0) {
paramsStr = util.inspect(params);
}
//console.log('%c Oh my heavens! ', 'background: #222; color: #bada55');
//console.log('%c Oh my %cheavens! ', 'background: #002;', 'color: #bada55');
console.log('%c' + message + paramsStr, this.styleString);
};
TestLog.prototype.start = function () {
this.log(this.testName + ' test');
};
TestLog.prototype.end = function () {
this.log('~' + this.testName + ' test');
};
TestLog.i = function (testName) {
return new TestLog(testName, true);
};
TestLog.wrap = function (testName, func) {
var testLog = TestLog.i(testName);
func();
testLog.end();
};
// 'background: #222; color: #bada55'
TestLog.styleString = 'background: azure;color: blue;';
return TestLog;
}());
exports.TestLog = TestLog;
var NameTyp = /** @class */ (function () {
function NameTyp(name, value) {
this.name = name;
this.value = value;
}
NameTyp.valMap = function (nameTyp) {
if (nameTyp) {
return nameTyp.value;
}
else {
return undefined;
}
};
return NameTyp;
}());
exports.NameTyp = NameTyp;
/* ~ export class */
var recursive_1 = require("./recursive");
Object.defineProperty(exports, "runActionFirst", { enumerable: true, get: function () { return recursive_1.runActionFirst; } });
Object.defineProperty(exports, "runDeepFirst", { enumerable: true, get: function () { return recursive_1.runDeepFirst; } });