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

integrity-check

v1.3.6

Published

Runtime integrity checking for node.js programs and broswer javascript programs

Downloads

160

Readme

Integrity check

This library can be used in:

  • Node (plain javascript)
  • Node (typescript)
  • Browser (plain javascript)
  • Browser (typescript)

Use with Node

Use npm to install it:

cd into the root directory of yuor project, where the json.package file is, and do:

> npm install integrity-check --save

import it into your code

const { Integrity } = require('integrity-check')

function myFunc(a) {
    Integrity.checkIsValidNumber(a, "a is not a number, it was {}", a)
}

Use in browser

How to obtain the integrity-check-X-X-X.js file

You can use npm to fetch the integrity-check module and extract the script from there. Otherwise you can go to github and get the file:

https://github.com/ArtFab/integrity-js/tree/master/browser (the file will be called integrity-X-X-X.js)

If you use npm, create a folder and do

> npm install integrity-check

The relevant file can be found in the installed package. Look in yourfolder\node_modules\integrity-check\browser\integrity-check-X-X-X.js

How to use inside your browser side script

Copy this file to the area on your server where you serve up scripts. In your html file, at the following into the head section:

<html>
    <head>
        <!-- other stuff-->
        <script src="/pathtoyourscripts/integrity-check.js"></script>
    </head>
    <!-- other stuff-->
</html>

Then inside one of your JavaScript scripts you can do (for example):

function myFunc(a) {
    Integrity.checkIsValidNumber(a, "a is not a number, it was {}", a)
}

Integrity API

The full set of functions is:

    Integrity.check(condition, *msg)
    Integrity.checkNotNull(test, *msg)
    Integrity.checkIsBool(test, *msg)
    Integrity.checkIsBoolOrNull(test, *msg)
    Integrity.checkIsString(test, *msg)
    Integrity.checkIsStringOrNull(test, *msg)
    Integrity.checkStringNotNullOrEmpty(test, *msg)
    Integrity.checkIsValidNumber(test, *msg)
    Integrity.checkIsValidNumberOrNull(test, *msg)
    Integrity.checkIsFunction(test, *msg)
    Integrity.checkIsFunctionOrNull(test, *msg)

    Integrity.fail(*msg)

Exceptions

The following exceptions are thrown:

  • TypeError - when the test is not the right type (which may include null if not allowed)
  • ReferenceError - when test is undefined (and it is not allowed to be undefined)
  • Error - to indicate a check failed

Examples

// const Integrity = require('integrity-check'); // needed for node scripts, not browser side ones

function extractByAgeRange(students, ageMin, ageMax) {
    Integrity.checkNotNull(students);
    Integrity.checkIsValidNumber(ageMin);
    Integrity.checkIsValidNumber(ageMax);
    Integrity.check(ageMin <= ageMax, "ageMin must be <= ageMax, was {} and {}", ageMin, ageMax);

    const retArray = [];

    for (let student in students) {
        Integrity.checkNotNull(student);
        Integrity.checkNotNull(student.age, "Student item not a student? Missing age. {}", student);
        Integrity.checkIsValidNumber(student.age);

        if (student.age >= ageMin && student.age <= ageMax) {
            retArray.push(student);
        }
    }

    return retArray;
}