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

@typedefs/parser

v1.3.4

Published

The Parser For JSDoc Types.

Downloads

206

Readme

@typedefs/parser

npm version

@typedefs/parser is The Parser For JSDoc Types Written Using Google Closure Compiler Annotations (no TypeScript support). Although most of the typing rules are the same, the main difference is for functions and arrays:

  • function(string, number=): void
  • ⛔️ (arg: string, optional?: number) => void The arrow function notation is not supported. Cannot write ? for optional arguments, need to use =.
  • !Array<string>
  • ⛔️ string[] The double array bracket notation will not work.
  • { record: (string|undefined) }
  • ⛔️ { record?: string } Optional properties cannot be denoted with a question mark.
yarn add @typedefs/parser

Table Of Contents

API

The package is available by importing its default function:

import parse from '@typedefs/parser'

parse(  type: string,): !Type

Parses a Google Closure Compiler type recursively.

Type: The representation of a type.

| Name | Type | Description | | ----------- | -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | nullable | boolean | Whether the type is nullable. This is defined by writing ? before the type name to state nullability and ! otherwise. The parser does not infer nullability from types being primitive and Function/function. | | name | string | The name of the type. | | union | !Array<!Type> | If the type is defined as a union, e.g., (string\|number), contains the united types. Must include parenthesis. | | record | !Object<string, Type> | If the type is a record, contains its representation. If a property of the record does not have a type, it will be set to null. | | application | !Array<!Type> | The application of the type, e.g., the inner type of Object<Application>. | | function | !FunctionType | The function info with args and return if the type is a function. | | optional | boolean | If the type is returned as an optional argument of a function (function(string=)), this will be set to true. |

FunctionType: The meta information about the function.

| Name | Type | Description | | ------------ | ------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | args* | !Array<!Type> | The arguments of the function. | | return* | ?Type | The return type of the function. When the value is set to null, it means the function does not have a return. If the return was actually null, it would be specified as return: { name: 'null' }. | | this | !Type | The type of the this argument specified as function(this: Type). | | new | !Type | The type of the new argument specified as function(new: Type). | | variableArgs | !Type | The type of the variable arguments, e.g., function(...Type). |

import parser from '@typedefs/parser'

logHeading('Applications')
log(parser('!Object<string, Promise<Array<!Type>>>'))

logHeading('Unions (parenthesis are required)')
log(parser('(string | number | !Promise<?(string | symbol)>)'))

logHeading('Records')
log(parser(`{
  a: string, b: ?number, c,
  d: !Promise<Array<{e: number}>>,
  f: { g: boolean }
}`))

logHeading('Functions')
log(parser(`function(
  this: Type,
  string,
  function(),
  function(): *=
): function(): null`))

// special case when name is nullable empty string ''
log(parser(`function(): ?`))
Applications:
------------

{ nullable: false,
  name: 'Object',
  application: 
   [ { name: 'string' },
     { name: 'Promise',
       application: 
        [ { name: 'Array',
            application: [ { nullable: false, name: 'Type' } ] } ] } ] }

Unions (parenthesis are required):
---------------------------------

{ union: 
   [ { name: 'string' },
     { name: 'number' },
     { nullable: false,
       name: 'Promise',
       application: 
        [ { union: [ { name: 'string' }, { name: 'symbol' } ],
            nullable: true } ] } ] }

Records:
-------

{ record: 
   { a: { name: 'string' },
     b: { nullable: true, name: 'number' },
     c: null,
     d: 
      { nullable: false,
        name: 'Promise',
        application: 
         [ { name: 'Array',
             application: [ { record: { e: { name: 'number' } } } ] } ] },
     f: { record: { g: { name: 'boolean' } } } } }

Functions:
---------

{ name: 'function',
  function: 
   { return: 
      { name: 'function',
        function: { return: { name: 'null' }, args: [] } },
     args: 
      [ { name: 'string' },
        { name: 'function', function: { return: null, args: [] } },
        { name: 'function',
          function: { return: { name: 'any' }, args: [] },
          optional: true } ],
     this: { name: 'Type' } } }
{ name: 'function',
  function: { return: { nullable: true, name: '' }, args: [] } }

Copyright