main.js (2977B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2020 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); 24 var iteratorSymbol = require( '@stdlib/symbol/iterator' ); 25 var FLOAT64_MAX_SAFE_INTEGER = require( '@stdlib/constants/float64/max-safe-integer' ); 26 var validate = require( './validate.js' ); 27 28 29 // MAIN // 30 31 /** 32 * Returns an iterator which generates a negative integer sequence. 33 * 34 * ## Notes 35 * 36 * - If an environment supports `Symbol.iterator`, the returned iterator is iterable. 37 * 38 * @param {Options} [options] - function options 39 * @param {NonNegativeInteger} [options.iter=9007199254740991] - number of iterations 40 * @throws {TypeError} options argument must be an object 41 * @throws {TypeError} must provide valid options 42 * @returns {Iterator} iterator 43 * 44 * @example 45 * var iter = iterNegativeIntegersSeq(); 46 * 47 * var v = iter.next().value; 48 * // returns -1 49 * 50 * v = iter.next().value; 51 * // returns -2 52 * 53 * v = iter.next().value; 54 * // returns -3 55 * 56 * // ... 57 */ 58 function iterNegativeIntegersSeq( options ) { 59 var opts; 60 var iter; 61 var FLG; 62 var err; 63 var i; 64 65 opts = { 66 'iter': FLOAT64_MAX_SAFE_INTEGER 67 }; 68 if ( arguments.length ) { 69 err = validate( opts, options ); 70 if ( err ) { 71 throw err; 72 } 73 } 74 i = 0; 75 76 // Create an iterator protocol-compliant object: 77 iter = {}; 78 setReadOnly( iter, 'next', next ); 79 setReadOnly( iter, 'return', end ); 80 81 // If an environment supports `Symbol.iterator`, make the iterator iterable: 82 if ( iteratorSymbol ) { 83 setReadOnly( iter, iteratorSymbol, factory ); 84 } 85 return iter; 86 87 /** 88 * Returns an iterator protocol-compliant object containing the next iterated value. 89 * 90 * @private 91 * @returns {Object} iterator protocol-compliant object 92 */ 93 function next() { 94 i += 1; 95 if ( FLG || i > opts.iter ) { 96 return { 97 'done': true 98 }; 99 } 100 return { 101 'value': -i, 102 'done': false 103 }; 104 } 105 106 /** 107 * Finishes an iterator. 108 * 109 * @private 110 * @param {*} [value] - value to return 111 * @returns {Object} iterator protocol-compliant object 112 */ 113 function end( value ) { 114 FLG = true; 115 if ( arguments.length ) { 116 return { 117 'value': value, 118 'done': true 119 }; 120 } 121 return { 122 'done': true 123 }; 124 } 125 126 /** 127 * Returns a new iterator. 128 * 129 * @private 130 * @returns {Iterator} iterator 131 */ 132 function factory() { 133 return iterNegativeIntegersSeq( opts ); 134 } 135 } 136 137 138 // EXPORTS // 139 140 module.exports = iterNegativeIntegersSeq;