UNPKG

create-elmer-ui-app

Version:

create an develop envirnment for elmer-ui-core

278 lines (277 loc) 10.5 kB
var fs = require('fs'); var Base = (function(){ function Base(){ } Base.prototype.readSyn = function(){ process.stdin.pause(); var response = fs.readSync(process.stdin.fd, 1000, 0, "utf8"); process.stdin.resume(); return response[0].trim(); }; Base.prototype.getType = function (val) { return Object.prototype.toString.call(val); }; Base.prototype.isString = function (val) { return this.getType(val) === "[object String]"; }; Base.prototype.isObject = function (val) { return this.getType(val) === "[object Object]"; }; Base.prototype.isArray = function (val) { return this.getType(val) === "[object Array]"; }; Base.prototype.isNumeric = function (val) { return !isNaN(val); }; Base.prototype.isDOM = function (val) { return /^(\[object\s*)HTML([a-zA-Z]*)(Element\])$/.test(this.getType(val)); }; Base.prototype.isFunction = function (val) { return this.getType(val) === "[object Function]"; }; Base.prototype.isNodeList = function (val) { return this.getType(val) === "[object NodeList]"; }; Base.prototype.isRegExp = function (val) { return this.getType(val) === "[object RegExp]"; }; Base.prototype.isEmpty = function (val) { return val === undefined || val === null || (this.isString(val) && val.length <= 0); }; Base.prototype.sleepCall = function (fn, timeout, obj) { if (typeof fn === "function") { var tim = timeout || 200; var sleep = function () { if (obj) { fn.call(obj); } else { fn(); } }; setTimeout(sleep, tim); } }; Base.prototype.getValue = function (data, key, defaultValue) { var keyValue = key !== undefined && key !== null ? key : ""; if (/\./.test(keyValue)) { var keyArr = keyValue.split("."); var isFind = false; var index = 0; var keyStr = ""; var tmpData = data; while (index <= keyArr.length - 1) { keyStr = keyArr[index]; if (tmpData && this.isObject(tmpData[keyStr])) { tmpData = tmpData[keyStr]; isFind = index === keyArr.length - 1; } else { isFind = index === keyArr.length - 1; if (!isFind && tmpData && this.isArray(tmpData[keyStr]) && index === keyArr.length - 2) { var nextKey = keyArr[keyArr.length - 1]; if (nextKey === "key") { tmpData = tmpData[keyStr].key; isFind = true; break; } else if (nextKey === "length") { tmpData = tmpData[keyStr].length; isFind = true; break; } } tmpData = isFind && tmpData ? tmpData[keyStr] : null; break; } index++; } return isFind ? tmpData : defaultValue; } else { return data ? data[keyValue] : defaultValue; } }; Base.prototype.setValue = function (data, key, value, fn) { var keyValue = key !== undefined && key !== null ? key : ""; if (/\./.test(keyValue)) { var keyArr = keyValue.split("."); var isFind = false; var index = 0; var keyStr = ""; var tmpData = data; while (index < keyArr.length - 1) { keyStr = keyArr[index]; if (tmpData && this.isObject(tmpData[keyStr])) { tmpData = tmpData[keyStr]; isFind = index === keyArr.length - 2; } else { isFind = index === keyArr.length - 2; tmpData = isFind && tmpData ? tmpData[keyStr] : null; break; } index++; } if (isFind) { if (!this.isFunction(fn)) { tmpData[keyArr[keyArr.length - 1]] = value; } else { fn(tmpData, keyArr[keyArr.length - 1], value); } return true; } } else { if (!this.isFunction(fn)) { data[keyValue] = value; } else { fn(data, keyValue, value); } return true; } return false; }; Base.prototype.getRandomID = function () { var now = new Date(); var year = now.getFullYear().toString(), month = now.getMonth() + 1 < 10 ? "0" + (now.getMonth() + 1).toString() : (now.getMonth() + 1).toString(), date = now.getDate() < 10 ? "0" + now.getDate().toString() : now.getDate().toString(), hour = now.getHours() < 10 ? ["0", now.getHours()].join("") : now.getHours().toString(), minute = now.getMinutes() < 10 ? ["0", now.getMinutes()].join("") : now.getMinutes().toString(), second = now.getSeconds() < 10 ? ["0", now.getSeconds()].join("") : now.getSeconds().toString(), reSecond = now.getMilliseconds(); var randValue = parseInt((Math.random() * 9999 + 1000).toString(), 10); return [year, month, date, hour, minute, second, reSecond, randValue].join(""); }; Base.prototype.log = function (data) { elmerData.$console.log(data); }; Base.prototype.toHumpStr = function (val, firstUpperCase) { if (!this.isEmpty(val)) { var vStr = val.replace(/(^\-)|(\-$)/, ""); var vArr = vStr.split("-"); for (var i = 0; i < vArr.length; i++) { if ((i === 0 && firstUpperCase) || i > 0) { vArr[i] = vArr[i].substr(0, 1).toUpperCase() + vArr[i].substr(1); } } return vArr.join(""); } else { return val; } }; Base.prototype.humpToStr = function (val) { if (!this.isEmpty(val)) { var vStr = val.substr(0, 1).toLowerCase() + val.substr(1); var rStr = vStr.replace(/([A-Z])/g, function ($1) { return "-" + $1.toLowerCase(); }); return rStr; } return val; }; Base.prototype.getNowFormatDate = function () { var date = new Date(); var seperator1 = "-"; var year = date.getFullYear(); var month = date.getMonth() + 1; var strDate = date.getDate(); var hour = date.getHours(); var minute = date.getMinutes(); var second = date.getMinutes(); month = month > 9 ? month : "0" + month; strDate = strDate > 9 ? strDate : "0" + strDate; hour = hour > 9 ? hour : "0" + hour; minute = minute > 9 ? minute : "0" + minute; second = second > 9 ? second : "0" + second; var currentdate = year + seperator1 + month + seperator1 + strDate + " " + hour + ":" + minute + ":" + second; return currentdate; }; Base.prototype.formatLog = function (msg, type) { var now = this.getNowFormatDate(); var result = "[" + type.toString() + "][" + now + "] " + msg; switch (type) { case EnumLogType.INFO: { result = result.green; break; } case EnumLogType.ERROR: { result = result.red; break; } case EnumLogType.SUCCESS: { result = result.green; break; } case EnumLogType.WARNING: { result = result.yellow; break; } default: { result = result.magenta; } } return result; }; Base.prototype.extend = function (desc, src) { if (this.isObject(desc) && this.isObject(src)) { if (Object.assign) { Object.assign(desc, src); } else { for (var key in src) { desc[key] = src[key]; } } } }; Base.prototype.val = function (data) { if (this.isString(data)) { if (!isNaN(data)) { return data.indexOf(".") >= 0 ? parseFloat(data) : parseInt(data, 10); } else { return /^(true|false)$/.test(data) ? Boolean(data) : data; } } else { return data; } }; Base.prototype.defineReadOnlyProperty = function (obj, propertyKey, propertyValue) { obj && Object.defineProperty(obj, propertyKey, { configurable: false, enumerable: true, value: propertyValue, writable: false }); }; Base.prototype.launchFullscreen = function (element) { if (element.requestFullscreen) { element.requestFullscreen(); } else if (element["mozRequestFullScreen"]) { element["mozRequestFullScreen"](); } else if (element["webkitRequestFullscreen"]) { element["webkitRequestFullscreen"](); } else if (element["msRequestFullscreen"]) { element["msRequestFullscreen"](); } }; Base.prototype.exitFullscreen = function () { if (document.exitFullscreen) { document.exitFullscreen(); } else if (document["mozCancelFullScreen"]) { document["mozCancelFullScreen"](); } else if (document["webkitExitFullscreen"]) { document["webkitExitFullscreen"](); } }; Base.prototype.isFullScreen = function () { return document["isFullScreen"] || document["mozIsFullScreen"] || document["webkitIsFullScreen"]; }; return Base; })(); module.exports = Base;