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

proptypes-parser

v0.2.1

Published

PropTypes parser / generator for React and React Native components with GraphQL-like syntax.

Downloads

10

Readme

proptypes-parser

Build Status Coverage Status npm version Dependency Status License

PropTypes parser / generator for React and React Native components with GraphQL-like syntax.

Don't you just hate writing PropTypes for React components?

proptypes-parser is cleaner, easier and less error prone way to define your PropTypes for both React and React Native applications.

It uses GraphQL schema like syntax to define PropTypes in string.

It also allows Type Composition via named definitions and spread operator ....

Install

npm install --save proptypes-parser

Now proptypes-parser provides a default parser as parsePropTypes.

import {parsePropTypes} from 'proptypes-parser';

const propTypes = parsePropTypes(`{
  number: Number
  string: String!
}`)

Alternatively, if you like template literals,

import {PT} from 'proptypes-parser';

const propTypes = PT`{
  number: Number
  string: String!
}`

Advanced Usage

in proptypes.js.

import createPropTypesParser from 'proptypes-parser';
import {PropTypes} from 'react';

// Provide PropTypes to the parser (Required).
// Also, provide any custom type definitions (Optional).
const parsePropTypes = createPropTypesParser(PropTypes, {
  Message: class Message {} // To use 'Message' instance type. 
});

export default parsePropTypes;

in component.js.

const propTypes = parsePropTypes(`{
  number: Number
  string: String!
  boolean: Boolean
  function: Function!
  date: Date!
  object: Object!
  shape: {
    nested: Number
    array: [Number]
    must: Boolean!
  }!
  array: [Number!]!
  arrayOfObjects: [{
    value: String
  }!]
  node: Node
  element: Element!
  message: Message!
  any: Any!
  optionalUnion: String | Number | Boolean
  union: (String | Number)!
}`);

is equivalent to

const propTypes = {
  number: PropTypes.number,
  string: PropTypes.string.isRequired,
  boolean: PropTypes.bool,
  function: PropTypes.func.isRequired,
  date: PropTypes.instanceOf(Date).isRequired,
  object: PropTypes.object.isRequired,
  shape: PropTypes.shape({
    nested: PropTypes.number,
    array: PropTypes.arrayOf(PropTypes.number),
    must: PropTypes.bool.isRequired,
  }).isRequired,
  array: PropTypes.arrayOf(
    PropTypes.Number.isRequired,
  ).isRequired,
  arrayOfObjects: PropTypes.arrayOf(
    PropTypes.shape({
      value: PropTypes.string,
    }).isRequired
  ),
  node: PropTypes.node
  element: PropTypes.element.isRequired,
  message: PropTypes.instanceOf(Message).isRequired,
  any: PropTypes.any.isRequired,
  optionalUnion: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.number,
    PropTypes.bool,
  ]),
  union: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.number,
  ]).isRequired,
};

How wonderful!

Composition via named definition

Compose types with named definitions and spread operator ....

// Define 'Car' type.
const carPropTypes = parsePropTypes(`
  Car {
    year: Number!
    model: String!
  }
`);

// Use previously defined 'Car' type.
const garagePropTypes = parsePropTypes(`
  Garage {
    address: String!
    cars: [Car!]!
  }
`);

// Use spread operator on 'Car' type.
const carWithMakePropTypes = parsePropTypes(`
  CarWithMake {
    ...Car
    make: String!
  }
`);

addType(name, type)

Add new types to the type dictionary.

// Add class instance type.
class Message {}
parsePropTypes.addType('Message', Message);

// Add propTypes definition.
// Same as named definition.
const carPropTypes = parsePropTypes(`{
  year: Number!
  model: String!
}`);
parsePropTypes.addType('Car', carPropTypes);

// Add React.PropTypes type.
const newsOrPhotosEnum = PropTypes.oneOf(['News', 'Photos']);
parsePropTypes.addType('NewsOrPhotos', newsOrPhotosEnum);

// Use above types.
parsePropTypes(`{
  message: Message
  car: Car
  mediaType: NewsOrPhotos
}`);

Enums

Currently, Enums are not supported. However, you can do this instead:

// Provide type extensions for this parser.
const parsePropTypes = createPropTypesParser(PropTypes, {
  OptionalEnum: PropTypes.oneOf(['News', 'Photos']),
});

const propTypes = parsePropTypes(`{
  optionalEnumValue: OptionalEnum
  requiredEnumValue: OptionalEnum!
}`);

or

// Provide local one-time type extensions.
const propTypes = parsePropTypes(`{
  optionalEnumValue: OptionalEnum
  requiredEnumValue: OptionalEnum!
}`, {
  OptionalEnum: PropTypes.oneOf(['News', 'Photos']),
});

Examples

See test.

Production Use

Use babel-plugin-transform-react-remove-prop-types to strip propTypes from your react components.

TODO

  • Support Enums: value: ['News', 'Photos']
  • More extensive validations
  • Babel plugin
  • Webpack plugin
  • PRs are welcome!

License

The MIT License (MIT)

Copyright (c) 2016 Joon Ho Cho

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.