main.js (2954B)
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 factorial = require( './../../../../base/special/factorial' ); 26 var validate = require( './validate.js' ); 27 28 29 // VARIABLES // 30 31 var MAX_ITER = 1e308; 32 33 34 // MAIN // 35 36 /** 37 * Returns an iterator which generates a sequence of factorials. 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=1e308] - 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 = iterFactorialsSeq(); 51 * 52 * var v = iter.next().value; 53 * // returns 1 54 * 55 * v = iter.next().value; 56 * // returns 1 57 * 58 * v = iter.next().value; 59 * // returns 2 60 * 61 * // ... 62 */ 63 function iterFactorialsSeq( options ) { 64 var opts; 65 var iter; 66 var FLG; 67 var err; 68 var i; 69 70 opts = { 71 'iter': MAX_ITER 72 }; 73 if ( arguments.length ) { 74 err = validate( opts, options ); 75 if ( err ) { 76 throw err; 77 } 78 } 79 i = -1; 80 81 // Create an iterator protocol-compliant object: 82 iter = {}; 83 setReadOnly( iter, 'next', next ); 84 setReadOnly( iter, 'return', end ); 85 86 // If an environment supports `Symbol.iterator`, make the iterator iterable: 87 if ( iteratorSymbol ) { 88 setReadOnly( iter, iteratorSymbol, factory ); 89 } 90 return iter; 91 92 /** 93 * Returns an iterator protocol-compliant object containing the next iterated value. 94 * 95 * @private 96 * @returns {Object} iterator protocol-compliant object 97 */ 98 function next() { 99 i += 1; 100 if ( FLG || i >= opts.iter ) { 101 return { 102 'done': true 103 }; 104 } 105 return { 106 'value': factorial( i ), 107 'done': false 108 }; 109 } 110 111 /** 112 * Finishes an iterator. 113 * 114 * @private 115 * @param {*} [value] - value to return 116 * @returns {Object} iterator protocol-compliant object 117 */ 118 function end( value ) { 119 FLG = true; 120 if ( arguments.length ) { 121 return { 122 'value': value, 123 'done': true 124 }; 125 } 126 return { 127 'done': true 128 }; 129 } 130 131 /** 132 * Returns a new iterator. 133 * 134 * @private 135 * @returns {Iterator} iterator 136 */ 137 function factory() { 138 return iterFactorialsSeq( opts ); 139 } 140 } 141 142 143 // EXPORTS // 144 145 module.exports = iterFactorialsSeq;