-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
282 additions
and
345 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,56 @@ | ||
// // import assert from '../../utils'; // TODO | ||
|
||
const TYPE_DEFINITIONS = { | ||
boolean: { | ||
validate(value, propType) { | ||
return true; | ||
} | ||
}, | ||
number: { | ||
validateType(value, propType) { | ||
return ( | ||
'value' in propType && | ||
(!('max' in propType) || Number.isFinite(propType.max)) && | ||
(!('min' in propType) || Number.isFinite(propType.min)) | ||
); | ||
}, | ||
validate(value, propType) { | ||
return ( | ||
Number.isFinite(value) && | ||
(!('max' in propType) || value <= propType.max) && | ||
(!('min' in propType) || value >= propType.min) | ||
); | ||
} | ||
}, | ||
array: { | ||
validate(value, propType) { | ||
return Array.isArray(value) || ArrayBuffer.isView(value); | ||
} | ||
} | ||
}; | ||
|
||
export function parsePropTypes(propDefs) { | ||
const propTypes = {}; | ||
const defaultProps = {}; | ||
for (const [propName, propDef] of Object.entries(propDefs)) { | ||
const propType = parsePropType(propName, propDef); | ||
for (const propName in propDefs) { | ||
const propDef = propDefs[propName]; | ||
const propType = parsePropType(propDef); | ||
propTypes[propName] = propType; | ||
defaultProps[propName] = propType.value; | ||
} | ||
return {propTypes, defaultProps}; | ||
return propTypes; | ||
} | ||
|
||
// Parses one property definition entry. Either contains: | ||
// * a valid prop type object ({type, ...}) | ||
// * or just a default value, in which case type and name inference is used | ||
function parsePropType(name, propDef) { | ||
switch (getTypeOf(propDef)) { | ||
case 'object': | ||
propDef = normalizePropType(name, propDef); | ||
return parsePropDefinition(propDef); | ||
|
||
case 'array': | ||
return guessArrayType(name, propDef); | ||
|
||
case 'boolean': | ||
return {name, type: 'boolean', value: propDef}; | ||
|
||
case 'number': | ||
return guessNumberType(name, propDef); | ||
|
||
case 'function': | ||
return {name, type: 'function', value: propDef}; | ||
// return guessFunctionType(name, propDef); | ||
|
||
default: | ||
return {name, type: 'unknown', value: propDef}; | ||
} | ||
} | ||
|
||
function guessArrayType(name, array) { | ||
if (/color/i.test(name) && (array.length === 3 || array.length === 4)) { | ||
return {name, type: 'color', value: array}; | ||
} | ||
return {name, type: 'array', value: array}; | ||
} | ||
|
||
function normalizePropType(name, propDef) { | ||
if (!('type' in propDef)) { | ||
function parsePropType(propDef) { | ||
let type = getTypeOf(propDef); | ||
if (type === 'object') { | ||
if (!propDef) { | ||
return {type: 'object', value: null}; | ||
} | ||
if ('type' in propDef) { | ||
return Object.assign({}, propDef, TYPE_DEFINITIONS[propDef.type]); | ||
} | ||
if (!('value' in propDef)) { | ||
// If no type and value this object is likely the value | ||
return {name, type: 'object', value: propDef}; | ||
return {type: 'object', value: propDef}; | ||
} | ||
return Object.assign({name, type: getTypeOf(propDef.value)}, propDef); | ||
type = getTypeOf(propDef.value); | ||
return Object.assign({type}, propDef, TYPE_DEFINITIONS[type]); | ||
} | ||
return Object.assign({name}, propDef); | ||
} | ||
|
||
function parsePropDefinition(propDef) { | ||
const {type} = propDef; | ||
const typeDefinition = TYPE_DEFINITIONS[type] || {}; | ||
const {typeValidator} = typeDefinition; | ||
if (typeValidator) { | ||
// assert(typeValidator(propDef), 'Illegal prop type'); | ||
} | ||
|
||
return propDef; | ||
} | ||
|
||
function guessNumberType(name, value) { | ||
const isKnownProp = | ||
/radius|scale|width|height|pixel|size|miter/i.test(name) && /^((?!scale).)*$/.test(name); | ||
const max = isKnownProp ? 100 : 1; | ||
const min = 0; | ||
return { | ||
name, | ||
type: 'number', | ||
max: Math.max(value, max), | ||
min: Math.min(value, min), | ||
value | ||
}; | ||
return Object.assign({type, value: propDef}, TYPE_DEFINITIONS[type]); | ||
} | ||
|
||
// improved version of javascript typeof that can distinguish arrays and null values | ||
function getTypeOf(value) { | ||
if (Array.isArray(value) || ArrayBuffer.isView(value)) { | ||
return 'array'; | ||
} | ||
if (value === null) { | ||
return 'null'; | ||
} | ||
return typeof value; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.