init
This commit is contained in:
70
xhs-mini-demos/util/formatJson.js
Normal file
70
xhs-mini-demos/util/formatJson.js
Normal file
@@ -0,0 +1,70 @@
|
||||
//格式化json
|
||||
function formatJsonForNotes (json, options) {
|
||||
var reg = null,
|
||||
formatted = '',
|
||||
pad = 0,
|
||||
PADDING = ' '; // (缩进)可以使用'\t'或不同数量的空格
|
||||
// 可选设置
|
||||
options = options || {};
|
||||
// 在 '{' or '[' follows ':'位置移除新行
|
||||
options.newlineAfterColonIfBeforeBraceOrBracket = (options.newlineAfterColonIfBeforeBraceOrBracket === true) ? true : false;
|
||||
// 在冒号后面加空格
|
||||
options.spaceAfterColon = (options.spaceAfterColon === false) ? false : true;
|
||||
// 开始格式化...
|
||||
if (typeof json !== 'string') {
|
||||
// 确保为JSON字符串
|
||||
json = JSON.stringify(json);
|
||||
} else {
|
||||
//已经是一个字符串,所以解析和重新字符串化以删除额外的空白
|
||||
json = JSON.parse(json);
|
||||
json = JSON.stringify(json);
|
||||
}
|
||||
// 在花括号前后添加换行
|
||||
reg = /([{}])/g;
|
||||
json = json.replace(reg, '\r\n$1\r\n');
|
||||
// 在方括号前后添加新行
|
||||
reg = /([[\]])/g;
|
||||
json = json.replace(reg, '\r\n$1\r\n');
|
||||
// 在逗号后添加新行
|
||||
reg = /(,)/g;
|
||||
json = json.replace(reg, '$1\r\n');
|
||||
// 删除多个换行
|
||||
reg = /(\r\n\r\n)/g;
|
||||
json = json.replace(reg, '\r\n');
|
||||
// 删除逗号前的换行
|
||||
reg = /\r\n,/g;
|
||||
json = json.replace(reg, ',');
|
||||
// 可选格式...
|
||||
if (!options.newlineAfterColonIfBeforeBraceOrBracket) {
|
||||
reg = /:\r\n\{/g;
|
||||
json = json.replace(reg, ':{');
|
||||
reg = /:\r\n\[/g;
|
||||
json = json.replace(reg, ':[');
|
||||
}
|
||||
if (options.spaceAfterColon) {
|
||||
reg = /:/g;
|
||||
json = json.replace(reg, ': ');
|
||||
}
|
||||
json.split('\r\n').forEach(function(node) {
|
||||
var i = 0,
|
||||
indent = 0,
|
||||
padding = '';
|
||||
if (node.match(/\{$/) || node.match(/\[$/)) {
|
||||
indent = 1;
|
||||
} else if (node.match(/\}/) || node.match(/\]/)) {
|
||||
if (pad !== 0) {
|
||||
pad -= 1;
|
||||
}
|
||||
} else {
|
||||
indent = 0;
|
||||
}
|
||||
for (i = 0; i < pad; i++) {
|
||||
padding += PADDING;
|
||||
}
|
||||
formatted += padding + node + '\r\n';
|
||||
pad += indent;
|
||||
});
|
||||
return formatted;
|
||||
}
|
||||
|
||||
module.exports = formatJsonForNotes;
|
||||
12
xhs-mini-demos/util/mergePageOptions.js
Normal file
12
xhs-mini-demos/util/mergePageOptions.js
Normal file
@@ -0,0 +1,12 @@
|
||||
module.exports = function(options, templateJsOptions) {
|
||||
const data = (options.data || (options.data = {}));
|
||||
options.data = {
|
||||
...data,
|
||||
...templateJsOptions.data
|
||||
};
|
||||
|
||||
return {
|
||||
...templateJsOptions,
|
||||
...options
|
||||
};
|
||||
};
|
||||
94
xhs-mini-demos/util/util.js
Normal file
94
xhs-mini-demos/util/util.js
Normal file
@@ -0,0 +1,94 @@
|
||||
function formatTime(time) {
|
||||
if (typeof time !== 'number' || time < 0) {
|
||||
return time;
|
||||
}
|
||||
|
||||
const hour = parseInt(time / 3600, 10);
|
||||
time %= 3600;
|
||||
const minute = parseInt(time / 60, 10);
|
||||
time = parseInt(time % 60, 10);
|
||||
const second = time;
|
||||
|
||||
return ([hour, minute, second]).map(n => {
|
||||
n = n.toString();
|
||||
return n[1] ? n : `0${n}`;
|
||||
}).join(':');
|
||||
}
|
||||
|
||||
function formatLocation(longitude, latitude) {
|
||||
if (typeof longitude === 'string' && typeof latitude === 'string') {
|
||||
longitude = parseFloat(longitude);
|
||||
latitude = parseFloat(latitude);
|
||||
}
|
||||
|
||||
longitude = longitude.toFixed(2);
|
||||
latitude = latitude.toFixed(2);
|
||||
|
||||
return {
|
||||
longitude: longitude.toString().split('.'),
|
||||
latitude: latitude.toString().split('.'),
|
||||
};
|
||||
}
|
||||
|
||||
function fib(n) {
|
||||
if (n < 1) return 0;
|
||||
if (n <= 2) return 1;
|
||||
return fib(n - 1) + fib(n - 2);
|
||||
}
|
||||
|
||||
function formatLeadingZeroNumber(n, digitNum = 2) {
|
||||
n = n.toString();
|
||||
const needNum = Math.max(digitNum - n.length, 0);
|
||||
return new Array(needNum).fill(0).join('') + n;
|
||||
}
|
||||
|
||||
function formatDateTime(date, withMs = false) {
|
||||
const year = date.getFullYear();
|
||||
const month = date.getMonth() + 1;
|
||||
const day = date.getDate();
|
||||
const hour = date.getHours();
|
||||
const minute = date.getMinutes();
|
||||
const second = date.getSeconds();
|
||||
const ms = date.getMilliseconds();
|
||||
|
||||
let ret = `${[year, month, day].map(value => formatLeadingZeroNumber(value, 2)).join('-')
|
||||
} ${[hour, minute, second].map(value => formatLeadingZeroNumber(value, 2)).join(':')}`;
|
||||
if (withMs) {
|
||||
ret += `.${formatLeadingZeroNumber(ms, 3)}`;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
function compareVersion(v1, v2) {
|
||||
v1 = v1.split('.');
|
||||
v2 = v2.split('.');
|
||||
const len = Math.max(v1.length, v2.length);
|
||||
|
||||
while (v1.length < len) {
|
||||
v1.push('0');
|
||||
}
|
||||
while (v2.length < len) {
|
||||
v2.push('0');
|
||||
}
|
||||
|
||||
for (let i = 0; i < len; i++) {
|
||||
const num1 = parseInt(v1[i], 10);
|
||||
const num2 = parseInt(v2[i], 10);
|
||||
|
||||
if (num1 > num2) {
|
||||
return 1;
|
||||
} if (num1 < num2) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
formatTime,
|
||||
formatLocation,
|
||||
fib,
|
||||
formatDateTime,
|
||||
compareVersion,
|
||||
};
|
||||
Reference in New Issue
Block a user