typeof-jsonc
Convert json or jsonc to typescript type (interface)
Last updated 3 months ago by wulunyi .
MIT · Repository · Original npm · Tarball · package.json
$ cnpm install typeof-jsonc 
SYNC missed versions from official npm registry.

typeof-jsonc npm version

typeof-jsonc is a library for Convert json or jsonc string to typescript type (interface)

Online Playgrounds

Support

  • Basic types
  • Array types (support auto merge)
  • Same struct auto merge
  • Comments
  • JsDocComments
  • Custom naming
  • Json
  • Jsonc
  • Not standard jsonc or json

Usage

install

npm install typeof-jsonc -S

or

yarn add typeof-jsonc -S

Demo

Jsonc string

{
  "barr": [
    // aaa
    "aaa",
    "bbb"
  ],
  "name": "lanfeng", // this is name
  // this is demo
  "demo": {
    "hello": "world"
  },
  /** this is arr */
  "arr": [
    {
      "age": 1
    },
    2
  ],
  "a": "hello",
  "b": ["a", "b"]
}

Convert script

import fs from 'fs';
import path from 'path';

import { typeofJsonc } from '../src/index';

const text = fs.readFileSync(path.resolve('./demo/test'), {
  encoding: 'utf-8',
});

console.log(
  typeofJsonc(text, 'Response', {
    prefix: 'I',
    rootFlags: 1,
    disallowComments: false,
    addExport: true,
    singleLineJsDocComments: true,
  }),
);

Output

export interface IResponse {
  /**  aaa  */
  barr: string[];
  /**  this is name  */
  name: string;
  /**  this is demo  */
  demo: IDemo;
  /**  this is arr  */
  arr: (IArr | number)[];
  a: string;
  b: string[];
}

/**  this is arr  */
export interface IArr {
  age: number;
}

export interface IDemo {
  hello: string;
}

API

  • typeofJsonc(jsonc: string, name: string, options?: Options)
interface Options {
  /** type name prefix */
  prefix?: string; // default ''
  /**  customer named */
  onName?: (name: string) => string;
  /** Add export keywords */
  addExport?: boolean; // default false
  /**
   * Identifiers are e.g. legal variable names. They may not be reserved words
   * None = 0,
   * Module = 1,
   * InAmbientNamespace = 2,
   */
  rootFlags?: number; // default 0
  disallowComments?: boolean; // defalut true
  singleLineJsDocComments?: boolean; // default false
}
option type default desc
prefix string "" type name prefix
onName (name: string) => string (name: string) => ${prefix}${name} Custom naming If this option set prefix will not work
addExport boolean false Add export keywords, when set true rootFlags set will not work
rootFlags number 0 Identifiers are e.g. legal variable names. They may not be reserved words None = 0 Module = 1 InAmbientNamespace = 2
disallowComments boolean true Whether to prohibit the generation of comments
singleLineJsDocComments boolean false Single-line display when single-line comment

Version

1.1.9

  • Implementation refactor
  • Same struct auto merge

1.1.8

  • Fix addExport bug
  • Pack umd and es lib

1.1.6

  • Support singleLineJsDocComments options

1.1.5

  • Support convert not standard jsonc or json

Standard jsonc or json demo

{
  "name": "test",
  "age": 13,
  "loves": ["A", "B"]
}

Not standard jsonc or json demo

{
  name: '',
  age: 13,
  loves: ['A', 'B']
}

License

MIT

Current Tags

  • 1.1.11-beta.2                                ...           beta (2 months ago)
  • 1.1.9-0                                ...           es3 (3 months ago)
  • 1.1.10                                ...           latest (3 months ago)
  • 1.1.5-0                                ...           next (5 months ago)

27 Versions

  • 1.1.11-beta.2                                ...           2 months ago
  • 1.1.11-beta.1                                ...           2 months ago
  • 1.1.11-beta.0                                ...           2 months ago
  • 1.1.10                                ...           3 months ago
  • 1.1.9                                ...           3 months ago
  • 1.1.9-0                                ...           3 months ago
  • 1.1.8                                ...           3 months ago
  • 1.1.8-0                                ...           3 months ago
  • 1.1.7                                ...           4 months ago
  • 1.1.6                                ...           4 months ago
  • 1.1.5                                ...           4 months ago
  • 1.1.5-0                                ...           5 months ago
  • 1.1.4                                ...           5 months ago
  • 1.1.4-0                                ...           5 months ago
  • 1.1.3                                ...           5 months ago
  • 1.1.2-1                                ...           5 months ago
  • 1.1.2-0                                ...           5 months ago
  • 1.1.1                                ...           5 months ago
  • 1.1.0                                ...           5 months ago
  • 1.0.3-2                                ...           7 months ago
  • 1.0.3-1                                ...           7 months ago
  • 1.0.3-0                                ...           7 months ago
  • 1.0.2                                ...           8 months ago
  • 1.0.2-0                                ...           9 months ago
  • 1.0.1                                ...           9 months ago
  • 1.0.1-0                                ...           9 months ago
  • 1.0.0                                ...           9 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 20
Dependencies (4)
Dev Dependencies (17)
Dependents (1)

Copyright 2014 - 2017 © taobao.org |