npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-json-checker

v0.2.0-0

Published

Type checking function generator for TypeScript

Downloads

2

Readme

Build Statuscodecovnpm version

ts-json-checker

ts-json-checker is type check function generator from type assertion for TypeScript.

It is mainly used for JSON input check.

Feature

  • Generate type check function for JSON.parsed object

  • Custom convert for specified type

  • No required library at runtime

Environment

  • Node.js 10
  • TypeScript 3.3.3333

Install

npm install --save-dev ts-json-checker

Configure

ts-json-config.ts (default file name)

import { generate, convert } from 'ts-json-checker'

// output file name
const fileName = './generated.ts'

export interface Item {
    name: string
    price: number
    releaseDate: Date
}

// string to Date type convertion
convert<Date>(v => {
    if (v instanceof Date) return v
    const dt = typeof v === "string" ? Date.parse(v) : NaN
    if (isNaN(dt)) throw new TypeError('Unable to convert to date. value: ' + v)
    return new Date(dt)
})

// generate Item type check function
generate<Item>("parseItem")

// generate Array of Item type check function
generate<Item[]>("parseItems")

// union type
generate<Item | null>("parseUnion")

more config sample

Usage

Command Lines:

Usage: ts-json-checker [options]

Options:
  -v, --version               output the version number
  -c, --config <config-file>  specify config file
  -n, --linefeedNewLine       set new line chars to linefeed(LF)
  -h, --help                  output usage information

example:

# use default config file name 'ts-json-config.ts'
npx ts-json-checker

# specify config file name
npx ts-json-checker --config ./other-ts-json-config.ts

Output

generated.ts

import { X } from "./types";

export function parseNumer(v: any): number {
    if (typeof v === "number") { }
    else
        throw new TypeError("v is not Number.");
    return <number>v;
}

export function parseX(v: any): X {
    if (v !== null && typeof v === "object")
        __check_1(v, "v");
    else
        throw new TypeError("v is not Object.");
    return <X>v;
}

export function parseNumberArray(v: any): number[] {
    if (Array.isArray(v))
        for (let i = 0; i < v.length; i++)
            if (typeof v[i] === "number") { }
            else
                throw new TypeError("v[" + i + "] is not Number.");
    else
        throw new TypeError("v is not Array.");
    return <number[]>v;
}

export function parseArrayOfNumberArray(v: any): number[][] {
    if (Array.isArray(v))
        for (let i = 0; i < v.length; i++)
            if (Array.isArray(v[i]))
                for (let j = 0; j < v[i].length; j++)
                    if (typeof v[i][j] === "number") { }
                    else
                        throw new TypeError("v[" + i + "][" + j + "] is not Number.");
            else
                throw new TypeError("v[" + i + "] is not Array.");
    else
        throw new TypeError("v is not Array.");
    return <number[][]>v;
}

export function parseUnion(v: any): number | string | boolean | null | undefined {
    if (typeof v === "undefined") { }
    else if (v === null) { }
    else if (typeof v === "string") { }
    else if (typeof v === "number") { }
    else if (typeof v === "boolean") { }
    else
        throw new TypeError("v is not Undefined | Null | String | Number | Boolean.");
    return <number | string | boolean | null | undefined>v;
}

export function parseXOrUndefined(v: any): X | undefined {
    if (typeof v === "undefined") { }
    else if (v !== null && typeof v === "object")
        __check_1(v, "v");
    else
        throw new TypeError("v is not Undefined | Object.");
    return <X | undefined>v;
}

function __check_1(v: any, r: string) {
    if (typeof v.abc === "string") { }
    else
        throw new TypeError(r + ".abc is not String.");
    if (typeof v.x === "undefined") { }
    else if (v.x !== null && typeof v.x === "object")
        __check_1(v.x, r + ".x");
    else
        throw new TypeError(r + ".x is not Undefined | Object.");
    if (typeof v.date === "undefined") { }
    else
        v.date = __convert_1(v.date);
}

function __convert_1(v: any): Date {
    if (v instanceof Date)
        return v;
    const dt = typeof v === "string" ? Date.parse(v) : NaN;
    if (isNaN(dt))
        throw new TypeError('Unable to convert to date. value: ' + v);
    return new Date(dt);
}