Skip to content

Commit be0d536

Browse files
committed
feat: initial release
1 parent 884009c commit be0d536

File tree

10 files changed

+8358
-0
lines changed

10 files changed

+8358
-0
lines changed

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["@babel/preset-env", "@babel/preset-react"]
3+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
/dist/

.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
!/dist/**/*.js
3+
!/src/**/*.js
4+
*.test.js

.travis.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
language: node_js
2+
3+
node_js:
4+
- 10
5+
6+
before_install:
7+
- curl -o- -L https://yarnpkg.com/install.sh
8+
- export PATH="$HOME/.yarn/bin:$PATH"
9+
10+
notifications:
11+
email: false
12+
13+
cache:
14+
yarn: true
15+
directories:
16+
- 'node_modules'
17+
18+
git:
19+
depth: 5

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2019 Smooth Code
2+
3+
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:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
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.

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# prop-desc
2+
3+
[![License](https://img.shields.io/npm/l/prop-desc.svg)](https://github.com/smooth-code/prop-desc/blob/master/LICENSE)
4+
[![npm package](https://img.shields.io/npm/v/prop-desc/latest.svg)](https://www.npmjs.com/package/prop-desc)
5+
[![Build Status](https://img.shields.io/travis/smooth-code/prop-desc.svg)](https://travis-ci.org/smooth-code/prop-desc)
6+
[![DevDependencies](https://img.shields.io/david/dev/smooth-code/prop-desc.svg)](https://david-dm.org/smooth-code/prop-desc?type=dev)
7+
8+
React prop-types with metadata inside ✨
9+
10+
It is a drop-in replacement for prop-types that includes metadata to generate documentation from prop-types consistently.
11+
12+
```sh
13+
npm install prop-desc prop-types
14+
```
15+
16+
## Example
17+
18+
```js
19+
import React from 'react'
20+
import PropTypes from 'prop-desc'
21+
22+
function MyComponent() {
23+
// ... do things with the props
24+
}
25+
26+
MyComponent.propTypes = {
27+
optionalArray: PropTypes.array,
28+
optionalBool: PropTypes.bool,
29+
optionalFunc: PropTypes.func,
30+
}
31+
32+
console.log(PropTypes.getMetadata(MyComponent.propTypes))
33+
```
34+
35+
## Why?
36+
37+
Generating documentation from prop types is useful but not easy. A project call [react-docgen](https://github.com/reactjs/react-docgen) try to introspect code to extract type but it remains static and does not work with complex prop types (in other files). prop-desc solves this and permits to generate consistent documentation from your prop-types.
38+
39+
# License
40+
41+
MIT

package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "prop-desc",
3+
"description": "Add metadata to React prop-types",
4+
"keywords": [
5+
"react",
6+
"prop-types"
7+
],
8+
"version": "1.0.0",
9+
"author": "Greg Bergé <berge.greg@gmail.com>",
10+
"license": "MIT",
11+
"main": "dist/index.js",
12+
"umd:main": "dist/index.umd.js",
13+
"module": "dist/index.mjs",
14+
"source": "src/index.js",
15+
"scripts": {
16+
"test": "jest",
17+
"prebuild": "rm -rf dist/",
18+
"build": "microbundle --external react",
19+
"prepublishOnly": "yarn build",
20+
"release": "standard-version && conventional-github-releaser --preset angular"
21+
},
22+
"devDependencies": {
23+
"@babel/core": "^7.5.5",
24+
"@babel/preset-env": "^7.5.5",
25+
"@babel/preset-react": "^7.0.0",
26+
"babel-core": "^7.0.0-0",
27+
"babel-jest": "^24.8.0",
28+
"conventional-github-releaser": "^3.1.3",
29+
"jest": "^24.8.0",
30+
"microbundle": "^0.11.0",
31+
"react": "^16.9.0",
32+
"standard-version": "^7.0.0"
33+
}
34+
}

src/index.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import PropTypes from 'prop-types'
2+
3+
function typeToString(prop) {
4+
const meta = prop.__meta
5+
if (!meta) return 'unknown'
6+
const propName = meta.type.name
7+
if (propName === 'arrayOf') {
8+
return `[${typeToString(meta.type.value)}]`
9+
}
10+
if (propName === 'objectOf') {
11+
return `{${typeToString(meta.type.value)}}`
12+
}
13+
if (propName === 'shape' || propName === 'exact') {
14+
return `{ ${Object.entries(meta.type.value)
15+
.map(([name, type]) => `${name}: ${typeToString(type)}`)
16+
.join(' , ')} }`
17+
}
18+
if (propName === 'union') {
19+
return `${meta.type.value.map(type => typeToString(type)).join(' | ')}`
20+
}
21+
if (propName === 'enum') {
22+
return `${meta.type.value.map(type => type.value).join(' | ')}`
23+
}
24+
return propName
25+
}
26+
27+
const createType = typeDesc => {
28+
const checkPropType = (...args) => typeDesc.propType(...args)
29+
checkPropType.__meta = {
30+
...typeDesc,
31+
toString() {
32+
return typeToString(checkPropType)
33+
},
34+
}
35+
Object.defineProperties(checkPropType, {
36+
isRequired: {
37+
get() {
38+
return createType({
39+
...typeDesc,
40+
propType: typeDesc.propType.isRequired,
41+
required: true,
42+
})
43+
},
44+
},
45+
defaultTo: {
46+
get() {
47+
return value =>
48+
createType({ ...typeDesc, defaultValue: { value: String(value) } })
49+
},
50+
},
51+
desc: {
52+
get() {
53+
return description => createType({ ...typeDesc, description })
54+
},
55+
},
56+
})
57+
return checkPropType
58+
}
59+
60+
const type = (propType, name, { value = null } = {}) => {
61+
return createType({
62+
propType,
63+
type: {
64+
name,
65+
value,
66+
},
67+
required: false,
68+
defaultValue: undefined,
69+
})
70+
}
71+
72+
export const getMetadata = descriptors =>
73+
Object.keys(descriptors).reduce(
74+
(props, key) => ({
75+
...props,
76+
[key]: descriptors[key].__meta,
77+
}),
78+
{},
79+
)
80+
81+
export const array = type(PropTypes.array, 'array')
82+
export const bool = type(PropTypes.bool, 'boolean')
83+
export const func = type(PropTypes.func, 'function')
84+
export const number = type(PropTypes.number, 'number')
85+
export const object = type(PropTypes.object, 'object')
86+
export const string = type(PropTypes.string, 'string')
87+
export const symbol = type(PropTypes.symbol, 'symbol')
88+
export const any = type(PropTypes.any, 'any')
89+
export const arrayOf = value =>
90+
type(PropTypes.arrayOf(value), 'arrayOf', { value })
91+
export const element = type(PropTypes.element, 'element')
92+
export const elementType = type(PropTypes.elementType, 'elementType')
93+
export const instanceOf = value =>
94+
type(PropTypes.instanceOf(value), 'instanceOf', { value })
95+
export const node = type(PropTypes.node, 'node')
96+
export const objectOf = value =>
97+
type(PropTypes.objectOf(value), 'objectOf', { value })
98+
export const oneOf = values =>
99+
type(PropTypes.oneOf(values), 'enum', {
100+
value: values.map(value => ({ value })),
101+
})
102+
export const oneOfType = types =>
103+
type(PropTypes.oneOfType(types), 'union', { value: types })
104+
export const shape = shape =>
105+
type(PropTypes.shape(shape), 'shape', {
106+
value: shape,
107+
})
108+
export const exact = shape =>
109+
type(PropTypes.exact(shape), 'exact', {
110+
value: shape,
111+
})
112+
113+
const PropDesc = {
114+
array,
115+
bool,
116+
func,
117+
number,
118+
object,
119+
string,
120+
symbol,
121+
any,
122+
arrayOf,
123+
element,
124+
elementType,
125+
instanceOf,
126+
node,
127+
objectOf,
128+
oneOf,
129+
oneOfType,
130+
shape,
131+
exact,
132+
getMetadata,
133+
checkPropTypes: PropTypes.checkPropTypes,
134+
resetWarningCache: PropTypes.resetWarningCache,
135+
}
136+
137+
PropDesc.PropTypes = PropDesc
138+
139+
export default PropDesc

0 commit comments

Comments
 (0)