diff --git a/lib/node_modules/@stdlib/number/uint64/parse/README.md b/lib/node_modules/@stdlib/number/uint64/parse/README.md new file mode 100644 index 000000000000..44e7586b1cf9 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/parse/README.md @@ -0,0 +1,126 @@ + + +# parseUint64 + +> Parse a string representation of a 64-bit unsigned integer. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var parseUint64 = require( '@stdlib/number/uint64/parse' ); +``` + +#### parseUint64( str\[, radix\] ) + +Parses a string representation of a 64-bit unsigned integer. + +```javascript +var a = parseUint64( '1234' ); +// returns [ 1234n ] + +a = parseUint64( '0xffffffffffffffff' ); +// returns [ 18446744073709551615n ] + +a = parseUint64( '123abcxyz', 36 ); +// returns [ 2984992324091n ] +``` + +
+ + + + + +
+ +## Notes + +- The input string must be a valid representation of an 64-bit unsigned integer. +- If the provided string is malformed (e.g., contains invalid characters or is incomplete), the function throws a `SyntaxError`. +- If the provided string represents a value greater than `2^64-1` or the radix is not an integer on the interval `[2, 36]`, the function throws a `RangeError`. + +
+ + + + + +
+ +## Examples + + + +```javascript +var Uint64 = require( '@stdlib/number/uint64/ctor' ); +var parseUint64 = require( '@stdlib/number/uint64/parse' ); + +var a = parseUint64( '9876543210' ); +console.log( 'value: %s', a ); +// => 'value: 9876543210' + +a = parseUint64( '0xdeadbeef0badf00d' ); +console.log( 'value: %s', a ); +// => 'value: 16045690981293355021' + +a = parseUint64( 'ilovestdlib', 36 ); +console.log( 'value: %s', a ); +// => 'value: 68013779155385123' + +console.log( a instanceof Uint64 ); +// => true +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/number/uint64/parse/benchmark/benchmark.js b/lib/node_modules/@stdlib/number/uint64/parse/benchmark/benchmark.js new file mode 100644 index 000000000000..50ff1c0189c9 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/parse/benchmark/benchmark.js @@ -0,0 +1,108 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var UINT32_MAX = require( '@stdlib/constants/uint32/max' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var Uint64 = require( '@stdlib/number/uint64/ctor' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var parseUint64 = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'uint32' +}; + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var N; + var a; + var i; + var x; + + N = 100; + x = discreteUniform( N, 0, UINT32_MAX, options ); + values = []; + for ( i = 0; i < N; i++ ) { + a = Uint64.of( x[ i ], x[ (i+1)%N ] ); + values.push( a.toString( 10 ) ); + values.push( '0b'+a.toString( 2 ) ); + values.push( '0o'+a.toString( 8 ) ); + values.push( '0x'+a.toString( 16 ) ); + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + a = parseUint64( values[ i % N ] ); + if ( typeof a !== 'object' ) { + b.fail( 'should return a Uint64 instance' ); + } + } + b.toc(); + if ( !( a instanceof Uint64 ) ) { + b.fail( 'should return a Uint64 instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::radix', pkg ), function benchmark( b ) { + var radices; + var values; + var rad; + var N; + var a; + var i; + var x; + + N = 100; + x = discreteUniform( N, 0, UINT32_MAX, options ); + values = []; + radices = []; + for ( i = 0; i < N; i++ ) { + a = Uint64.of( x[ i ], x[ (i+1)%N ] ); + for ( rad = 2; rad <= 36; rad++ ) { + values.push( a.toString( rad ) ); + radices.push( rad ); + } + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + a = parseUint64( values[ i % N ], radices[ i % N ] ); + if ( typeof a !== 'object' ) { + b.fail( 'should return a Uint64 instance' ); + } + } + b.toc(); + if ( !( a instanceof Uint64 ) ) { + b.fail( 'should return a Uint64 instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/number/uint64/parse/docs/repl.txt b/lib/node_modules/@stdlib/number/uint64/parse/docs/repl.txt new file mode 100644 index 000000000000..00343068f348 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/parse/docs/repl.txt @@ -0,0 +1,29 @@ + +{{alias}}( str[, radix] ) + Parses a string representation of a 64-bit unsigned integer. + + Parameters + ---------- + str: string + String representation of a 64-bit unsigned integer. + + radix: integer (optional) + Radix (base) to use for string conversion (2-36). Default: 10. + + Returns + ------- + v: Uint64 + 64-bit unsigned integer. + + Examples + -------- + > var a = {{alias}}( '1234' ) + [ 1234n ] + > a = {{alias}}( '0xffffffffffffffff' ) + [ 18446744073709551615n ] + > a = {{alias}}( '123abcxyz', 36 ) + [ 2984992324091n ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/number/uint64/parse/docs/types/index.d.ts b/lib/node_modules/@stdlib/number/uint64/parse/docs/types/index.d.ts new file mode 100644 index 000000000000..accbd20fe16b --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/parse/docs/types/index.d.ts @@ -0,0 +1,45 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +import Uint64 = require( '@stdlib/number/uint64/ctor' ); + +/** +* Parses a string representation of a 64-bit unsigned integer. +* +* @param str - string representation of a 64-bit unsigned integer +* @param radix - radix (base) to use for string conversion (2-36) +* @returns 64-bit unsigned integer +* +* @example +* var a = parseUint64( '1234' ); +* // returns [ 1234n ] +* +* a = parseUint64( '0xffffffffffffffff' ); +* // returns [ 18446744073709551615n ] +* +* a = parseUint64( '123abcxyz', 36 ); +* // returns [ 2984992324091n ] +*/ +declare function parseUint64( str: string, radix?: number ): Uint64; + + +// EXPORTS // + +export = parseUint64; diff --git a/lib/node_modules/@stdlib/number/uint64/parse/docs/types/test.ts b/lib/node_modules/@stdlib/number/uint64/parse/docs/types/test.ts new file mode 100644 index 000000000000..feeefdcb344c --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/parse/docs/types/test.ts @@ -0,0 +1,62 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import parseUint64 = require( './index' ); + + +// TESTS // + +// The function returns a 64-bit unsigned integer... +{ + parseUint64( '1234' ); // $ExpectType Uint64 + parseUint64( '0xabcd' ); // $ExpectType Uint64 + parseUint64( 'wxyz', 36 ); // $ExpectType Uint64 +} + +// The compiler throws an error if the function is provided a first argument which is not a string... +{ + parseUint64( true ); // $ExpectError + parseUint64( false ); // $ExpectError + parseUint64( null ); // $ExpectError + parseUint64( 123 ); // $ExpectError + parseUint64( {} ); // $ExpectError + parseUint64( ( x: number ): number => x ); // $ExpectError + + parseUint64( 5, 36 ); // $ExpectError + parseUint64( true, 36 ); // $ExpectError + parseUint64( false, 36 ); // $ExpectError + parseUint64( null, 36 ); // $ExpectError + parseUint64( {}, 36 ); // $ExpectError + parseUint64( ( x: number ): number => x, 36 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a number... +{ + parseUint64( '1234', '5' ); // $ExpectError + parseUint64( '1234', true ); // $ExpectError + parseUint64( '1234', false ); // $ExpectError + parseUint64( '1234', null ); // $ExpectError + parseUint64( '1234', {} ); // $ExpectError + parseUint64( '1234', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + parseUint64(); // $ExpectError + parseUint64( '1234', 10, 1 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/number/uint64/parse/examples/index.js b/lib/node_modules/@stdlib/number/uint64/parse/examples/index.js new file mode 100644 index 000000000000..d6688b660eea --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/parse/examples/index.js @@ -0,0 +1,37 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Uint64 = require( '@stdlib/number/uint64/ctor' ); +var parseUint64 = require( './../lib' ); + +var a = parseUint64( '9876543210' ); +console.log( 'value: %s', a ); +// => 'value: 9876543210' + +a = parseUint64( '0xdeadbeef0badf00d' ); +console.log( 'value: %s', a ); +// => 'value: 16045690981293355021' + +a = parseUint64( 'ilovestdlib', 36 ); +console.log( 'value: %s', a ); +// => 'value: 68013779155385123' + +console.log( a instanceof Uint64 ); +// => true diff --git a/lib/node_modules/@stdlib/number/uint64/parse/lib/index.js b/lib/node_modules/@stdlib/number/uint64/parse/lib/index.js new file mode 100644 index 000000000000..fba6afc1a26c --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/parse/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Parse a string representation of a 64-bit unsigned integer. +* +* @module @stdlib/number/uint64/parse +* +* @example +* var parseUint64 = require( '@stdlib/number/uint64/parse' ); +* +* var a = parseUint64( '1234' ); +* // returns [ 1234n ] +* +* a = parseUint64( '0xffffffffffffffff' ); +* // returns [ 18446744073709551615n ] +* +* a = parseUint64( '123abcxyz', 36 ); +* // returns [ 2984992324091n ] +*/ + +// MAIN // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/number/uint64/parse/lib/main.js b/lib/node_modules/@stdlib/number/uint64/parse/lib/main.js new file mode 100644 index 000000000000..24dc7a0845c3 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/parse/lib/main.js @@ -0,0 +1,169 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var Uint32Array = require( '@stdlib/array/uint32' ); +var isBetween = require( '@stdlib/assert/is-between' ); +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var isEmptyString = require( '@stdlib/assert/is-empty-string' ).isPrimitive; +var string2words = require( '@stdlib/number/uint64/base/string2words' ).assign; +var Uint64 = require( '@stdlib/number/uint64/ctor' ); +var ltrim = require( '@stdlib/string/base/left-trim' ); +var lowercase = require( '@stdlib/string/base/lowercase' ); +var slice = require( '@stdlib/string/base/slice' ); +var trim = require( '@stdlib/string/base/trim' ); +var format = require( '@stdlib/string/format' ); + + +// VARIABLES // + +var RE_HEX = /^0x/; +var RE_BIN = /^0b/; +var RE_OCT = /^0o/; +var DIGITS = '0123456789abcdefghijklmnopqrstuvwxyz'; +var WORKSPACE = new Uint32Array( 2 ); + + +// FUNCTIONS // + +/** +* Returns a regular expression that matches any character that is not a valid digit in the number system of the given radix. +* +* @private +* @param {PositiveInteger} radix - radix (base) of the number system (2-36) +* @returns {RegExp} regular expression +* +* @example +* var v = regexp( 10 ); +* // returns /[^0123456789]/ +* +* v = regexp( 16 ); +* // returns /[^0123456789abcdef]/ +* +*/ +function regexp( radix ) { + var validDigits = slice( DIGITS, 0, radix ); + return new RegExp( format( '[^%s]', validDigits ) ); +} + + +// MAIN // + +/** +* Parses a string representation of a 64-bit unsigned integer. +* +* @param {string} str - string representation of a 64-bit unsigned integer +* @param {PositiveInteger} radix - radix (base) to use for string conversion (2-36) +* @throws {TypeError} first argument must be a string +* @throws {SyntaxError} first argument must be a string encoding a nonnegative integer +* @throws {RangeError} first argument must be a string encoding a nonnegative integer on the interval [0, 2^64-1] +* @throws {TypeError} second argument must be an integer +* @throws {RangeError} second argument must be an integer on the interval [2, 36] +* @returns {Uint64} 64-bit unsigned integer +* +* @example +* var a = parseUint64( '1234' ); +* // returns [ 1234n ] +* +* a = parseUint64( '0xffffffffffffffff' ); +* // returns [ 18446744073709551615n ] +* +* a = parseUint64( '123abcxyz', 36 ); +* // returns [ 2984992324091n ] +*/ +function parseUint64( str, radix ) { + var rad; + var s; + var i; + + if ( !isString( str ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) ); + } + + s = trim( str ); + if ( s[ 0 ] === '-' ) { + throw new SyntaxError( format( 'invalid argument. First argument must be a string encoding a nonnegative integer. Value: `%s`.', str ) ); + } + + // Trim optional plus sign and following whitespace: + if ( s[ 0 ] === '+' ) { + s = ltrim( slice( s, 1, s.length ) ); + } + + // Convert to lowercase: + s = lowercase( s ); + + if ( arguments.length < 2 ) { + if ( RE_BIN.test( s ) ) { + rad = 2; + } else if ( RE_OCT.test( s ) ) { + rad = 8; + } else if ( RE_HEX.test( s ) ) { + rad = 16; + } else { + rad = 10; + } + + if ( rad !== 10 ) { + s = slice( s, 2 ); // remove the radix prefix + } + } else if ( !isInteger( radix ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', radix ) ); + } else if ( isBetween( radix, 2, 36 ) ) { + rad = radix; + } else { + throw new RangeError( format( 'invalid argument. Second argument must be an integer on the interval [2, 36]. Value: `%s`.', radix ) ); + } + + // Handles cases such as '', ' ', '0x': + if ( isEmptyString( s ) ) { + throw new SyntaxError( format( 'invalid argument. First argument must be a string encoding a nonnegative integer. Value: `%s`.', str ) ); + } + + // Trim leading zeros: + i = 0; + while ( ( i < s.length-1 ) && ( s[ i ] === '0' ) ) { + i += 1; + } + s = slice( s, i, s.length ); + + // Validate digits with respect to the radix: + if ( regexp( rad ).test( s ) ) { + throw new SyntaxError( format( 'invalid argument. First argument must be a string encoding a nonnegative integer. Value: `%s`.', str ) ); + } + + try { + string2words( s, rad, WORKSPACE, 1, 0 ); + } catch ( err ) { + if ( err instanceof RangeError ) { + throw new RangeError( format( 'invalid argument. First argument must be a string encoding a nonnegative integer on the interval [0, 2^64-1]. Value: `%s`.', str ) ); + } + throw err; + } + + return Uint64.of( WORKSPACE[ 0 ], WORKSPACE[ 1 ] ); +} + + +// EXPORTS // + +module.exports = parseUint64; diff --git a/lib/node_modules/@stdlib/number/uint64/parse/package.json b/lib/node_modules/@stdlib/number/uint64/parse/package.json new file mode 100644 index 000000000000..19cdc99fc9b4 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/parse/package.json @@ -0,0 +1,64 @@ +{ + "name": "@stdlib/number/uint64/parse", + "version": "0.0.0", + "description": "Parse a string representation of a 64-bit unsigned integer.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "types", + "data", + "structure", + "uint64", + "unsigned", + "64-bit", + "integer", + "int", + "parse" + ] +} diff --git a/lib/node_modules/@stdlib/number/uint64/parse/test/test.js b/lib/node_modules/@stdlib/number/uint64/parse/test/test.js new file mode 100644 index 000000000000..cb54ceea88d9 --- /dev/null +++ b/lib/node_modules/@stdlib/number/uint64/parse/test/test.js @@ -0,0 +1,343 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasBigIntSupport = require( '@stdlib/assert/has-bigint-support' ); +var BigInt = require( '@stdlib/bigint/ctor' ); +var isEqual = require( '@stdlib/number/uint64/base/assert/is-equal' ); +var Uint64 = require( '@stdlib/number/uint64/ctor' ); +var format = require( '@stdlib/string/format' ); +var parseUint64 = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasBigIntSupport() +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof parseUint64, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns a 64-bit unsigned integer', function test( t ) { + var a = parseUint64( '1234' ); + t.strictEqual( a instanceof Uint64, true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function throws an error if the first argument is not a string', function test( t ) { + var values; + var i; + + values = [ + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var a = parseUint64( value ); // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'the function throws an error if the second argument is not an integer', function test( t ) { + var values; + var i; + + values = [ + 0.1, + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var a = parseUint64( '0', value ); // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'the function throws an error if the first argument is not a string encoding a nonnegative integer (without radix)', function test( t ) { + var values; + var i; + + values = [ + '-', + '-1', + '1.0', + '++', + '--', + 'foo', + '', + ' ', + '0b', + '0o', + '0x', + '0b2', + '0o8', + '0xg' + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), SyntaxError, 'throws an error when provided '+values[i] ); + } + + t.end(); + + function badValue( value ) { + return function badValue() { + var a = parseUint64( value ); // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'the function throws an error if the first argument is not a string encoding a nonnegative integer (with radix)', function test( t ) { + var radix; + var str; + + for ( radix = 2; radix <= 35; radix++ ) { + str = radix.toString( 36 ); + t.throws( badValue( str, radix ), SyntaxError, format( 'throws an error when provided %s and %s', str, radix ) ); + } + + t.end(); + + function badValue( str, radix ) { + return function badValue() { + var a = parseUint64( str, radix ); // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'the function throws an error if the first argument is not a string encoding a nonnegative integer on the interval [0, 2^64-1] (without radix)', function test( t ) { + var values; + var i; + + values = [ + '18446744073709551616', + '0b10000000000000000000000000000000000000000000000000000000000000000', + '0o2000000000000000000000', + '0x10000000000000000' + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), RangeError, 'throws an error when provided '+values[i] ); + } + + t.end(); + + function badValue( value ) { + return function badValue() { + var a = parseUint64( value ); // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'the function throws an error if the first argument is not a string encoding a nonnegative integer on the interval [0, 2^64-1] (with radix)', opts, function test( t ) { + var radix; + var value; + var str; + + value = BigInt( '0x10000000000000000' ); + + for ( radix = 2; radix <= 35; radix++ ) { + str = value.toString( radix ); + t.throws( badValue( str, radix ), RangeError, format( 'throws an error when provided %s and %s', str, radix ) ); + } + + t.end(); + + function badValue( str, radix ) { + return function badValue() { + var a = parseUint64( str, radix ); // eslint-disable-line no-unused-vars + }; + } +}); + +tape( 'the function throws an error if the second argument is not an integer on the interval [2, 36]', function test( t ) { + var values; + var i; + + values = [ + 0, + 1, + 37, + 38, + 100 + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, format( 'throws an error when provided %s', values[ i ] ) ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + parseUint64( '10', value ); + }; + } +}); + +tape( 'the function uses a default radix of 10 if the second argument is omitted and the first argument is an unprefixed numeric string', function test( t ) { + var a; + var b; + + a = parseUint64( '1234' ); + b = parseUint64( '1234', 10 ); + t.ok( isEqual( a, b ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the function infers the appropriate radix if the second argument is omitted and the first argument is a prefixed numeric string', function test( t ) { + var a; + var b; + + a = parseUint64( '0b1010' ); + b = parseUint64( '1010', 2 ); + t.ok( isEqual( a, b ), 'returns expected value' ); + + a = parseUint64( '0o1234' ); + b = parseUint64( '1234', 8 ); + t.ok( isEqual( a, b ), 'returns expected value' ); + + a = parseUint64( '0x1234' ); + b = parseUint64( '1234', 16 ); + t.ok( isEqual( a, b ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports both lowercase and uppercase radix prefix and alphabetic digits', function test( t ) { + var a; + var b; + + a = parseUint64( '0B1010' ); + b = parseUint64( '0b1010' ); + t.ok( isEqual( a, b ), 'returns expected value' ); + + a = parseUint64( '0O1234' ); + b = parseUint64( '0o1234' ); + t.ok( isEqual( a, b ), 'returns expected value' ); + + a = parseUint64( '0X1234' ); + b = parseUint64( '0x1234' ); + t.ok( isEqual( a, b ), 'returns expected value' ); + + a = parseUint64( '0XABCD' ); + b = parseUint64( '0xabcd' ); + t.ok( isEqual( a, b ), 'returns expected value' ); + + a = parseUint64( '0XaBcD' ); + b = parseUint64( '0xAbCd' ); + t.ok( isEqual( a, b ), 'returns expected value' ); + + a = parseUint64( 'WXYZ', 36 ); + b = parseUint64( 'wxyz', 36 ); + t.ok( isEqual( a, b ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the function correctly handles leading and trailing whitespace', function test( t ) { + var a; + var b; + + a = parseUint64( ' 1234 ' ); + b = parseUint64( '1234' ); + t.ok( isEqual( a, b ), 'returns expected value' ); + + a = parseUint64( ' 0x1234 ' ); + b = parseUint64( '0x1234' ); + t.ok( isEqual( a, b ), 'returns expected value' ); + + a = parseUint64( ' 1234 ', 36 ); + b = parseUint64( '1234', 36 ); + t.ok( isEqual( a, b ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the function correctly handles optional plus sign', function test( t ) { + var a; + var b; + + a = parseUint64( '+1234' ); + b = parseUint64( '1234' ); + t.ok( isEqual( a, b ), 'returns expected value' ); + + a = parseUint64( ' + 1234' ); + b = parseUint64( '1234' ); + t.ok( isEqual( a, b ), 'returns expected value' ); + + a = parseUint64( '+0x1234' ); + b = parseUint64( '0x1234' ); + t.ok( isEqual( a, b ), 'returns expected value' ); + + a = parseUint64( '+1234', 36 ); + b = parseUint64( '1234', 36 ); + t.ok( isEqual( a, b ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the function correctly handles extraneous leading zeros', function test( t ) { + var a; + var b; + + a = parseUint64( '00001234' ); + b = parseUint64( '1234' ); + t.ok( isEqual( a, b ), 'returns expected value' ); + + a = parseUint64( '0x00001234' ); + b = parseUint64( '0x1234' ); + t.ok( isEqual( a, b ), 'returns expected value' ); + + a = parseUint64( '00001234', 36 ); + b = parseUint64( '1234', 36 ); + t.ok( isEqual( a, b ), 'returns expected value' ); + + t.end(); +});