How to use the jsprim.parseInteger function in jsprim

To help you get started, we’ve selected a few jsprim examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github joyent / node-ip6addr / ip6addr.js View on Github external
}
  }

  /* Parse integer values */
  var field, num;
  for (i = 0; i < ip6Fields.length; i++) {
    field = ip6Fields[i];
    num = jsprim.parseInteger(field, { base: 16, allowSign: false });
    if (num instanceof Error || num < 0 || num > 65535) {
      throw new ParseError(input, 'Invalid field value: ' + field);
    }
    ip6Fields[i] = num;
  }
  for (i = 0; i < ip4Fields.length; i++) {
    field = ip4Fields[i];
    num = jsprim.parseInteger(field, { base: 10, allowSign: false });
    if (num instanceof Error || num < 0 || num > 255) {
      throw new ParseError(input, 'Invalid field value: ' + field);
    }
    ip4Fields[i] = num;
  }

  /* Collapse IPv4 portion, if necessary */
  if (ip4Fields.length !== 0) {
    ip6Fields.push((ip4Fields[0]*256) + ip4Fields[1]);
    ip6Fields.push((ip4Fields[2]*256) + ip4Fields[3]);
  }

  /* Expand '::' delimiter into implied 0s */
  if (ip6Fields.length < 8 && expIndex !== null) {
    var filler = [];
    for (i = 0; i < (8 - ip6Fields.length); i++) {
github joyent / node-ip6addr / ip6addr.js View on Github external
ip6Fields = ['ffff'];
      expIndex = 0;
    }

    if (ip6Fields.length > 6) {
      throw new ParseError(input, 'Too many fields');
    } else if (ip6Fields.length < 6 && expIndex === null) {
      throw new ParseError(input, 'Too few fields');
    }
  }

  /* Parse integer values */
  var field, num;
  for (i = 0; i < ip6Fields.length; i++) {
    field = ip6Fields[i];
    num = jsprim.parseInteger(field, { base: 16, allowSign: false });
    if (num instanceof Error || num < 0 || num > 65535) {
      throw new ParseError(input, 'Invalid field value: ' + field);
    }
    ip6Fields[i] = num;
  }
  for (i = 0; i < ip4Fields.length; i++) {
    field = ip4Fields[i];
    num = jsprim.parseInteger(field, { base: 10, allowSign: false });
    if (num instanceof Error || num < 0 || num > 255) {
      throw new ParseError(input, 'Invalid field value: ' + field);
    }
    ip4Fields[i] = num;
  }

  /* Collapse IPv4 portion, if necessary */
  if (ip4Fields.length !== 0) {
github joyent / moray / main.js View on Github external
function parsePort(str) {
    var port = jsprim.parseInteger(str);

    if (port instanceof Error) {
        LOG.fatal({ port: str }, 'Invalid port');
        throw new VError(port, 'Invalid port %j', str);
    }

    if (port < MIN_PORT || port > MAX_PORT) {
        throw new VError('Invalid port %j: should be in range %d-%d',
            port, MIN_PORT, MAX_PORT);
    }

    return port;
}
github joyent / kang / lib / kang.js View on Github external
portstr = matched[2];
		if (!mod_net.isIPv6(host)) {
			throw new Error('bad IPv6 address: ' +
			    JSON.stringify(host));
		}
	} else if ((matched = DOMAIN_HOST.exec(str)) !== null) {
		host = matched[1];
		portstr = matched[2];
	} else {
		throw (new Error('no host specified'));
	}

	if (portstr !== undefined) {
		mod_assert.equal(portstr[0], ':');
		portstr = portstr.slice(1);
		port = mod_jsprim.parseInteger(portstr, { allowSign: false });

		if (port instanceof Error) {
			throw (new VError(port, 'invalid port'));
		}

		if (port < 1 || port > 65535) {
			throw (new Error('invalid port: ' +
			    portstr + ' (must be in range 1-65535)'));
		}
	}

	return ({
		protocol: protocol,
		host: host,
		port: port,
		path: uri
github joyent / moray / lib / objects / common.js View on Github external
* Depending on the type of the "_id" and "_idx" column the PostgreSQL
     * client may return the value as either a number or a string.  Moray
     * clients expect that the property value will always be a number; if
     * needed, convert it now.
     */
    switch (typeof (idval)) {
    case 'number':
        return (idval);

    case 'string':
        /*
         * Parse the ID value as a positive integer.  If the integer is too
         * large to be expressed precisely in the native number type, return an
         * error.
         */
        var idnum = jsprim.parseInteger(idval, { allowSign: false,
          allowImprecise: false });
        if (typeof (idnum) === 'number') {
            return (idnum);
        }
        throw (new VE(idnum, 'invalid "_id" value (bucket "%s"; key "%s")',
          bucket.name, key));

    default:
        throw (new VE('invalid "_id" type (bucket "%s"; key "%s"): %j',
          bucket.name, key, idval));
    }
}
github joyent / eng / lib / index.js View on Github external
function
answer(correct)
{
	mod_assert.bool(correct, 'correct');

	var out = mod_jsprim.parseInteger(correct ? THE_ANSWER :
	    THE_WRONG_ANSWER);

	if (out instanceof Error) {
		return (new mod_verror.VError(out, 'wrong answer'));
	}

	return (out);
}