expectation v0.3.16
expectation
Define rules by which a single parameter can be validated and revised to contain the desired value. Has built-in checks to remove common undesirable values, check value type, etc and hooks where additional methods can easily be added to further clean up values.
options
Options is an object which defines properties that will define how an expectation will behave. These properties may also be set on Expectation to define your own default values.
nameDefines what it is known by. Useful when assigning the resulting values to objects, maps, etc.
Default: 'Expectation'
acceptEmptyShould empty collections (strings, arrays, maps, sets, buffers) be allowed. In addition to these well known collection types any iterator object with a length or size property will be considered a colection.
Default: undefined
acceptErrorsShould error values be allowed. If undefined and either the defaultValue or requiredType is an error then will be true.
Default: undefined
acceptFunctionsShould function values be allowed. If undefined and either the defaultValue or requiredType is a function then will be true.
Default: undefined
acceptMultiplesShould arrays of values be allowed.
Default: undefined
acceptNaNShould NaN be allowed.
Default: undefined
acceptNullShould null be allowed.
Default: undefined
acceptSymbolsShould symbol values be allowed. If undefined and either the defaultValue or requiredType is a symbol then will be true.
Default: undefined
defaultValueValue that will be returned if given value fails to meet any of the criteria.
Default: undefined
preferMultiplesShould the return value be an array whenever possible? Only considered if acceptMultiples is true.
If true, will push any indidivual values into an array. If acceptEmpty is true will return an empty array instead of the default value.
If false, empty arrays will instead return the default value and arrays with a single member will return just that indidivual.
If undefined, doesn't do anything.
Default: undefined
requiredIf true and given value fails to meet any of the criteria then will throw an error instead of returning a default value.
Default: undefined
requiredTypeA class/prototype function or a string to compare the values type against.
If a function then will check if value is an instanceof that function.
If a string then will check if typeof value equals string.
If typeof doesn't match then will attemp to convert the value using a matching implicit conversion function.
If undefined then any value is fine.
Default: undefined
revisorA function ( value, expectation ) => value that gets a copy of the working value after the build-in methods have had their turn evaluating and revising it but before the default value is applied. Whatever value returned from this function will then be checked and the default value applied if needed or error thrown.
Default: undefined
implicitConvertersAn object with implicit conversion functions ( value ) => value as members. If an implicit conversion operation is being done then the string name of the required type will be used to lookup a matching function. If that function exists then it will be passed the value and whatever value it returns will be the new working value.
If a matching function isn't found on the instance then it will also be looked for in Expectation.implicitConverters.
Default: undefined
static members
Expectation.create(options) => expectationCreate a new expectation instance with the given options object to set the criteria. Returns the new expectation.
Alternatively, options can be either: a two element array with default name and value, or a string to use as the default name.
Expectation.createList([options1,options2,..optionsN]) => [expectation1,expectation2,..expectationN]Create an array of expectations from option objects passed as criteria.
instance members
nameThe expectation's name. Where appropiate this value should be used in naming revised values.
revise(value) => valueValidates and revises a value against an expectation's criteria. Returns the revised value.
initialize() => valueProduces an initial value based on the expectation's criteria. Ignores the required rule that otherwise might throw an error.
included implicit convertors
stringIf it's null, NaN, or undefined return undefined. Otherwise return a string.
uppercaseSame as string but returns in locale uppercase.
lowercaseSame as string but returns in locale lowercase.
jsonIf it's undefined return undefined. Otherwise return as JSON.
dateIf it's a Date or can be parsed into a Date from a string or number then return as a Date. Otherwise return undefined.
datestringSame as date but returns in locale date format.
timestringSame as date but returns in locale time format.
isodateSame as date but returns in JavaScript ISO format.
utcdateSame as date but returns in JavaScript UTC format.
iso8601Same as date but returns in iso8601 format.
numberIf it's a number or can be parsed into a number return as a number. Otherwise return NaN.
integerIf it's an integer or can be parsed into an integer return as an integer. Otherwise return NaN.
booleanIf it's a boolean or can be parsed into a boolean return as a boolean. Otherwise return undefined.
byteTry to parse into a byte value. If it's a number 0-255 then return that value as an integer. If it's a single char string and the char code is 0-255 then return that value. Otherwise return undefined.
finiteIf it parses to a finite number then return that value. Otherwise return undefined.
urlIf it's a Url object or can be parsed to a Url object then return a Url. Otherwise return undefined.
emailIf it's a emailaddress object or can be parsed into a emailaddress object then will return a string representation of the email addresses. If optional emailaddress dependency isn't installed then just treats it as string.
epochsIf it's a number it is treated as epochs. If it is a date or can be parsed as a date then will be output as epochs.
Requires Node 4 with harmony features or Traceur.
License
ISC
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago