factory.js (2838B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 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 copy = require( '@stdlib/utils/copy' ); 24 var StridedArrayStream = require( './main.js' ); 25 26 27 // MAIN // 28 29 /** 30 * Returns a function for creating readable streams from strided array-like values. 31 * 32 * @param {Options} [options] - stream options 33 * @param {boolean} [options.objectMode=false] - specifies whether a stream should operate in object mode 34 * @param {(string|null)} [options.encoding=null] - specifies how `Buffer` objects should be decoded to `strings` 35 * @param {NonNegativeNumber} [options.highWaterMark] - specifies the maximum number of bytes to store in an internal buffer before pausing streaming 36 * @param {string} [options.sep='\n'] - separator used to join streamed data 37 * @param {Function} [options.serialize] - custom serialization function 38 * @returns {Function} stream factory 39 * 40 * @example 41 * var Float64Array = require( '@stdlib/array/float64' ); 42 * var randu = require( '@stdlib/random/base/randu' ); 43 * 44 * var arr = new Float64Array( 10 ); 45 * var i; 46 * for ( i = 0; i < arr.length; i++ ) { 47 * arr[ i ] = randu(); 48 * } 49 * 50 * var opts = { 51 * 'sep': ',', 52 * 'objectMode': false, 53 * 'encoding': 'utf8', 54 * 'highWaterMark': 64 55 * }; 56 * 57 * var createStream = factory( opts ); 58 * 59 * // Create 10 identically configured streams... 60 * var streams = []; 61 * for ( i = 0; i < 10; i++ ) { 62 * streams.push( createStream( arr.length, arr, 1, 0 ) ); 63 * } 64 */ 65 function factory( options ) { 66 var opts; 67 if ( arguments.length ) { 68 opts = copy( options, 1 ); 69 } else { 70 opts = {}; 71 } 72 return createStream; 73 74 /** 75 * Returns a readable stream from an array-like object. 76 * 77 * @private 78 * @param {NonNegativeInteger} N - number of values to stream 79 * @param {Collection} buffer - source array-like object 80 * @param {integer} stride - stride length 81 * @param {NonNegativeInteger} offset - starting index 82 * @throws {TypeError} must provide an array-like object 83 * @throws {TypeError} options argument must be an object 84 * @throws {TypeError} must provide valid options 85 * @returns {StridedArrayStream} Stream instance 86 */ 87 function createStream( N, buffer, stride, offset ) { 88 return new StridedArrayStream( N, buffer, stride, offset, opts ); 89 } 90 } 91 92 93 // EXPORTS // 94 95 module.exports = factory;