@stdlib/random-streams-improved-ziggurat 0.0.7 → 0.2.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/LICENSE +0 -304
- package/NOTICE +1 -1
- package/README.md +36 -89
- package/SECURITY.md +5 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +7 -0
- package/docs/types/index.d.ts +16 -16
- package/lib/factory.js +8 -2
- package/lib/index.js +4 -4
- package/lib/main.js +2 -2
- package/lib/object_mode.js +4 -3
- package/lib/validate.js +9 -8
- package/package.json +23 -51
- package/bin/cli +0 -183
- package/docs/repl.txt +0 -239
- package/docs/types/test.ts +0 -103
- package/docs/usage.txt +0 -15
- package/etc/cli_opts.json +0 -24
package/docs/types/index.d.ts
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
* limitations under the License.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
// TypeScript Version:
|
|
19
|
+
// TypeScript Version: 4.1
|
|
20
20
|
|
|
21
21
|
/// <reference types="node"/>
|
|
22
22
|
/// <reference types="@stdlib/types"/>
|
|
@@ -92,7 +92,7 @@ declare class RandomStream extends Readable {
|
|
|
92
92
|
* @returns stream instance
|
|
93
93
|
*
|
|
94
94
|
* @example
|
|
95
|
-
* var inspectStream = require(
|
|
95
|
+
* var inspectStream = require( '@stdlib/streams-node-inspect-sink' );
|
|
96
96
|
*
|
|
97
97
|
* function log( chunk ) {
|
|
98
98
|
* console.log( chunk.toString() );
|
|
@@ -111,37 +111,37 @@ declare class RandomStream extends Readable {
|
|
|
111
111
|
/**
|
|
112
112
|
* Destruction state.
|
|
113
113
|
*/
|
|
114
|
-
private readonly _destroyed: boolean;
|
|
114
|
+
private readonly _destroyed: boolean;
|
|
115
115
|
|
|
116
116
|
/**
|
|
117
117
|
* Flag indicating whether a stream is operating in object mode.
|
|
118
118
|
*/
|
|
119
|
-
private readonly _objectMode: boolean;
|
|
119
|
+
private readonly _objectMode: boolean;
|
|
120
120
|
|
|
121
121
|
/**
|
|
122
122
|
* Data separator.
|
|
123
123
|
*/
|
|
124
|
-
private readonly _sep: string;
|
|
124
|
+
private readonly _sep: string;
|
|
125
125
|
|
|
126
126
|
/**
|
|
127
127
|
* Total number of iterations.
|
|
128
128
|
*/
|
|
129
|
-
private readonly _iter: number;
|
|
129
|
+
private readonly _iter: number;
|
|
130
130
|
|
|
131
131
|
/**
|
|
132
132
|
* Number of iterations after which to emit the underlying PRNG state.
|
|
133
133
|
*/
|
|
134
|
-
private readonly _siter: number;
|
|
134
|
+
private readonly _siter: number;
|
|
135
135
|
|
|
136
136
|
/**
|
|
137
137
|
* Iteration counter.
|
|
138
138
|
*/
|
|
139
|
-
private _i: number;
|
|
139
|
+
private _i: number;
|
|
140
140
|
|
|
141
141
|
/**
|
|
142
142
|
* Pseudorandom number generator for generating standard normally distributed pseudorandom numbers.
|
|
143
143
|
*/
|
|
144
|
-
private readonly _prng: random.PRNG;
|
|
144
|
+
private readonly _prng: random.PRNG;
|
|
145
145
|
|
|
146
146
|
/**
|
|
147
147
|
* Underlying PRNG.
|
|
@@ -178,7 +178,7 @@ declare class RandomStream extends Readable {
|
|
|
178
178
|
*
|
|
179
179
|
* @param size - number (of bytes) to read
|
|
180
180
|
*/
|
|
181
|
-
_read( size: number ): void;
|
|
181
|
+
_read( size: number ): void;
|
|
182
182
|
|
|
183
183
|
/**
|
|
184
184
|
* Gracefully destroys a stream, providing backward compatibility.
|
|
@@ -219,7 +219,7 @@ interface Constructor {
|
|
|
219
219
|
* @returns Stream instance
|
|
220
220
|
*
|
|
221
221
|
* @example
|
|
222
|
-
* var inspectStream = require(
|
|
222
|
+
* var inspectStream = require( '@stdlib/streams-node-inspect-sink' );
|
|
223
223
|
*
|
|
224
224
|
* function log( chunk ) {
|
|
225
225
|
* console.log( chunk.toString() );
|
|
@@ -255,7 +255,7 @@ interface Constructor {
|
|
|
255
255
|
* @returns Stream instance
|
|
256
256
|
*
|
|
257
257
|
* @example
|
|
258
|
-
* var inspectStream = require(
|
|
258
|
+
* var inspectStream = require( '@stdlib/streams-node-inspect-sink' );
|
|
259
259
|
*
|
|
260
260
|
* function log( chunk ) {
|
|
261
261
|
* console.log( chunk.toString() );
|
|
@@ -323,7 +323,7 @@ interface Constructor {
|
|
|
323
323
|
* @returns Stream instance
|
|
324
324
|
*
|
|
325
325
|
* @example
|
|
326
|
-
* var inspectStream = require(
|
|
326
|
+
* var inspectStream = require( '@stdlib/streams-node-inspect-sink' );
|
|
327
327
|
*
|
|
328
328
|
* function log( v ) {
|
|
329
329
|
* console.log( v );
|
|
@@ -349,7 +349,7 @@ interface Constructor {
|
|
|
349
349
|
* @returns stream instance
|
|
350
350
|
*
|
|
351
351
|
* @example
|
|
352
|
-
* var inspectStream = require(
|
|
352
|
+
* var inspectStream = require( '@stdlib/streams-node-inspect-sink' );
|
|
353
353
|
*
|
|
354
354
|
* function log( chunk ) {
|
|
355
355
|
* console.log( chunk.toString() );
|
|
@@ -365,7 +365,7 @@ interface Constructor {
|
|
|
365
365
|
* stream.pipe( inspectStream( log ) );
|
|
366
366
|
*
|
|
367
367
|
* @example
|
|
368
|
-
* var inspectStream = require(
|
|
368
|
+
* var inspectStream = require( '@stdlib/streams-node-inspect-sink' );
|
|
369
369
|
*
|
|
370
370
|
* function log( chunk ) {
|
|
371
371
|
* console.log( chunk.toString() );
|
|
@@ -397,7 +397,7 @@ interface Constructor {
|
|
|
397
397
|
* }
|
|
398
398
|
*
|
|
399
399
|
* @example
|
|
400
|
-
* var inspectStream = require(
|
|
400
|
+
* var inspectStream = require( '@stdlib/streams-node-inspect-sink' );
|
|
401
401
|
*
|
|
402
402
|
* function log( v ) {
|
|
403
403
|
* console.log( v );
|
package/lib/factory.js
CHANGED
|
@@ -20,7 +20,9 @@
|
|
|
20
20
|
|
|
21
21
|
// MODULES //
|
|
22
22
|
|
|
23
|
-
var
|
|
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
|
|
|
@@ -40,6 +42,7 @@ var RandomStream = require( './main.js' );
|
|
|
40
42
|
* @param {PRNGStateMT19937} [options.state] - pseudorandom number generator state
|
|
41
43
|
* @param {boolean} [options.copy=true] - boolean indicating whether to copy a provided pseudorandom number generator state
|
|
42
44
|
* @param {PositiveInteger} [options.siter] - number of iterations after which to emit the PRNG state
|
|
45
|
+
* @throws {TypeError} options argument must be an object
|
|
43
46
|
* @returns {Function} stream factory
|
|
44
47
|
*
|
|
45
48
|
* @example
|
|
@@ -62,7 +65,10 @@ var RandomStream = require( './main.js' );
|
|
|
62
65
|
function factory( options ) {
|
|
63
66
|
var opts;
|
|
64
67
|
if ( arguments.length > 0 ) {
|
|
65
|
-
|
|
68
|
+
if ( !isPlainObject( options ) ) {
|
|
69
|
+
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
|
|
70
|
+
}
|
|
71
|
+
opts = assign( {}, options );
|
|
66
72
|
} else {
|
|
67
73
|
opts = {};
|
|
68
74
|
}
|
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
|
|
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(
|
|
90
|
-
setReadOnly(
|
|
89
|
+
setReadOnly( main, 'objectMode', objectMode );
|
|
90
|
+
setReadOnly( main, 'factory', factory );
|
|
91
91
|
|
|
92
92
|
|
|
93
93
|
// EXPORTS //
|
|
94
94
|
|
|
95
|
-
module.exports =
|
|
95
|
+
module.exports = main;
|
package/lib/main.js
CHANGED
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
|
|
23
23
|
var Readable = require( 'readable-stream' ).Readable;
|
|
24
24
|
var isError = require( '@stdlib/assert-is-error' );
|
|
25
|
-
var
|
|
25
|
+
var assign = require( '@stdlib/object-assign' );
|
|
26
26
|
var inherit = require( '@stdlib/utils-inherit' );
|
|
27
27
|
var setNonEnumerable = require( '@stdlib/utils-define-nonenumerable-property' );
|
|
28
28
|
var setNonEnumerableReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
|
|
@@ -227,7 +227,7 @@ function RandomStream( options ) {
|
|
|
227
227
|
}
|
|
228
228
|
return new RandomStream();
|
|
229
229
|
}
|
|
230
|
-
opts =
|
|
230
|
+
opts = assign( {}, DEFAULTS );
|
|
231
231
|
if ( arguments.length > 0 ) {
|
|
232
232
|
err = validate( opts, options );
|
|
233
233
|
if ( err ) {
|
package/lib/object_mode.js
CHANGED
|
@@ -21,7 +21,8 @@
|
|
|
21
21
|
// MODULES //
|
|
22
22
|
|
|
23
23
|
var isObject = require( '@stdlib/assert-is-plain-object' );
|
|
24
|
-
var
|
|
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
|
|
|
@@ -64,9 +65,9 @@ function objectMode( options ) {
|
|
|
64
65
|
if ( arguments.length > 0 ) {
|
|
65
66
|
opts = options;
|
|
66
67
|
if ( !isObject( opts ) ) {
|
|
67
|
-
throw new TypeError( 'invalid argument. Options must be an object. Value:
|
|
68
|
+
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', opts ) );
|
|
68
69
|
}
|
|
69
|
-
opts =
|
|
70
|
+
opts = assign( {}, options );
|
|
70
71
|
} else {
|
|
71
72
|
opts = {};
|
|
72
73
|
}
|
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:
|
|
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. `
|
|
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. `
|
|
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. `
|
|
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. `
|
|
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. `
|
|
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. `
|
|
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-improved-ziggurat",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Create a readable stream for generating pseudorandom numbers drawn from a standard normal distribution using the Improved Ziggurat algorithm.",
|
|
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-improved-ziggurat": "./bin/cli"
|
|
18
|
-
},
|
|
19
16
|
"main": "./lib",
|
|
20
17
|
"directories": {
|
|
21
18
|
"benchmark": "./benchmark",
|
|
@@ -40,58 +37,33 @@
|
|
|
40
37
|
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
41
38
|
},
|
|
42
39
|
"dependencies": {
|
|
43
|
-
"@stdlib/
|
|
44
|
-
"@stdlib/
|
|
45
|
-
"@stdlib/assert-
|
|
46
|
-
"@stdlib/assert-is-
|
|
47
|
-
"@stdlib/assert-is-
|
|
48
|
-
"@stdlib/assert-is-
|
|
49
|
-
"@stdlib/assert-is-
|
|
50
|
-
"@stdlib/assert-is-
|
|
51
|
-
"@stdlib/
|
|
52
|
-
"@stdlib/
|
|
53
|
-
"@stdlib/
|
|
54
|
-
"@stdlib/
|
|
55
|
-
"@stdlib/
|
|
56
|
-
"@stdlib/
|
|
57
|
-
"@stdlib/
|
|
58
|
-
"@stdlib/
|
|
59
|
-
"@stdlib/
|
|
60
|
-
"@stdlib/
|
|
61
|
-
"@stdlib/process-cwd": "^0.0.x",
|
|
62
|
-
"@stdlib/random-base-improved-ziggurat": "^0.0.x",
|
|
63
|
-
"@stdlib/streams-node-stdout": "^0.0.x",
|
|
64
|
-
"@stdlib/types": "^0.0.x",
|
|
65
|
-
"@stdlib/utils-copy": "^0.0.x",
|
|
66
|
-
"@stdlib/utils-define-nonenumerable-property": "^0.0.x",
|
|
67
|
-
"@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x",
|
|
68
|
-
"@stdlib/utils-define-read-only-accessor": "^0.0.x",
|
|
69
|
-
"@stdlib/utils-define-read-write-accessor": "^0.0.x",
|
|
70
|
-
"@stdlib/utils-inherit": "^0.0.x",
|
|
71
|
-
"@stdlib/utils-next-tick": "^0.0.x",
|
|
40
|
+
"@stdlib/assert-has-own-property": "^0.2.0",
|
|
41
|
+
"@stdlib/assert-is-boolean": "^0.2.0",
|
|
42
|
+
"@stdlib/assert-is-error": "^0.2.0",
|
|
43
|
+
"@stdlib/assert-is-nonnegative-integer": "^0.2.0",
|
|
44
|
+
"@stdlib/assert-is-nonnegative-number": "^0.2.0",
|
|
45
|
+
"@stdlib/assert-is-plain-object": "^0.2.0",
|
|
46
|
+
"@stdlib/assert-is-positive-integer": "^0.2.0",
|
|
47
|
+
"@stdlib/assert-is-string": "^0.2.0",
|
|
48
|
+
"@stdlib/buffer-from-string": "^0.2.0",
|
|
49
|
+
"@stdlib/object-assign": "^0.2.0",
|
|
50
|
+
"@stdlib/random-base-improved-ziggurat": "^0.2.0",
|
|
51
|
+
"@stdlib/string-format": "^0.2.0",
|
|
52
|
+
"@stdlib/utils-define-nonenumerable-property": "^0.2.0",
|
|
53
|
+
"@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.0",
|
|
54
|
+
"@stdlib/utils-define-read-only-accessor": "^0.2.0",
|
|
55
|
+
"@stdlib/utils-define-read-write-accessor": "^0.2.0",
|
|
56
|
+
"@stdlib/utils-inherit": "^0.2.0",
|
|
57
|
+
"@stdlib/utils-next-tick": "^0.2.0",
|
|
72
58
|
"debug": "^2.6.9",
|
|
73
59
|
"readable-stream": "^2.1.4"
|
|
74
60
|
},
|
|
75
61
|
"devDependencies": {
|
|
76
|
-
"@stdlib/
|
|
77
|
-
"@stdlib/assert-is-buffer": "^0.0.x",
|
|
78
|
-
"@stdlib/assert-is-uint32array": "^0.0.x",
|
|
79
|
-
"@stdlib/assert-is-windows": "^0.0.x",
|
|
80
|
-
"@stdlib/bench": "^0.0.x",
|
|
81
|
-
"@stdlib/constants-uint32-max": "^0.0.x",
|
|
82
|
-
"@stdlib/fs-exists": "^0.0.x",
|
|
83
|
-
"@stdlib/fs-unlink": "^0.0.x",
|
|
84
|
-
"@stdlib/math-base-assert-is-nan": "^0.0.x",
|
|
85
|
-
"@stdlib/process-exec-path": "^0.0.x",
|
|
86
|
-
"@stdlib/random-base-arcsine": "^0.0.x",
|
|
87
|
-
"@stdlib/random-base-minstd": "^0.0.x",
|
|
88
|
-
"@stdlib/streams-node-inspect-sink": "^0.0.x",
|
|
89
|
-
"@stdlib/string-replace": "^0.0.x",
|
|
90
|
-
"@stdlib/time-now": "^0.0.x",
|
|
62
|
+
"@stdlib/streams-node-inspect-sink": "^0.2.0",
|
|
91
63
|
"tape": "git+https://github.com/kgryte/tape.git#fix/globby",
|
|
92
64
|
"proxyquire": "^2.0.0",
|
|
93
65
|
"istanbul": "^0.4.1",
|
|
94
|
-
"tap-
|
|
66
|
+
"tap-min": "git+https://github.com/Planeshifter/tap-min.git"
|
|
95
67
|
},
|
|
96
68
|
"engines": {
|
|
97
69
|
"node": ">=0.10.0",
|
|
@@ -135,7 +107,7 @@
|
|
|
135
107
|
"seedable"
|
|
136
108
|
],
|
|
137
109
|
"funding": {
|
|
138
|
-
"type": "
|
|
139
|
-
"url": "https://
|
|
110
|
+
"type": "opencollective",
|
|
111
|
+
"url": "https://opencollective.com/stdlib"
|
|
140
112
|
}
|
|
141
113
|
}
|
package/bin/cli
DELETED
|
@@ -1,183 +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 cli;
|
|
96
|
-
var dir;
|
|
97
|
-
var i;
|
|
98
|
-
|
|
99
|
-
// Create a command-line interface:
|
|
100
|
-
cli = new CLI({
|
|
101
|
-
'pkg': require( './../package.json' ),
|
|
102
|
-
'options': require( './../etc/cli_opts.json' ),
|
|
103
|
-
'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
|
|
104
|
-
'encoding': 'utf8'
|
|
105
|
-
})
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
// Get any provided command-line options:
|
|
109
|
-
flags = cli.flags();
|
|
110
|
-
if ( flags.help || flags.version ) {
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// Get the current working directory:
|
|
115
|
-
dir = cwd();
|
|
116
|
-
|
|
117
|
-
opts = {};
|
|
118
|
-
if ( flags.iter ) {
|
|
119
|
-
opts.iter = parseInt( flags.iter, 10 );
|
|
120
|
-
}
|
|
121
|
-
if ( flags.sep ) {
|
|
122
|
-
opts.sep = flags.sep;
|
|
123
|
-
}
|
|
124
|
-
if ( flags.state ) {
|
|
125
|
-
opts.state = loadState( resolve( dir, flags.state ) );
|
|
126
|
-
if ( opts.state instanceof Error ) {
|
|
127
|
-
return onError( opts.state );
|
|
128
|
-
}
|
|
129
|
-
} else if ( flags.seed ) {
|
|
130
|
-
opts.seed = flags.seed.split( ',' );
|
|
131
|
-
for ( i = 0; i < opts.seed.length; i++ ) {
|
|
132
|
-
opts.seed[ i ] = parseInt( opts.seed[ i ], 10 );
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
if ( flags.snapshot ) {
|
|
136
|
-
proc.on( 'exit', onExit );
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// Create a source stream and pipe to `stdout`:
|
|
140
|
-
stream = randomStream( opts );
|
|
141
|
-
stream.on( 'end', onEnd );
|
|
142
|
-
|
|
143
|
-
stream.pipe( stdout );
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Callback invoked upon exiting the process.
|
|
147
|
-
*
|
|
148
|
-
* @private
|
|
149
|
-
* @param {integer} code - exit code
|
|
150
|
-
*/
|
|
151
|
-
function onExit( code ) {
|
|
152
|
-
var state;
|
|
153
|
-
var err;
|
|
154
|
-
|
|
155
|
-
// Get the current PRNG state:
|
|
156
|
-
state = stream.state;
|
|
157
|
-
|
|
158
|
-
// Create a byte array "view":
|
|
159
|
-
state = new Uint8Array( state.buffer, state.byteOffset, stream.byteLength ); // eslint-disable-line max-len
|
|
160
|
-
|
|
161
|
-
// Convert the byte array to a `Buffer` (with support for older Node.js environments):
|
|
162
|
-
state = array2buffer( state );
|
|
163
|
-
|
|
164
|
-
// Attempt to write the state to file:
|
|
165
|
-
err = writeFileSync( resolve( dir, flags.snapshot ), state );
|
|
166
|
-
if ( err ) {
|
|
167
|
-
onError( err, code || 1 );
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Callback invoked upon encountering an error.
|
|
173
|
-
*
|
|
174
|
-
* @private
|
|
175
|
-
* @param {Error} error - error
|
|
176
|
-
* @param {integer} [code] - exit code
|
|
177
|
-
*/
|
|
178
|
-
function onError( error, code ) {
|
|
179
|
-
cli.error( error, code || 1 );
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
main();
|