@stdlib/random-streams-normal 0.0.7 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/factory.js CHANGED
@@ -20,7 +20,9 @@
20
20
 
21
21
  // MODULES //
22
22
 
23
- var copy = require( '@stdlib/utils-copy' );
23
+ var isPlainObject = require( '@stdlib/assert-is-plain-object' );
24
+ var assign = require( '@stdlib/object-assign' );
25
+ var format = require( '@stdlib/string-format' );
24
26
  var RandomStream = require( './main.js' );
25
27
 
26
28
 
@@ -42,6 +44,7 @@ var RandomStream = require( './main.js' );
42
44
  * @param {PRNGStateMT19937} [options.state] - pseudorandom number generator state
43
45
  * @param {boolean} [options.copy=true] - boolean indicating whether to copy a provided pseudorandom number generator state
44
46
  * @param {PositiveInteger} [options.siter] - number of iterations after which to emit the PRNG state
47
+ * @throws {TypeError} options argument must be an object
45
48
  * @returns {Function} stream factory
46
49
  *
47
50
  * @example
@@ -68,9 +71,15 @@ function factory( mu, sigma, options ) {
68
71
 
69
72
  nargs = arguments.length;
70
73
  if ( nargs === 1 ) {
71
- opts = copy( mu, 1 );
74
+ if ( !isPlainObject( mu ) ) {
75
+ throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', mu ) );
76
+ }
77
+ opts = assign( {}, mu );
72
78
  } else if ( nargs > 2 ) {
73
- opts = copy( options, 1 );
79
+ if ( !isPlainObject( options ) ) {
80
+ throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
81
+ }
82
+ opts = assign( {}, options );
74
83
  } else {
75
84
  opts = {};
76
85
  }
package/lib/index.js CHANGED
@@ -79,17 +79,17 @@
79
79
  // MODULES //
80
80
 
81
81
  var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
82
- var stream = require( './main.js' );
82
+ var main = require( './main.js' );
83
83
  var objectMode = require( './object_mode.js' );
84
84
  var factory = require( './factory.js' );
85
85
 
86
86
 
87
87
  // MAIN //
88
88
 
89
- setReadOnly( stream, 'objectMode', objectMode );
90
- setReadOnly( stream, 'factory', factory );
89
+ setReadOnly( main, 'objectMode', objectMode );
90
+ setReadOnly( main, 'factory', factory );
91
91
 
92
92
 
93
93
  // EXPORTS //
94
94
 
95
- module.exports = stream;
95
+ module.exports = main;
package/lib/main.js CHANGED
@@ -25,7 +25,7 @@ var isPositiveNumber = require( '@stdlib/assert-is-positive-number' ).isPrimitiv
25
25
  var isNumber = require( '@stdlib/assert-is-number' ).isPrimitive;
26
26
  var isnan = require( '@stdlib/math-base-assert-is-nan' );
27
27
  var isError = require( '@stdlib/assert-is-error' );
28
- var copy = require( '@stdlib/utils-copy' );
28
+ var assign = require( '@stdlib/object-assign' );
29
29
  var inherit = require( '@stdlib/utils-inherit' );
30
30
  var setNonEnumerable = require( '@stdlib/utils-define-nonenumerable-property' );
31
31
  var setNonEnumerableReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
@@ -34,6 +34,7 @@ var setReadWriteAccessor = require( '@stdlib/utils-define-read-write-accessor' )
34
34
  var rnorm = require( '@stdlib/random-base-normal' ).factory;
35
35
  var string2buffer = require( '@stdlib/buffer-from-string' );
36
36
  var nextTick = require( '@stdlib/utils-next-tick' );
37
+ var format = require( '@stdlib/string-format' );
37
38
  var DEFAULTS = require( './defaults.json' );
38
39
  var validate = require( './validate.js' );
39
40
  var debug = require( './debug.js' );
@@ -235,12 +236,12 @@ function RandomStream( mu, sigma, options ) {
235
236
  return new RandomStream( mu, sigma );
236
237
  }
237
238
  if ( !isNumber( mu ) || isnan( mu ) ) {
238
- throw new TypeError( 'invalid argument. First argument must be a number primitive and not `NaN`. Value: `'+mu+'`.' );
239
+ throw new TypeError( format( 'invalid argument. First argument must be a number and not NaN. Value: `%s`.', mu ) );
239
240
  }
240
241
  if ( !isPositiveNumber( sigma ) ) {
241
- throw new TypeError( 'invalid argument. Second argument must be a positive number. Value: `'+sigma+'`.' );
242
+ throw new TypeError( format( 'invalid argument. Second argument must be a positive number. Value: `%s`.', sigma ) );
242
243
  }
243
- opts = copy( DEFAULTS );
244
+ opts = assign( {}, DEFAULTS );
244
245
  if ( arguments.length > 2 ) {
245
246
  err = validate( opts, options );
246
247
  if ( err ) {
@@ -21,7 +21,8 @@
21
21
  // MODULES //
22
22
 
23
23
  var isObject = require( '@stdlib/assert-is-plain-object' );
24
- var copy = require( '@stdlib/utils-copy' );
24
+ var assign = require( '@stdlib/object-assign' );
25
+ var format = require( '@stdlib/string-format' );
25
26
  var RandomStream = require( './main.js' );
26
27
 
27
28
 
@@ -68,9 +69,9 @@ function objectMode( mu, sigma, options ) {
68
69
  if ( arguments.length > 2 ) {
69
70
  opts = options;
70
71
  if ( !isObject( opts ) ) {
71
- throw new TypeError( 'invalid argument. Options must be an object. Value: `' + opts + '`.' );
72
+ throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', opts ) );
72
73
  }
73
- opts = copy( options, 1 );
74
+ opts = assign( {}, options );
74
75
  } else {
75
76
  opts = {};
76
77
  }
package/lib/validate.js CHANGED
@@ -27,6 +27,7 @@ var isNonNegative = require( '@stdlib/assert-is-nonnegative-number' ).isPrimitiv
27
27
  var isString = require( '@stdlib/assert-is-string' ).isPrimitive;
28
28
  var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;
29
29
  var isNonNegativeInteger = require( '@stdlib/assert-is-nonnegative-integer' ).isPrimitive;
30
+ var format = require( '@stdlib/string-format' );
30
31
 
31
32
 
32
33
  // MAIN //
@@ -54,49 +55,49 @@ var isNonNegativeInteger = require( '@stdlib/assert-is-nonnegative-integer' ).is
54
55
  * var options = {
55
56
  * 'objectMode': true
56
57
  * };
57
- * var err= validate( opts, options );
58
+ * var err = validate( opts, options );
58
59
  * if ( err ) {
59
60
  * throw err;
60
61
  * }
61
62
  */
62
63
  function validate( opts, options ) {
63
64
  if ( !isObject( options ) ) {
64
- return new TypeError( 'invalid argument. Options must be an object. Value: `' + options + '`.' );
65
+ return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
65
66
  }
66
67
  if ( hasOwnProp( options, 'sep' ) ) {
67
68
  opts.sep = options.sep;
68
69
  if ( !isString( opts.sep ) ) {
69
- return new TypeError( 'invalid option. `sep` option must be a primitive string. Option: `' + opts.sep + '`.' );
70
+ return new TypeError( format( 'invalid option. `%s` option must be a string. Option: `%s`.', 'sep', opts.sep ) );
70
71
  }
71
72
  }
72
73
  if ( hasOwnProp( options, 'objectMode' ) ) {
73
74
  opts.objectMode = options.objectMode;
74
75
  if ( !isBoolean( opts.objectMode ) ) {
75
- return new TypeError( 'invalid option. `objectMode` option must be a primitive boolean. Option: `' + opts.objectMode + '`.' );
76
+ return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'objectMode', opts.objectMode ) );
76
77
  }
77
78
  }
78
79
  if ( hasOwnProp( options, 'encoding' ) ) {
79
80
  opts.encoding = options.encoding;
80
81
  if ( !isString( opts.encoding ) && opts.encoding !== null ) {
81
- return new TypeError( 'invalid option. `encoding` option must be a primitive string or null. Option: `' + opts.encoding + '`.' );
82
+ return new TypeError( format( 'invalid option. `%s` option must be a string or null. Option: `%s`.', 'encoding', opts.encoding ) );
82
83
  }
83
84
  }
84
85
  if ( hasOwnProp( options, 'highWaterMark' ) ) {
85
86
  opts.highWaterMark = options.highWaterMark;
86
87
  if ( !isNonNegative( opts.highWaterMark ) ) {
87
- return new TypeError( 'invalid option. `highWaterMark` option must be a nonnegative number. Option: `' + opts.highWaterMark + '`.' );
88
+ return new TypeError( format( 'invalid option. `%s` option must be a nonnegative number. Option: `%s`.', 'highWaterMark', opts.highWaterMark ) );
88
89
  }
89
90
  }
90
91
  if ( hasOwnProp( options, 'iter' ) ) {
91
92
  opts.iter = options.iter;
92
93
  if ( !isNonNegativeInteger( opts.iter ) ) {
93
- return new TypeError( 'invalid option. `iter` option must be a nonnegative integer. Option: `' + opts.iter + '`.' );
94
+ return new TypeError( format( 'invalid option. `%s` option must be a nonnegative integer. Option: `%s`.', 'iter', opts.iter ) );
94
95
  }
95
96
  }
96
97
  if ( hasOwnProp( options, 'siter' ) ) {
97
98
  opts.siter = options.siter;
98
99
  if ( !isPositiveInteger( opts.siter ) ) {
99
- return new TypeError( 'invalid option. `siter` option must be a positive integer. Option: `' + opts.siter + '`.' );
100
+ return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'siter', opts.siter ) );
100
101
  }
101
102
  }
102
103
  // Pass through options...
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stdlib/random-streams-normal",
3
- "version": "0.0.7",
3
+ "version": "0.1.0",
4
4
  "description": "Create a readable stream for generating pseudorandom numbers drawn from a normal distribution.",
5
5
  "license": "Apache-2.0",
6
6
  "author": {
@@ -13,9 +13,6 @@
13
13
  "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
14
14
  }
15
15
  ],
16
- "bin": {
17
- "random-normal": "./bin/cli"
18
- },
19
16
  "main": "./lib",
20
17
  "directories": {
21
18
  "benchmark": "./benchmark",
@@ -40,59 +37,36 @@
40
37
  "url": "https://github.com/stdlib-js/stdlib/issues"
41
38
  },
42
39
  "dependencies": {
43
- "@stdlib/array-uint32": "^0.0.x",
44
- "@stdlib/array-uint8": "^0.0.x",
45
- "@stdlib/assert-has-own-property": "^0.0.x",
46
- "@stdlib/assert-is-boolean": "^0.0.x",
47
- "@stdlib/assert-is-error": "^0.0.x",
48
- "@stdlib/assert-is-integer": "^0.0.x",
49
- "@stdlib/assert-is-nonnegative-integer": "^0.0.x",
50
- "@stdlib/assert-is-nonnegative-number": "^0.0.x",
51
- "@stdlib/assert-is-number": "^0.0.x",
52
- "@stdlib/assert-is-plain-object": "^0.0.x",
53
- "@stdlib/assert-is-positive-integer": "^0.0.x",
54
- "@stdlib/assert-is-positive-number": "^0.0.x",
55
- "@stdlib/assert-is-string": "^0.0.x",
56
- "@stdlib/assert-is-uint8array": "^0.0.x",
57
- "@stdlib/blas-base-gcopy": "^0.0.x",
58
- "@stdlib/buffer-from-array": "^0.0.x",
59
- "@stdlib/buffer-from-string": "^0.0.x",
60
- "@stdlib/cli-ctor": "^0.0.x",
61
- "@stdlib/fs-read-file": "^0.0.x",
62
- "@stdlib/fs-write-file": "^0.0.x",
63
- "@stdlib/math-base-assert-is-nan": "^0.0.x",
64
- "@stdlib/process-cwd": "^0.0.x",
65
- "@stdlib/random-base-normal": "^0.0.x",
66
- "@stdlib/streams-node-stdout": "^0.0.x",
67
- "@stdlib/types": "^0.0.x",
68
- "@stdlib/utils-copy": "^0.0.x",
69
- "@stdlib/utils-define-nonenumerable-property": "^0.0.x",
70
- "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x",
71
- "@stdlib/utils-define-read-only-accessor": "^0.0.x",
72
- "@stdlib/utils-define-read-write-accessor": "^0.0.x",
73
- "@stdlib/utils-inherit": "^0.0.x",
74
- "@stdlib/utils-next-tick": "^0.0.x",
40
+ "@stdlib/assert-has-own-property": "^0.1.0",
41
+ "@stdlib/assert-is-boolean": "^0.1.0",
42
+ "@stdlib/assert-is-error": "^0.1.0",
43
+ "@stdlib/assert-is-nonnegative-integer": "^0.1.0",
44
+ "@stdlib/assert-is-nonnegative-number": "^0.1.0",
45
+ "@stdlib/assert-is-number": "^0.1.0",
46
+ "@stdlib/assert-is-plain-object": "^0.1.0",
47
+ "@stdlib/assert-is-positive-integer": "^0.1.0",
48
+ "@stdlib/assert-is-positive-number": "^0.1.0",
49
+ "@stdlib/assert-is-string": "^0.1.0",
50
+ "@stdlib/buffer-from-string": "^0.1.0",
51
+ "@stdlib/math-base-assert-is-nan": "^0.1.0",
52
+ "@stdlib/object-assign": "^0.1.0",
53
+ "@stdlib/random-base-normal": "^0.1.0",
54
+ "@stdlib/string-format": "^0.1.0",
55
+ "@stdlib/utils-define-nonenumerable-property": "^0.1.0",
56
+ "@stdlib/utils-define-nonenumerable-read-only-property": "^0.1.0",
57
+ "@stdlib/utils-define-read-only-accessor": "^0.1.0",
58
+ "@stdlib/utils-define-read-write-accessor": "^0.1.0",
59
+ "@stdlib/utils-inherit": "^0.1.0",
60
+ "@stdlib/utils-next-tick": "^0.1.0",
75
61
  "debug": "^2.6.9",
76
62
  "readable-stream": "^2.1.4"
77
63
  },
78
64
  "devDependencies": {
79
- "@stdlib/assert-is-browser": "^0.0.x",
80
- "@stdlib/assert-is-buffer": "^0.0.x",
81
- "@stdlib/assert-is-uint32array": "^0.0.x",
82
- "@stdlib/assert-is-windows": "^0.0.x",
83
- "@stdlib/bench": "^0.0.x",
84
- "@stdlib/constants-uint32-max": "^0.0.x",
85
- "@stdlib/fs-exists": "^0.0.x",
86
- "@stdlib/fs-unlink": "^0.0.x",
87
- "@stdlib/process-exec-path": "^0.0.x",
88
- "@stdlib/random-base-minstd": "^0.0.x",
89
- "@stdlib/streams-node-inspect-sink": "^0.0.x",
90
- "@stdlib/string-replace": "^0.0.x",
91
- "@stdlib/time-now": "^0.0.x",
65
+ "@stdlib/streams-node-inspect-sink": "^0.1.0",
92
66
  "tape": "git+https://github.com/kgryte/tape.git#fix/globby",
93
67
  "proxyquire": "^2.0.0",
94
68
  "istanbul": "^0.4.1",
95
- "tap-spec": "5.x.x"
69
+ "tap-min": "git+https://github.com/Planeshifter/tap-min.git"
96
70
  },
97
71
  "engines": {
98
72
  "node": ">=0.10.0",
@@ -130,7 +104,7 @@
130
104
  "seedable"
131
105
  ],
132
106
  "funding": {
133
- "type": "patreon",
134
- "url": "https://www.patreon.com/athan"
107
+ "type": "opencollective",
108
+ "url": "https://opencollective.com/stdlib"
135
109
  }
136
110
  }
package/bin/cli DELETED
@@ -1,194 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * @license Apache-2.0
5
- *
6
- * Copyright (c) 2018 The Stdlib Authors.
7
- *
8
- * Licensed under the Apache License, Version 2.0 (the "License");
9
- * you may not use this file except in compliance with the License.
10
- * You may obtain a copy of the License at
11
- *
12
- * http://www.apache.org/licenses/LICENSE-2.0
13
- *
14
- * Unless required by applicable law or agreed to in writing, software
15
- * distributed under the License is distributed on an "AS IS" BASIS,
16
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
- * See the License for the specific language governing permissions and
18
- * limitations under the License.
19
- */
20
-
21
- 'use strict';
22
-
23
- // MODULES //
24
-
25
- var proc = require( 'process' );
26
- var resolve = require( 'path' ).resolve;
27
- var readFileSync = require( '@stdlib/fs-read-file' ).sync;
28
- var writeFileSync = require( '@stdlib/fs-write-file' ).sync;
29
- var CLI = require( '@stdlib/cli-ctor' );
30
- var stdout = require( '@stdlib/streams-node-stdout' );
31
- var cwd = require( '@stdlib/process-cwd' );
32
- var Uint8Array = require( '@stdlib/array-uint8' );
33
- var Uint32Array = require( '@stdlib/array-uint32' );
34
- var isUint8Array = require( '@stdlib/assert-is-uint8array' );
35
- var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive;
36
- var gcopy = require( '@stdlib/blas-base-gcopy' );
37
- var array2buffer = require( '@stdlib/buffer-from-array' );
38
- var randomStream = require( './../lib' );
39
-
40
-
41
- // FUNCTIONS //
42
-
43
- /**
44
- * Callback invoked once a source stream ends.
45
- *
46
- * @private
47
- */
48
- function onEnd() {
49
- // Append a trailing newline in accordance with standard POSIX behavior:
50
- console.log( '' ); // eslint-disable-line no-console
51
- }
52
-
53
- /**
54
- * Attempts to load a PRNG state.
55
- *
56
- * @private
57
- * @param {string} filepath - absolute path to file containing PRNG state
58
- * @returns {(Uint32Array|Error)} PRNG state or an error
59
- */
60
- function loadState( filepath ) {
61
- var state;
62
- var len;
63
-
64
- state = readFileSync( filepath );
65
- if ( state instanceof Error ) {
66
- return state;
67
- }
68
- len = state.length;
69
-
70
- // For older Node.js environments, convert the `Buffer` to a `Uint8Array`...
71
- if ( !isUint8Array( state ) ) {
72
- state = gcopy( len, state, 1, new Uint8Array( len ), 1 );
73
- }
74
- // Create a PRNG state array "view":
75
- len /= Uint32Array.BYTES_PER_ELEMENT;
76
- if ( !isInteger( len ) ) {
77
- return new RangeError( 'invalid option. `state` has an invalid length.' );
78
- }
79
- return new Uint32Array( state.buffer, state.byteOffset, len );
80
- }
81
-
82
-
83
- // MAIN //
84
-
85
- /**
86
- * Main execution sequence.
87
- *
88
- * @private
89
- * @returns {void}
90
- */
91
- function main() {
92
- var stream;
93
- var flags;
94
- var opts;
95
- var args;
96
- var cli;
97
- var err;
98
- var dir;
99
- var i;
100
-
101
- // Create a command-line interface:
102
- cli = new CLI({
103
- 'pkg': require( './../package.json' ),
104
- 'options': require( './../etc/cli_opts.json' ),
105
- 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
106
- 'encoding': 'utf8'
107
- })
108
- });
109
-
110
- // Get any provided command-line options:
111
- flags = cli.flags();
112
- if ( flags.help || flags.version ) {
113
- return;
114
- }
115
-
116
- // Get the current working directory:
117
- dir = cwd();
118
-
119
- // Get any provided command-line arguments:
120
- args = cli.args();
121
- if ( args.length < 2 ) {
122
- err = new Error( 'insufficient arguments. Must provide distribution parameters.' );
123
- return onError( err );
124
- }
125
- args[ 0 ] = parseFloat( args[ 0 ] );
126
- args[ 1 ] = parseFloat( args[ 1 ] );
127
-
128
- opts = {};
129
- if ( flags.iter ) {
130
- opts.iter = parseInt( flags.iter, 10 );
131
- }
132
- if ( flags.sep ) {
133
- opts.sep = flags.sep;
134
- }
135
- if ( flags.state ) {
136
- opts.state = loadState( resolve( dir, flags.state ) );
137
- if ( opts.state instanceof Error ) {
138
- return onError( opts.state );
139
- }
140
- } else if ( flags.seed ) {
141
- opts.seed = flags.seed.split( ',' );
142
- for ( i = 0; i < opts.seed.length; i++ ) {
143
- opts.seed[ i ] = parseInt( opts.seed[ i ], 10 );
144
- }
145
- }
146
- if ( flags.snapshot ) {
147
- proc.on( 'exit', onExit );
148
- }
149
-
150
- // Create a source stream and pipe to `stdout`:
151
- stream = randomStream( args[ 0 ], args[ 1 ], opts );
152
- stream.on( 'end', onEnd );
153
-
154
- stream.pipe( stdout );
155
-
156
- /**
157
- * Callback invoked upon exiting the process.
158
- *
159
- * @private
160
- * @param {integer} code - exit code
161
- */
162
- function onExit( code ) {
163
- var state;
164
- var err;
165
-
166
- // Get the current PRNG state:
167
- state = stream.state;
168
-
169
- // Create a byte array "view":
170
- state = new Uint8Array( state.buffer, state.byteOffset, stream.byteLength ); // eslint-disable-line max-len
171
-
172
- // Convert the byte array to a `Buffer` (with support for older Node.js environments):
173
- state = array2buffer( state );
174
-
175
- // Attempt to write the state to file:
176
- err = writeFileSync( resolve( dir, flags.snapshot ), state );
177
- if ( err ) {
178
- onError( err, code || 1 );
179
- }
180
- }
181
-
182
- /**
183
- * Callback invoked upon encountering an error.
184
- *
185
- * @private
186
- * @param {Error} error - error
187
- * @param {integer} [code] - exit code
188
- */
189
- function onError( error, code ) {
190
- cli.error( error, code || 1 );
191
- }
192
- }
193
-
194
- main();