main.js (3097B)
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 an interleaved sequence of odd integers. 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=9007199254740992] - 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 = iterOddIntegersSeq(); 46 * 47 * var v = iter.next().value; 48 * // returns 1 49 * 50 * v = iter.next().value; 51 * // returns -1 52 * 53 * v = iter.next().value; 54 * // returns 3 55 * 56 * // ... 57 */ 58 function iterOddIntegersSeq( options ) { 59 var opts; 60 var iter; 61 var FLG; 62 var err; 63 var sgn; 64 var i; 65 var j; 66 67 opts = { 68 'iter': FLOAT64_MAX_SAFE_INTEGER 69 }; 70 if ( arguments.length ) { 71 err = validate( opts, options ); 72 if ( err ) { 73 throw err; 74 } 75 } 76 sgn = -1; 77 i = 0; 78 j = -1; 79 80 // Create an iterator protocol-compliant object: 81 iter = {}; 82 setReadOnly( iter, 'next', next ); 83 setReadOnly( iter, 'return', end ); 84 85 // If an environment supports `Symbol.iterator`, make the iterator iterable: 86 if ( iteratorSymbol ) { 87 setReadOnly( iter, iteratorSymbol, factory ); 88 } 89 return iter; 90 91 /** 92 * Returns an iterator protocol-compliant object containing the next iterated value. 93 * 94 * @private 95 * @returns {Object} iterator protocol-compliant object 96 */ 97 function next() { 98 i += 1; 99 if ( FLG || i > opts.iter ) { 100 return { 101 'done': true 102 }; 103 } 104 // Increment every other iteration... 105 if ( sgn < 0 ) { 106 j += 2; 107 } 108 sgn *= -1; 109 return { 110 'value': sgn * j, 111 'done': false 112 }; 113 } 114 115 /** 116 * Finishes an iterator. 117 * 118 * @private 119 * @param {*} [value] - value to return 120 * @returns {Object} iterator protocol-compliant object 121 */ 122 function end( value ) { 123 FLG = true; 124 if ( arguments.length ) { 125 return { 126 'value': value, 127 'done': true 128 }; 129 } 130 return { 131 'done': true 132 }; 133 } 134 135 /** 136 * Returns a new iterator. 137 * 138 * @private 139 * @returns {Iterator} iterator 140 */ 141 function factory() { 142 return iterOddIntegersSeq( opts ); 143 } 144 } 145 146 147 // EXPORTS // 148 149 module.exports = iterOddIntegersSeq;