add createChaindFunction
This commit is contained in:
parent
46213ae083
commit
b7adea5b0f
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"presets": ["es2015-loose", "stage-1"],
|
||||
|
||||
"env": {
|
||||
"test": {
|
||||
"plugins": ["istanbul"]
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
function createChainedFunction(...funcs) {
|
||||
return funcs
|
||||
.filter(f => f != null)
|
||||
.reduce((acc, f) => {
|
||||
if (typeof f !== 'function') {
|
||||
throw new Error('Invalid Argument Type, must only provide functions, undefined, or null.');
|
||||
}
|
||||
|
||||
if (acc === null) {
|
||||
return f;
|
||||
}
|
||||
|
||||
return function chainedFunction(...args) {
|
||||
acc.apply(this, args);
|
||||
f.apply(this, args);
|
||||
};
|
||||
}, null);
|
||||
}
|
||||
export default createChainedFunction;
|
|
@ -4,3 +4,4 @@ export deprecated from './deprecated';
|
|||
export elementType from './elementType';
|
||||
export isRequiredForA11y from './isRequiredForA11y';
|
||||
export splitComponent from './splitComponent';
|
||||
export createChainedFunction from './createChainedFunction';
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
'use strict';
|
||||
|
||||
exports.__esModule = true;
|
||||
exports.default = all;
|
||||
|
||||
var _createChainableTypeChecker = require('./utils/createChainableTypeChecker');
|
||||
|
||||
var _createChainableTypeChecker2 = _interopRequireDefault(_createChainableTypeChecker);
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
function all() {
|
||||
for (var _len = arguments.length, validators = Array(_len), _key = 0; _key < _len; _key++) {
|
||||
validators[_key] = arguments[_key];
|
||||
}
|
||||
|
||||
function allPropTypes() {
|
||||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
||||
args[_key2] = arguments[_key2];
|
||||
}
|
||||
|
||||
var error = null;
|
||||
|
||||
validators.forEach(function (validator) {
|
||||
if (error != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
var result = validator.apply(undefined, args);
|
||||
if (result != null) {
|
||||
error = result;
|
||||
}
|
||||
});
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
return (0, _createChainableTypeChecker2.default)(allPropTypes);
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
'use strict';
|
||||
|
||||
exports.__esModule = true;
|
||||
|
||||
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; };
|
||||
|
||||
var _react = require('react');
|
||||
|
||||
var _react2 = _interopRequireDefault(_react);
|
||||
|
||||
var _createChainableTypeChecker = require('./utils/createChainableTypeChecker');
|
||||
|
||||
var _createChainableTypeChecker2 = _interopRequireDefault(_createChainableTypeChecker);
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
function validate(props, propName, componentName, location, propFullName) {
|
||||
var propValue = props[propName];
|
||||
var propType = typeof propValue === 'undefined' ? 'undefined' : _typeof(propValue);
|
||||
|
||||
if (_react2.default.isValidElement(propValue)) {
|
||||
return new Error('Invalid ' + location + ' `' + propFullName + '` of type ReactElement ' + ('supplied to `' + componentName + '`, expected a ReactComponent or a ') + 'DOMElement. You can usually obtain a ReactComponent or DOMElement ' + 'from a ReactElement by attaching a ref to it.');
|
||||
}
|
||||
|
||||
if ((propType !== 'object' || typeof propValue.render !== 'function') && propValue.nodeType !== 1) {
|
||||
return new Error('Invalid ' + location + ' `' + propFullName + '` of value `' + propValue + '` ' + ('supplied to `' + componentName + '`, expected a ReactComponent or a ') + 'DOMElement.');
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
exports.default = (0, _createChainableTypeChecker2.default)(validate);
|
|
@ -0,0 +1,41 @@
|
|||
'use strict';
|
||||
|
||||
exports.__esModule = true;
|
||||
exports.default = deprecated;
|
||||
|
||||
var _warning = require('warning');
|
||||
|
||||
var _warning2 = _interopRequireDefault(_warning);
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
var warned = {};
|
||||
|
||||
function deprecated(validator, reason) {
|
||||
return function validate(props, propName, componentName, location, propFullName) {
|
||||
var componentNameSafe = componentName || '<<anonymous>>';
|
||||
var propFullNameSafe = propFullName || propName;
|
||||
|
||||
if (props[propName] != null) {
|
||||
var messageKey = componentName + '.' + propName;
|
||||
|
||||
(0, _warning2.default)(warned[messageKey], 'The ' + location + ' `' + propFullNameSafe + '` of ' + ('`' + componentNameSafe + '` is deprecated. ' + reason + '.'));
|
||||
|
||||
warned[messageKey] = true;
|
||||
}
|
||||
|
||||
for (var _len = arguments.length, args = Array(_len > 5 ? _len - 5 : 0), _key = 5; _key < _len; _key++) {
|
||||
args[_key - 5] = arguments[_key];
|
||||
}
|
||||
|
||||
return validator.apply(undefined, [props, propName, componentName, location, propFullName].concat(args));
|
||||
};
|
||||
}
|
||||
|
||||
/* eslint-disable no-underscore-dangle */
|
||||
function _resetWarned() {
|
||||
warned = {};
|
||||
}
|
||||
|
||||
deprecated._resetWarned = _resetWarned;
|
||||
/* eslint-enable no-underscore-dangle */
|
|
@ -0,0 +1,32 @@
|
|||
'use strict';
|
||||
|
||||
exports.__esModule = true;
|
||||
|
||||
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; };
|
||||
|
||||
var _react = require('react');
|
||||
|
||||
var _react2 = _interopRequireDefault(_react);
|
||||
|
||||
var _createChainableTypeChecker = require('./utils/createChainableTypeChecker');
|
||||
|
||||
var _createChainableTypeChecker2 = _interopRequireDefault(_createChainableTypeChecker);
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
function elementType(props, propName, componentName, location, propFullName) {
|
||||
var propValue = props[propName];
|
||||
var propType = typeof propValue === 'undefined' ? 'undefined' : _typeof(propValue);
|
||||
|
||||
if (_react2.default.isValidElement(propValue)) {
|
||||
return new Error('Invalid ' + location + ' `' + propFullName + '` of type ReactElement ' + ('supplied to `' + componentName + '`, expected an element type (a string ') + 'or a ReactClass).');
|
||||
}
|
||||
|
||||
if (propType !== 'function' && propType !== 'string') {
|
||||
return new Error('Invalid ' + location + ' `' + propFullName + '` of value `' + propValue + '` ' + ('supplied to `' + componentName + '`, expected an element type (a string ') + 'or a ReactClass).');
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
exports.default = (0, _createChainableTypeChecker2.default)(elementType);
|
|
@ -0,0 +1,37 @@
|
|||
'use strict';
|
||||
|
||||
exports.__esModule = true;
|
||||
exports.splitComponent = exports.isRequiredForA11y = exports.elementType = exports.deprecated = exports.componentOrElement = exports.all = undefined;
|
||||
|
||||
var _all2 = require('./all');
|
||||
|
||||
var _all3 = _interopRequireDefault(_all2);
|
||||
|
||||
var _componentOrElement2 = require('./componentOrElement');
|
||||
|
||||
var _componentOrElement3 = _interopRequireDefault(_componentOrElement2);
|
||||
|
||||
var _deprecated2 = require('./deprecated');
|
||||
|
||||
var _deprecated3 = _interopRequireDefault(_deprecated2);
|
||||
|
||||
var _elementType2 = require('./elementType');
|
||||
|
||||
var _elementType3 = _interopRequireDefault(_elementType2);
|
||||
|
||||
var _isRequiredForA11y2 = require('./isRequiredForA11y');
|
||||
|
||||
var _isRequiredForA11y3 = _interopRequireDefault(_isRequiredForA11y2);
|
||||
|
||||
var _splitComponent2 = require('./splitComponent');
|
||||
|
||||
var _splitComponent3 = _interopRequireDefault(_splitComponent2);
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
exports.all = _all3.default;
|
||||
exports.componentOrElement = _componentOrElement3.default;
|
||||
exports.deprecated = _deprecated3.default;
|
||||
exports.elementType = _elementType3.default;
|
||||
exports.isRequiredForA11y = _isRequiredForA11y3.default;
|
||||
exports.splitComponent = _splitComponent3.default;
|
|
@ -0,0 +1,20 @@
|
|||
'use strict';
|
||||
|
||||
exports.__esModule = true;
|
||||
exports.default = isRequiredForA11y;
|
||||
function isRequiredForA11y(validator) {
|
||||
return function validate(props, propName, componentName, location, propFullName) {
|
||||
var componentNameSafe = componentName || '<<anonymous>>';
|
||||
var propFullNameSafe = propFullName || propName;
|
||||
|
||||
if (props[propName] == null) {
|
||||
return new Error('The ' + location + ' `' + propFullNameSafe + '` is required to make ' + ('`' + componentNameSafe + '` accessible for users of assistive ') + 'technologies such as screen readers.');
|
||||
}
|
||||
|
||||
for (var _len = arguments.length, args = Array(_len > 5 ? _len - 5 : 0), _key = 5; _key < _len; _key++) {
|
||||
args[_key - 5] = arguments[_key];
|
||||
}
|
||||
|
||||
return validator.apply(undefined, [props, propName, componentName, location, propFullName].concat(args));
|
||||
};
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
"use strict";
|
||||
|
||||
exports.__esModule = true;
|
||||
exports.default = splitComponentProps;
|
||||
/**
|
||||
* 分割要传入父元素和子元素的props
|
||||
* @param {[object]} props 传入的属性
|
||||
* @param {[reactElement]} Component 组件
|
||||
* @return {[array]} 返回数组,第一个元素为父元素props对象,第二个子元素props对象
|
||||
*/
|
||||
function splitComponentProps(props, Component) {
|
||||
var componentPropTypes = Component.propTypes;
|
||||
|
||||
var parentProps = {};
|
||||
var childProps = {};
|
||||
|
||||
Object.entries(props).forEach(function (_ref) {
|
||||
var propName = _ref[0],
|
||||
propValue = _ref[1];
|
||||
|
||||
if (componentPropTypes[propName]) {
|
||||
parentProps[propName] = propValue;
|
||||
} else {
|
||||
childProps[propName] = propValue;
|
||||
}
|
||||
});
|
||||
|
||||
return [parentProps, childProps];
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
'use strict';
|
||||
|
||||
exports.__esModule = true;
|
||||
exports.default = createChainableTypeChecker;
|
||||
/**
|
||||
* Copyright 2013-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
// Mostly taken from ReactPropTypes.
|
||||
|
||||
function createChainableTypeChecker(validate) {
|
||||
function checkType(isRequired, props, propName, componentName, location, propFullName) {
|
||||
var componentNameSafe = componentName || '<<anonymous>>';
|
||||
var propFullNameSafe = propFullName || propName;
|
||||
|
||||
if (props[propName] == null) {
|
||||
if (isRequired) {
|
||||
return new Error('Required ' + location + ' `' + propFullNameSafe + '` was not specified ' + ('in `' + componentNameSafe + '`.'));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
for (var _len = arguments.length, args = Array(_len > 6 ? _len - 6 : 0), _key = 6; _key < _len; _key++) {
|
||||
args[_key - 6] = arguments[_key];
|
||||
}
|
||||
|
||||
return validate.apply(undefined, [props, propName, componentNameSafe, location, propFullNameSafe].concat(args));
|
||||
}
|
||||
|
||||
var chainedCheckType = checkType.bind(null, false);
|
||||
chainedCheckType.isRequired = checkType.bind(null, true);
|
||||
|
||||
return chainedCheckType;
|
||||
}
|
14
package.json
14
package.json
|
@ -1,8 +1,8 @@
|
|||
{
|
||||
"name": "tinper-bee-core",
|
||||
"version": "0.0.6",
|
||||
"version": "0.0.8",
|
||||
"description": "style core and js util for tinper-bee",
|
||||
"main": "js/index.js",
|
||||
"main": "lib/index.js",
|
||||
"engines": {
|
||||
"node": ">=4.0.0"
|
||||
},
|
||||
|
@ -12,10 +12,18 @@
|
|||
"bugs": "https://github.com/tinper-bee/tinper-bee-core/issues",
|
||||
"keywords": "tinper bee react util",
|
||||
"scripts": {
|
||||
"test": "test"
|
||||
"build": "rimraf lib && babel js -d lib"
|
||||
},
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"warning": "^3.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-cli": "^6.18.0",
|
||||
"babel-plugin-istanbul": "^3.0.0",
|
||||
"babel-preset-es2015": "^6.18.0",
|
||||
"babel-preset-es2015-loose": "^8.0.0",
|
||||
"babel-preset-stage-1": "^6.16.0",
|
||||
"babel-register": "^6.18.0"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue