fastchar-appjs
Version:
快速搭建VUE项目工具类的基本库,主要用于每个功能页面独立生成html,不使用vue单页面功能。
1,108 lines (1,107 loc) • 37.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FastHelper = void 0;
/**
* FastHelper 常用的工具类
* @author Janesen
*/
var FastHelper;
(function (FastHelper) {
/**
* base64相关
*/
class Base64 {
/**
* 转码base64
* @param content
*/
static encode(content) {
const Base64 = require("js-base64");
return Base64.encode(content);
}
/**
* 解码base64
* @param content
*/
static decode(content) {
const Base64 = require("js-base64");
return Base64.decode(content);
}
/**
* 将base64转为file对象
* @param content base64内容
* @param fileName 文件名
*/
static base64ToFile(content, fileName) {
let arr = content.split(','), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
const blob = new Blob([u8arr], { type: mime });
blob.lastModifiedDate = new Date();
blob.name = fileName;
return blob;
}
/**
* 将file对象转为base64
* @param file
*/
static async fileToBase64(file) {
return new Promise(function (resolved, rejected) {
const fileReader = new FileReader();
fileReader.onload = function (readEvent) {
let base64 = readEvent.target.result;
resolved(base64);
};
fileReader.readAsDataURL(file);
});
}
}
FastHelper.Base64 = Base64;
/**
* 对象操作类
*/
class Objects {
/**
* 判断目标是否为对象类型
* @param source
*/
static isObject(source) {
if ((toString.call(null) === '[object Object]')) {
return source !== null && source !== undefined && toString.call(source) === '[object Object]' && source.ownerDocument === undefined;
}
return toString.call(source) === '[object Object]';
}
/**
* 判断是否为空,包含undefined
* @param source 目标值
*/
static isEmpty(source) {
if (source === undefined) {
return true;
}
if (source === null) {
return true;
}
if (source.toString() === "") {
return true;
}
return source.toString().length === 0;
}
/**
* 判断目标是否没有任何属性值,如果为空数组,则数组有实体属性length
* @param source
*/
static isEmptyProperty(source) {
if (this.isEmpty(source)) {
return true;
}
return Object.getOwnPropertyNames(source).length === 0;
}
/**
* 判断目标是否没有任何key
* @param source
*/
static isEmptyKey(source) {
if (this.isEmpty(source)) {
return true;
}
return Object.keys(source).length === 0;
}
/**
* 判断两个对象是否相同,深度判断属性中的值是否相同
* 如果比较的属性类型都为函数类型,则跳过比较
* @param first
* @param second
*/
static isSame(first, second) {
if (!this.isObject(first)) {
return false;
}
if (!this.isObject(second)) {
return false;
}
let firstProps = Object.getOwnPropertyNames(first);
let secondProps = Object.getOwnPropertyNames(second);
if (firstProps.length !== secondProps.length) {
return false;
}
for (let i = 0; i < firstProps.length; i++) {
let propName = firstProps[i];
let propFist = first[propName];
let propSecond = second[propName];
if (FastHelper.Functions.isFunction(propFist) && FastHelper.Functions.isFunction(propSecond)) {
continue;
}
if (this.isObject(propFist)) {
if (!this.isSame(propFist, propSecond)) {
return false;
}
}
else if (FastHelper.Arrays.isArray(propFist)) {
if (!FastHelper.Arrays.isSame(propFist, propSecond)) {
return false;
}
}
else if (propFist !== propSecond) {
return false;
}
}
return true;
}
}
FastHelper.Objects = Objects;
/**
* 数组相关
*/
class Arrays {
/**
* 判断目标是否为数组类型
* @param source
*/
static isArray(source) {
if (source == null) {
return false;
}
if (source instanceof Array) {
return true;
}
return toString.call(source) == '[object Array]';
}
/**
* 合并数组,返回一个新的数组
* @param items
*/
static merge(...items) {
let newArray = [];
items.forEach(item => {
for (let i = 0; i < item.length; i++) {
newArray.push(item[i]);
}
});
return newArray;
}
/**
* 批量追加
* @param source 原数组
* @param items 添加的选项,如果选项类型为数组,则合并到数组中
*/
static append(source, ...items) {
items.forEach(item => {
if (item instanceof Array) {
for (let i = 0; i < item.length; i++) {
source.push(item[i]);
}
}
else {
source.push(item);
}
});
}
/**
* 清空数组
* @param source
*/
static clear(source) {
source.splice(0, source.length);
}
/**
* 判断是否存在于数组中
* @param source 数组
* @param value 值
*/
static exists(source, value) {
for (let i = 0; i < source.length; i++) {
if (source[i] === value) {
return true;
}
}
return false;
}
/**
* 获取值在数组的下标
* @param source 数组
* @param value 值
*/
static indexOf(source, value) {
for (let i = 0; i < source.length; i++) {
if (source[i] === value) {
return i;
}
}
return -1;
}
/**
* 将值插入指定的位置
* @param source 数组
* @param index 位置
* @param value 值
*/
static insert(source, index, value) {
source.splice(index, 0, value);
}
/**
* 将值替换到指定下标
* @param source 数组
* @param index 位置
* @param value 值
*/
static replace(source, index, value) {
source.splice(index, 1, value);
}
/**
* 删除指定位置的数据
* @param source 数组
* @param index 下标
*/
static remove(source, index) {
source.splice(index, 1);
}
/**
* 删除指定值
* @param source 数组
* @param value 值
*/
static removeItem(source, value) {
this.remove(source, this.indexOf(source, value));
}
/**
* 判断两个数组是否相同
* 如果比较的值类型都为函数类型,则跳过比较
* @param first
* @param second
*/
static isSame(first, second) {
if (!this.isArray(first)) {
return false;
}
if (!this.isArray(second)) {
return false;
}
if (first.length !== second.length) {
return false;
}
for (let i = 0; i < first.length; i++) {
let firstValue = first[i];
let secondValue = second[i];
if (FastHelper.Functions.isFunction(firstValue) && FastHelper.Functions.isFunction(secondValue)) {
continue;
}
if (FastHelper.Objects.isObject(firstValue)) {
if (!FastHelper.Objects.isSame(firstValue, secondValue)) {
return false;
}
}
else if (this.isArray(firstValue)) {
if (!this.isSame(firstValue, secondValue)) {
return false;
}
}
else if (firstValue !== secondValue) {
return false;
}
}
return true;
}
}
FastHelper.Arrays = Arrays;
/**
* 字符串相关
*/
class Strings {
/**
* 构建唯一标识符
* @param prefix 前缀
*/
static buildOnlyCode(prefix) {
const md5 = require('md5');
const { v4: uuidv4 } = require('uuid');
return prefix + md5(uuidv4());
}
/**
* 判断目标值是否为字符串
* @param source
*/
static isString(source) {
if (!source) {
return false;
}
if (source instanceof String) {
return true;
}
return typeof source === 'string';
}
/**
* 判断是否为空,包含undefined
* @param source 目标值
*/
static isEmpty(source) {
return FastHelper.Objects.isEmpty(source);
}
/**
* 判断是否不为空
* @param source 目标值
*/
static isNotEmpty(source) {
return !this.isEmpty(source);
}
/**
* 获取字符串内容,如果为null或undefined则返回defaultValue
* @param source 目标值
* @param defaultValue 默认值
*/
static defaultValue(source, defaultValue) {
if (FastHelper.Strings.isEmpty(source)) {
return defaultValue;
}
return source;
}
/**
* 判断字符串是否以某个字符开头
* @param source 字符串
* @param prefix 字符
*/
static startWith(source, prefix) {
if (!prefix || prefix === "" || FastHelper.Strings.isEmpty(source) || prefix.length > source.length)
return false;
return source.substr(0, prefix.length) === prefix;
}
/**
* 判断字符串是否以某个字符结尾
* @param source 字符串
* @param suffix 字符
*/
static endWith(source, suffix) {
if (!suffix || suffix === "" || FastHelper.Strings.isEmpty(source) || suffix.length > source.length)
return false;
return source.substring(source.length - suffix.length) === suffix;
}
/**
* 首字母大写
* @param source 字符串
*/
static firstUpperCase(source) {
return source.replace(/^\S/, function (s) {
return s.toUpperCase();
});
}
/**
* 替换字符
* @param source 字符串
* @param oldStr 老的字符
* @param newStr 新的字符
*/
static replaceAll(source, oldStr, newStr) {
return source.replace(new RegExp(oldStr, 'g'), newStr);
}
/**
* 数字补0
* @param source
* @param length
*/
static prefixInteger(source, length) {
return (Array(length).join('0') + source).slice(-length);
}
}
FastHelper.Strings = Strings;
/**
* 布尔值相关操作
*/
class Booleans {
/**
* 判断目标值是否为布尔值
* @param source
*/
static isBoolean(source) {
if (source == null) {
return false;
}
if (source instanceof Boolean) {
return true;
}
return typeof source === 'boolean';
}
/**
* 转为布尔值
* @param source 目标值
* @param defaultValue 默认值,当为空或无效的布尔值时 返回
*/
static parse(source, defaultValue) {
if (FastHelper.Strings.isEmpty(defaultValue)) {
defaultValue = false;
}
if (FastHelper.Strings.isEmpty(source)) {
return defaultValue;
}
if (FastHelper.Strings.isString(source)) {
if (source === "0" || source.toLowerCase() === "false") {
return false;
}
if (source === "1" || source.toLowerCase() === "true") {
return true;
}
return defaultValue;
}
if (FastHelper.Numbers.isNumber(source)) {
if (source === 0) {
return false;
}
if (source === 1) {
return true;
}
return defaultValue;
}
if (this.isBoolean(source)) {
return source;
}
return defaultValue;
}
}
FastHelper.Booleans = Booleans;
/**
* 数字相关
*/
class Numbers {
/**
* 判断目标值是否为布尔值
* @param source
*/
static isNumber(source) {
if (source == null) {
return false;
}
if (source instanceof Number) {
return true;
}
return typeof source === 'number' && isFinite(source);
}
/**
* 转为纯数字
* @param source
* @param defaultValue
*/
static parse(source, defaultValue) {
if (FastHelper.Strings.isEmpty(source)) {
if (!defaultValue) {
defaultValue = 0;
}
return defaultValue;
}
const value = parseFloat(source);
if (isNaN(value)) {
if (!defaultValue) {
defaultValue = 0;
}
return defaultValue;
}
return value;
}
/**
* 提取纯数字[0-9],不包含小数点
* @param source 目标值
*/
static getNumberValue(source) {
if (FastHelper.Strings.isEmpty(source)) {
return 0;
}
return parseFloat(source.toString().replace(/[^0-9]/ig, ""));
}
}
FastHelper.Numbers = Numbers;
/**
* 日期相关
*/
class Dates {
/**
* 判断目标值是否是日期类型
* @param source
*/
static isDate(source) {
if (source == null) {
return false;
}
if (source instanceof Date) {
return true;
}
return toString.call(source) === '[object Date]';
}
/**
* 格式化日期
* @param source 日期
* @param pattern 格式类型,例如:yyyy-MM-DD HH:mm:ss,更多查看:https://momentjs.com/docs/#/displaying/
*/
static format(source, pattern) {
if (FastHelper.Strings.isEmpty(pattern)) {
return null;
}
if (FastHelper.Strings.isEmpty(source)) {
return null;
}
const moment = require('moment');
return moment(source).format(pattern);
}
/**
* 将字符串日期转换为Date对象
* @param source 日期值
*/
static parse(source) {
if (FastHelper.Strings.isEmpty(source)) {
return null;
}
const moment = require('moment');
return moment(source).toDate();
}
/**
* 格式化日期的友好展示,例如:1分钟前,3分钟前
* @param source
* @param level 精确级别 1 分钟 2 小时 3 天 4 周 5 月
*/
static formatNice(source, level) {
if (FastHelper.Strings.isEmpty(source)) {
return null;
}
if (level === undefined) {
level = 2;
}
const seconds = 1000;
const minute = seconds * 60;
const hour = minute * 60;
const day = hour * 24;
const week = day * 7;
const month = day * 30;
const time1 = new Date().getTime(); //当前的时间戳
const sourceDate = this.parse(source);
if (!sourceDate) {
return null;
}
const time2 = sourceDate.getTime();
const time = time1 - time2;
if (time <= minute) {
return "刚刚";
}
if (time / month >= 3) {
return this.format(sourceDate, "yyyy-MM-DD HH:mm");
}
let nice = true;
if (time / month >= 1) {
if (level >= 5) {
return parseInt(String(time / month)) + "个月前";
}
nice = false;
}
if (nice && time / week >= 1) {
if (level >= 4) {
return parseInt(String(time / week)) + "周前";
}
nice = false;
}
if (nice && time / day >= 1) {
if (level >= 3) {
return parseInt(String(time / day)) + "天前";
}
nice = false;
}
if (nice && time / hour >= 1) {
if (level >= 2) {
return parseInt(String(time / hour)) + "小时前";
}
nice = false;
}
if (nice && time / minute >= 1) {
if (level >= 1) {
return parseInt(String(time / minute)) + "分钟前";
}
}
return this.format(sourceDate, "yyyy-MM-DD HH:mm");
}
/**
* 将总时间转换为中文描述,最高描述到:天,例如:2天03时45分09秒
* @param timestamp 时间戳 单位:毫秒
* @param chinese 是否用中文表达,默认:true
*/
static toDescription(timestamp, chinese) {
if (chinese === undefined) {
chinese = true;
}
let seconds = 1000, minuteUnit = 1000 * 60, hourUnit = 1000 * 60 * 60, dayUnit = 1000 * 60 * 60 * 24;
let descriptions = [];
if (timestamp < seconds) {
descriptions.push("00" + (chinese ? '秒' : ''));
return descriptions.join("");
}
else if (timestamp < minuteUnit) {
descriptions.push(FastHelper.Strings.prefixInteger(parseInt(String(timestamp / seconds)), 2) + (chinese ? '秒' : ''));
return descriptions.join("");
}
if (timestamp < hourUnit) {
let minute = parseInt(String(timestamp / minuteUnit));
descriptions.push(FastHelper.Strings.prefixInteger(minute, 2) + (chinese ? '分' : ':'));
let nextTimestamp = timestamp - parseInt(String(minute * minuteUnit));
if (nextTimestamp === 0) {
descriptions.push("00" + (chinese ? '秒' : ''));
}
else {
descriptions.push(FastHelper.Dates.toDescription(nextTimestamp, chinese));
}
return descriptions.join("");
}
if (timestamp < dayUnit) {
let hour = parseInt(String(timestamp / hourUnit));
descriptions.push(FastHelper.Strings.prefixInteger(hour, 2) + (chinese ? '时' : ':'));
let nextTimestamp = timestamp - parseInt(String(hour * hourUnit));
if (nextTimestamp === 0) {
descriptions.push("00" + (chinese ? '分' : ':'));
descriptions.push("00" + (chinese ? '秒' : ''));
}
else {
descriptions.push(FastHelper.Dates.toDescription(nextTimestamp, chinese));
}
return descriptions.join("");
}
let day = parseInt(String(timestamp / dayUnit));
descriptions.push(day + (chinese ? '天' : 'day '));
let nextTimestamp = timestamp - parseInt(String(day * dayUnit));
if (nextTimestamp === 0) {
descriptions.push("00" + (chinese ? '时' : ':'));
descriptions.push("00" + (chinese ? '分' : ':'));
descriptions.push("00" + (chinese ? '秒' : ''));
}
else {
descriptions.push(FastHelper.Dates.toDescription(nextTimestamp, chinese));
}
return descriptions.join("");
}
}
FastHelper.Dates = Dates;
/**
* 函数相关操作
*/
class Functions {
/**
* 判断目标类型是否为函数
* @param source
*/
static isFunction(source) {
if (source == null) {
return false;
}
return !!source && typeof source === 'function';
}
/**
* 批量执行函数并获取返回值
* @param source 单个函数或函数数组
* @param params 执行函数携带的参数
*/
static run(source, ...params) {
const result = [];
if (this.isFunction(source)) {
result.push(source.apply(this, params));
}
else if (FastHelper.Arrays.isArray(source)) {
for (let i = 0; i < source.length; i++) {
result.push(source[i].apply(this, params));
}
}
else if (FastHelper.Strings.isString(source)) {
result.push(eval(source));
}
if (result.length == 1) {
return result[0];
}
else if (result.length > 1) {
return result;
}
return null;
}
/**
* 按顺序同步执行函数,当其中一个函数返回false时,则终止后续执行
* @param source 单个函数或函数数组
* @param params 执行函数携带的参数
*/
static async syncRun(source, ...params) {
if (FastHelper.Arrays.isArray(source)) {
for (let i = 0; i < source.length; i++) {
if (FastHelper.Functions.isFunction(source[i])) {
let result = await source[i].apply(this, params);
if (!FastHelper.Booleans.parse(result, false)) {
return;
}
}
}
}
else if (this.isFunction(source)) {
source.apply(this, params);
}
else if (FastHelper.Strings.isString(source)) {
eval(source);
}
}
}
FastHelper.Functions = Functions;
/**
* 节点相关
*/
class Elements {
/**
* 绑定节点属性变动的监听
* @param source 节点对象
* @param attrs 需要监听的属性集合
* @param callBack 回调
*/
static onAttributesChange(source, attrs, callBack) {
if (source) {
const observe = new MutationObserver(mutationsList => {
mutationsList.forEach((item, index) => {
if (callBack) {
callBack(item);
}
});
});
observe.observe(source, {
attributes: true,
attributeFilter: attrs
});
return observe;
}
return null;
}
/**
* 绑定当前节点子节点的变动的监听
* @param source 节点对象
* @param callBack 回调
*/
static onChildrenChange(source, callBack) {
if (source) {
const observe = new MutationObserver(mutationsList => {
mutationsList.forEach((item, index) => {
if (callBack) {
callBack(item);
}
});
});
observe.observe(source, {
childList: true,
subtree: true
});
return observe;
}
return null;
}
/**
* 在目标节点后追加新的节点
* @param targetEl 目标节点
* @param newEl 新的节点
*/
static insertAfter(targetEl, newEl) {
let parent = targetEl.parentNode;
if (parent.lastChild === targetEl) {
parent.appendChild(newEl);
}
else {
parent.insertBefore(newEl, targetEl.nextSibling);
}
}
/**
* 在目标节点前追加新的节点
* @param targetEl 目标节点
* @param newEl 新的节点
*/
static insertBefore(targetEl, newEl) {
let parent = targetEl.parentNode;
if (parent.lastChild === targetEl) {
parent.appendChild(newEl);
}
else {
parent.insertBefore(newEl, targetEl);
}
}
/**
* 获取目标节点的实际占位空间,包含的内外边距
* @param targetEl
*/
static getRealSize(targetEl) {
let childStyle = window.getComputedStyle(targetEl);
let height = FastHelper.Numbers.parse(childStyle.height);
let width = FastHelper.Numbers.parse(childStyle.width);
let vSpace = FastHelper.Numbers.parse(childStyle.marginTop)
+ FastHelper.Numbers.parse(childStyle.marginBottom)
+ FastHelper.Numbers.parse(childStyle.paddingTop)
+ FastHelper.Numbers.parse(childStyle.paddingBottom);
let hSpace = FastHelper.Numbers.parse(childStyle.marginLeft)
+ FastHelper.Numbers.parse(childStyle.marginRight)
+ FastHelper.Numbers.parse(childStyle.paddingLeft)
+ FastHelper.Numbers.parse(childStyle.paddingRight);
return { width: width + hSpace, height: height + vSpace };
}
/**
* 获取目标节点的selector
* @param targetEl
*/
static getPath(targetEl) {
let currEl = targetEl;
let domPath = [];
if (currEl.id) {
domPath.unshift('#' + currEl.id);
}
else {
while (currEl.nodeName.toLowerCase() !== "html") {
if (currEl.id) {
domPath.unshift('#' + currEl.id);
break;
}
else if (currEl.tagName.toLocaleLowerCase() === "body") {
domPath.unshift(currEl.tagName.toLowerCase());
}
else {
for (let i = 0; i < currEl.parentNode.childElementCount; i++) {
if (currEl.parentNode.children[i] === currEl) {
let className = currEl.getAttribute('class');
if (className) {
let selectors = currEl.className.split(/\s/g), array = [];
for (let j = 0; j < selectors.length; ++j) {
if (selectors[j].length > 0) {
array.push('.' + selectors[j]);
}
}
className = array.join("");
}
else {
className = "";
}
domPath.unshift(currEl.tagName.toLowerCase() + className + ':nth-child(' + (i + 1) + ')');
}
}
}
currEl = currEl.parentNode;
}
}
return domPath.join(' > ');
}
/**
* 判断目标元素的滚动条 是否已滚动到底部
* @param targetEl
* @param scrollNimble 检测的灵敏度,越大越灵敏
*/
static isScrollBottom(targetEl, scrollNimble) {
const scrollTop = targetEl.scrollTop;
const elHeight = targetEl.getBoundingClientRect().height;
const scrollHeight = targetEl.scrollHeight;
return scrollTop + elHeight >= (scrollHeight - scrollNimble);
}
}
FastHelper.Elements = Elements;
/**
* css样式相关代码
*/
class Styles {
/**
* 动态加载css代码
* @param style css代码
*/
static loadCssCode(style) {
let oHead = document.getElementsByTagName('head').item(0);
let oStyle = document.createElement("style");
oStyle.type = "text/css";
if (oStyle["styleSheet"]) {
oStyle["styleSheet"].cssText = style;
}
else {
oStyle.innerHTML = style;
}
oHead.appendChild(oStyle);
}
}
FastHelper.Styles = Styles;
/**
* 事件相关工具
*/
class Events {
/**
* 获取异常信息的堆栈信息
* @param event
*/
static geErrorInfo(event) {
if (event) {
if (event.error && event.error.stack) {
return event.error.stack;
}
else if (event.stack) {
return event.stack;
}
else if (event.message) {
return event.message;
}
else if (event.reason) {
return event.reason;
}
return event.toString();
}
return "";
}
}
FastHelper.Events = Events;
/**
* 高德地图操作工具类
*/
class AMap {
/**
* 加载高德地图的js文件,具体查看 https://lbs.amap.com/api/jsapi-v2/guide/abc/load
* @param options
*/
static load(options) {
let AMapLoader = require("fastchar-appjs/src/amap/amap-loader.js");
return AMapLoader.load(options);
}
}
FastHelper.AMap = AMap;
/**
* 窗口window操作对象
*/
class Windows {
/**
* 复制文本到剪贴板里
* @param content 内容
*/
static copyToBoard(content) {
let oInput = document.createElement('textarea');
oInput.value = content;
document.body.appendChild(oInput);
oInput.select();
document.execCommand("Copy");
oInput.style.display = 'none';
oInput.remove();
}
/**
* 添加window的onLoad事件,如果存在onLoad函数名则进行合并
* @param callBack
*/
static addOnLoad(callBack) {
let oldOnload = window.onload;
if (FastHelper.Functions.isFunction(oldOnload)) {
window.onload = function () {
oldOnload();
callBack();
};
}
else {
window.onload = callBack;
}
}
/**
* 添加window的函数,如果存在相同的函数名则进行合并
* @param functionName 函数名
* @param functionBody 函数执行的代码
*/
static addFunction(functionName, functionBody) {
let targetFunction = window[functionName];
if (FastHelper.Functions.isFunction(targetFunction)) {
window[functionName] = function () {
targetFunction.apply(this, arguments);
functionBody.apply(this, arguments);
};
}
else {
window[functionName] = functionBody;
}
}
/**
* 安全的获取窗口宽度,有的手机渲染后document.body.clientWidth 莫明会返回0
*/
static safeGetClientWidth() {
return Math.max(document.documentElement.clientWidth, document.body.clientWidth);
}
/**
* 安全的获取窗口高度,有的手机渲染后document.body.clientHeight 莫明会返回0
*/
static safeGetClientHeight() {
return Math.max(document.documentElement.clientHeight, document.body.clientHeight);
}
}
FastHelper.Windows = Windows;
/**
* JSON相关功能
*/
class Json {
constructor() {
}
/**
* 将json字符串转成对象
* @param jsonStr json字符串
* @returns {Object}
*/
static jsonToObject(jsonStr) {
try {
return JSON.parse(jsonStr);
}
catch (e) {
}
return null;
}
/**
* 将对象转成json字符串
* @param jsonObj 待转换的对象
* @returns {string}
*/
static objectToJson(jsonObj) {
try {
return JSON.stringify(jsonObj);
}
catch (e) {
}
return null;
}
/**
* 将对象转成json字符串,注意此方法将转换函数对象,慎用!
* @param jsonObj 待转换的对象
* @return {string}
*/
static objectToJsonUnsafe(jsonObj) {
return JSON.stringify(jsonObj, function (key, val) {
if (typeof val === 'function') {
return val.toString();
}
return val;
});
}
/**
* 将json字符串转成对象
* @param jsonStr json字符串
* @returns {Object}
*/
static jsonToObjectUnsafe(jsonStr) {
try {
return JSON.parse(jsonStr, function (k, v) {
if (v.indexOf && v.indexOf('function') > -1) {
// noinspection UnnecessaryReturnStatementJS
return eval("(function(){return " + v + " })()");
}
return v;
});
}
catch (e) {
}
return null;
}
/**
* 合并两个json对象
* @param jsonData1 json对象
* @param jsonData2 json对象
* @return 合并后的新对象
*/
static mergeJson(jsonData1, jsonData2) {
let newJsonData = {};
if (!Objects.isEmpty(jsonData1)) {
for (let property in jsonData1) {
newJsonData[property] = jsonData1[property];
}
}
if (!Objects.isEmpty(jsonData2)) {
for (let property in jsonData2) {
newJsonData[property] = jsonData2[property];
}
}
return newJsonData;
}
}
FastHelper.Json = Json;
})(FastHelper = exports.FastHelper || (exports.FastHelper = {}));