Skip to content

Commit bc3a802

Browse files
committed
Add support for serializing a PRNG as JSON
1 parent d2bfa82 commit bc3a802

File tree

6 files changed

+113
-0
lines changed

6 files changed

+113
-0
lines changed

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

+9
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,15 @@ var sz = triangular.byteLength;
248248
// returns <number>
249249
```
250250

251+
#### triangular.toJSON()
252+
253+
Serializes the pseudorandom number generator as a JSON object.
254+
255+
```javascript
256+
var o = triangular.toJSON();
257+
// returns { 'type': 'PRNG', 'name': '...', 'state': {...} }
258+
```
259+
251260
</section>
252261

253262
<!-- /.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/triangular/docs/repl.txt

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

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

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

+21
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
30+
var typedarray2json = require( '@stdlib/array/to-json' );
2931
var validate = require( './validate.js' );
3032
var triangular0 = require( './triangular.js' );
3133

@@ -109,6 +111,7 @@ function factory() {
109111
setReadWriteAccessor( prng, 'state', getState, setState );
110112
setReadOnlyAccessor( prng, 'stateLength', getStateLength );
111113
setReadOnlyAccessor( prng, 'byteLength', getStateSize );
114+
setNonEnumerableReadOnly( prng, 'toJSON', toJSON );
112115
setReadOnly( prng, 'PRNG', rand );
113116

114117
rand = rand.normalized;
@@ -177,6 +180,24 @@ function factory() {
177180
rand.state = s;
178181
}
179182

183+
/**
184+
* Serializes the pseudorandom number generator as a JSON object.
185+
*
186+
* ## Notes
187+
*
188+
* - `JSON.stringify()` implicitly calls this method when stringifying a PRNG.
189+
*
190+
* @private
191+
* @returns {Object} JSON representation
192+
*/
193+
function toJSON() {
194+
var out = {};
195+
out.type = 'PRNG';
196+
out.name = prng.NAME;
197+
out.state = typedarray2json( rand.state );
198+
return out;
199+
}
200+
180201
/**
181202
* Returns a pseudorandom number drawn from a triangular distribution with bound parameters.
182203
*

lib/node_modules/@stdlib/random/base/triangular/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

@@ -489,6 +490,21 @@ tape( 'attached to the returned function is the generator state size', function
489490
t.end();
490491
});
491492

493+
tape( 'attached to the returned function is a method to serialize the generator as a JSON object', function test( t ) {
494+
var triangular;
495+
var o;
496+
497+
triangular = factory();
498+
t.equal( typeof triangular.toJSON, 'function', 'has method' );
499+
500+
o = triangular.toJSON();
501+
t.equal( o.type, 'PRNG', 'has property' );
502+
t.equal( o.name, triangular.NAME, 'has property' );
503+
t.deepEqual( o.state, typedarray2json( triangular.state ), 'has property' );
504+
505+
t.end();
506+
});
507+
492508
tape( 'when called without arguments, the function returns a function that returns `NaN` if `a` is `NaN`', function test( t ) {
493509
var triangular;
494510
var r;

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

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

41+
tape( 'attached to the main export is a method to serialize a pseudorandom number generator as JSON', function test( t ) {
42+
t.equal( typeof triangular.toJSON, 'function', 'has method' );
43+
t.end();
44+
});
45+
4146
tape( 'attached to the main export is the generator name', function test( t ) {
4247
t.equal( triangular.NAME, 'triangular', 'has property' );
4348
t.end();

0 commit comments

Comments
 (0)