mirror of
https://github.com/YMFE/yapi.git
synced 2025-03-01 14:05:44 +08:00
fix: 封装regexp_parse
This commit is contained in:
parent
b0745dd616
commit
fff8a8befd
@ -3,3 +3,32 @@ import moment from 'moment'
|
||||
exports.formatTime = (timestamp) => {
|
||||
return moment.unix(timestamp).format("YYYY-MM-DD HH:mm:ss")
|
||||
}
|
||||
|
||||
exports.regexp_parse = regexp_parse;
|
||||
function regexp_parse(p,c) {//遍历json中的regexp字符串,并将其转为RegExp对象。其中p为被转换的json,c为存储对象。
|
||||
c = c || {};
|
||||
for (var i in p) {
|
||||
if(! p.hasOwnProperty(i)){
|
||||
continue;
|
||||
}
|
||||
if (typeof p[i] === 'object') {
|
||||
c[i] = (p[i].constructor === Array) ? [] : {};
|
||||
regexp_parse(p[i], c[i]);
|
||||
} else {
|
||||
if(/^\/.+\/$/.test(p[i])){
|
||||
var regexpStr = p[i].substring(1,p[i].length-1);
|
||||
try{
|
||||
c[i] = new RegExp(regexpStr);
|
||||
}
|
||||
catch(e)
|
||||
{
|
||||
c[i] = p[i];
|
||||
}
|
||||
}else{
|
||||
c[i] = p[i];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
@ -3,41 +3,8 @@ import { Card } from 'antd'
|
||||
import { connect } from 'react-redux'
|
||||
import PropTypes from 'prop-types'
|
||||
import Mock from 'mockjs'
|
||||
import common from '../../../common';
|
||||
|
||||
|
||||
function regexp_parse(p,c) {
|
||||
c = c || {};
|
||||
for (let i in p) {
|
||||
if(! p.hasOwnProperty(i)){
|
||||
continue;
|
||||
}
|
||||
if (typeof p[i] === 'object') {
|
||||
c[i] = (p[i].constructor === Array) ? [] : {};
|
||||
regexp_parse(p[i], c[i]);
|
||||
} else {
|
||||
if(/^\/.+\/$/.test(p[i])){
|
||||
try{
|
||||
let regexpStr = p[i].substring(1,p[i].length-1);
|
||||
// for(let i = 0;i<regexpStr.length;i++){
|
||||
// if("* . ? + $ ^ [ ] ( ) { } | \ /".indexOf(regexpStr[i])>-1){
|
||||
// regexpStr[i] = "\\"+regexpStr[i];
|
||||
// }
|
||||
// }
|
||||
|
||||
c[i] = new RegExp(regexpStr);
|
||||
}
|
||||
catch(e)
|
||||
{
|
||||
c[i] = p[i];
|
||||
}
|
||||
}else{
|
||||
c[i] = p[i];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return c;
|
||||
}
|
||||
@connect(
|
||||
state => {
|
||||
return {
|
||||
@ -63,12 +30,17 @@ class Result extends Component {
|
||||
let json, j;
|
||||
try{
|
||||
json = JSON.parse(resParams);
|
||||
json = regexp_parse(json);
|
||||
json = common.regexp_parse(json);
|
||||
}catch(e){
|
||||
json = false;
|
||||
}
|
||||
if(json !== false){
|
||||
j = JSON.stringify(Mock.mock(json), null, " ");
|
||||
try{
|
||||
j = JSON.stringify(Mock.mock(json), null, " ");
|
||||
}catch(e){
|
||||
j = ""
|
||||
}
|
||||
|
||||
}else{
|
||||
j = mockJson
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user