Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/bright-adults-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@obosbbl/format": minor
---

initial release
39 changes: 39 additions & 0 deletions packages/format/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# @obosbbl/format

[![NPM Version](https://img.shields.io/npm/v/%40obosbbl%2Fformat)](https://www.npmjs.com/package/@obosbbl/format)


A collection of formatting functions for both 🇳🇴 and 🇸🇪 with zero dependencies.

## Install

```sh
# npm
npm install @obosbbl/format

# pnpm
pnpm add @obosbbl/format
```

## Usage

The package has two entrypoints, one for `no` and one for `se`. That allows you to import for only the locale you need.

Note that the methods are very lenient when attempting to format the input. It will strip all characters that aren't digits or letters before it applies the formatting.
That way any existing format of the input won't affect the formatted output

If unable to format the input, the method will return the (cleaned) input as is.

```js
// 🇳🇴 example
import { formatOrganizationNumber } from '@obosbbl/format/no';
formatOrganizationNumber('000000000') // => '000 000 000'

// 🇸🇪 example
import { formatOrganizationNumber } from '@obosbbl/format/se';
formatOrganizationNumber('0000000000') // => '000000-0000'
```

## Methods

* formatOrganizationNumber
28 changes: 28 additions & 0 deletions packages/format/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@obosbbl/format",
"version": "0.0.0",
"description": "A collection of formatting methods for OBOS",
"repository": {
"url": "https://github.com/code-obos/public-frontend-modules"
},
"license": "MIT",
"sideEffects": false,
"type": "module",
"exports": {
"./no": {
"types": "./dist/no.d.mts",
"default": "./dist/no.mjs"
},
"./se": {
"types": "./dist/se.d.mts",
"default": "./dist/se.mjs"
}
},
"files": [
"dist"
],
"scripts": {
"build": "bunchee"
},
"dependencies": {}
}
27 changes: 27 additions & 0 deletions packages/format/src/format.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, expect, test } from 'vitest';
import { formatOrganizationNumber as formatOrganizationNumberNo } from './no';
import { formatOrganizationNumber as formatOrganizationNumberSe } from './se';

describe('no', () => {
test.each([
['000000000', '000 000 000'],
['000 000 000', '000 000 000'],
['000-000-000', '000 000 000'],
['abc', 'abc'],
])('formatOrganizationNumber(%s) -> %s', (input, expected) => {
expect(formatOrganizationNumberNo(input)).toBe(expected);
});
});

describe('se', () => {
test.each([
['0000000000', '000000-0000'],
['000000-0000', '000000-0000'],
['000000 0000', '000000-0000'],
[' 000000 0000 ', '000000-0000'],
['000', '000'],
['abc', 'abc'],
])('formatOrganizationNumber(%s) -> %s', (input, expected) => {
expect(formatOrganizationNumberSe(input)).toBe(expected);
});
});
14 changes: 14 additions & 0 deletions packages/format/src/no.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { replaceIfMatch } from './utils';

const ORG_NUMBER_FORMAT = /^(\d{3})(\d{3})(\d{3})$/;

/**
* Format an organization number
* @example
* ```
* formatOrganizationNumber('000000000') // => '000 000 000'
* ```
*/
export function formatOrganizationNumber(input: string): string {
return replaceIfMatch(input, ORG_NUMBER_FORMAT, '$1 $2 $3');
}
14 changes: 14 additions & 0 deletions packages/format/src/se.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { replaceIfMatch } from './utils';

const ORG_NUMBER_FORMAT = /^(\d{6})(\d{4})$/;

/**
* Format an organization number
* @example
* ```
* formatOrganizationNumber('0000000000') // => '000000-0000'
* ```
*/
export function formatOrganizationNumber(input: string): string {
return replaceIfMatch(input, ORG_NUMBER_FORMAT, '$1-$2');
}
12 changes: 12 additions & 0 deletions packages/format/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function replaceIfMatch(
input: string,
regex: RegExp,
replacerPattern: string,
): string {
// We're extremely lenient when attemtping to format the input.
// We remove everything that isn't a letter or a number, that way we can get rid of any
// formatting that might already be present in the input, eg spaces, hyphens or dots
const cleaned = input.replace(/[^a-zA-Z0-9]/g, '');

return cleaned.replace(regex, replacerPattern);
}
7 changes: 7 additions & 0 deletions packages/format/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"include": ["./src/*"],
"compilerOptions": {
"outDir": "dist"
}
}
Loading