Skip to content

Commit d383618

Browse files
committed
Add support for serializing a PRNG as JSON
1 parent 9717452 commit d383618

File tree

6 files changed

+113
-0
lines changed

6 files changed

+113
-0
lines changed

lib/node_modules/@stdlib/random/base/hypergeometric/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,15 @@ var sz = hypergeometric.byteLength;
251251
// returns <number>
252252
```
253253

254+
#### hypergeometric.toJSON()
255+
256+
Serializes the pseudorandom number generator as a JSON object.
257+
258+
```javascript
259+
var o = hypergeometric.toJSON();
260+
// returns { 'type': 'PRNG', 'name': '...', 'state': {...} }
261+
```
262+
254263
</section>
255264

256265
<!-- /.usage -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 bench = require( '@stdlib/bench' );
24+
var isObject = require( '@stdlib/assert/is-plain-object' );
25+
var pkg = require( './../package.json' ).name;
26+
var rand = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg+':toJSON', function benchmark( b ) {
32+
var o;
33+
var i;
34+
35+
b.tic();
36+
for ( i = 0; i < b.iterations; i++ ) {
37+
o = rand.toJSON();
38+
if ( typeof o !== 'object' ) {
39+
b.fail( 'should return an object' );
40+
}
41+
}
42+
b.toc();
43+
if ( !isObject( o ) ) {
44+
b.fail( 'should return an object' );
45+
}
46+
b.pass( 'benchmark finished' );
47+
b.end();
48+
});

lib/node_modules/@stdlib/random/base/hypergeometric/docs/repl.txt

+14
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,20 @@
165165
--------
166166
> var sz = {{alias}}.byteLength;
167167

168+
169+
{{alias}}.toJSON()
170+
Serializes the pseudorandom number generator as a JSON object.
171+
172+
Returns
173+
-------
174+
out: Object
175+
JSON representation.
176+
177+
Examples
178+
--------
179+
> var o = {{alias}}.toJSON()
180+
{ 'type': 'PRNG', 'name': '...', 'state': {...} }
181+
168182
See Also
169183
--------
170184

lib/node_modules/@stdlib/random/base/hypergeometric/lib/factory.js

+21
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
var setReadOnly = require( '@stdlib/utils/define-read-only-property' );
2424
var setReadOnlyAccessor = require( '@stdlib/utils/define-read-only-accessor' );
2525
var setReadWriteAccessor = require( '@stdlib/utils/define-read-write-accessor' );
26+
var setNonEnumerableReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
2627
var isObject = require( '@stdlib/assert/is-plain-object' );
2728
var randu = require( '@stdlib/random/base/mt19937' ).factory;
2829
var isNonNegativeInteger = require( '@stdlib/math/base/assert/is-nonnegative-integer' );
2930
var PINF = require( '@stdlib/constants/math/float64-pinf' );
31+
var typedarray2json = require( '@stdlib/array/to-json' );
3032
var validate = require( './validate.js' );
3133
var hypergeometric0 = require( './hypergeometric.js' );
3234

@@ -115,6 +117,7 @@ function factory() {
115117
setReadWriteAccessor( prng, 'state', getState, setState );
116118
setReadOnlyAccessor( prng, 'stateLength', getStateLength );
117119
setReadOnlyAccessor( prng, 'byteLength', getStateSize );
120+
setNonEnumerableReadOnly( prng, 'toJSON', toJSON );
118121
setReadOnly( prng, 'PRNG', rand );
119122

120123
rand = rand.normalized;
@@ -183,6 +186,24 @@ function factory() {
183186
rand.state = s;
184187
}
185188

189+
/**
190+
* Serializes the pseudorandom number generator as a JSON object.
191+
*
192+
* ## Notes
193+
*
194+
* - `JSON.stringify()` implicitly calls this method when stringifying a PRNG.
195+
*
196+
* @private
197+
* @returns {Object} JSON representation
198+
*/
199+
function toJSON() {
200+
var out = {};
201+
out.type = 'PRNG';
202+
out.name = prng.NAME;
203+
out.state = typedarray2json( rand.state );
204+
return out;
205+
}
206+
186207
/**
187208
* Returns a random number drawn from a hypergeometric distribution with bound parameters.
188209
*

lib/node_modules/@stdlib/random/base/hypergeometric/test/test.factory.js

+16
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' );
2828
var isUint32Array = require( '@stdlib/assert/is-uint32array' );
2929
var UINT32_MAX = require( '@stdlib/constants/math/uint32-max' );
3030
var Uint32Array = require( '@stdlib/array/uint32' );
31+
var typedarray2json = require( '@stdlib/array/to-json' );
3132
var factory = require( './../lib/factory.js' );
3233

3334

@@ -519,6 +520,21 @@ tape( 'attached to the returned function is the generator state size', function
519520
t.end();
520521
});
521522

523+
tape( 'attached to the returned function is a method to serialize the generator as a JSON object', function test( t ) {
524+
var hypergeometric;
525+
var o;
526+
527+
hypergeometric = factory();
528+
t.equal( typeof hypergeometric.toJSON, 'function', 'has method' );
529+
530+
o = hypergeometric.toJSON();
531+
t.equal( o.type, 'PRNG', 'has property' );
532+
t.equal( o.name, hypergeometric.NAME, 'has property' );
533+
t.deepEqual( o.state, typedarray2json( hypergeometric.state ), 'has property' );
534+
535+
t.end();
536+
});
537+
522538
tape( 'when called without arguments, the function returns a PRNG that returns `NaN` when provided a population size equal to `NaN`', function test( t ) {
523539
var hypergeometric;
524540
var r;

lib/node_modules/@stdlib/random/base/hypergeometric/test/test.js

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ tape( 'attached to the main export is a method to generate pseudorandom number g
4141
t.end();
4242
});
4343

44+
tape( 'attached to the main export is a method to serialize a pseudorandom number generator as JSON', function test( t ) {
45+
t.equal( typeof hypergeometric.toJSON, 'function', 'has method' );
46+
t.end();
47+
});
48+
4449
tape( 'attached to the main export is the generator name', function test( t ) {
4550
t.equal( hypergeometric.NAME, 'hypergeometric', 'has property' );
4651
t.end();

0 commit comments

Comments
 (0)