0.3.0 • Published 12 months ago
qing-shared-const v0.3.0
qing-shared-const
Generate JavaScript/Go constants from JSON.
Installation
npm i qing-shared-const
Usage
Go
import { go } from 'qing-shared-const';
go.convert(
{
str: 'hello world',
intProp: 123,
doubleProp: 12.3,
__enums: {
color: { values: ['red', 'blue'] },
color2: { values: ['red', 'blue'] },
},
},
{ packageName: 'test', typeName: 'Test' },
);
Output:
package test
const Str = "hello world"
const IntProp = 123
const DoubleProp = 12.3
type Color int
const (
ColorRed Color = iota
ColorBlue
)
type Color2 int
const (
Color2Red Color2 = iota
Color2Blue
)
JavaScript
import { js } from 'qing-shared-const';
js.convert({
intValue: -12,
strValue: 'haha"\'',
nullValue: null,
arrayValue: [32, 'wow', null],
__enums: {
color: { values: ['red', 'blue'] },
color2: { values: ['red', 'blue'] },
},
});
Output:
export const intValue = -12;
export const strValue = 'haha"\'';
export const nullValue = null;
export const arrayValue = [32, 'wow', null];
export var Color;
(function (Color) {
Color[(Color['red'] = 0)] = 'red';
Color[(Color['blue'] = 0)] = 'blue';
})(Color || (Color = {}));
export var Color2;
(function (Color2) {
Color2[(Color2['red'] = 0)] = 'red';
Color2[(Color2['blue'] = 0)] = 'blue';
})(Color2 || (Color2 = {}));
TypeScript definition is also supported:
import { js } from 'qing-shared-const';
js.convert(
{
intValue: -12,
strValue: 'haha"\'',
nullValue: null,
arrayValue: [32, 'wow', null],
__enums: {
color: { values: ['red', 'blue'] },
color2: { values: ['red', 'blue'] },
},
},
{ dts: true },
);
Output:
export declare const intValue = -12;
export declare const strValue = 'haha"\'';
export declare const nullValue = null;
export declare const arrayValue: number[];
export declare enum Color {
red = 1,
blue = 2,
}
export declare enum Color2 {
red = 1,
blue = 2,
}