add comments

This commit is contained in:
Nick Sweeting 2021-05-19 01:52:37 -04:00 committed by GitHub
parent 77ea05a233
commit 0ffc960523
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,16 +5,19 @@ const { randomBytes } = require('crypto');
convict.addFormat({ convict.addFormat({
name: 'positive-int-array', name: 'positive-int-array',
validate: (ints, schema) => { coerce: (ints, schema) => { // can take: int[] | string[] | string (csv), returns -> int[]
const ints_arr = Array.isArray(ints) ? ints : ints.trim().split(',')
return ints_arr.map(int =>
(typeof int === 'number')
? int
: parseInt(int.trim(), 10))
},
validate: (ints, schema) => { // takes: int[], errors if any NaNs, negatives, or floats present
for (const int of ints) { for (const int of ints) {
if (isNaN(int) || int < 0) if (typeof(int) !== 'number' || isNaN(int) || int < 0 || int % 1 > 0)
throw new Error('must be a comma-separated list of positive integers') throw new Error('must be a comma-separated list of positive integers')
} }
}, },
coerce: (ints, schema) => {
const ints_arr = Array.isArray(ints_str) ? ints : ints.trim().split(',')
return ints_arr.map(int => (typeof int === 'number') ? int : parseInt(int.trim(), 10))
},
}); });
const conf = convict({ const conf = convict({