All files main.js

100% Statements 612/612
100% Branches 58/58
100% Functions 17/17
100% Lines 612/612

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 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 6136x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 85x 85x 85x 85x 63x 85x 85x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 39x 39x 1x 1x 38x 38x 38x 38x 39x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 3x 3x 3x 3x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 28x 28x 28x 28x 10x 10x 28x 9x 9x 9x 28x 26x 2x 2x 24x 24x 7x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 7x 4x 4x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 31x 31x 31x 31x 9x 9x 31x 11x 11x 31x 2x 2x 9x 9x 8x 8x 9x 6x 6x 6x 6x 6x 6x 6x 6x 6x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6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 4x 4x 4x 4x 6x 6x 6x 6x 6x 6x  
/**
* @license Apache-2.0
*
* Copyright (c) 2018 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 no-restricted-syntax, no-invalid-this */
 
'use strict';
 
// MODULES //
 
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
var setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' );
var isFunction = require( '@stdlib/assert/is-function' );
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
var format = require( '@stdlib/string/format' );
var iteratorSymbol = require( '@stdlib/symbol/iterator' );
var Node = require( './node.js' ); // eslint-disable-line stdlib/no-redeclare
 
 
// FUNCTIONS //
 
/**
* Returns a boolean indicating if a value is a `FIFO`.
*
* @private
* @param {*} value - value to test
* @returns {boolean} boolean indicating if a value is a `FIFO`
*/
function isFIFO( value ) {
	return (
		typeof value === 'object' &&
		value !== null &&
		value.constructor.name === 'FIFO'
	);
}
 
 
// MAIN //
 
/**
* First-in-first-out queue constructor.
*
* @constructor
* @returns {FIFO} FIFO queue instance
*
* @example
* var queue = new FIFO();
*
* // Add values to the queue:
* queue.push( 'foo' ).push( 'bar' );
*
* // Remove the first value:
* var v = queue.pop();
* // returns 'foo'
*
* // Add a new value to the queue:
* queue.push( 'beep' );
*
* // Remove the "oldest" queue value:
* v = queue.pop();
* // returns 'bar'
*/
function FIFO() {
	if ( !(this instanceof FIFO) ) {
		return new FIFO();
	}
	this._length = 0;
	this._first = null;
	this._last = null;
	return this;
}
 
/**
* Clears the queue.
*
* @name clear
* @memberof FIFO.prototype
* @type {Function}
* @returns {FIFO} queue instance
*
* @example
* var queue = new FIFO();
*
* // Add values to the queue:
* queue.push( 'foo' ).push( 'bar' );
*
* // Peek at the first value:
* var v = queue.first();
* // returns 'foo'
*
* // Examine the queue length:
* var len = queue.length;
* // returns 2
*
* // Clear all queue items:
* queue.clear();
*
* // Peek at the first value:
* v = queue.first();
* // returns undefined
*
* // Examine the queue length:
* len = queue.length;
* // returns 0
*/
setReadOnly( FIFO.prototype, 'clear', function clear() {
	this._length = 0;
	this._first = null;
	this._last = null;
	return this;
});
 
/**
* Tests whether all elements in a queue pass a test implemented by a predicate function.
*
* ## Notes
*
* -   If the queue is empty, the method returns `true`.
*
* @name every
* @memberof FIFO.prototype
* @type {Function}
* @param {Function} predicate - predicate function to test queue elements
* @param {*} [thisArg] - predicate function execution context
* @throws {TypeError} `this` must be a FIFO
* @throws {TypeError} first argument must be a function
* @returns {boolean} boolean indicating whether all elements pass a test
*
* @example
* function predicate( v ) {
*     return v > 0;
* }
*
* var queue = new FIFO();
*
* queue.push( 1 ).push( 2 ).push( 3 );
*
* var bool = queue.every( predicate );
* // returns true
*/
setReadOnly( FIFO.prototype, 'every', function every( predicate, thisArg ) {
	var node;
	var i;
 
	if ( !isFIFO( this ) ) {
		throw new TypeError( 'invalid invocation. `this` is not a FIFO.' );
	}
	if ( !isFunction( predicate ) ) {
		throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
	}
	node = this._first;
	for ( i = 0; i < this._length; i++ ) {
		if ( !predicate.call( thisArg, node.value, i, this ) ) {
			return false;
		}
		node = node.next;
	}
	return true;
});
 
/**
* Returns the "oldest" queue value (i.e., the value which is "first-out").
*
* @name first
* @memberof FIFO.prototype
* @type {Function}
* @returns {(*|void)} "oldest" queue value
*
* @example
* var queue = new FIFO();
*
* // Add values to the queue:
* queue.push( 'foo' ).push( 'bar' );
*
* // Peek at the first value:
* var v = queue.first();
* // returns 'foo'
*/
setReadOnly( FIFO.prototype, 'first', function first() {
	if ( this._length ) {
		return this._first.value;
	}
});
 
/**
* Returns a queue element located at a nonnegative integer position (index) `i`.
*
* @name get
* @memberof FIFO.prototype
* @type {Function}
* @param {NonNegativeInteger} idx - element index
* @throws {TypeError} `this` must be a FIFO
* @throws {TypeError} index argument must be a nonnegative integer
* @returns {(*|void)} queue element or undefined
*
* @example
* var queue = new FIFO();
*
* queue.push( 'foo' ).push( 'bar' ).push( 'baz' );
*
* var v = queue.get( 0 );
* // returns 'foo'
*
* v = queue.get( 1 );
* // returns 'bar'
*
* v = queue.get( 100 );
* // returns undefined
*/
setReadOnly( FIFO.prototype, 'get', function get( idx ) {
	var node;
	var i;
 
	if ( !isFIFO( this ) ) {
		throw new TypeError( 'invalid invocation. `this` is not a FIFO.' );
	}
	if ( !isNonNegativeInteger( idx ) ) {
		throw new TypeError( format( 'invalid argument. Index argument must be a nonnegative integer. Value: `%s`.', idx ) );
	}
	if ( idx >= this._length ) {
		return;
	}
	node = this._first;
	for ( i = 0; i < idx; i++ ) {
		node = node.next;
	}
	return node.value;
});
 
/**
* Returns an iterator for iterating over a queue.
*
* ## Notes
*
* -   In order to prevent confusion arising from queue mutation during iteration, a returned iterator **always** iterates over a queue "snapshot", which is defined as the list of queue elements at the time of this method's invocation.
*
* @name iterator
* @memberof FIFO.prototype
* @type {Function}
* @returns {Iterator} iterator
*
* @example
* var queue = new FIFO();
*
* // Add values to the queue:
* queue.push( 'foo' ).push( 'bar' );
*
* // Create an iterator:
* var it = queue.iterator();
*
* // Iterate over the queue...
* var v = it.next().value;
* // returns 'foo'
*
* v = it.next().value;
* // returns 'bar'
*
* var bool = it.next().done;
* // returns true
*/
setReadOnly( FIFO.prototype, 'iterator', function iterator() {
	var values;
	var iter;
	var self;
	var FLG;
	var i;
 
	self = this;
 
	// Initialize the iteration index:
	i = -1;
 
	// Create a copy of queue values (necessary in order to "snapshot" the queue; otherwise, values could come and go between calls to `next`):
	values = this.toArray();
 
	// Create an iterator protocol-compliant object:
	iter = {};
	setReadOnly( iter, 'next', next );
	setReadOnly( iter, 'return', end );
	if ( iteratorSymbol ) {
		setReadOnly( iter, iteratorSymbol, factory );
	}
	return iter;
 
	/**
	* Returns an iterator protocol-compliant object containing the next iterated value.
	*
	* @private
	* @returns {Object} iterator protocol-compliant object
	*/
	function next() {
		i += 1;
		if ( FLG || i >= values.length ) {
			return {
				'done': true
			};
		}
		return {
			'value': values[ i ],
			'done': false
		};
	}
 
	/**
	* Finishes an iterator.
	*
	* @private
	* @param {*} [value] - value to return
	* @returns {Object} iterator protocol-compliant object
	*/
	function end( value ) {
		FLG = true;
		if ( arguments.length ) {
			return {
				'value': value,
				'done': true
			};
		}
		return {
			'done': true
		};
	}
 
	/**
	* Returns a new iterator.
	*
	* @private
	* @returns {Iterator} iterator
	*/
	function factory() {
		return self.iterator();
	}
});
 
/**
* Returns the "newest" queue value (i.e., the value which is currently "last-out").
*
* @name last
* @memberof FIFO.prototype
* @type {Function}
* @returns {(*|void)} "newest" queue value
*
* @example
* var queue = new FIFO();
*
* // Add values to the queue:
* queue.push( 'foo' ).push( 'bar' );
*
* // Peek at the last value:
* var v = queue.last();
* // returns 'bar'
*/
setReadOnly( FIFO.prototype, 'last', function last() {
	if ( this._length ) {
		return this._last.value;
	}
});
 
/**
* Queue length.
*
* @name length
* @memberof FIFO.prototype
* @type {NonNegativeInteger}
*
* @example
* var queue = new FIFO();
*
* // Examine the initial queue length:
* var len = queue.length;
* // returns 0
*
* // Add values to the queue:
* queue.push( 'foo' ).push( 'bar' );
*
* // Retrieve the current queue length:
* len = queue.length;
* // returns 2
*/
setReadOnlyAccessor( FIFO.prototype, 'length', function get() {
	return this._length;
});
 
/**
* Removes a value from the queue.
*
* @name pop
* @memberof FIFO.prototype
* @type {Function}
* @returns {(*|void)} removed value
*
* @example
* var queue = new FIFO();
*
* // Add values to the queue:
* queue.push( 'foo' ).push( 'bar' );
*
* // Remove the first value:
* var v = queue.pop();
* // returns 'foo'
*
* // Add a new value to the queue:
* queue.push( 'beep' );
*
* // Remove the "oldest" queue value:
* v = queue.pop();
* // returns 'bar'
*/
setReadOnly( FIFO.prototype, 'pop', function pop() {
	var value;
	if ( this._length ) {
		// Retrieve the "first-out" value:
		value = this._first.value;
 
		// Check whether we have a new "first-out" or whether we have drained the queue...
		if ( this._first.next ) {
			this._first = this._first.next;
			this._first.prev = null;
		} else {
			// Queue is empty:
			this._first = null;
			this._last = null;
		}
		// Decrement the queue length:
		this._length -= 1;
	}
	return value;
});
 
/**
* Adds a value to the queue.
*
* @name push
* @memberof FIFO.prototype
* @type {Function}
* @returns {FIFO} queue instance
*
* @example
* var queue = new FIFO();
*
* // Add values to the queue:
* queue.push( 'foo' ).push( 'bar' );
*
* // Remove the first value:
* var v = queue.pop();
* // returns 'foo'
*
* // Add a new value to the queue:
* queue.push( 'beep' );
*
* // Remove the "oldest" queue value:
* v = queue.pop();
* // returns 'bar'
*/
setReadOnly( FIFO.prototype, 'push', function push( value ) {
	var node;
 
	// Create a new queue node:
	node = new Node( value );
 
	// Check whether the queue is currently empty...
	if ( this._length === 0 ) {
		// This is the only queued node, making it both the first and last node:
		this._first = node;
		this._last = node;
	} else {
		// Link the node to the previous most "recent" node:
		node.prev = this._last;
 
		// Link the previous most "recent" node to the new node:
		this._last.next = node;
 
		// Update the queue pointer for the "last" node to the new node:
		this._last = node;
	}
	// Increment the queue length:
	this._length += 1;
 
	return this;
});
 
/**
* Sets a queue element at a specified index.
*
* ## Notes
*
* -   If an index argument is not provided, the method sets a value at index `0`.
*
* @name set
* @memberof FIFO.prototype
* @type {Function}
* @param {*} value - value to set
* @param {NonNegativeInteger} idx - element index (default: 0)
* @throws {TypeError} `this` must be a FIFO
* @throws {TypeError} index argument must be a nonnegative integer
* @throws {RangeError} index argument is out-of-bounds
* @returns {void}
*
* @example
* var queue = new FIFO();
*
* queue.push( 'foo' ).push( 'bar' ).push( 'baz' );
*
* queue.set( 'beep', 0 );
* var v = queue.get( 0 );
* // returns 'beep'
*
* queue.set( 'boop', 1 );
* v = queue.get( 1 );
* // returns 'boop'
*/
setReadOnly( FIFO.prototype, 'set', function set( value ) {
	var node;
	var idx;
	var i;
 
	if ( !isFIFO( this ) ) {
		throw new TypeError( 'invalid invocation. `this` is not a FIFO.' );
	}
	idx = ( arguments.length > 1 ) ? arguments[ 1 ] : 0;
	if ( !isNonNegativeInteger( idx ) ) {
		throw new TypeError( format( 'invalid argument. Index argument must be a nonnegative integer. Value: `%s`.', idx ) );
	}
	if ( idx >= this._length ) {
		throw new RangeError( format( 'invalid argument. Index argument is out-of-bounds. Value: `%u`.', idx ) );
	}
	node = this._first;
	for ( i = 0; i < idx; i++ ) {
		node = node.next;
	}
	node.value = value;
});
 
/**
* Returns an array of queue values.
*
* @name toArray
* @memberof FIFO.prototype
* @type {Function}
* @returns {Array} queue values
*
* @example
* var queue = new FIFO();
*
* // Add values to the queue:
* queue.push( 'foo' ).push( 'bar' );
*
* // Get an array of queue values:
* var vals = queue.toArray();
* // returns [ 'foo', 'bar' ]
*/
setReadOnly( FIFO.prototype, 'toArray', function toArray() {
	var node;
	var out;
	var i;
 
	out = [];
	node = this._first;
	for ( i = 0; i < this._length; i++ ) {
		out.push( node.value );
		node = node.next;
	}
	return out;
});
 
/**
* Serializes a queue as JSON.
*
* ## Notes
*
* -   `JSON.stringify()` implicitly calls this method when stringifying a `FIFO` instance.
*
* @name toJSON
* @memberof FIFO.prototype
* @type {Function}
* @returns {Object} serialized queue
*
* @example
* var queue = new FIFO();
*
* // Add values to the queue:
* queue.push( 'foo' ).push( 'bar' );
*
* // Serialize to JSON:
* var o = queue.toJSON();
* // returns { 'type': 'fifo', 'data': [ 'foo', 'bar' ] }
*/
setReadOnly( FIFO.prototype, 'toJSON', function toJSON() {
	var out = {};
	out.type = 'fifo';
	out.data = this.toArray();
	return out;
});
 
 
// EXPORTS //
 
module.exports = FIFO;