Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@dhis2/prop-types

Package Overview
Dependencies
Maintainers
14
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dhis2/prop-types - npm Package Compare versions

Comparing version 1.0.2 to 1.0.3

111

build/cjs/arrayWithLength.js

@@ -1,56 +0,89 @@

import propTypes from 'prop-types';
"use strict";
const arrayWithLengthFactory = ({
min = 0,
max = Infinity,
propType,
isRequired
}) => (props, propName, componentName) => {
const arr = props[propName];
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.arrayWithLength = arrayWithLength;
if (isRequired && typeof arr === 'undefined') {
return new Error(`${propName} is required.`);
}
var _propTypes = _interopRequireDefault(require("prop-types"));
if (arr && !Array.isArray(arr)) {
return new Error(`${propName} is not an array.`);
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
if (arr && arr.length > max) {
return new Error( // prettier-ignore
`${propName} array has a length of ${arr.length}, but the maximum is ${max}`);
}
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
if (arr && arr.length < min) {
return new Error( // prettier-ignore
`${propName} array has a length of ${arr.length}, but the minimum is ${min}`);
}
var arrayWithLengthFactory = function arrayWithLengthFactory(_ref) {
var _ref$min = _ref.min,
min = _ref$min === void 0 ? 0 : _ref$min,
_ref$max = _ref.max,
max = _ref$max === void 0 ? Infinity : _ref$max,
propType = _ref.propType,
isRequired = _ref.isRequired;
return function (props, propName, componentName) {
var arr = props[propName];
if (arr && propType) {
const len = arr.length;
if (isRequired && typeof arr === 'undefined') {
return new Error("".concat(propName, " is required."));
}
for (let i = 0; i < len; i++) {
propTypes.checkPropTypes({
[i]: propType
}, arr, propName, componentName);
if (arr && !Array.isArray(arr)) {
return new Error("".concat(propName, " is not an array."));
}
}
return null;
if (arr && arr.length > max) {
return new Error( // prettier-ignore
"".concat(propName, " array has a length of ").concat(arr.length, ", but the maximum is ").concat(max));
}
if (arr && arr.length < min) {
return new Error( // prettier-ignore
"".concat(propName, " array has a length of ").concat(arr.length, ", but the minimum is ").concat(min));
}
if (arr && propType) {
var len = arr.length;
for (var i = 0; i < len; i++) {
_propTypes.default.checkPropTypes(_defineProperty({}, i, propType), arr, propName, componentName);
}
}
return null;
};
};
/**
* Ensure the prop value is an array with a length between a minimum and maximum.
* If a third `propType` argument is passed each item in the array needs to be of that prop-type
* @param {number} [min=0] - The minimal array length
* @param {number} [max=Infinity] - The maximal array length
* @param {function} [propType] - The prop-type that each array item needs to conform to
* @return {Error|null} Returns null if all conditions are met, or an error
* @example
* import React from 'react'
* import { arrayWithLength } from '@dhis2/prop-types'
*
* const LotsOfLists = props => <div {...props}>Does nothing</div>
*
* LotsOfLists.propTypes = {
* arrayWithMaxThreeNumbers: arrayWithLength(0, 3, propTypes.number),
* arrayWithAtLeastSixStrings: arrayWithLength(6, undefined, propTypes.string),
* arrayWithAtLeastTenItems: arrayWithLength(10),
* mandatoryArrayBetweenOneAndTen: arrayWithLength(1,10).isRequired,
* }
*/
export const arrayWithLength = (min, max, propType) => {
const fn = arrayWithLengthFactory({
min,
max,
propType,
function arrayWithLength(min, max, propType) {
var fn = arrayWithLengthFactory({
min: min,
max: max,
propType: propType,
isRequired: false
});
fn.isRequired = arrayWithLengthFactory({
min,
max,
propType,
min: min,
max: max,
propType: propType,
isRequired: true
});
return fn;
};
}

@@ -1,3 +0,29 @@

export { arrayWithLength } from './arrayWithLength';
export { instanceOfComponent } from './instanceOfComponent';
export { mutuallyExclusive } from './mutuallyExclusive';
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "arrayWithLength", {
enumerable: true,
get: function get() {
return _arrayWithLength.arrayWithLength;
}
});
Object.defineProperty(exports, "instanceOfComponent", {
enumerable: true,
get: function get() {
return _instanceOfComponent.instanceOfComponent;
}
});
Object.defineProperty(exports, "mutuallyExclusive", {
enumerable: true,
get: function get() {
return _mutuallyExclusive.mutuallyExclusive;
}
});
var _arrayWithLength = require("./arrayWithLength");
var _instanceOfComponent = require("./instanceOfComponent");
var _mutuallyExclusive = require("./mutuallyExclusive");

@@ -1,27 +0,60 @@

import React from 'react';
"use strict";
const instanceOfComponentFactory = (Component, isRequired) => (props, propName) => {
const children = props[propName];
const count = React.Children.count(children);
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.instanceOfComponent = instanceOfComponent;
if (isRequired && count === 0) {
return new Error(`${propName} is required.`);
}
var _react = _interopRequireDefault(require("react"));
if (count > 1) {
return new Error(`Prop validator 'instanceOfComponent' expected 1 component instance, instead found ${count}.`);
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
if (children.type !== Component) {
const componentName = Component.name || Component.displayName;
return new Error(`Child at index ${propName} is not an instance of component ${componentName}.`);
}
var instanceOfComponentFactory = function instanceOfComponentFactory(Component, isRequired) {
return function (props, propName) {
var children = props[propName];
return null;
var count = _react.default.Children.count(children);
if (isRequired && count === 0) {
return new Error("".concat(propName, " is required."));
}
if (count > 1) {
return new Error("Prop validator 'instanceOfComponent' expected 1 component instance, instead found ".concat(count, "."));
}
if (children.type !== Component) {
var componentName = Component.name || Component.displayName;
return new Error("Child at index ".concat(propName, " is not an instance of component ").concat(componentName, "."));
}
return null;
};
};
/**
* Ensure the prop value is an instance of a certain component
* @param {function} Component - The component that is expected
* @return {Error|null} Returns null if all conditions are met, or an error
* @example
* import React from 'react'
* import { instanceOfComponent } from '@dhis2/prop-types'
* import { Button } from './Button'
*
* const ButtonWrap = ({ children }) => <div>{children}</div>
* // This would allow the ButtonWrap to be empty
* ButtonWrap.propTypes = {
* children: instanceOfComponent(Button)
* }
*
* // Enforce presence of a Button instance
* ButtonWrap.propTypes = {
* children: instanceOfComponent(Button).isRequired
* }
*/
export const instanceOfComponent = Component => {
const fn = instanceOfComponentFactory(Component, false);
function instanceOfComponent(Component) {
var fn = instanceOfComponentFactory(Component, false);
fn.isRequired = instanceOfComponentFactory(Component, true);
return fn;
};
}

@@ -1,33 +0,74 @@

import propTypes from 'prop-types';
"use strict";
const mutuallyExclusiveFactory = (exlusivePropNames, propType, isRequired) => (props, propName, componentName) => {
if (exlusivePropNames.length === 0) {
return new Error(`mutuallyExclusive was called without any arguments for property '${propName}'.`);
}
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.mutuallyExclusive = mutuallyExclusive;
if (isRequired && typeof props[propName] === 'undefined') {
return new Error(`${propName} is required.`);
} // This is how to programatically invoke a propTypes check
// https://github.com/facebook/prop-types#proptypescheckproptypes
var _propTypes = _interopRequireDefault(require("prop-types"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
propTypes.checkPropTypes({
[propName]: propType
}, props, propName, componentName);
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
if (props[propName]) {
const thruthySiblingPropName = exlusivePropNames.find(name => props[name] && name !== propName);
var mutuallyExclusiveFactory = function mutuallyExclusiveFactory(exlusivePropNames, propType, isRequired) {
return function (props, propName, componentName) {
if (exlusivePropNames.length === 0) {
return new Error("mutuallyExclusive was called without any arguments for property '".concat(propName, "'."));
}
if (thruthySiblingPropName) {
return new Error(`Property '${propName}' is mutually exclusive with '${thruthySiblingPropName}', but both have a value.`);
if (isRequired && typeof props[propName] === 'undefined') {
return new Error("".concat(propName, " is required."));
} // This is how to programatically invoke a propTypes check
// https://github.com/facebook/prop-types#proptypescheckproptypes
_propTypes.default.checkPropTypes(_defineProperty({}, propName, propType), props, propName, componentName);
if (props[propName]) {
var thruthySiblingPropName = exlusivePropNames.find(function (name) {
return props[name] && name !== propName;
});
if (thruthySiblingPropName) {
return new Error("Property '".concat(propName, "' is mutually exclusive with '").concat(thruthySiblingPropName, "', but both have a value."));
}
}
}
return null;
return null;
};
};
/**
* Ensure that only one property within a specified list is thruthy
* This function will also check if the current property value is of the specified type
* @param {array<string>} exlusivePropNames - The prop names to be checked
* @param {function} propType - The prop-type that the current prop-value needs to conform to
* @return {Error|null} Returns null if all conditions are met, or an error
* @example
* import React from 'react'
* import cx from 'classnames'
* import propTypes from 'prop-types'
* import { mutuallyExclusive } from '@dhis2/prop-types'
*
* const Alert = ({ danger, warning, success, children }) => (
* <div className={cx({danger, warning, success})}>
* {children}
* </div>
* )
*
* const statusPropType = mutuallyExclusive(['danger', 'warning', 'success'], propTypes.bool)
*
* Alert.propTypes = {
* children: propTypes.node,
* danger: statusPropType,
* warning: statusPropType,
* success: statusPropType,
* }
*/
export const mutuallyExclusive = (exlusivePropNames, propType) => {
const fn = mutuallyExclusiveFactory(exlusivePropNames, propType, false);
function mutuallyExclusive(exlusivePropNames, propType) {
var fn = mutuallyExclusiveFactory(exlusivePropNames, propType, false);
fn.isRequired = mutuallyExclusiveFactory(exlusivePropNames, propType, true);
return fn;
};
}

@@ -41,4 +41,25 @@ import propTypes from 'prop-types';

};
/**
* Ensure the prop value is an array with a length between a minimum and maximum.
* If a third `propType` argument is passed each item in the array needs to be of that prop-type
* @param {number} [min=0] - The minimal array length
* @param {number} [max=Infinity] - The maximal array length
* @param {function} [propType] - The prop-type that each array item needs to conform to
* @return {Error|null} Returns null if all conditions are met, or an error
* @example
* import React from 'react'
* import { arrayWithLength } from '@dhis2/prop-types'
*
* const LotsOfLists = props => <div {...props}>Does nothing</div>
*
* LotsOfLists.propTypes = {
* arrayWithMaxThreeNumbers: arrayWithLength(0, 3, propTypes.number),
* arrayWithAtLeastSixStrings: arrayWithLength(6, undefined, propTypes.string),
* arrayWithAtLeastTenItems: arrayWithLength(10),
* mandatoryArrayBetweenOneAndTen: arrayWithLength(1,10).isRequired,
* }
*/
export const arrayWithLength = (min, max, propType) => {
export function arrayWithLength(min, max, propType) {
const fn = arrayWithLengthFactory({

@@ -57,2 +78,2 @@ min,

return fn;
};
}

@@ -22,7 +22,28 @@ import React from 'react';

};
/**
* Ensure the prop value is an instance of a certain component
* @param {function} Component - The component that is expected
* @return {Error|null} Returns null if all conditions are met, or an error
* @example
* import React from 'react'
* import { instanceOfComponent } from '@dhis2/prop-types'
* import { Button } from './Button'
*
* const ButtonWrap = ({ children }) => <div>{children}</div>
* // This would allow the ButtonWrap to be empty
* ButtonWrap.propTypes = {
* children: instanceOfComponent(Button)
* }
*
* // Enforce presence of a Button instance
* ButtonWrap.propTypes = {
* children: instanceOfComponent(Button).isRequired
* }
*/
export const instanceOfComponent = Component => {
export function instanceOfComponent(Component) {
const fn = instanceOfComponentFactory(Component, false);
fn.isRequired = instanceOfComponentFactory(Component, true);
return fn;
};
}

@@ -28,7 +28,35 @@ import propTypes from 'prop-types';

};
/**
* Ensure that only one property within a specified list is thruthy
* This function will also check if the current property value is of the specified type
* @param {array<string>} exlusivePropNames - The prop names to be checked
* @param {function} propType - The prop-type that the current prop-value needs to conform to
* @return {Error|null} Returns null if all conditions are met, or an error
* @example
* import React from 'react'
* import cx from 'classnames'
* import propTypes from 'prop-types'
* import { mutuallyExclusive } from '@dhis2/prop-types'
*
* const Alert = ({ danger, warning, success, children }) => (
* <div className={cx({danger, warning, success})}>
* {children}
* </div>
* )
*
* const statusPropType = mutuallyExclusive(['danger', 'warning', 'success'], propTypes.bool)
*
* Alert.propTypes = {
* children: propTypes.node,
* danger: statusPropType,
* warning: statusPropType,
* success: statusPropType,
* }
*/
export const mutuallyExclusive = (exlusivePropNames, propType) => {
export function mutuallyExclusive(exlusivePropNames, propType) {
const fn = mutuallyExclusiveFactory(exlusivePropNames, propType, false);
fn.isRequired = mutuallyExclusiveFactory(exlusivePropNames, propType, true);
return fn;
};
}

@@ -0,1 +1,8 @@

## [1.0.3](https://github.com/dhis2/prop-types/compare/v1.0.2...v1.0.3) (2019-07-29)
### Bug Fixes
* build cjs module correctly ([#31](https://github.com/dhis2/prop-types/issues/31)) ([2dc51da](https://github.com/dhis2/prop-types/commit/2dc51da))
## [1.0.2](https://github.com/dhis2/prop-types/compare/v1.0.1...v1.0.2) (2019-07-17)

@@ -2,0 +9,0 @@

{
"name": "@dhis2/prop-types",
"version": "1.0.2",
"version": "1.0.3",
"main": "./build/cjs/index.js",

@@ -15,2 +15,3 @@ "module": "./build/es/index.js",

"scripts": {
"docs": "jsdoc2md -t jsdoc2md/README.hbs src/*.js > README.md; echo",
"prebuild": "rm -rf ./build/**",

@@ -23,6 +24,7 @@ "build:commonjs": "BABEL_ENV=commonjs babel src --out-dir ./build/cjs --copy-files --verbose",

"devDependencies": {
"@babel/cli": "^7.5.0",
"@babel/core": "^7.5.4",
"@babel/preset-env": "^7.5.4",
"@dhis2/cli-style": "4.1.0"
"@babel/cli": "^7.5.5",
"@babel/core": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@dhis2/cli-style": "4.1.1",
"jsdoc-to-markdown": "^5.0.0"
},

@@ -29,0 +31,0 @@ "peerDependencies": {

@@ -10,28 +10,113 @@ # DHIS2 propTypes

## Usage
## Available prop-types
Just import the propType that you'd like to use
## Functions
```jsx
<dl>
<dt><a href="#arrayWithLength">arrayWithLength([min], [max], [propType])</a> ⇒ <code>Error</code> | <code>null</code></dt>
<dd><p>Ensure the prop value is an array with a length between a minimum and maximum.
If a third <code>propType</code> argument is passed each item in the array needs to be of that prop-type</p>
</dd>
<dt><a href="#instanceOfComponent">instanceOfComponent(Component)</a> ⇒ <code>Error</code> | <code>null</code></dt>
<dd><p>Ensure the prop value is an instance of a certain component</p>
</dd>
<dt><a href="#mutuallyExclusive">mutuallyExclusive(exlusivePropNames, propType)</a> ⇒ <code>Error</code> | <code>null</code></dt>
<dd><p>Ensure that only one property within a specified list is thruthy
This function will also check if the current property value is of the specified type</p>
</dd>
</dl>
<a name="arrayWithLength"></a>
## arrayWithLength([min], [max], [propType]) ⇒ <code>Error</code> \| <code>null</code>
Ensure the prop value is an array with a length between a minimum and maximum.
If a third `propType` argument is passed each item in the array needs to be of that prop-type
**Kind**: global function
**Returns**: <code>Error</code> \| <code>null</code> - Returns null if all conditions are met, or an error
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [min] | <code>number</code> | <code>0</code> | The minimal array length |
| [max] | <code>number</code> | <code>Infinity</code> | The maximal array length |
| [propType] | <code>function</code> | | The prop-type that each array item needs to conform to |
**Example**
```js
import React from 'react'
import propTypes from 'propTypes'
import { arrayWithLength } from '@dhis2/prop-types'
const LotsOfLists = props => <div {...props}>Does nothing</div>
LotsOfLists.propTypes = {
arrayWithMaxThreeNumbers: arrayWithLength(0, 3, propTypes.number),
arrayWithAtLeastSixStrings: arrayWithLength(6, undefined, propTypes.string),
arrayWithAtLeastTenItems: arrayWithLength(10),
mandatoryArrayBetweenOneAndTen: arrayWithLength(1,10).isRequired,
}
```
<a name="instanceOfComponent"></a>
## instanceOfComponent(Component) ⇒ <code>Error</code> \| <code>null</code>
Ensure the prop value is an instance of a certain component
**Kind**: global function
**Returns**: <code>Error</code> \| <code>null</code> - Returns null if all conditions are met, or an error
| Param | Type | Description |
| --- | --- | --- |
| Component | <code>function</code> | The component that is expected |
**Example**
```js
import React from 'react'
import { instanceOfComponent } from '@dhis2/prop-types'
import { Button } from './Button'
const ButtonWrap = ({ children }) => <div>{children}</div>
// This would allow the ButtonWrap to be empty
ButtonWrap.propTypes = {
children: instanceOfComponent(Button)
}
// Enforce presence of a Button instance
ButtonWrap.propTypes = {
children: instanceOfComponent(Button).isRequired
}
```
<a name="mutuallyExclusive"></a>
## mutuallyExclusive(exlusivePropNames, propType) ⇒ <code>Error</code> \| <code>null</code>
Ensure that only one property within a specified list is thruthy
This function will also check if the current property value is of the specified type
**Kind**: global function
**Returns**: <code>Error</code> \| <code>null</code> - Returns null if all conditions are met, or an error
| Param | Type | Description |
| --- | --- | --- |
| exlusivePropNames | <code>array.&lt;string&gt;</code> | The prop names to be checked |
| propType | <code>function</code> | The prop-type that the current prop-value needs to conform to |
**Example**
```js
import React from 'react'
import cx from 'classnames'
import propTypes from 'prop-types'
import { mutuallyExclusive } from '@dhis2/prop-types'
const Comp = (foo, bar, baz) => (
foo ? 'foo'
: bar ? 'bar'
: baz ? 'baz'
: 'foobarbaz'
const Alert = ({ danger, warning, success, children }) => (
<div className={cx({danger, warning, success})}>
{children}
</div>
)
const propTypeFooBarBaz = mutuallyExclusive(
[ 'foo', 'bar', 'baz' ],
propTypes.bool,
)
const statusPropType = mutuallyExclusive(['danger', 'warning', 'success'], propTypes.bool)
Comp.propTypes = {
foo: propTypeFooBarBaz,
bar: propTypeFooBarBaz,
baz: propTypeFooBarBaz,
Alert.propTypes = {
children: propTypes.node,
danger: statusPropType,
warning: statusPropType,
success: statusPropType,
}
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc