Git Product home page Git Product logo

typeof-jsonc's Introduction

typeof-jsonc npm version

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

推荐使用 typeof-sjsonc

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

typeof-jsonc's People

Contributors

wulunyi avatar

Stargazers

pgg avatar stephzhu avatar  avatar  avatar  avatar HuangShao avatar  avatar Callan Bi avatar Kaifeng avatar 叶克难 avatar wmvm avatar collins avatar chj88life avatar 凌莞~(=^▽^=) avatar  avatar Viki avatar 张雨帆 avatar jimmy avatar Mingliang ZHOU avatar Caico Leung avatar Rick avatar zoux avatar  avatar Paolo pix Carraro avatar 风中追风 avatar IMGSS avatar  avatar Andrew Redican avatar zhang avatar Gerry Jarl avatar liaokai avatar kayak avatar  avatar  小杜 avatar  avatar life avatar 码农小余 avatar  avatar 贵志 avatar  avatar Alex avatar zoomdong avatar Youn avatar 吃土小2叉(Tony Xu) avatar 黄海平 avatar 明夷居士 avatar j-xzy avatar  avatar Tom Lee avatar  avatar wangpengli avatar 年轮 avatar yh94 avatar  avatar kiminozo avatar An Yan avatar  avatar 王晓聪 avatar Youmu avatar mio avatar  avatar  avatar Genluo avatar H avatar  avatar TimLi avatar seeia avatar 双愚 avatar CangHai xx avatar jinjuxiaodian avatar SlaneYang avatar 孟庆申 avatar He Li  avatar Twinner avatar  avatar

Watchers

 avatar seeia avatar

typeof-jsonc's Issues

支持 Record 吗?

你好,刚刚在网上看到这个npm。这对我们开发非常有帮助。非常感谢你的制作。
请问如果写泛型的支持。比如说我希望能翻译出 一个字典的数据类型,或者枚举型?

declare interface CustomEventDeclarations {
    k_count_down:{
        player: Record<string,string>
        state: 1 | 2 | 3 | 4 | 5
    }

}

会考虑出 JetBrains IDE的插件吗?

首先向你的项目表示欢迎,它对我非常有用。我看到这个项目出了vscode的插件。你知道,JetBrains 系列的产品也有很多人在用,请问你有计划推出 JetBrains 系列的插件吗?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.