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

ocrgenerator

v1.0.8

Published

Swedish invoice no generator

Downloads

753

Readme

OCR generator

BuildCoverage Status

Swedish invoice number generator based on modulus 10.

Introduction

Swedish banks can take an invoice number that is validated against four algorithms, all based on modulus 10 or Luhn. The purpose of this project is to generate such invoice numbers from almost any given string.

  • soft algorithm: invalid control digit is accepted
  • hard algorithm: invalid control digit is unacceptable
  • variable length algorithm: invalid control digit is unacceptable and second to last digit is the length control digit and must match total length % 10 of invoice number. The length control digit is also included in the modulus 10 calculation
  • fixed length algorithm: up to two lengths are agreed with the bank and must be matched by the invoice number and an invalid control digit is unacceptable

Api

Functions:

  • generate(from[, options]): generate invoice number with length control and checksum digits
  • soft(from): same as generate without options
  • hard(from): same as generate without options
  • fixed(from, fixedLength): generate with fixed length, padded with preceeding zeros if too short and capped from left if too long
  • calculateChecksumReversed(ocr[, options]): calculate checksum from right
  • validate(ocr[, options]): validate ocr according to modulus 10
  • validateHard(ocr): validate against hard algorithm, invalid control digit is unacceptable
  • validateSoft(ocr): same as validateHard, invalid control digit is unacceptable, i.e. a falsy result is accepted
  • validateVariableLength(ocr): controls checksum and length control
  • validateFixedLength(ocr, length1[, length2]): validate fixed length, takes ocr and one length, and one optional length, either must match

Properties:

  • MIN_LENGTH: 2
  • MAX_LENGTH: 25

The above tresholds - MIN_LENGTH and MAX_LENGTH - is the expected invoice number length range for bankgirot, for plusgirot it is 5 and 15.

generate(from[, options])

Generate invoice number with length control and checksum.

Arguments:

  • from: any given string, e.g. customer number + date + amount
  • options: optional options
    • minLength: defaults to MIN_LENGTH
    • maxLength: defaults to MAX_LENGTH

Returns:

  • numbers: the actual generated invoice number
  • lengthControl: length control digit
  • control: control digit
  • length: length
  • sum: checksum
  • error_code: occasional error
    • ERR_OCR_OUT_OF_RANGE: OCR reference length was out of range, i.e. < minLength or > maxLength
  • message: occasional error message

Example:

import { generate } from 'ocrgenerator';

const invoiceNo = generate('Customer007:Date2019-12-24:Amount$200');
console.log(invoiceNo); // {numbers: '0072019122420063'}

Modulus 10 reversed:

Customer007:Date2019-12-24:Amount$200
        007     2019 12 24        200 l = 16 % 10 = 6
*       212     1212 12 12        121 2
----------------------------------------------
        001     2011 14 28        200 1
+       004     0008 00 00        000 2
----------------------------------------------
sum     005     2009 14 28        200 3 = 37

The length is the expected total number of digits in the invoice number, i.e. add one for length control and one for control digit. total invoice number length: n = 14 + 1 + 1 = 16 length control: l = n % 10 = 6

Since the algoritm is in reversed, the length control is at first position - index 0, so times 2:

reference control digit: c = 10 - sum % 10 = 10 - 37 % 10 = 10 - 7 = 3

Invoice number: '00720191224200' + l + c = '0072019122420063'

validate(ocr[, options])

Validate ocr according to modulus 10 and return object describing what went wrong if invalid.

Arguments:

  • ocr: invoice number
  • options: optional options
    • minLength: defaults to MIN_LENGTH
    • maxLength: defaults to MAX_LENGTH

Returns:

  • valid: boolean indicating that the modulus 10 check was successfull
  • sum: checksum
  • control: expected control digit
  • error_code: occasional error
    • ERR_OCR_INVALID_CHAR: a character has sneeked into ocr
    • ERR_OCR_OUT_OF_RANGE: OCR reference was out of range, i.e. < 2 or > 25
  • message: occasional error message

Example:

import { validate } from 'ocrgenerator';

console.log(validate('0072019122420063')); // true

console.log(validate('0072019122420063' + '7')); // false

console.log(validate('0072019122420063' + 'A')); // sneaky character

console.log(validate('0072019122420063', { maxLength: 15 }));

References

Validate your invoice number at bankgirot