@rahulv.dev/rng
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -1,1 +0,17 @@ | ||
"use strict";var w=t=>{throw TypeError(t)};var y=(t,e,n)=>e.has(t)||w("Cannot "+n);var a=(t,e,n)=>(y(t,e,"read from private field"),n?n.call(t):e.get(t)),d=(t,e,n)=>e.has(t)?w("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(t):e.set(t,n),s=(t,e,n,r)=>(y(t,e,"write to private field"),r?r.call(t,n):e.set(t,n),n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var i;const l=class l{constructor(){d(this,i,null)}static async init(){const e=new l;return await e.initializeCrypto(),e}async initializeCrypto(){if(typeof window<"u"&&window.crypto)s(this,i,window.crypto);else{const e=await Promise.resolve().then(()=>require("./__vite-browser-external-BcPniuRQ.js"));s(this,i,e.webcrypto)}}async next(e){if(!a(this,i))throw new Error("Rng not initialized. Use RNG.init() to initialize.");return this.generateSecureRandomLong(0,e)}async nextRange(e){return Promise.all(e.map(n=>this.next(n)))}generateSecureRandomLong(e,n){if(e>=n)throw new Error("minValue must be less than maxValue");const r=n-e,u=new Uint8Array(Math.ceil(Math.log2(r)/8));a(this,i).getRandomValues(u);let o=0;return u.forEach(h=>{o=(o<<8)+h}),e+o%r}};i=new WeakMap;let c=l;exports.RNG=c; | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./interfaces/IRNG"), exports); |
{ | ||
"name": "@rahulv.dev/rng", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A secure random number generator.", | ||
"main": "dist/index.js", | ||
"main": "dist/rng.node.js", | ||
"browser": "dist/rng.browser.js", | ||
"types": "types/index.d.ts", | ||
"exports": { | ||
".": { | ||
"import": "./dist/rng.node.js", | ||
"require": "./dist/rng.node.js", | ||
"types": "./types/rng.node.d.ts" | ||
}, | ||
"./browser": { | ||
"import": "./dist/rng.browser.js", | ||
"require": "./dist/rng.browser.js", | ||
"types": "./types/rng.browser.d.ts" | ||
} | ||
}, | ||
"scripts": { | ||
"build": "tsc && vite build", | ||
"dev": "vite", | ||
"build": "tsc", | ||
"test": "vitest --run" | ||
@@ -11,0 +23,0 @@ }, |
@@ -35,3 +35,7 @@ # @rahulv.dev/rng | ||
```typescript | ||
// For Node environment | ||
import { RNG } from "@rahulv.dev/rng"; | ||
// For browser environment | ||
import { RNG } from "@rahulv.dev/rng/browser"; | ||
``` | ||
@@ -44,4 +48,4 @@ | ||
```typescript | ||
async function setupRng() { | ||
const rng = await RNG.init(); | ||
function setupRng() { | ||
const rng = new RNG(); | ||
return rng; | ||
@@ -56,5 +60,5 @@ } | ||
```typescript | ||
async function generateRandom() { | ||
const rng = await setupRng(); | ||
const randomNumber = await rng.next(100); // Generates a random number between 0 and 99 | ||
function generateRandom() { | ||
const rng = setupRng(); | ||
const randomNumber = rng.next(100); // Generates a random number between 0 and 99 | ||
console.log(randomNumber); | ||
@@ -71,5 +75,5 @@ } | ||
```typescript | ||
async function generateMultipleRandoms() { | ||
const rng = await setupRng(); | ||
const randomNumbers = await rng.nextRange([10, 100, 1000]); | ||
function generateMultipleRandoms() { | ||
const rng = setupRng(); | ||
const randomNumbers = rng.nextRange([10, 100, 1000]); | ||
console.log(randomNumbers); // e.g., [2, 59, 874] | ||
@@ -83,7 +87,6 @@ } | ||
Here’s a detailed look at the API methods provided by `@rahulv.dev/rng`: | ||
Here’s a detailed look at the API methods provided by `@rahulv.dev/rng` and `@rahulv.dev/rng/browser`: | ||
| Method | Description | Parameters | Returns | | ||
| ------------------- | ------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- | | ||
| `RNG.init()` | Static method to initialize the RNG. Returns an instance of `RNG`. | None | `Promise<RNG>`: An initialized RNG | | ||
| `next(upperLimit)` | Generates a secure random number between `0` and `upperLimit - 1`. | `upperLimit: number`: The upper limit for the random number (exclusive) | `Promise<number>`: A random number within the specified range. | | ||
@@ -99,11 +102,11 @@ | `nextRange(ranges)` | Generates random numbers for an array of upper limits. | `ranges: number[]`: An array of upper limits. Each element specifies the exclusive upper limit for the corresponding random number. | `Promise<number[]>`: An array of random numbers, each within the range specified by the corresponding element in the input array. | | ||
async function main() { | ||
const rng = await RNG.init(); | ||
function main() { | ||
const rng = new RNG(); | ||
// Generating a single random number | ||
const randomNumber = await rng.next(100); | ||
const randomNumber = rng.next(100); | ||
console.log(`Random Number: ${randomNumber}`); | ||
// Generating multiple random numbers | ||
const randomNumbers = await rng.nextRange([10, 100, 1000]); | ||
const randomNumbers = rng.nextRange([10, 100, 1000]); | ||
console.log(`Random Numbers: ${randomNumbers.join(", ")}`); | ||
@@ -115,2 +118,4 @@ } | ||
**NOTE**: If you are unable to use package with vite, install [Vite Pollyfills](https://www.npmjs.com/package/vite-plugin-node-polyfills). | ||
## Contributing | ||
@@ -117,0 +122,0 @@ |
@@ -1,2 +0,1 @@ | ||
export * from "./RNG"; | ||
export * from "./interfaces/IRNG"; |
export interface IRNG { | ||
next(upperLimit: number): Promise<number>; | ||
nextRange(ranges: number[]): Promise<number[]>; | ||
next(upperLimit: number): number; | ||
nextRange(ranges: number[]): number[]; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
9902
13
98
0
121