Skip to content

Commit

Permalink
feat!: use built-in PRNG (#13)
Browse files Browse the repository at this point in the history
* feat!: replace `rand-seed` with a built-in PRNG

* docs: remove mention of Prando in `PRNG` TSDocs
  • Loading branch information
angeloashmore authored Dec 19, 2022
1 parent 1ef56ee commit a661c8b
Show file tree
Hide file tree
Showing 101 changed files with 4,931 additions and 4,324 deletions.
7,674 changes: 4,074 additions & 3,600 deletions package-lock.json

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
"src"
],
"scripts": {
"build": "siroc build",
"dev": "siroc build --watch",
"build": "vite build",
"dev": "vite build --watch",
"format": "prettier --write .",
"lint": "eslint --ext .js,.ts .",
"prepare": "npm run build",
Expand All @@ -57,28 +57,28 @@
"unit": "nyc --reporter=lcovonly --reporter=text --exclude-after-remap=false ava"
},
"dependencies": {
"@prismicio/types": "^0.2.0",
"change-case": "^4.1.2",
"rand-seed": "^1.0.1"
"@prismicio/types": "^0.2.4",
"change-case": "^4.1.2"
},
"devDependencies": {
"@size-limit/preset-small-lib": "^7.0.8",
"@typescript-eslint/eslint-plugin": "^5.30.5",
"@typescript-eslint/parser": "^5.30.5",
"ava": "^4.3.0",
"esbuild": "^0.14.48",
"esbuild-register": "^3.3.3",
"eslint": "^8.19.0",
"@size-limit/preset-small-lib": "^8.1.0",
"@typescript-eslint/eslint-plugin": "^5.46.1",
"@typescript-eslint/parser": "^5.46.1",
"ava": "^5.1.0",
"esbuild": "^0.16.9",
"esbuild-register": "^3.4.2",
"eslint": "^8.30.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-tsdoc": "^0.2.16",
"eslint-plugin-tsdoc": "^0.2.17",
"nyc": "^15.1.0",
"prettier": "^2.7.1",
"prettier-plugin-jsdoc": "^0.3.38",
"siroc": "^0.16.0",
"size-limit": "^7.0.8",
"prettier": "^2.8.1",
"prettier-plugin-jsdoc": "^0.4.2",
"size-limit": "^8.1.0",
"standard-version": "^9.5.0",
"typescript": "^4.7.4"
"typescript": "^4.9.4",
"vite": "^4.0.2",
"vite-plugin-sdk": "^0.1.0"
},
"engines": {
"node": ">=12.7.0"
Expand Down
9 changes: 0 additions & 9 deletions siroc.config.ts

This file was deleted.

88 changes: 88 additions & 0 deletions src/lib/PRNG.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* The following code is a modified version of Prando from `zeh/prando` on
* GitHub.
*
* Source:
* https://github.com/zeh/prando/blob/acc2a3c09df12a41b5c82fbf44e49e070e6f60ac/src/Prando.ts
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Zeh Fernando
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

const MIN = -2147483648; // Int32 min
const MAX = 2147483647; // Int32 max

const xorshift = (value: number): number => {
// Xorshift*32
// Based on George Marsaglia's work: http://www.jstatsoft.org/v08/i14/paper
value ^= value << 13;
value ^= value >> 17;
value ^= value << 5;

return value;
};

/**
* Pseudorandom number generator.
*/
export class PRNG {
private _value: number;

/**
* Generate a new pseudo-random number generator.
*
* @param seed - A number or string seed that determines which pseudo-random
* number sequence will be created.
*/
constructor(seed: number | string) {
if (typeof seed === "string") {
let hash = 0;
if (seed) {
const l = seed.length;
for (let i = 0; i < l; i++) {
hash = (hash << 5) - hash + seed.charCodeAt(i);
hash |= 0;
hash = xorshift(hash);
}
}

this._value = hash;
} else {
this._value = seed;
}

if (this._value === 0) {
this._value = 1;
}
}

/**
* Generates a pseudo-random number.
*
* @returns The generated pseudo-random number.
*/
public next(): number {
this._value = xorshift(this._value);

return (this._value - MIN) / (MAX - MIN);
}
}
15 changes: 7 additions & 8 deletions src/lib/createFaker.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Rand from "rand-seed";

import { Seed } from "../types";

import { lorem, loremWords } from "./lorem";
import { PRNG } from "./PRNG";

export const createFaker = (seed: Seed): Faker => {
return new Faker(seed);
Expand All @@ -15,20 +14,20 @@ const YEAR_2022_MS = 52 * (YEAR_MS + DAY_MS / 4);
export class Faker {
seed: Seed;

private rand: Rand;
private prng: PRNG;

constructor(seed: Seed) {
this.seed = seed;

this.rand = new Rand(seed.toString());
this.prng = new PRNG(seed.toString());
}

boolean(): boolean {
return this.random() >= 0.5;
random(): number {
return this.prng.next();
}

random(): number {
return this.rand.next();
boolean(): boolean {
return this.random() >= 0.5;
}

randomElement<T>(elements: readonly T[]): T {
Expand Down
3 changes: 1 addition & 2 deletions src/lib/lorem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ export const lorem = (length: `${number}w` | `${number}c`, wordOffset = 0) => {
) {
sentence += wordsString.substring(startSpaceIndex + 1) + " ";

const wraparoundCounts =
Math.floor(count / lorem.spaceIndices.length) - 1;
const wraparoundCounts = Math.floor(count / lorem.spaceIndices.length);
for (let i = 0; i < wraparoundCounts; i++) {
sentence += wordsString + " ";
}
Expand Down
22 changes: 11 additions & 11 deletions test/lib-faker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,37 @@ import { createFaker } from "../src/lib/createFaker";
test("random", (t) => {
const faker = createFaker(t.title);

t.is(faker.random(), 0.5526393386535347);
t.is(faker.random(), 0.41176102636381984);
t.is(faker.random(), 0.2936111260889124);
t.is(faker.random(), 0.03282859386709253);
});

test("randomElement", (t) => {
const faker = createFaker(t.title);

t.is(faker.randomElement([1, 2, 3]), 3);
t.is(faker.randomElement([1, 2, 3]), 1);
t.is(faker.randomElement([] as undefined[]), undefined);
});

test("range", (t) => {
const faker = createFaker(t.title);

t.is(faker.range(0, 100), 27);
t.is(faker.range(10, 20), 17);
t.is(faker.range(0, 100), 44);
t.is(faker.range(10, 20), 19);
});

test("rangeFloat", (t) => {
const faker = createFaker(t.title);

t.is(faker.rangeFloat(0, 100), 39.08171590883285);
t.is(faker.rangeFloat(10, 20), 16.042331447824836);
t.is(faker.rangeFloat(0, 100), 32.577370417438765);
t.is(faker.rangeFloat(10, 20), 11.09656356999105);
});

test("lorem", (t) => {
const faker = createFaker(t.title);

t.is(
faker.lorem("10w"),
"ut tellus elementum sagittis vitae et leo duis ut diam",
"orci porta non pulvinar neque laoreet suspendisse interdum consectetur libero",
);
t.is(
faker.lorem("10w", 0),
Expand All @@ -45,13 +45,13 @@ test("lorem", (t) => {
faker.lorem("10w", 1),
"ipsum dolor sit amet, consectetur adipiscing elit ut aliquam, purus",
);
t.is(faker.lorem("200000w").split(" ").length, 200000);
t.is(faker.lorem("20000w").split(" ").length, 20000);
t.is(faker.lorem("10c"), "lorem ipsu");
});

test("word", (t) => {
const faker = createFaker(t.title);

t.is(faker.word(), "lacus");
t.is(faker.word(), "vestibulum");
t.is(faker.word(), "amet,");
t.is(faker.word(), "diam");
});
24 changes: 12 additions & 12 deletions test/snapshots/api-ref.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ Generated by [AVA](https://avajs.dev).
[
{
id: 'd7db379cbcfad1aa',
id: 'f41c712a7ad5da69',
isMasterRef: false,
label: 'Bibendum',
ref: 'a3bc30c2274b5259',
label: 'Neque',
ref: '34a575aeb79b4bff',
},
{
id: 'd7db379cbcfad1aa',
id: 'f41c712a7ad5da69',
isMasterRef: false,
label: 'Bibendum',
ref: 'a3bc30c2274b5259',
label: 'Neque',
ref: '34a575aeb79b4bff',
},
]

Expand All @@ -29,15 +29,15 @@ Generated by [AVA](https://avajs.dev).
[
{
id: '47b8bf8a9b4dbd55',
id: '2dd98a8a9eba2cb2',
isMasterRef: false,
label: 'Nunc',
ref: 'e694ebb2adc3a99e',
label: 'Diam Phasellus',
ref: 'c3ced5a3606bede2',
},
{
id: '47b8bf8a9b4dbd55',
id: '2dd98a8a9eba2cb2',
isMasterRef: false,
label: 'Nunc',
ref: 'e694ebb2adc3a99e',
label: 'Diam Phasellus',
ref: 'c3ced5a3606bede2',
},
]
Binary file modified test/snapshots/api-ref.test.ts.snap
Binary file not shown.
Loading

0 comments on commit a661c8b

Please sign in to comment.