npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tslint-no-math-random

v1.0.1

Published

TSLint rule to disallow Math.random() usage

Downloads

7

Readme

TSLint no-math-random Rule

TSLint rule to disallow Math.random() usage.

JavaScripts's built in Math.random() function is not seed-able, not secure, and not really that random. So, how about we disable it completely in projects where using it could cause something bad.

Usage

Add this to your package's devDependencies, then in your tsconfig.json add the following lines:

{
    "extends": {
        "tslint-no-math-random"
    },
    "rules": {
        "no-math-random": true
    }
}

That's it! With this rule enabled any calls to Math.random() will result in a linter error.

The ban alternative

TSLint's built in rule ban can accomplish much of what this package allows.

Example:

{
    "rules": {
        "ban": [
            true,
            {
                "name": ["Math", "random"],
                "message": "Use a better PRNG."
            }
        ]
    }
}

The main upside to this is that you don't need this package; Hurrah! The only downside is that if you selectively disable the ban rule for a line/block of code, you've disabled all banned syntax which could hypothetically allow other errors.

Also, I think the rule name is clearer as no-math-random rather than ban; but to each their own.

However, in reality using ban is probably easier. I won't be offended.

Motivation

I made this rule for two simple reasons:

  1. To make sure developers do not use JS's built in Math.random when better solutions exist.
  2. Because I wanted to learn some under the hood stuff with how TS and TSLint work.

The only thing you probably care about is the first. But if you did not know, JS's built in Math.random() leaves lot to be desired (seeding), and is an easy trap to fall into if you want to get into cryptography.

If in your project you use some superior source of randomness via some PRNG package, then you can ban the usage of JS's built-in PRNG using this rule!

But mostly this exists for the second reason :P