sonparser
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

node-sonparser

Safe parser of JSON and CSON for config parsing like ConfigParser of python.

NPM version Build status License Downloads Dependency status

For more information, view the documentation.

Installation

npm install sonparser --save

Quick Example

Javascript

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"
  }
}); // throw Error!

Typescript

/// <reference path="node_modules/sonparser/lib-typings/sonparser.d.ts" />
 
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"
  }
}); // throw Error!

License

MIT

Package Sidebar

Install

npm i sonparser

Weekly Downloads

1

Version

0.2.0

License

MIT

Last publish

Collaborators

  • mizunashi_mana