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

@tds/util-prop-types

Package Overview
Dependencies
Maintainers
7
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tds/util-prop-types - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

__tests__/componentWithName.spec.jsx

177

__tests__/prop-types.spec.jsx

@@ -1,174 +0,15 @@

/* eslint-disable import/prefer-default-export, react/prefer-stateless-function */
import * as PropTypes from '../prop-types'
import React from 'react'
import { componentWithName } from '../prop-types'
const FunctionalComponent = () => {}
class ClassComponent extends React.Component {}
describe('prop-types', () => {
describe('componentWithName', () => {
it('returns a function', () => {
expect(typeof componentWithName('label')).toEqual('function')
})
it('exports componentWithName', () => {
expect(PropTypes.componentWithName).toEqual(expect.any(Function))
})
it('throws an error when not given a string', () => {
expect(() => componentWithName()).toThrowError()
expect(() => componentWithName({})).toThrowError()
expect(() => componentWithName([])).toThrowError()
expect(() => componentWithName(123)).toThrowError()
expect(() => componentWithName(true)).toThrowError()
expect(() => componentWithName(false)).toThrowError()
expect(() => componentWithName(undefined)).toThrowError()
expect(() => componentWithName(null)).toThrowError()
})
it('exports responsiveProps', () => {
expect(PropTypes.responsiveProps).toEqual(expect.any(Function))
})
describe('single child', () => {
it('passes validation with a FunctionalComponent', () => {
const validator = componentWithName('FunctionalComponent')
const status = validator(
{ children: <FunctionalComponent>Some content</FunctionalComponent> },
'children',
'FunctionalComponent'
)
expect(status).toBeUndefined()
})
it('passes validation with a ClassComponent', () => {
const validator = componentWithName('ClassComponent')
const status = validator(
{ children: <ClassComponent>Some content</ClassComponent> },
'children',
'ClassComponent'
)
expect(status).toBeUndefined()
})
it('passes when a single required element is supplied and matching', () => {
const validator = componentWithName('FunctionalComponent').isRequired
const status = validator(
{ children: <FunctionalComponent /> },
'children',
'FunctionalComponent'
)
expect(status).toBeUndefined()
})
it('fails validation with a FunctionalComponent', () => {
const validator = componentWithName('FunctionalComponent')
const status = validator({ children: 'Button' }, 'children', 'FunctionalComponent')
expect(status).toBeInstanceOf(Error)
})
it('fails validation with a ClassComponent', () => {
const validator = componentWithName('ClassComponent')
const status = validator({ children: 'Button' }, 'children', 'ClassComponent')
expect(status).toBeInstanceOf(Error)
})
it('fails when a single required element is not supplied', () => {
const validator = componentWithName('FunctionalComponent').isRequired
const status = validator({}, 'children', 'FunctionalComponent')
expect(status).toBeInstanceOf(Error)
})
it('fails when a single required element is supplied but not matching', () => {
const validator = componentWithName('FunctionalComponent').isRequired
const status = validator({ children: 'Button' }, 'children', 'FunctionalComponent')
expect(status).toBeInstanceOf(Error)
})
})
describe('multiple children', () => {
it('passes validation with a FunctionalComponent', () => {
const validator = componentWithName('FunctionalComponent')
const status = validator(
{
children: [
<FunctionalComponent key={1}>One</FunctionalComponent>,
<FunctionalComponent key={2}>Two</FunctionalComponent>,
],
},
'children',
'FunctionalComponent'
)
expect(status).toBeUndefined()
})
it('passes validation with a ClassComponent', () => {
const validator = componentWithName('ClassComponent')
const status = validator(
{
children: [
<ClassComponent key={1}>One</ClassComponent>,
<ClassComponent key={2}>Two</ClassComponent>,
],
},
'children',
'ClassComponent'
)
expect(status).toBeUndefined()
})
it('passes when all supplied elements are matching and required', () => {
const validator = componentWithName('FunctionalComponent').isRequired
const status = validator(
{
children: [<FunctionalComponent key={1} />, <FunctionalComponent key={2} />],
},
'children',
'FunctionalComponent'
)
expect(status).toBeUndefined()
})
it('fails validation with a FunctionalComponent', () => {
const validator = componentWithName('FunctionalComponent')
const status = validator(
{
children: [
<FunctionalComponent key={1}>One</FunctionalComponent>,
<FunctionalComponent key={2}>Two</FunctionalComponent>,
'String',
],
},
'children',
'FunctionalComponent'
)
expect(status).toBeInstanceOf(Error)
})
it('fails validation with a ClassComponent', () => {
const validator = componentWithName('ClassComponent')
const status = validator(
{
children: [
<ClassComponent key={1}>One</ClassComponent>,
<ClassComponent key={2}>Two</ClassComponent>,
'String',
],
},
'children',
'ClassComponent'
)
expect(status).toBeInstanceOf(Error)
})
it('fails when at least one supplied array elements are not matching when required', () => {
const validator = componentWithName('FunctionalComponent').isRequired
const status = validator(
{
children: [
<FunctionalComponent key={1}>One</FunctionalComponent>,
<FunctionalComponent key={2}>Two</FunctionalComponent>,
'String',
],
},
'children',
'FunctionalComponent'
)
expect(status).toBeInstanceOf(Error)
})
})
it('exports or', () => {
expect(PropTypes.or).toEqual(expect.any(Function))
})
})

@@ -6,25 +6,27 @@ # Change Log

<a name="1.1.0"></a>
# [1.1.0](https://github.com/telusdigital/tds/compare/@tds/util-prop-types@1.0.0...@tds/util-prop-types@1.1.0) (2018-12-10)
# [1.2.0](https://github.com/telusdigital/tds/compare/@tds/util-prop-types@1.1.0...@tds/util-prop-types@1.2.0) (2019-01-10)
### Features
* **util-prop-types:** add responsiveProps ([5b4bc18](https://github.com/telusdigital/tds/commit/5b4bc18))
- **util-prop-types:** add or proptype ([79ca8ac](https://github.com/telusdigital/tds/commit/79ca8ac))
<a name="1.1.0"></a>
# [1.1.0](https://github.com/telusdigital/tds/compare/@tds/util-prop-types@1.0.0...@tds/util-prop-types@1.1.0) (2018-12-10)
### Features
- **util-prop-types:** add responsiveProps ([5b4bc18](https://github.com/telusdigital/tds/commit/5b4bc18))
<a name="1.0.0"></a>
# 1.0.0 (2018-10-24)
### Bug Fixes
* **util-prop-types:** return errors when isRequired is used ([a0a9aab](https://github.com/telusdigital/tds/commit/a0a9aab))
- **util-prop-types:** return errors when isRequired is used ([a0a9aab](https://github.com/telusdigital/tds/commit/a0a9aab))
### Features
* **util-prop-types:** add new proptype package ([f855bd7](https://github.com/telusdigital/tds/commit/f855bd7))
* **util-prop-types:** document code ([598656a](https://github.com/telusdigital/tds/commit/598656a))
- **util-prop-types:** add new proptype package ([f855bd7](https://github.com/telusdigital/tds/commit/f855bd7))
- **util-prop-types:** document code ([598656a](https://github.com/telusdigital/tds/commit/598656a))

@@ -7,10 +7,39 @@ 'use strict';

var PropTypes = _interopDefault(require('prop-types'));
var PropTypes = require('prop-types');
var PropTypes__default = _interopDefault(PropTypes);
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
function _typeof(obj) {
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof(obj);
}
function _toConsumableArray(arr) {
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
}
function _arrayWithoutHoles(arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
return arr2;
}
}
function _iterableToArray(iter) {
if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
}
function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance");
}
var componentWithName = function componentWithName(passedName) {

@@ -20,2 +49,3 @@ if (typeof passedName !== 'string') {

}
var checkProp = function checkProp(props, propName, componentName) {

@@ -29,4 +59,5 @@ if (Array.isArray(props[propName])) {

} else if (props[propName] && _typeof(props[propName]) !== 'object' || props[propName] && props[propName].type.name !== passedName) {
return new Error(componentName + ': Component passed to `' + propName + '` prop should be ' + passedName);
return new Error("".concat(componentName, ": Component passed to `").concat(propName, "` prop should be ").concat(passedName));
}
return undefined;

@@ -37,4 +68,5 @@ };

if (props[propName] === undefined) {
return new Error('The prop `' + propName + '` is marked as required in `' + componentName + '`, but its value is ' + props[propName] + '.');
return new Error("The prop `".concat(propName, "` is marked as required in `").concat(componentName, "`, but its value is ").concat(props[propName], "."));
}
return undefined;

@@ -61,3 +93,2 @@ };

validate.isRequired = createValidate(true);
return validate;

@@ -71,4 +102,5 @@ };

*/
function responsiveProps(type) {
return PropTypes.oneOfType([type, PropTypes.shape({
return PropTypes__default.oneOfType([type, PropTypes__default.shape({
xs: type,

@@ -82,4 +114,65 @@ sm: type,

var createValidator = function createValidator(validators) {
var validator = function validator(props, propName, componentName) {
for (var _len = arguments.length, rest = new Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) {
rest[_key - 3] = arguments[_key];
}
if (props[propName] === undefined) {
return null;
}
var errors = validators.map(function (v) {
return v.apply(void 0, [props, propName, componentName].concat(rest));
}).filter(function (error) {
return error;
});
if (errors.length >= validators.length) {
return new Error("Invalid value supplied to ".concat(propName, " in ").concat(componentName, "."));
}
return null;
};
validator.isRequired = function (props, propName, componentName) {
for (var _len2 = arguments.length, rest = new Array(_len2 > 3 ? _len2 - 3 : 0), _key2 = 3; _key2 < _len2; _key2++) {
rest[_key2 - 3] = arguments[_key2];
}
if (props[propName] === undefined) {
return new Error("The prop `".concat(propName, "` is marked as required in `").concat(componentName, "`, but its value is ").concat(props[propName], "."));
}
var errors = validators.map(function (v) {
return v.apply(void 0, [props, propName, componentName].concat(rest));
}).filter(function (error) {
return error;
});
if (errors.length === validators.length) {
return new Error("Invalid value ".concat(errors, " supplied to required prop `").concat(propName, "` in `").concat(componentName, "`."));
}
return null;
};
return validator;
};
var or = function or(validators) {
if (!Array.isArray(validators)) {
throw new Error('2 or more validators are required to use or');
}
if (validators.length < 2) {
throw new Error('2 or more validators are required to use or');
}
return createValidator([PropTypes.arrayOf(createValidator(validators))].concat(_toConsumableArray(validators)));
};
exports.componentWithName = componentWithName;
exports.responsiveProps = responsiveProps;
exports.or = or;
//# sourceMappingURL=index.cjs.js.map

@@ -1,9 +0,37 @@

import PropTypes from 'prop-types';
import PropTypes, { arrayOf } from 'prop-types';
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
function _typeof(obj) {
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof(obj);
}
function _toConsumableArray(arr) {
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
}
function _arrayWithoutHoles(arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
return arr2;
}
}
function _iterableToArray(iter) {
if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
}
function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance");
}
var componentWithName = function componentWithName(passedName) {

@@ -13,2 +41,3 @@ if (typeof passedName !== 'string') {

}
var checkProp = function checkProp(props, propName, componentName) {

@@ -22,4 +51,5 @@ if (Array.isArray(props[propName])) {

} else if (props[propName] && _typeof(props[propName]) !== 'object' || props[propName] && props[propName].type.name !== passedName) {
return new Error(componentName + ': Component passed to `' + propName + '` prop should be ' + passedName);
return new Error("".concat(componentName, ": Component passed to `").concat(propName, "` prop should be ").concat(passedName));
}
return undefined;

@@ -30,4 +60,5 @@ };

if (props[propName] === undefined) {
return new Error('The prop `' + propName + '` is marked as required in `' + componentName + '`, but its value is ' + props[propName] + '.');
return new Error("The prop `".concat(propName, "` is marked as required in `").concat(componentName, "`, but its value is ").concat(props[propName], "."));
}
return undefined;

@@ -54,3 +85,2 @@ };

validate.isRequired = createValidate(true);
return validate;

@@ -64,2 +94,3 @@ };

*/
function responsiveProps(type) {

@@ -75,3 +106,63 @@ return PropTypes.oneOfType([type, PropTypes.shape({

export { componentWithName, responsiveProps };
var createValidator = function createValidator(validators) {
var validator = function validator(props, propName, componentName) {
for (var _len = arguments.length, rest = new Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) {
rest[_key - 3] = arguments[_key];
}
if (props[propName] === undefined) {
return null;
}
var errors = validators.map(function (v) {
return v.apply(void 0, [props, propName, componentName].concat(rest));
}).filter(function (error) {
return error;
});
if (errors.length >= validators.length) {
return new Error("Invalid value supplied to ".concat(propName, " in ").concat(componentName, "."));
}
return null;
};
validator.isRequired = function (props, propName, componentName) {
for (var _len2 = arguments.length, rest = new Array(_len2 > 3 ? _len2 - 3 : 0), _key2 = 3; _key2 < _len2; _key2++) {
rest[_key2 - 3] = arguments[_key2];
}
if (props[propName] === undefined) {
return new Error("The prop `".concat(propName, "` is marked as required in `").concat(componentName, "`, but its value is ").concat(props[propName], "."));
}
var errors = validators.map(function (v) {
return v.apply(void 0, [props, propName, componentName].concat(rest));
}).filter(function (error) {
return error;
});
if (errors.length === validators.length) {
return new Error("Invalid value ".concat(errors, " supplied to required prop `").concat(propName, "` in `").concat(componentName, "`."));
}
return null;
};
return validator;
};
var or = function or(validators) {
if (!Array.isArray(validators)) {
throw new Error('2 or more validators are required to use or');
}
if (validators.length < 2) {
throw new Error('2 or more validators are required to use or');
}
return createValidator([arrayOf(createValidator(validators))].concat(_toConsumableArray(validators)));
};
export { componentWithName, responsiveProps, or };
//# sourceMappingURL=index.es.js.map
{
"name": "@tds/util-prop-types",
"version": "1.1.0",
"version": "1.2.0",
"description": "TDS proptypes",

@@ -25,3 +25,4 @@ "main": "dist/index.cjs.js",

"prop-types": "^15.6.2"
}
},
"gitHead": "2ab495dd4f6206a40ddab995ec163f0f37856e4e"
}
import componentWithName from './componentWithName'
import responsiveProps from './responsiveProps'
import or from './or'
export { componentWithName, responsiveProps }
export { componentWithName, responsiveProps, or }

@@ -18,3 +18,5 @@ ```js static

const PlanChooser = () => <fieldset>{children}</fieldset>
const PlanChooser = () => {
return <fieldset>{children}</fieldset>
}

@@ -33,2 +35,24 @@ const App = () => (

### `or(array)`
Limits the prop to only allow PropTypes that match those specified.
```jsx noeditor static
import { or } from '@tds/util-prop-types'
const Button = () => {
return <button>{children}</button>
}
Button.propTypes = {
children: or([PropTypes.string, componentWithName('A11yContent')]).isRequired,
}
const App = () => (
<Button>
Purchase <A11yContent>Pixel 3</A11yContent>
</Button>
)
```
### <a name="responsiveProps"></a>`responsiveProps(propType)`

@@ -35,0 +59,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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