Skip to content

Commit

Permalink
upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
CN-Tower committed Apr 5, 2021
1 parent d8a27cb commit f2f5b75
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 129 deletions.
59 changes: 30 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@
- [In HTML](#in-html)
- [In JavaScript](#in-javascript)
* [Intterface](#Interface)
- [format2json](#mehtod-format2json)
- [FormatOptions](#interface-formatoptions)
- [FormatResult](#interface-formatresult)
- [fmt2json](#mehtod-fmt2json)
- [Options](#interface-options)
- [Result](#interface-result)
* [Terminal](#Terminal)

## Usages

#### In HTML
```html
<script src="https://unpkg.com/[email protected]/format2json.min.js"></script>
<script src="https://unpkg.com/[email protected]/fmt2json.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 });
const jsonLike = await fmt2json(source, { resultOnly: true });
console.log(jsonLike);
</script>
```
#### In Javascript
Run: `npm install format-to-json --save`;
```javascript
const format2json = require('format-to-json');
const fmt2json = require('format-to-json');

(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"}}}';

fmtInfo = await format2json(source);
fmtInfo = await fmt2json(source);
console.log(fmtInfo.result);
})();
```
Expand Down Expand Up @@ -79,32 +79,32 @@ Result:

## Interface

#### [Mehtod] format2json
#### [Mehtod] fmt2json
```typescript
format2json(source: string, options?: FormatOptions): Promise<FormatResult | string>;
fmt2json(source: string, options?: Options): Promise<Result | string>;
```
#### [Interface] FormatOptions
#### [Interface] Options
```typescript
interface FormatOptions {
interface Options {
indent?: number; // Integer, Large then 0, default: 2
isExpand?: boolean; // Default: true
isStrict?: boolean; // Default: false
isEscape?: boolean; // Default: false
isUnscape?: boolean; // Default: false
expand?: boolean; // Default: true
strict?: boolean; // Default: false
escape?: boolean; // Default: false
unscape?: boolean; // Default: false
keyQtMark?: "'" | "\"" | ""; // Default: "\""
valQtMark?: "'" | "\""; // Default: "\""
}
```
#### [Interface] FormatResult
#### [Interface] Result
```typescript
// If `{ resultOnly: true }` in option,
// Just eturn the format result string.
interface FormatResult {
interface Result {
result: string;
status: {
fmtLines: number;
fmtType: 'info' | 'success' | 'warning' | 'danger';
fmtSign: 'ost' | 'col' | 'val' | 'end' | 'war' | 'scc' | 'err';
fmtLines: number;
message: string;
errFormat: boolean;
errIndex: number;
Expand All @@ -121,21 +121,22 @@ interface FormatResult {
</p>

Run: `npm install -g format-to-json`
Run: `format2json -h`
Run: `fmt2json -h`
```terminal
Usage: format2json [options]
Usage: fmt2json [options]
Options:
-V, --version output the version number
-i, --indent [indent] Indnet for the format.
-k, --keyQtMark [keyQtMark] Key quotation mark.
-v, --valQtMark [valQtMark] Value quotation mark.
-S, --isStrict Strict format to a JSON template.
-U, --isUnescape Unescape the source.
-R, --resultOnly Print the format result only.
-h, --help output usage information
-V, --version output the version number
-v, --version output the version number
-i, --indent <indent> Indnet number.
-q, --qtMark <qtMark> Quotation mark, one of ['""', "''", '"', "'"] (default: "\"\"")
-e, --escape Escape format result.
-u, --unescape Unescape source before format.
-s, --strict Strict mode.
-r, --resultOnly Result only, not return the format info.
-h, --help output usage information
```
Run: `format2json -i 4 -k "" -v "'"`
Run: `fmt2json -i 4 -q "'"`
```terminal
√ Input a string to foramt: · [{name: "Tom", age: 28, gender: "male"}]
Expand Down
59 changes: 59 additions & 0 deletions bin/fmt2json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env node

const fn = require('funclib');
const program = require('commander');
const { prompt } = require('enquirer');
const package = require('../package.json');
const fmt2json = require('../fmt2json');

program.version(package.version)
.option('-v, --version', 'output the version number')
.option('-i, --indent <indent>', 'Indnet number.')
.option('-q, --qtMark <qtMark>', `Quotation mark, one of ['""', "''", '"', "'"]`, '""')
.option('-e, --escape', 'Escape format result.')
.option('-u, --unescape', 'Unescape source before format.')
.option('-s, --strict', 'Strict mode.')
.option('-r, --resultOnly', 'Result only, not return the format info.')
.parse(process.argv);

const { indent, qtMark, strict, unescape, resultOnly } = program;

const options = {};
if (indent && indent.match(/^([1-9])$/)) {
options.indent = +indent;
}
if(['""', "''", '"', "'"].includes(qtMark)) {
const qtArr = qtMark.split();
if (qtMark.length === 2) {
options.keyQtMark = qtArr[0];
options.valQtMark = qtArr[1];
} else {
options.keyQtMark = '';
options.valQtMark = qtArr[0];
}
} else {
throw new Error(`qtMark mast one of ['""', "''", '"', "'"], for key and value, single mark means no key mark just for value.`);
}
if (strict) options.strict = true;
if (unescape) options.unescape = true;
if (resultOnly) options.resultOnly = true;

prompt({
type: 'input',
name: 'source',
message: 'Input a string to foramt:'
}).then(({ source }) => {
if (resultOnly) {
fmt2json(source, options).then(result => {
console.log(fn.chalk(result, 'cyan'));
});
} else {
fmt2json(source, options).then(res => {
fn.log('', { title: `format-to-json(${package.version})`, pre: true });
console.log(fn.chalk(res.result, 'cyan'));
console.log(fn.array(66, '-').join(''));
console.log(res.status);
fn.log('', { end: true });
});
}
});
47 changes: 0 additions & 47 deletions bin/format2json.js

This file was deleted.

18 changes: 9 additions & 9 deletions format2json.js → fmt2json.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license
* format-to-json v1.0.4
* format-to-json v2.0.1
* GitHub Repository <https://github.com/CN-Tower/format-to-json>
* Released under MIT license <https://github.com/CN-Tower/format-to-json/blob/master/LICENSE>
*/
Expand Down Expand Up @@ -88,17 +88,17 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol
if (typeof options.resultOnly === 'boolean') {
resultOnly = options.resultOnly;
}
if (typeof options.isExpand === 'boolean') {
OPTIONS.isExpand = options.isExpand;
if (typeof options.expand === 'boolean') {
OPTIONS.isExpand = options.expand;
}
if (typeof options.isStrict === 'boolean') {
OPTIONS.isStrict = options.isStrict;
if (typeof options.strict === 'boolean') {
OPTIONS.isStrict = options.strict;
}
if (typeof options.isEscape === 'boolean') {
OPTIONS.isEscape = options.isEscape;
if (typeof options.escape === 'boolean') {
OPTIONS.isEscape = options.escape;
}
if (typeof options.isUnscape === 'boolean') {
OPTIONS.isUnscape = options.isUnscape;
if (typeof options.unscape === 'boolean') {
OPTIONS.isUnscape = options.unscape;
}
if (typeof options.indent === 'number' && options.indent > 0) {
OPTIONS.indent = options.indent;
Expand Down
Loading

0 comments on commit f2f5b75

Please sign in to comment.