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

norwegian-utils

v0.4.1

Published

Utilities used in our Norwegian applications.

Downloads

7,510

Readme

Utilities to format different string on Norwegian format

This library returns hard spaces \u00a0 for formatting, so there are no line breaks in the middle of phone numbers, account numbers or other data.

formatOrganizationNumber

Format a norwegian business entity number ("brreg").

Can also check if a number looks like a brreg number, but does not currently validate the checksum.

Examples

import { formatOrganizationNumber } from 'norwegian-utils/formatOrganizationNumber'
formatOrganizationNumber('995222183') === '995 222 183'

import { isValidOrganizationNumber } from 'norwegian-utils/formatOrganizationNumber'
isValidOrganizationNumber('995222183') === true
isValidOrganizationNumber('99522218') === false

formatPhoneNumber

Provides 5 methods:

  • isValidPhoneNumber - Is the phone number a valid phone number, somewhere in the world? Will be true for both 00123467890 style, +46123467890, or norwegian 8-digit numbers 12345678. Phone number can can be passed with spaces etc. and will still pass. This is not exhausitve validation, invalid phone numbers can pass, and valid one be rejected (like the emergency number 112).
  • isNorwegianPhoneNumber - Returns true if the number looks like a Norwegian number. Does not validate short / SMS numbers ie. 112 or 32200.
  • isNorwegianMobilePhoneNumber - Returns true if the number looks like a Norwegian mobile phone number (9xxxxxxx or 4xxxxxxx). Second parameter acceptM2M can also enable validation for M2M mobile phones (58xxxxxxxxxx or 59xxxxxx) (default value for acceptM2M parameter is false)
  • shortFormatPhoneNumber - Returns the phone number, with international prefix (so Norwegian 8-digit numbers are converted to the form +4712345678). This is appropriate to compare two phone numbers for example, or to store in databases. Removes any formatting like spaces, and always prefixes with +. If the format is invalid, this simply returns the original string.
  • prettyFormatPhoneNumber - Formats the phone number for output to the user, with spaces. Avoids +47 prefix for Norwegian numbers. For example 004712345678 is formatted as 12 34 56 78. Business numbers are formatted on the form 12 34 56 78. Personal numbers are formatted as 423 45 678. If the format is invalid, this simply returns the original string.

Examples

import { shortFormatPhoneNumber } from 'norwegian-utils/formatPhoneNumber'
shortFormatPhoneNumber('112') === '112'
shortFormatPhoneNumber('12345678') === '+4712345678'
shortFormatPhoneNumber('+44 12 34 56') === '+44123456'

import { prettyFormatPhoneNumber } from 'norwegian-utils/formatPhoneNumber'
prettyFormatPhoneNumber('12345678') === '12 34 56 78'
prettyFormatPhoneNumber('+44 12 34 56') === '+44 123456'
prettyFormatPhoneNumber('0048 781-296-647') === '+48 78 12 96 647'

import { isValidPhoneNUmber } from 'norwegian-utils/formatPhoneNumber'
isValidPhoneNUmber('112') === false
isValidPhoneNUmber('+44 12 34 56') === true
isValidPhoneNUmber('0048 781-296-647') === true

import { isNorwegianPhoneNumber } from 'norwegian-utils/formatPhoneNumber'
isNorwegianPhoneNumber('112') === false
shortFormatPhoneNumber('12345678') === true
shortFormatPhoneNumber('004712345678') === true
isNorwegianPhoneNumber('+44 12 34 56') === false
isNorwegianPhoneNumber('0048 781-296-647') === false

formatAccountNumber

Formats a Norwegian 11-digit bank account number on the standard form (4, 2, 5 digits). Numbers that are not 11 digits are formatted by "best effort".

Examples

import formatAccountNumber from 'norwegian-utils/formatAccountNumber'
formatAccountNumber('36242412345') === '3624 24 12345'
formatAccountNumber('362424123451') === '3624 241 23451'

validateAccountNumber

Validates norwegian account number to have 11 digits and checking the last control digit according to MOD11 algorithm. Accepts strings with spaces and dots, those are stripped before validation. Returns true if the number satisfies the constraints and false otherwise.

Examples

import validateAccountNumber from 'norwegian-utils/validateAccountNumber'
validateAccountNumber('1234.56.78903') //true
validateAccountNumber('1234.56.78909') //false
validateAccountNumber('1234 56 78903') //true
validateAccountNumber('123437q897e9w87qw89e75678903') //false

formatAmount

Formats a number with thousand separators and 2 decimals. This is mainly appropriate for displaying currency amounts.

Accepts a options object as the second argument, two options are possible:

  • alwaysNegative: true (default: false) - Always return a negative amount (to display a debt for example).
  • decimals: false (default true) - Return the number with 2 decimals.

Examples

import { formatAmount } from 'norwegian-utils/formatAmount'
formatAmount('1000') === '1,000.00'
formatAmount('1000', { decimals: false }) === '1,000'
formatAmount('1000', { alwayNegative: true }) === '-1,000.00'