main.js (3107B)
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 // VARIABLES // 30 31 var MAX_ITER = (FLOAT64_MAX_SAFE_INTEGER+1) / 2; 32 33 34 // MAIN // 35 36 /** 37 * Returns an iterator which generates a sequence of negative odd integers. 38 * 39 * ## Notes 40 * 41 * - If an environment supports `Symbol.iterator`, the returned iterator is iterable. 42 * 43 * @param {Options} [options] - function options 44 * @param {NonNegativeInteger} [options.iter=4503599627370496] - number of iterations 45 * @throws {TypeError} options argument must be an object 46 * @throws {TypeError} must provide valid options 47 * @returns {Iterator} iterator 48 * 49 * @example 50 * var iter = iterNegativeOddIntegersSeq(); 51 * 52 * var v = iter.next().value; 53 * // returns -1 54 * 55 * v = iter.next().value; 56 * // returns -3 57 * 58 * v = iter.next().value; 59 * // returns -5 60 * 61 * // ... 62 */ 63 function iterNegativeOddIntegersSeq( options ) { // eslint-disable-line id-length 64 var opts; 65 var iter; 66 var FLG; 67 var err; 68 var i; 69 var j; 70 71 opts = { 72 'iter': MAX_ITER 73 }; 74 if ( arguments.length ) { 75 err = validate( opts, options ); 76 if ( err ) { 77 throw err; 78 } 79 } 80 i = 0; 81 j = 1; 82 83 // Create an iterator protocol-compliant object: 84 iter = {}; 85 setReadOnly( iter, 'next', next ); 86 setReadOnly( iter, 'return', end ); 87 88 // If an environment supports `Symbol.iterator`, make the iterator iterable: 89 if ( iteratorSymbol ) { 90 setReadOnly( iter, iteratorSymbol, factory ); 91 } 92 return iter; 93 94 /** 95 * Returns an iterator protocol-compliant object containing the next iterated value. 96 * 97 * @private 98 * @returns {Object} iterator protocol-compliant object 99 */ 100 function next() { 101 i += 1; 102 if ( FLG || i > opts.iter ) { 103 return { 104 'done': true 105 }; 106 } 107 j -= 2; 108 return { 109 'value': j, 110 'done': false 111 }; 112 } 113 114 /** 115 * Finishes an iterator. 116 * 117 * @private 118 * @param {*} [value] - value to return 119 * @returns {Object} iterator protocol-compliant object 120 */ 121 function end( value ) { 122 FLG = true; 123 if ( arguments.length ) { 124 return { 125 'value': value, 126 'done': true 127 }; 128 } 129 return { 130 'done': true 131 }; 132 } 133 134 /** 135 * Returns a new iterator. 136 * 137 * @private 138 * @returns {Iterator} iterator 139 */ 140 function factory() { 141 return iterNegativeOddIntegersSeq( opts ); 142 } 143 } 144 145 146 // EXPORTS // 147 148 module.exports = iterNegativeOddIntegersSeq;