All Projects → avito-tech → prop-types-definition

avito-tech / prop-types-definition

Licence: MIT license
Patch for prop-types to get property type definition in runtime

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to prop-types-definition

napari-hub
Discover, install, and share napari plugins
Stars: ✭ 44 (+193.33%)
Mutual labels:  analysis
plugin-flutter-patch
flutter patch generator in gradle
Stars: ✭ 22 (+46.67%)
Mutual labels:  patch
CilTools
A set of tools to work with CIL in .NET applications
Stars: ✭ 19 (+26.67%)
Mutual labels:  analysis
remark-bookmarks
plugin to manage links
Stars: ✭ 15 (+0%)
Mutual labels:  definition
sane patch
Making monkey patches sane again
Stars: ✭ 63 (+320%)
Mutual labels:  patch
OpenWrt-UEFI-Support
Add UEFI-Boot Support to Openwrt v19.07 & v18.06 (Unofficial Support)
Stars: ✭ 13 (-13.33%)
Mutual labels:  patch
textalyzer
Analyze key metrics like number of words, readability, complexity, etc. of any kind of text
Stars: ✭ 50 (+233.33%)
Mutual labels:  analysis
UnofficialCrusaderPatch
Unofficial balancing patch installer for Stronghold Crusader 1
Stars: ✭ 373 (+2386.67%)
Mutual labels:  patch
yara-exporter
Exporting MISP event attributes to yara rules usable with Thor apt scanner
Stars: ✭ 22 (+46.67%)
Mutual labels:  analysis
orb
Orb is a dynamic network observability platform
Stars: ✭ 437 (+2813.33%)
Mutual labels:  analysis
ctn
Unofficial Android patch for GTASA Android with various quality of life improvements
Stars: ✭ 44 (+193.33%)
Mutual labels:  patch
SDA
SDA is a rich cross-platform tool for reverse engineering that focused firstly on analysis of computer games. I'm trying to create a mix of the Ghidra, Cheat Engine and x64dbg. My tool will combine static and dynamic analysis of programs. Now SDA is being developed.
Stars: ✭ 98 (+553.33%)
Mutual labels:  analysis
MacOS-All-In-One-Update-Script
Mac update shell script (Appstore, macOS, Homebrew and others)
Stars: ✭ 39 (+160%)
Mutual labels:  patch
graphql-liftoff
Generate GraphQL schema language from API specifications and more
Stars: ✭ 44 (+193.33%)
Mutual labels:  definition
magento1-open-source-patches
Magento Open Source 1.x patches mirror repository.
Stars: ✭ 38 (+153.33%)
Mutual labels:  patch
sharppcap
Official repository - Fully managed, cross platform (Windows, Mac, Linux) .NET library for capturing packets
Stars: ✭ 1,054 (+6926.67%)
Mutual labels:  analysis
PyEarthScience
The PyEarthScience repository created by DKRZ (German Climate Computing Center) provides Python scripts and Jupyter notebooks in particular for scientific data processing and visualization used in climate science. It contains scripts for visualization, I/O, and analysis using PyNGL, PyNIO, xarray, cfgrib, xesmf, cartopy, and others.
Stars: ✭ 56 (+273.33%)
Mutual labels:  analysis
layer
Analyzer: Checks whether there are dependencies that illegal cross-border the layer structure.
Stars: ✭ 43 (+186.67%)
Mutual labels:  analysis
intellij-diff-plugin
Syntax highlighting for .diff files and .patch files in IntelliJ IDEs
Stars: ✭ 17 (+13.33%)
Mutual labels:  patch
sbt-findbugs
FindBugs static analysis plugin for sbt.
Stars: ✭ 47 (+213.33%)
Mutual labels:  analysis

prop-types-definition

NPM version Build Status Coverage Status

Patch for prop-types to get property type definition in runtime.

Sponsored by Avito

Why?

When using React components, prop-types is commonly used to define properties type checking. Unfortunally, prop-type doesn't provide definition details that can be useful for documentation generation or component's playground. This library adds ability to get property type definition in runtime by calling a getTypeDefinition() method.

As anternative, tools like react-docgen can be used. However, such tools are usualy based on static code analysis and have many limitations.

Install

npm install prop-types-definition

Usage

prop-types-definition can be used in two ways:

  • As prop-types patch on demand
  • As a webpack loader

When prop-types is patched, you can get property type definition by calling getTypeDefinition() method.

NOTE: getTypeDefinition() method can be missed for certain cases. Usually it's a custom type cheching. You need to check that a property checking has a method getTypeDefinition() before invoking it.

import PropTypes from 'prop-types';

// patch PropTypes somehow, see below

export default class Component extends React.Component {
    static propTypes = {
        foo: PropTypes.string,
        bar: PropTypes.number.isRequired
    }

    // ...
}

// now we can get prop-types definitions
for (let name in Component.propTypes) {
    const type = Component.propTypes[key];

    // some type definitions can have no getTypeDefinition method
    if (typeof type.getTypeDefinition === 'function') {
        console.log(key, type.getTypeDefinition());
    }
}

// output
// foo {
//     required: false,
//     type: {
//         name: 'string'
//     }
// }
// bar: {
//     required: true,
//     type: {
//         name: 'number'
//     }
// }

Patch prop-type by your own

import PropTypes from 'prop-types';
import patchPropTypes from 'prop-types-definition';

patchPropTypes(PropTypes);

export default class Component extends React.Component {
    static propTypes = {
        foo: PropTypes.string,
        // ...
    }

    // ...
}

Using as webpack loader

prop-types can be patched via a webpack loader. In this case you need to add a rule for prop-types index file in this way:

const webpackConfig = {
    // ...
    module: {
        rules: [
            {
                test: /\/prop-types\/index\.js$/i,
                loader: 'prop-types-definition/loader'
            },
            // ...
        ]
    }
};

Custom property types

In case you're implementing a custom property validator and want geting a definition details, you need to add getTypeDefinition() method to a validator.

const myCustomType = function() {
    // ...
};

myCustomType.getTypeDefinition = function() {
    return {
        type: {
            name: 'myCustomType',
            // any extra details
        }
    }
}

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].