How to use the optionalNumber function from assert-plus

Find comprehensive JavaScript assert-plus.optionalNumber code examples handpicked from public code repositorys.

assert-plus.optionalNumber is a function provided by the assert-plus library that validates whether an input parameter is a number or undefined.

3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
MantaClient.prototype.createUpload = function createUpload(p, opts, cb) {
    assert.string(p, 'path');
    assert.object(opts, 'options');
    assert.string(opts.account, 'opts.account');
    assert.optionalObject(opts.headers, 'opts.headers');
    assert.optionalNumber(opts.copies, 'opts.copies');
    assert.optionalNumber(opts.size, 'opts.size');
    assert.optionalString(opts.md5, 'opts.md5');
    assert.func(cb, 'callback');

fork icon41
star icon75
watch icon0

+ 3 other calls in file

3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
 * @param {Function} callback - called like `function (err, volume, res)`
 */
CloudApi.prototype.createVolume = function createVolume(options, cb) {
    assert.object(options, 'options');
    assert.optionalString(options.name, 'options.name');
    assert.optionalNumber(options.size, 'options.size');
    assert.optionalArrayOfUuid(options.networks, 'options.networks');
    assert.string(options.type, 'options.type');
    assert.func(cb, 'cb');

fork icon38
star icon55
watch icon0

+ 132 other calls in file

How does assert-plus.optionalNumber work?

assert-plus.optionalNumber is a function provided by the assert-plus library that validates whether an input parameter is a number or undefined. When you call assert-plus.optionalNumber, you provide the name of the parameter and the value of the parameter to be validated. The function checks whether the input value is either a number or undefined. If the value is not a number or undefined, the function throws an error with a message indicating that the parameter is invalid. By checking for undefined, assert-plus.optionalNumber allows for optional parameters to be validated without throwing errors if they are not present. Overall, assert-plus.optionalNumber provides a simple way to validate whether a parameter is a number or undefined in JavaScript, helping to ensure that functions and methods receive the expected input types.

63
64
65
66
67
68
69
70
71
72

assert.optionalObject(options, 'options.retry');
var r = options || {};
assert.optionalNumber(r.minTimeout, 'options.retry.minTimeout');
assert.optionalNumber(r.maxTimeout, 'options.retry.maxTimeout');
assert.optionalNumber(r.retries, 'options.retry.retries');
assert.optionalObject(defaults, 'defaults');
var normalizedDefaults = defaults || {};

return ({
fork icon34
star icon55
watch icon0

+ 7 other calls in file

89
90
91
92
93
94
95
96
97
assert.object(client, 'client');
assert.uuid(volumeUuid, 'volumeUuid');
assert.object(options, 'options');
assert.arrayOfString(options.successStates, 'options.successStates');
assert.arrayOfString(options.failureStates, 'options.failureStates');
assert.optionalNumber(options.timeout, 'options.timeout');
assert.optionalBool(options.successOnVolumeNotFound,
        'options.successOnVolumeNotFound');
assert.func(callback, 'callback');
fork icon13
star icon2
watch icon0

+ 5 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
const assert = require("assert-plus");

function calculateAge(birthYear, currentYear) {
  assert.optionalNumber("birthYear", birthYear);
  assert.number("currentYear", currentYear);

  return currentYear - birthYear;
}

console.log(calculateAge(1980, 2022)); // Output: 42
console.log(calculateAge(undefined, 2022)); // Output: NaN

In this example, assert-plus.optionalNumber is used to validate an optional birthYear parameter in the calculateAge() function. The function first calls assert.optionalNumber to check that the birthYear parameter is either a number or undefined. It then calls assert.number to check that the currentYear parameter is a number. If the birthYear parameter is not a number or undefined, the assert.optionalNumber function throws an error with a message indicating that the parameter is invalid. If the currentYear parameter is not a number, the assert.number function throws an error. The calculateAge() function then calculates the difference between the currentYear and birthYear parameters, returning the result. Note that when the birthYear parameter is undefined, the calculateAge() function returns NaN because the subtraction of undefined from currentYear results in NaN. However, since assert.optionalNumber allows for undefined values, no error is thrown by the function.

1231
1232
1233
1234
1235
1236
1237
1238
1239
assert.string(arg.vmobj.state, 'arg.vmobj.state');
if (!arg.vmobj.hasOwnProperty('zone_state')) {
    arg.vmobj.zone_state = arg.vmobj.state;
}

assert.optionalNumber(arg.vmobj.pid, 'arg.vmobj.pid');
if (arg.vmobj.pid !== undefined) {
    arg.vmobj.zoneid = arg.vmobj.pid;
}
fork icon3
star icon3
watch icon0

+ 3 other calls in file

5
6
7
8
9
10
11
12
13
14
15
16
// local globals
let log;
const exportedPojo = {};


function init(level) {
    assert.optionalNumber(level, 'level');


    log = lj.create({
        name: 'git-tag-changelog',
        level: level || 30
fork icon0
star icon2
watch icon0

1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
assert.func(opts.logCb, 'opts.logCb');
// As from `source.getImportInfo`.
assert.object(opts.importInfo, 'opts.importInfo');
// As from `source.getImgMeta`.
assert.object(opts.imgMeta, 'opts.imgMeta');
assert.optionalNumber(opts.imgMeta.size, 'opts.imgMeta.size');
assert.optionalString(opts.imgMeta.checksum, 'opts.imgMeta.checksum');

var log = self.log;
var uuid = opts.importInfo.uuid;
fork icon248
star icon0
watch icon0

+ 5 other calls in file

1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
}
assert.optionalString(opts.token, 'opts.token');  // for Bearer auth
assert.optionalBool(opts.insecure, 'opts.insecure');
assert.optionalString(opts.scheme, 'opts.scheme');
assert.optionalBool(opts.acceptManifestLists, 'opts.acceptManifestLists');
assert.optionalNumber(opts.maxSchemaVersion, 'opts.maxSchemaVersion');
// TODO: options to control the trust db for CA verification
// TODO add passing through other restify options: userAgent, ...
// Restify/Node HTTP client options.
assert.optionalBool(opts.agent, 'opts.agent');
fork icon35
star icon59
watch icon0

+ 2 other calls in file

356
357
358
359
360
361
362
363
364
    bytes = opts;
    opts = {};
}
assert.number(bytes, 'bytes');
// The number of decimal places, default 1.
assert.optionalNumber(opts.precision, 'opts.precision');
var precision = opts.precision === undefined ? 1 : opts.precision;
assert.ok(precision >= 0);
assert.optionalBool(opts.narrow, 'opts.narrow');
fork icon38
star icon55
watch icon41

+ 3 other calls in file

1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
assert.object(bucketConfig, 'bucketConfig');
assert.object(bucketConfig.schema, 'bucketConfig.schema');
assert.optionalObject(bucketConfig.schema.options,
    'bucketConfig.schema.options');
if (bucketConfig.schema.options) {
    assert.optionalNumber(bucketConfig.schema.options.version,
        'bucketConfig.schema.options.version');
}

assert.func(cb, 'cb');
fork icon20
star icon11
watch icon0

1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
var now = Date.now();

assertplus.object(args, 'args');
assertplus.string(args.id, 'args.id');
assertplus.object(args.stream, 'args.stream');
assertplus.optionalNumber(args.nmaxfaults, 'args.nmaxfaults');
assertplus.notStrictEqual(
    this.ma_amon_deployed,
    null,
    'must call alarmsInit() with "configBasic" source first'
fork icon13
star icon7
watch icon42

1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
assert.string(source, 'source');
assert.object(options, 'options');
assert.optionalBool(options.skipOwnerCheck, 'options.skipOwnerCheck');
assert.optionalObject(options.headers, 'options.headers');
assert.optionalString(options.channel, 'options.channel');
assert.optionalNumber(options.retries, 'options.retries');
assert.func(callback, 'callback');

var path = self._path('/images/' + uuid, {
    channel: options.channel || self.channel,
fork icon13
star icon2
watch icon36

+ 4 other calls in file

109
110
111
112
113
114
115
116
117
118
 * @returns {Promise} a promise that upon success will be resolved with the index of the byte following the last byte written (convenient for successive writes)
 */
write(offset, data, first, length) {
  assert.number(offset, 'offset');
  assert.ok(Buffer.isBuffer(data), 'data (Buffer) is required');
  assert.optionalNumber(first, 'first');
  assert.optionalNumber(length, 'length');
  if (!this[$fd]) return Promise.reject(new Error(`Invalid state: file closed: ${this.name}`));
  if (!this.writable) return Promise.reject(new Error(`Invalid state; file opened read only: ${this.name}.`));
  first = (first !== undefined) ? first : 0;
fork icon2
star icon0
watch icon0

29
30
31
32
33
34
35
36
37
38
assert.object(opts, 'opts');
assert.optionalBool(opts.overrideParams, 'opts.overrideParams');
assert.optionalBool(opts.multiples, 'opts.multiples');
assert.optionalBool(opts.keepExtensions, 'opts.keepExtensions');
assert.optionalString(opts.uploadDir, 'opts.uploadDir');
assert.optionalNumber(opts.maxFieldsSize, 'opts.maxFieldsSize');
assert.optionalString(opts.hash, 'opts.hash');
assert.optionalFunc(opts.multipartFileHandler, 'opts.multipartFileHandler');
assert.optionalFunc(opts.multipartHandler, 'opts.multipartHandler');
assert.optionalBool(opts.mapParams, 'opts.mapParams');
fork icon0
star icon2
watch icon0

582
583
584
585
586
587
588
589
590
591
assert.optionalString(config.nameSort, 'config.nameSort');
var nameSort = config.nameSort || 'length';
assert.ok(~['length', 'none'].indexOf(nameSort),
    'invalid "config.nameSort"');
assert.optionalNumber(config.maxCol, 'config.maxCol');
assert.optionalNumber(config.maxHelpCol, 'config.maxHelpCol');
assert.optionalNumber(config.minHelpCol, 'config.minHelpCol');
assert.optionalNumber(config.helpCol, 'config.helpCol');
assert.optionalBool(config.includeEnv, 'config.includeEnv');
assert.optionalBool(config.includeDefault, 'config.includeDefault');
fork icon0
star icon0
watch icon1

+ 39 other calls in file

49
50
51
52
53
54
55
56
57
58
assert.object(morayClient, 'morayClient');
assert.string(bucketName, 'bucketName');
assert.string(filter, 'filter');
assert.object(options, 'options');
assert.object(expectedResults, 'expectedResults');
assert.optionalNumber(expectedResults.nbObjectsExpected,
    'expectedResults.nbObjectsExpected');
assert.func(callback, 'callback');

console.log('searching objects with filter [%s] and options [%j]',
fork icon0
star icon0
watch icon0