assign-prop-types
Advanced tools
Comparing version 1.1.0-beta to 1.1.0
28
index.js
exports.__esModule = true; | ||
var isDevelopment = typeof process === 'object' && process.env && process.env.NODE_ENV !== 'production'; | ||
var ASSIGNER = Symbol('PROP_TYPES_ASSIGNER'); | ||
var ASSIGNER = typeof Symbol === 'function' ? Symbol('PROP_TYPES_ASSIGNER') : 'PROP_TYPES_ASSIGNER'; | ||
function markAssigner(assigner) { | ||
Object.defineProperty(assigner, ASSIGNER, { | ||
enumerable: false, | ||
writable: false, | ||
configurable: false, | ||
value: true | ||
}); | ||
} | ||
function isNativeObject(something) { | ||
@@ -42,3 +51,3 @@ if (typeof something !== 'object') { | ||
var expectedType = 0; | ||
for (let i = 0; i < length; i++) { | ||
for (var i = 0; i < length; i++) { | ||
if (expectedType === 0) { | ||
@@ -90,8 +99,9 @@ if (isAssigner(arguments[i])) { | ||
}; | ||
originalAssigner[ASSIGNER] = true; | ||
const propTypesAssigner = function propTypesAssigner(component) { | ||
markAssigner(originalAssigner); | ||
var propTypesAssigner = function propTypesAssigner(component) { | ||
if (isNativeObject(component)) { | ||
// Extending | ||
const reassigner = assignPropTypes.apply(null, arguments); | ||
var reassigner = assignPropTypes.apply(null, arguments); | ||
return assignPropTypes( | ||
@@ -104,4 +114,4 @@ originalAssigner, | ||
} | ||
} | ||
propTypesAssigner[ASSIGNER] = true; | ||
}; | ||
markAssigner(propTypesAssigner); | ||
return propTypesAssigner; | ||
@@ -111,6 +121,6 @@ } | ||
exports.default = assignPropTypes; | ||
var combineAssigners = exports.combineAssigners = function combineAssigners() { | ||
exports.combineAssigners = function combineAssigners() { | ||
var pseudoComponent = function() {}; | ||
var assigners = Array.prototype.slice.call(arguments); | ||
assigners.forEach((assigner) => { | ||
assigners.forEach(function(assigner) { | ||
pseudoComponent = assigner(pseudoComponent); | ||
@@ -117,0 +127,0 @@ }); |
{ | ||
"name": "assign-prop-types", | ||
"version": "1.1.0-beta", | ||
"version": "1.1.0", | ||
"description": "Assign propTypes (and defaultProps & contextTypes) to the component in a functional style", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "jest test.js" | ||
"test": "./node_modules/.bin/eslint index.js && ./node_modules/.bin/jest test.js" | ||
}, | ||
@@ -16,2 +16,3 @@ "keywords": [ | ||
"devDependencies": { | ||
"eslint": "^3.19.0", | ||
"jest": "^20.0.0", | ||
@@ -18,0 +19,0 @@ "prop-types": "^15.5.8" |
@@ -7,5 +7,18 @@ assign-prop-types | ||
```js | ||
export default assignPropTypes({ | ||
children: PropTypes.node.isRequired, | ||
})(({ children }) => (<div>{children}</div>)); | ||
export default assignPropTypes( | ||
// propTypes | ||
{ | ||
selector: PropTypes.func, | ||
}, | ||
// defaultProps | ||
{ | ||
selector: () => 'No selector passed' | ||
}, | ||
// contextTypes | ||
{ | ||
store: PropTypes.object | ||
} | ||
)(({ selector }, { store }) => ( | ||
<div>{selector(store.getState())}</div> | ||
)); | ||
``` | ||
@@ -33,3 +46,3 @@ | ||
In most cases, you will also need to import packages [react](https://www.npmjs.com/package/react) and [prop-types](https://www.npmjs.com/package/prop-types) (or `React.PropTypes` for [React v15.5](https://facebook.github.io/react/warnings/dont-call-proptypes.html)). | ||
In most cases, you will also need to import packages [react](https://www.npmjs.com/package/react) and [prop-types](https://www.npmjs.com/package/prop-types) (or `React.PropTypes` for [React < v15.5](https://facebook.github.io/react/warnings/dont-call-proptypes.html)). | ||
@@ -45,3 +58,3 @@ ```js | ||
The function assignPropTypes accepts optional arguments `propTypes`, `defaultProp`, `contextTypes`. It returns function, called assigner, which, in turn, accepts React component and returns component, mutaded by passed properties. | ||
The function assignPropTypes accepts optional parameters [`propTypes`], [`defaultProp`], [`contextTypes`] and returns function, called assigner, which, in turn, accepts React component and returns component, mutaded with passed properties. | ||
@@ -71,9 +84,12 @@ ```js | ||
const assignUsualTypes = assignPropTypes({ | ||
children: PropTypes.node.isRequired, | ||
children: PropTypes.node, | ||
}, { | ||
children: "No children specified" | ||
}); | ||
``` | ||
And applied later: | ||
And applied later multiple times: | ||
```js | ||
export default assignUsualTypes(({ children }) => (<h2>{children}</h2>)); | ||
export const H1 = assignUsualTypes(({ children }) => (<h1>{children}</h1>)); | ||
export const H2 = assignUsualTypes(({ children }) => (<h2>{children}</h2>)); | ||
``` | ||
@@ -84,3 +100,3 @@ | ||
Assigners can be extended. To perform it, just call assigner with advanced configuration: | ||
Assigners can be extended. To extend assigner, just call it with advanced configuration: | ||
@@ -94,5 +110,7 @@ ```js | ||
})(YourComponent); | ||
// propTypes will be { children, title } | ||
``` | ||
Or passing another assigner(s): | ||
Or by passing another assigner(s) to combine them: | ||
@@ -99,0 +117,0 @@ ```js |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
8838
6
140
0
137
3