Safe parser of JSON and CSON for config parsing like ConfigParser of python.
var sonparser = require("sonparser");
var result;
var ExampleTsConfParser = sonparser.hasProperties([
["compilerOptions", sonparser.hasProperties([
["target", sonparser.string],
["module", sonparser.string],
["noImplicitAny", sonparser.boolean.option(false)],
["preserveConstEnums", sonparser.boolean.option(false)],
])],
["exclude", sonparser.array(sonparser.string).option([])],
]);
result = ExampleTsConfParser.parse({
"compilerOptions": {
"target": "es5",
"module": "commonjs"
},
"exclude": [
"node_modules",
"build"
]
});
result = ExampleTsConfParser.parse({
"compilerOptions": {
"target": 5,
"module": "commonjs"
}
});
import * as sonparser from "sonparser";
let result: TsConfig;
interface CompilerOptions {
target: string;
module: string;
noImplicitAny: boolean;
preserveConstEnums: boolean;
}
interface TsConfig {
compilerOptions: CompilerOptions;
exclude: string[];
}
const ExampleTsConfParser = sonparser.hasProperties<TsConfig>([
["compilerOptions", sonparser.hasProperties<CompilerOptions>([
["target", sonparser.string],
["module", sonparser.string],
["noImplicitAny", sonparser.boolean.option(false)],
["preserveConstEnums", sonparser.boolean.option(false)],
])],
["exclude", sonparser.array(sonparser.string).option([])],
]);
result = ExampleTsConfParser.parse({
compilerOptions: {
target: "es5",
module: "commonjs"
},
exclude: [
"node_modules",
"build"
]
});
result = ExampleTsConfParser.parse({
compilerOptions: {
target: 5,
module: "commonjs"
}
});