UNPKG

@eluvio/elv-js-helpers

Version:

A collection of Javascript helper functions used by several Eluvio libraries.

40 lines (37 loc) 1.23 kB
'use strict' const defBoundedNumModel = require('../ModelFactory/defBoundedNumModel') /** * An [ObjectModel](http://objectmodel.js.org/) which validates that an input is: * * * A [Javascript Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) * * Greater than zero * * If input passes validations, will return the input * * Throws an exception if passed in an invalid value. * * @class * @category Model * @sig * -> * | THROW * @param {*} - The input to validate * @returns {Number} The validated input * @example * * 'use strict' * const NonNegativeNumModel = require('@eluvio/elv-js-helpers/Model/NonNegativeNumModel') * * NonNegativeNumModel(42) //=> 42 * * NonNegativeNumModel(0) //=> 0 * * NonNegativeNumModel(-1) //=> EXCEPTION: 'Value must be >= 0 (got: -1)' * * NonNegativeNumModel('foo') //=> EXCEPTION: 'expecting Number, got String "foo"' * * NonNegativeNumModel(Infinity) //=> Infinity * * NonNegativeNumModel(-Infinity) //=> EXCEPTION: 'Value must be >= 0 (got: -Infinity)' * */ const NonNegativeNumModel = defBoundedNumModel('NonNegativeNumber', 0, null, true, null) module.exports = NonNegativeNumModel