All files main.js

91.43% Statements 299/327
88.57% Branches 31/35
100% Functions 1/1
91.43% Lines 299/327

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 3281x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 44x 44x 44x 44x 44x 1x 1x 43x 44x 3x 3x 40x 2x 1x 1x 1x       1x 1x 1x 1x 2x 38x 12x 10x 6x 6x 6x       6x 4x 4x 4x 10x 2x 2x 2x 12x 26x 9x 1x 1x 8x 8x 8x 8x 8x 8x 4x 4x 8x                                   9x 17x 11x 8x 8x 8x           8x 3x 3x 3x 11x 6x 6x 6x 22x 22x 22x 44x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/**
* @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.
*/
 
/* eslint-disable max-len */
 
'use strict';
 
// MODULES //
 
var BigInt = require( '@stdlib/bigint/ctor' );
var Number = require( '@stdlib/number/ctor' );
var UINT32_MAX = require( '@stdlib/constants/uint32/max' );
var Uint32Array = require( '@stdlib/array/uint32' );
var format = require( '@stdlib/string/format' );
var hasBigIntSupport = require( '@stdlib/assert/has-bigint-support' );
var isArray = require( '@stdlib/assert/is-array' );
var isBigInt = require( '@stdlib/assert/is-bigint' );
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
var isNan = require( '@stdlib/assert/is-nan' );
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
var isString = require( '@stdlib/assert/is-string' );
var isUint32Array = require( '@stdlib/assert/is-uint32array' );
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
var asBigUint64 = require( './asbiguint64.js' );
var getHighWord = require( './gethighword.js' );
var getLowWord = require( './getlowword.js' );
var parseUint64dw = require( './parseuint64dw.js' );
var toBigInt = require( './tobigint.js' );
var toJSON = require( './tojson.js' );
var toString = require( './tostring.js' ); // eslint-disable-line stdlib/no-redeclare
var valueOf = require( './valueof.js' ); // eslint-disable-line stdlib/no-redeclare
 
 
// VARIABLES //
 
var TWO_32 = 0x100000000;
var hasBigInt = hasBigIntSupport();
 
 
// MAIN //
 
/**
* Unsigned 64-bit integer constructor.
*
* @constructor
* @param {number|string|Array<number>|Uint32Array|BigInt} value - numeric value or high low words or BigInt
* @throws {TypeError}
* @returns {Uint64} Unsigned 64-bit integer
*
* @example // TODO: add more example
* var x = new Uint64( 5 );
* // returns <Uint64>
*/
function Uint64( value ) {
	var raw;
	var tmp;
 
	if ( !( this instanceof Uint64 ) ) {
		throw new TypeError( 'invalid invocation. Constructor must be called with the `new` keyword.' );
	}
 
	if ( isBigInt( value ) ) {
		raw = asBigUint64( value );
	}
	else if ( isUint32Array( value ) ) {
		if ( value.length === 2 ) {
			if ( hasBigInt ) {
				raw = ( BigInt( value[ 0 ] ) << BigInt( 32 ) ) | BigInt( value[ 1 ] );
			}
			else {
				raw = value; // TODO: should we copy it instead?
			}
		}
		else {
			throw new TypeError( format( 'invalid argument. Provided Uint32Array must have a length of 2. Value: `%s`.', value ) );
		}
	}
	else if ( isArray( value ) ) {
		if ( value.length === 2 ) {
			if ( isInteger( value[ 0 ] ) && isInteger( value[ 1 ] ) && value[ 0 ] >= 0 && value[ 0 ] <= UINT32_MAX && value[ 1 ] >= 0 && value[ 1 ] <= UINT32_MAX ) {
				if ( hasBigInt ) {
					raw = ( BigInt( value[ 0 ] ) << BigInt( 32 ) ) | BigInt( value[ 1 ] );
				}
				else {
					raw = new Uint32Array( value );
				}
			}
			else {
				throw new TypeError( format( 'invalid argument. Provided array must contain only integer values between 0 and %s. Value: `%s`.', UINT32_MAX, value ) );
			}
		}
		else {
			throw new TypeError( format( 'invalid argument. Provided array must have a length of 2. Value: `%s`.', value ) );
		}
	}
	else if ( isString( value ) ) {
		if ( value[ 0 ] === '-' ) {
			throw new TypeError( format( 'invalid argument. Provided value must be non-negative. Value: `%s`.', value ) );
		}
 
		if ( hasBigInt ) {
			try {
				raw = asBigUint64( BigInt( value ) );
			}
			catch ( error ) { // eslint-disable-line no-unused-vars
				throw new TypeError( format( 'invalid argument. Could not convert string to Uint64. Value: `%s`.', value ) );
			}
		}
		else {
			if ( value[ 0 ] === '+' ) {
				tmp = value.slice( 1 );
			}
			else {
				tmp = value;
			}

			tmp = parseUint64dw( tmp );

			if ( !isNaN( tmp[ 0 ] ) && !isNan( tmp[ 1 ] ) ) {
				raw = new Uint32Array( tmp );
			}
			else {
				throw new TypeError( format( 'invalid argument. Could not convert string to Uint64. Value: `%s`.', value ) );
			}
		}
	}
	else if ( isNumber( value ) ) {
		if ( value >= 0 && Number.isSafeInteger( value ) ) {
			if ( hasBigInt ) {
				raw = asBigUint64( BigInt( value ) );
			}
			else {
				raw = new Uint32Array( 2 );
				raw[ 0 ] = ( value / TWO_32 ) >>> 0;
				raw[ 1 ] = value >>> 0;
			}
		}
		else {
			throw new TypeError( format( 'invalid argument. Provided number must be between 0 and %s. Value: `%s`.', Number.MAX_SAFE_INTEGER, value ) );
		}
	}
	else {
		throw new TypeError( format( 'invalid argument. Must provide a number or string or Array<number> or Uint32Array or BigInt. Value: `%s`.', value ) );
	}
 
	setReadOnly( this, '_raw', raw );
	return this;
}
 
/**
* Constructor name.
*
* @name name
* @memberof Uint64
* @readonly
* @type {string}
* @default 'Uint64'
*
* @example
* var name = Uint64.name;
* // returns 'Uint64'
*/
setReadOnly( Uint64, 'name', 'Uint64' );
 
/**
* Size (in bytes) of the underlying value.
*
* @name BYTES_PER_ELEMENT
* @memberof Uint64
* @type {integer}
* @returns {integer} size in bytes
*
* @example
* var nbytes = Uint64.BYTES_PER_ELEMENT;
* // returns 8
*/
setReadOnly( Uint64, 'BYTES_PER_ELEMENT', 8 );
 
/**
* Size (in bytes) of the underlying value.
*
* @name BYTES_PER_ELEMENT
* @memberof Uint64.prototype
* @type {integer}
* @returns {integer} size in bytes
*
* @example
* var x = new Uint64( 5 );
*
* var nbytes = x.BYTES_PER_ELEMENT;
* // returns 8
*/
setReadOnly( Uint64.prototype, 'BYTES_PER_ELEMENT', 8 );
 
/**
* Serializes an unsigned 64-bit integer number as a string.
*
* @name toString
* @memberof Uint64.prototype
* @type {Function}
* @returns {string} serialized unsigned 64-bit integer number
*
* @example
* var str = new Uint64( 5 ).toString();
* // returns '5'
*
* str = new Uint64( [ 1, 0 ] ).toString();
* // returns '4294967296'
*
* str = new Uint64( 100000000001 ).toString();
* // returns '100000000001'
*
*/
setReadOnly( Uint64.prototype, 'toString', toString );
 
/**
* Serializes an unsigned 64-bit integer number as a JSON object.
*
* ## Notes
*
* -   `JSON.stringify()` implicitly calls this method when stringifying a `Uint64` instance.
*
* @name toJSON
* @memberof Uint64.prototype
* @type {Function}
* @returns {Object} serialized unsigned 64-bit integer number
*
* @example
* var x = new Uint64( 5 );
*
* var y = x.toJSON();
* // returns { 'type': 'Uint64', 'high': 0, 'low': 5 }
*/
setReadOnly( Uint64.prototype, 'toJSON', toJSON );
 
/**
* Converts an unsigned 64-bit integer number to a primitive value.
*
* @name valueOf
* @memberof Uint64.prototype
* @type {Function}
* @returns {number} primitive value
*
* @example
* var x = new Uint64( 5 );
*
* var v = x.valueOf();
* // returns 5.0
*/
setReadOnly( Uint64.prototype, 'valueOf', valueOf );
 
/**
* Returns the high 32-bit word of an unsigned 64-bit integer number.
*
* @name getHighWord
* @memberof Uint64.prototype
* @type {Function}
* @returns {number} 32-bit unsigned integer
*
* @example
*
* var x = new Uint64( [ 1234, 5678 ] );
*
* var y = x.getHighWord();
* // returns 1234.0
*/
setReadOnly( Uint64.prototype, 'getHighWord', getHighWord );
 
/**
* Returns the low 32-bit word of an unsigned 64-bit integer number.
*
* @name getLowWord
* @memberof Uint64.prototype
* @type {Function}
* @returns {number} 32-bit unsigned integer
*
* @example
*
* var x = new Uint64( [ 1234, 5678 ] );
*
* var y = x.getLowWord();
* // returns 5678.0
*/
setReadOnly( Uint64.prototype, 'getLowWord', getLowWord );
 
/**
* Converts an unsigned 64-bit integer number to a BigInt.
*
* @name toBigInt
* @memberof Uint64.prototype
* @type {Function}
* @returns {BigInt} BigInt value
*
* @example
* var hasBigInt = require( '@stdlib/assert/has-bigint-support' );
*
* var x = new Uint64( 5 );
*
* var v;
* if ( hasBigInt() ) {
*     v = x.toBigInt();
*     // returns 5.0
* }
*/
if ( hasBigInt ) {
	setReadOnly( Uint64.prototype, 'toBigInt', toBigInt );
}
 
 
// EXPORTS //
 
module.exports = Uint64;