Skip to content

Commit

Permalink
upgrade to 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
cntower committed Jan 30, 2023
1 parent e392aa3 commit cb972fb
Show file tree
Hide file tree
Showing 13 changed files with 1,355 additions and 1,255 deletions.
13 changes: 11 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
{
"presets": ["env"]
}
"presets": [
[
"@babel/preset-env",
{
"exclude": [
"transform-typeof-symbol"
]
}
]
]
}
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"printWidth": 120,
"singleQuote": true,
"semi": true,
"tabWidth": 2,
"trailingComma": "es5"
}
89 changes: 51 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,46 @@
[![npm](https://img.shields.io/npm/v/format-to-json.svg)](https://www.npmjs.com/package/format-to-json)
[![LICENSE MIT](https://img.shields.io/npm/l/format-to-json.svg)](https://github.com/CN-Tower/format-to-json/blob/master/LICENSE)

> Format string to a json like template
> Format string to a json like template
* [Usages](#Usages)
- [In HTML](#in-html)
- [In JavaScript](#in-javascript)
* [Interface](#Interface)
- [Usages](#Usages)
- [In html](#in-html)
- [In javascript](#in-javascript)
- [Interface](#Interface)
- [fmt2json](#mehtod-fmt2json)
- [Options](#interface-options)
- [Result](#interface-result)
* [Terminal](#Terminal)
- [FormatOptions](#interface-formatoptions)
- [FormatResult](#interface-formatresult)
- [Terminal](#Terminal)

## Usages

#### In HTML
#### In html

```html
<script src="https://unpkg.com/format-to-json@2.1.2/fmt2json.min.js"></script>
<script src="https://unpkg.com/format-to-json@3.0.0/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"}}}`;
(async () => {
const jsonLike = await fmt2json(source, { resultOnly: true });
console.log(jsonLike);
})()
const jsonString = fmt2json(source, { resultOnly: true });
console.log(jsonString);
</script>
```
#### In Javascript

#### In javascript

Run: `npm install format-to-json --save`;

```javascript
const fmt2json = require('format-to-json');
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"}}}';

(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 fmt2json(source);
console.log(fmtInfo.result);
})();
const fmtInfo = fmt2json(source);
console.log(fmtInfo.result);
```

Result:

```terminal
{
"zjson": "ZJSON",
Expand Down Expand Up @@ -82,26 +85,33 @@ Result:
## Interface

#### [Mehtod] fmt2json

```typescript
fmt2json(source: string, options?: Options): Promise<Result | string>;
declare function fmt2json(source: string, options?: FormatOptions): FormatResult;
declare function fmt2json(source: string, options: FormatOptionsResultOnly): string;
```
#### [Interface] Options

#### [Interface] FormatOptions

```typescript
interface Options {
indent?: number; // Integer, Large then 0, default: 2
expand?: boolean; // Default: true
strict?: boolean; // Default: false
escape?: boolean; // Default: false
unscape?: boolean; // Default: false
keyQtMark?: "'" | "\"" | ""; // Default: "\""
valQtMark?: "'" | "\""; // Default: "\""
interface FormatOptions {
indent?: number; // Integer, Large then 0, default: 2
expand?: boolean; // Default: true
strict?: boolean; // Default: false
escape?: boolean; // Default: false
unscape?: boolean; // Default: false
keyQtMark?: "'" | '"' | ''; // Default: "\""
valQtMark?: "'" | '"'; // Default: "\""
}
interface FormatOptionsResultOnly extends FormatOptions {
resultOnly: true
}
```
#### [Interface] Result

#### [Interface] FormatResult

```typescript
// If `{ resultOnly: true }` in option,
// Just return the format result string.
interface Result {
interface FormatResult {
result: string;
status: {
fmtLines: number;
Expand All @@ -113,7 +123,7 @@ interface Result {
errIndex: number;
errExpect: string;
errNear: string;
}
};
}
```

Expand All @@ -123,8 +133,9 @@ interface Result {
<img width="500" src="https://github.com/CN-Tower/format-to-json/blob/master/images/format_cmd.png?raw=true">
</p>

Run: `npm install -g format-to-json`
Run: `npm install -g format-to-json`
Run: `fmt2json -h`

```terminal
Usage: fmt2json [options]
Expand All @@ -140,12 +151,14 @@ Options:
-r, --resultOnly Result only, not return the formatted info.
-h, --help output usage information
```

Run: `fmt2json -i 4 -q "'"`

```terminal
√ Input a string to foramt: · [{name: "Tom", age: 28, gender: "male"}]
==================================================================
[21:50:53] format-to-json(2.1.2)
[21:50:53] format-to-json(3.0.0)
------------------------------------------------------------------
[
{
Expand Down
29 changes: 15 additions & 14 deletions bin/fmt2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const { prompt } = require('enquirer');
const package = require('../package.json');
const fmt2json = require('../fmt2json');

program.version(package.version)
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 ['""', "''", '"', "'"]`, '""')
Expand All @@ -24,7 +25,7 @@ const options = {};
if (indent && indent.match(/^([1-9])$/)) {
options.indent = +indent;
}
if(['""', "''", '"', "'"].includes(qtMark)) {
if (['""', "''", '"', "'"].includes(qtMark)) {
const qtArr = qtMark.split();
if (qtMark.length === 2) {
options.keyQtMark = qtArr[0];
Expand All @@ -34,7 +35,9 @@ if(['""', "''", '"', "'"].includes(qtMark)) {
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.`);
throw new Error(
`qtMark mast one of ['""', "''", '"', "'"], for key and value, single mark means no key mark just for value.`
);
}
if (collapse) options.expand = false;
if (escape) options.escape = true;
Expand All @@ -45,19 +48,17 @@ if (resultOnly) options.resultOnly = true;
prompt({
type: 'input',
name: 'source',
message: 'Input a string to foramt:'
message: 'Input a string to foramt:',
}).then(({ source }) => {
if (resultOnly) {
fmt2json(source, options).then(result => {
console.log(fn.chalk(result, 'cyan'));
});
const jsonString = fmt2json(source, options);
console.log(fn.chalk(jsonString, '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 });
});
const { result, status } = fmt2json(source, options)
fn.log('', { title: `format-to-json(${package.version})`, pre: true });
console.log(fn.chalk(result, 'cyan'));
console.log(fn.array(66, '-').join(''));
console.log(status);
fn.log('', { end: true });
}
});
Loading

0 comments on commit cb972fb

Please sign in to comment.