nsn-util
Version:
NSN工具辅助组件
130 lines (90 loc) • 4.12 kB
JavaScript
;
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "isDate", {
enumerable: true,
get: function get() {
return _lodash.isDate;
}
});
Object.defineProperty(exports, "isEqual", {
enumerable: true,
get: function get() {
return _lodash.isEqual;
}
});
Object.defineProperty(exports, "split", {
enumerable: true,
get: function get() {
return _lodash.split;
}
});
exports.trim = exports.isTrimEmpty = exports.isEmpty = exports.guid = exports.isUrl = exports.isBrowser = exports.isNode = exports.blankToEmpty = exports.blankToDefault = void 0;
var _lodash = require("lodash");
var _nsnConst = require("nsn-const");
var NRegex = _interopRequireWildcard(require("./NRegex"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/** 值为空,取默认值 */
var blankToDefault = function blankToDefault(value, defaultValue) {
return value || defaultValue;
};
/** 值为空,取空字符串 */
exports.blankToDefault = blankToDefault;
var blankToEmpty = function blankToEmpty(value) {
return value || _nsnConst.NConst.EMPTY;
};
/** 判定是否node环境 */
exports.blankToEmpty = blankToEmpty;
var isNode = function isNode() {
var flag = typeof process !== 'undefined' && process.versions != null && process.versions.node != null; // 若为 false ,返回 undefined,方便双问号 ?? 运算符
return flag || undefined;
};
/** 判定是否浏览器环境 */
exports.isNode = isNode;
var isBrowser = function isBrowser() {
var flag = typeof window !== 'undefined' && typeof window.document !== 'undefined' && !isNode(); // 若为 false ,返回 undefined,方便双问号 ?? 运算符
return flag || undefined;
};
/** 判定是否url */
exports.isBrowser = isBrowser;
var isUrl = function isUrl(path) {
return NRegex.RE_URL.test(path);
};
/** 随机值 */
exports.isUrl = isUrl;
var guid = function guid() {
return 'xxxxxxxx'.replace(/[xy]/g, function (c) {
// 暂时关闭 按位操作符 检查
// eslint-disable-next-line no-bitwise
var r = Math.random() * 16 | 0; // eslint-disable-next-line no-bitwise
var v = c === 'x' ? r : r & 0x3 | 0x8;
return v.toString(16);
});
};
/** 是否空值 */
exports.guid = guid;
var isEmpty = function isEmpty(str) {
var flag = (0, _lodash.isEmpty)(str); // 若为 false ,返回 undefined,方便双问号 ?? 运算符
return flag || undefined;
};
/** 去掉首尾空格 */
exports.isEmpty = isEmpty;
var trim = function trim(str) {
if (str) {
return str.trim();
}
return str;
};
/** 去掉前后空格后,是否为空 */
exports.trim = trim;
var isTrimEmpty = function isTrimEmpty(str) {
var flag = isEmpty(str);
if (!flag) {
flag = isEmpty(trim(str));
}
return flag;
};
exports.isTrimEmpty = isTrimEmpty;