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-types-parser

v1.0.3

Published

Parse TypeScript types definitions into... something else

Downloads

6

Readme

TS Types Parser

Parse TypeScript types definitions into... something else.

Using cases

Parsing TS types allows you automatically for example:

  • Prepare SQL code to generate tables according to TS models
  • Writing dynamic API in other languages and preparing other languages to work with data send by or for such TS script
  • Writing validation for models in any language (for example in TS itself)

Installing

  • Use NPM like that:
npm i -g ts-types-parser
  • ...or just clone repo.

Usage

CLI

  • If you installed as global module:
ts-types-parser rules.js
  • If clonned repo:
node cli.js rules.js
  • Example rules-file.js file for CLI usage

module.exports = p => {
    // Parser as p
    // ...and rules here :)
};

Module

  • If you installed as local module:
const Parser = require('ts-types-parser');
const parser = new Parser;

// Your rules here

parser.run();

Rules - Parser class

Methods

setSource(path: string | string[])

Allows to setup new single or multiple sources files. That files should be valid TypeScript files, all of them will be joined into single long string.

Remember: You have to add all needed files to sources with types because exports aren't resolved.


setTarget(path: string)

Allows to setup file which will contain result. Whole file will be replaced if no delimiters are set.


setDelimiters(start: string, end: string)

Allows to setup delimiters for target file between which content will be replaced.

Remember: Those delimiters are used inside regular expression so you have to escape all special charaters.


mainType(name: string)

Given name will be used as "entry point" at parsing. For example: if your tree of types begins from interface Main or type Main you should call p.mainType('Main'). There can exists only single "main" type, if you need more recurslivy parsing then using multiple instances of Parser class will be solution.


expandTypes(arr: string[])

Given here types will be resolved at occurring. For example:

// some.d.ts
type Point {
    x: number;
    y: number;
};

interface Main {
    one: Point;
    two: Point;
};

Without any expanding will iterate trough:

one # Point
two # Point

With p.expandTypes(['Point']) will iterate trough:

one   # Point
one   # object
one,x # number
one,y # number
two   # Point
two   # object
two,x # number
two,y # number

setPathProxy(cb: (path: string[]) => any)

Allows modify all paths before any call. For example:

p.setPathProxy(x => x.filter(x => !/\d+$/.test(x)).join('#'));

setIndent(indent: string)

Allows to set custom indent (default is 4 spaces). This indent will be used at leveling your result.


levelUp()

levelDown()

Level up or down current indent.


writeDescription(prefix: string = '// ')

Write description block into result in current place. Example:

// Automatically generated code by "ts-types-parser"
// Generated at Thu, 30 May 2019 22:07:35 GMT
// Source files:
// - types1.d.ts
// - types2.d.ts

log({ optional: boolean, path: string, type: string })

Uses same object such will be send to parser and log given data in such way:

// test.d.ts
type Point {
    x?: number;
    y: number;
};

interface Main {
    one?: Point;
    two: Point;
};

// Result
one                                 # Point      [optional]
one                                 # object
one,x                               # number     [optional]
one,y                               # number
two                                 # Point
two                                 # object
two,x                               # number     [optional]
two,y                               # number

used()

Set current path as used. See event unused for more informations.


write(txt: string)

Push new line into result, also marks current path as used.


run()

Runs parser ...and occasionally yells with errors.

Events

type Entry = { path: string[] | any, type: string, optional: boolean };

All paths in events are result of method which can be set using p.setPathProxy(cb). Default paths are array of strings.

walk -> Entry

Just walk through all paths.


type::<SOME_TYPE> -> Entry

Will be emitted only when selected type occurs.


unused -> Entry

Will be emitted when none of callbacks didn't call p.write() or p.used().


level-up -> Entry

level-down -> Entry

Fired when level of path has been level upped, it's something else than p.levelUp() and p.levelDown(). Useful when you need to close whole subtree in some scope. For Example:

p.on('level-up', ({ path }) => {
    if (type === 'Array') {
        p.write('Array(10).fill(0).forEach((_, i) => {');
        p.levelUp();
    }
});

p.on('level-down', ({ path }) => {
    if (type === 'Array') {
        p.levelDown();
        p.write('});');
    }
});

will close Array content inside .forEach.

done -> void

Parsing done, but you still can write something at end of result.

TODO

  • [ ] Ironically I should rewrite this script in TypeScript
  • [ ] Probably not all cases are parsed, for exmaple union types
  • [ ] Script needs tests
  • [ ] Script needs also more examples

License

This project is licensed under the Apache-2.0 License - see the LICENSE.md file for details