Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
CN-Tower committed Oct 4, 2020
1 parent f05452b commit e956a4b
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 77 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#### In HTML
```html
<script src="https://unpkg.com/[email protected].1/format2json.min.js"></script>
<script src="https://unpkg.com/[email protected].2/format2json.min.js"></script>
<script>
const source = `{"zjson":"ZJSON","description":"Online json formatter","version":"v4.1.8","updateTime":"2018-11-23","url":"http:https://zjson.net","project":"http:https://github.com/CN-Tower/zjson","language":["中文(简体)","English"],"keywords":["zjson","json formatter"],"content":{"array":["element 001","element 002"],"boolean":true,"null":null,"number":123,"string":"Hello World","object":{"property":"value","key":"val"}}}`;
const jsonLike = await format2json(source, { resultOnly: true });
Expand Down Expand Up @@ -134,7 +134,7 @@ Run: `format2json -i 4 -k "" -v "'"`
√ Input a string to foramt: · [{name: "Tom", age: 28, gender: "male"}]
==================================================================
[10:42:20] format-to-json(1.0.0)
[10:42:20] format-to-json(1.0.2)
------------------------------------------------------------------
[
{
Expand Down
81 changes: 43 additions & 38 deletions format2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,16 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol
err: function err() {
return 'Parse Error, an excessive abnormal Json!';
}
};

/**
* =================================================================
* The main function of `format-to-json` util.
* @param { string } source
* @param { object } options
* =================================================================
*/
};function formatToJson(source, options) {
/**
* =================================================================
* The main function of `format-to-json` util.
* @param { string } source
* @param { object } options
* =================================================================
*/
function formatToJson(source, options) {
return new Promise(function (resolve) {
initVariables(source);
if (options) {
Expand All @@ -126,46 +127,50 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol
if (typeof options.isUnscape === 'boolean') {
fmtOptions.isUnscape = options.isUnscape;
}
if (['\'', '"', ''].includes(options.keyQtMark)) {
if (['\'', '"', ''].indexOf(options.keyQtMark) > -1) {
fmtOptions.keyQtMark = options.keyQtMark;
}
if (['\'', '"'].includes(options.valQtMark)) {
if (['\'', '"'].indexOf(options.valQtMark) > -1) {
fmtOptions.valQtMark = options.valQtMark;
}
}
baseIndent = getBaseIndent();
doFormatBytry(resolve);
});
}

function doFormatBytry(resolve) {
try {
try {
try {
if (fmtSource !== '') eval('fmtSource = ' + fmtSource);
if (fmtSource === '' || ['object', 'boolean'].includes(typeof fmtSource === 'undefined' ? 'undefined' : _typeof(fmtSource))) {
doNormalFormat(fmtSource);
} else {
if (fmtOptions.isUnscape) {
fmtSource = fmtSource.replace(/\\"/mg, '"').replace(/\\\\/mg, '\\');
}
doSpecialFormat();
}
} catch (err) {
// console.log(err);
if (fmtSource !== '') eval('fmtSource = ' + fmtSource);
if (fmtSource === '' || ['object', 'boolean'].indexOf(typeof fmtSource === 'undefined' ? 'undefined' : _typeof(fmtSource)) > -1) {
doNormalFormat(fmtSource);
} else {
if (fmtOptions.isUnscape) {
fmtSource = fmtSource.replace(/\\"/mg, '"').replace(/\\\\/mg, '\\');
}
doSpecialFormat();
}
} catch (err) {
// console.log(err);
isFmtError = true;
} finally {
setFmtStatus();
resolve(resultOnly ? fmtResult : {
result: fmtResult,
status: {
fmtType: fmtType, fmtSign: fmtSign, fmtLines: fmtLines, message: message,
errFormat: errFormat, errIndex: errIndex, errExpect: errExpect, errNear: errNear
}
});
if (fmtOptions.isUnscape) {
fmtSource = fmtSource.replace(/\\"/mg, '"').replace(/\\\\/mg, '\\');
}
doSpecialFormat();
}
});
} catch (err) {
// console.log(err);
isFmtError = true;
} finally {
setFmtStatus();
resolve(resultOnly ? fmtResult : {
result: fmtResult,
status: {
fmtType: fmtType, fmtSign: fmtSign, fmtLines: fmtLines, message: message,
errFormat: errFormat, errIndex: errIndex, errExpect: errExpect, errNear: errNear
}
});
}
}

/**
Expand All @@ -175,7 +180,7 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol
* =================================================================
*/
function doNormalFormat(src) {
if ([true, false, null, ''].includes(src)) {
if ([true, false, null, ''].indexOf(src) > -1) {
return fmtResult += String(src);
}
if (fmtOptions.isStrict) {
Expand Down Expand Up @@ -465,16 +470,16 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol
if (isSrcValid) {
switch (exceptType) {
case 'val':
if (':,}])!'.includes(sign)) {
if (':,}])!'.indexOf(sign) > -1) {
setFmtError('val');
}break;
case 'ost':
if (!'\'"unbN'.includes(sign)) {
if ('\'"unbN'.indexOf(sign) === -1) {
setFmtError('ost');
}break;
case 'end':
var endBracket = getBracketPair(exceptSign);
if (![',', endBracket].includes(sign)) {
if ([',', endBracket].indexOf(sign) === -1) {
setFmtError('end', endBracket);
}break;
case 'col':
Expand Down Expand Up @@ -545,7 +550,7 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol
default:
fmtType = 'danger';break;
}
if (['ost', 'col', 'val', 'end'].includes(sign)) {
if (['ost', 'col', 'val', 'end'].indexOf(sign) > -1) {
errFormat = true;
isSrcValid = false;
errExpect = brc;
Expand Down
2 changes: 1 addition & 1 deletion format2json.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "format-to-json",
"version": "1.0.1",
"version": "1.0.2",
"description": "Format string to a json like template.",
"main": "index.js",
"bin": {
Expand Down
66 changes: 35 additions & 31 deletions src/format2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
war: rowIdx => `Formated ${rowIdx} lines, abnormal JSON source!`,
scc: rowIdx => `Success formated ${rowIdx} lines!`,
err: () => `Parse Error, an excessive abnormal Json!`,
}
};

/**
* =================================================================
Expand Down Expand Up @@ -114,46 +114,50 @@
if (typeof options.isUnscape === 'boolean') {
fmtOptions.isUnscape = options.isUnscape;
}
if (['\'', '"', ''].includes(options.keyQtMark)) {
if (['\'', '"', ''].indexOf(options.keyQtMark) > -1) {
fmtOptions.keyQtMark = options.keyQtMark;
}
if (['\'', '"'].includes(options.valQtMark)) {
if (['\'', '"'].indexOf(options.valQtMark) > -1) {
fmtOptions.valQtMark = options.valQtMark;
}
}
baseIndent = getBaseIndent();
doFormatBytry(resolve);
});
}

function doFormatBytry(resolve) {
try {
try {
try {
if (fmtSource !== '') eval(`fmtSource = ${fmtSource}`);
if (fmtSource === '' || ['object', 'boolean'].includes(typeof fmtSource)) {
doNormalFormat(fmtSource);
} else {
if (fmtOptions.isUnscape) {
fmtSource = fmtSource.replace(/\\"/mg, '"').replace(/\\\\/mg, '\\');
}
doSpecialFormat();
}
} catch (err) {
// console.log(err);
if (fmtSource !== '') eval(`fmtSource = ${fmtSource}`);
if (fmtSource === '' || ['object', 'boolean'].indexOf(typeof fmtSource) > -1) {
doNormalFormat(fmtSource);
} else {
if (fmtOptions.isUnscape) {
fmtSource = fmtSource.replace(/\\"/mg, '"').replace(/\\\\/mg, '\\');
}
doSpecialFormat();
}
} catch (err) {
// console.log(err);
isFmtError = true;
} finally {
setFmtStatus();
resolve(resultOnly ? fmtResult : {
result: fmtResult,
status: {
fmtType, fmtSign, fmtLines, message,
errFormat, errIndex, errExpect, errNear,
}
});
if (fmtOptions.isUnscape) {
fmtSource = fmtSource.replace(/\\"/mg, '"').replace(/\\\\/mg, '\\');
}
doSpecialFormat();
}
});
} catch (err) {
// console.log(err);
isFmtError = true;
} finally {
setFmtStatus();
resolve(resultOnly ? fmtResult : {
result: fmtResult,
status: {
fmtType, fmtSign, fmtLines, message,
errFormat, errIndex, errExpect, errNear,
}
});
}
}

/**
Expand All @@ -163,7 +167,7 @@
* =================================================================
*/
function doNormalFormat(src) {
if ([true, false, null, ''].includes(src)) {
if ([true, false, null, ''].indexOf(src) > -1) {
return fmtResult += String(src);
}
if (fmtOptions.isStrict) {
Expand Down Expand Up @@ -442,16 +446,16 @@
if (isSrcValid) {
switch (exceptType) {
case 'val':
if (':,}])!'.includes(sign)) {
if (':,}])!'.indexOf(sign) > -1) {
setFmtError('val');
} break;
case 'ost':
if (!'\'"unbN'.includes(sign)) {
if ('\'"unbN'.indexOf(sign) === -1) {
setFmtError('ost');
} break;
case 'end':
const endBracket = getBracketPair(exceptSign);
if (![',', endBracket].includes(sign)) {
if ([',', endBracket].indexOf(sign) === -1) {
setFmtError('end', endBracket);
} break;
case 'col':
Expand Down Expand Up @@ -517,7 +521,7 @@
case 'scc': fmtType = 'success'; break;
default: fmtType = 'danger'; break;
}
if (['ost', 'col', 'val', 'end'].includes(sign)) {
if (['ost', 'col', 'val', 'end'].indexOf(sign) > -1) {
errFormat = true;
isSrcValid = false;
errExpect = brc;
Expand Down
13 changes: 9 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
var format2json = require('../');
const fn = require('funclib');
const format2json = require('../');

(async () => {
const source = '{"zjson":"ZJSON","description":"Online json formatter","version":"v4.1.8","updateTime":"2018-11-23","url":"http:https://zjson.net","project":"http:https://github.com/CN-Tower/zjson","language":["中文(简体)","English"],"keywords":["zjson","json formatter"],"content":{"array":["element 001","element 002"],"boolean":true,"null":null,"number":123,"string":"Hello World","object":{"property":"value","key":"val"}}}';
jsonLike = await format2json(source, { resultOnly: true });
console.log(jsonLike);
const source = '{"zjson":"ZJSON","description","Online json formatter","version":"v4.1.8","updateTime":"2018-11-23","url":"http:https://zjson.net","project":"http:https://github.com/CN-Tower/zjson","language":["中文(简体)","English"],"keywords":["zjson","json formatter"],"content":{"array":["element 001","element 002"],"boolean":true,"null":null,"number":123,"string":"Hello World","object":{"property":"value","key":"val"}}}';
fmtInfo = await format2json(source);
fn.log('', { title: `format-to-json`, pre: true });
console.log(fn.chalk(fmtInfo.result, 'cyan'));
console.log(fn.array(66, '-').join(''));
console.log(fmtInfo.status);
fn.log('', { end: true });
})();

0 comments on commit e956a4b

Please sign in to comment.