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

tst-defaults

v1.3.1

Published

Get default values by Typescript type reflection

Downloads

4

Readme

Make your defaults expectable

This project uses tst-reflect library for making your schemas expectable. For example, if you are parsing JSON with predefined schema but didn't want to check that some fields is not defined in object and want to just get default values.

Simple example

This simple example shows how much problems we have when parsing JSON in TypeScript.

type JSONSchema = {
    n: number
    level: {
        n2: number
        bool: boolean
    },
    list: number[],
    b: boolean
}

const parsedJson = JSON.parse('{}') as JSONSchema;

console.log(parsedJson.level.n2 + 1) // error, level is undefined
console.log(parsedJson.list.map(el => el + 1)) // error, list is undefined and has no method map
console.log(parsedJson.n + 1) // NaN

if (parsedJson.b) { // it may have error, because undefined is not a false 
    makeRiskCall()
}

if (paredJson.b === true) { // why we need this in TypeScript?? So much code...
    makeRiskCall()
}

But you may say that we have a solution?

class JSONSchema {
    public n: number = 0;
    public level: {
        n2: number
        bool: boolean
    } = {
        n2: 0,
        bool: false,
    }
    public list: number[] = [];
    public b:boolean = false;
    constructor(v:any) {
        Object.assign(this, v);
    }
}

const parsedJson = new JSONSchema(JSON.parse('{"level": {"n2": 1}}'));

console.log(parsedJson.n + 1) // ok
console.log(parsedJson.level.n2 + 1) // ok, result is 2
console.log(parsedJson.level.bool) // undefined

Not so bad, but still not good. And we have much code again.

Solution

import { mergeDefaults } from "tst-defaults";

type JSONSchema = {
    n: number
    level: {
        n2: number
        bool: boolean
    },
    list: number[],
    b: boolean
}

const parsedJson = mergeDefaults<JSONSchema>(JSON.parse('{"level":{"bool":true}}'));

console.log(parsedJson.b === false) // ok
console.log(parsedJson.list.map(el => el + 1)) // ok
console.log(parsedJson.level.n2 + 1 === 1) // ok
console.log(parsedJson.level.bool == true) // ok
console.log(parsedJson.n + 1) // ok

Simple and fast.

Installation

yarn add --dev typescript ttypescript tst-reflect-transformer ts-node
yarn add tst-defaults

Setup your tsconfig.json

{
    "ts-node": {
      "compiler": "ttypescript"
    },
    "compilerOptions": {
        ...,
        "plugins": [
            {
                "transform": "tst-reflect-transformer"
            }
        ]
    }
}

Setup package.json

{
    "scripts": {
        "start": "ts-node index.ts",
        "build": "ttsc"
    }
}

API

Some api documentation.

useDefault

Saves type default value in storage for next time resolving.

import { useDefault, mergeDefaults } from 'tst-defaults';

type Duration = {
  seconds: number;
};

type Schema = {
  expires: Duration;
  options: Object;
  users: number;
};

useDefault<Duration>({ seconds: -1 });
const config = mergeDefaults<Schema>(JSON.parse('{}'));

console.log(config.expires); // { seconds: -1}
console.log(config.users); // 0
console.log(config.options); // {}

mergeDefaults

Merging default values into variable value. If variable is undefined, than it will create default value for it.

import { mergeDefaults } from "tst-defaults";

const jsonErrorIgnored = ():any => {}

type Schema = {
    n: number
};

const config = mergeDefaults<Schema>(jsonErrorIgnored());
console.log(config.n) // 0, config is created as object

getDefaultValue

Returns default value for type.

import { getDefaultValue } from "tst-defaults";

console.log(getDefaultValue<string>()) // ""

enum Person {
    UNKNOWN = 0,
    PRIVATE = 1,
    PUBLIC = 2,
}

console.log(getDefaultValue<string>()) // ""
console.log(getDefaultValue<boolean>()) // false
console.log(getDefaultValue<Object>()) // {}
console.log(getDefaultValue<number>()) // 0
console.log(getDefaultValue<Array<number[]>>()) // []
console.log(getDefaultValue<Function>()) // null
console.log(getDefaultValue<Person>() == Person.UNKNOWN) // true

deleteDefault

Removes type default value from storage.

import { deleteDefault, useDefault, getDefaultValue } from "tst-defaults";

type Duration = {
  seconds: number;
};

useDefault<Duration>({
    seconds: -1
});

console.log(getDefaultValue<Duration>();) // {seconds: -1}

deleteDefault<Duration>();
console.log(getDefaultValue<Duration>();) // {}