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

check-digit-compute

v0.1.0

Published

Compute and validate check digit

Downloads

11

Readme

check-digit-compute

Compute and validate check digit

extending npm-version build coverage outdated-deps

language: English also available in: Spanish

Main goal

Check the validity of code that has a check digit (like book ISBN).

Compute the check digit of new codes.

const ISBN10: CheckDigitParameters<number> = {
    cast: Number,
    multipliers: [9,8,7,6,5,4,3,2,1],
    divider: 11,
    overflowMap: {'10':'X'}
};

var valid = checkdigit("0-86243-680-X", ISBN10);

The seccond parameter contains the definition of the check digit

CheckDigitParameters

  • cast: numeric constructor. Use Number for the general case. You can use BigInt for special cases. It must match with the class parameter (number or bigint).
  • multipliers: numeric secuence of digit multipliers starting from the less significative digit of the code.
  • divider: modulus (divisor of the final sum)
  • shift: shift of the result
  • turn: (boolean) indicates that the result must be substracted from the divisor
  • overflowMap: caracter map to obtain check digits over the 9. Used to obtain the "X" in ISBN.

digitcheckCompute(code, config)

var incomplete_ean = "123456789041"
var digit = digitcheckCompute(incomplete_ean, {
        cast: Number,
        multipliers: [3,1,3,1,3,1,3,1,3,1,3,1],
        turn: true,
        divider: 10,
    }
console.log(incomplete_ean + digit); // 1234567890418

checkQualityOfCodeList(string[], relative)

var codeList:string[] = await fs.readFile('codes.txt','utf8');

console.log(checkQualityOfCodeList(codeList, 100));

Computes de quality of a list of codes. Computes the % of probability to obtain an existing code when types other code an makes some error:

  • only one type error
  • two type error
  • inverting digits

computePrefixedCodeList(maxCodes, prefix, conf, startingSufix, allowLessCodes)

Generates a list of codes using a check digit conf. You must specify the maximum number of codes that you want to generate and a prefix (it can be "" to no prefix). You can also specify the initial number to generate and the last number to generate. If you not, 0 is the first and the last is 9999 (with many nines to complete the code). The number of digits to generate in each code will depend on the number of multipliers of the configuration. If you cannot generate as many codes as maxCodes you will get an error unless true is passed in the allowLesCodes parameter.

// GET 8000 labels starting with 1000 ensuring not grater than 9900
import { CheckDigitParameters, checkQualityOfCodeList, computePrefixedCodeList } from "../lib/check-digit-compute";
import { promises as fs } from "fs";

const CONF: CheckDigitParameters = {
    multipliers: [2,3,4,7],
    divider: 11
}

const FIRST_LABEL = 1000;
const LAST_LABEL = 9900;

async function getList(){
    var allList = computePrefixedCodeList(8000, "", CONF, FIRST_LABEL, LAST_LABEL);
    console.log("List computed");
    var writing = fs.writeFile("local-codes.txt", allList.join("\n")).then(_=>console.log("List saved."));
    console.log('Quality report:', checkQualityOfCodeList(allList, 100));
    await writing;
}

getList();

License

MIT