-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from organisationsnummer/feat/initial-version
v1.1 specification implementation
- Loading branch information
Showing
10 changed files
with
334 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"extends": [ | ||
"config:base", | ||
":preserveSemverRanges" | ||
], | ||
"packageRules": [ | ||
{ | ||
"matchUpdateTypes": ["minor", "patch", "pin", "digest"], | ||
"automerge": true | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: test | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
|
||
# Run tests for any PRs. | ||
pull_request: | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
container: | ||
image: google/dart:latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Install dependencies | ||
run: pub get | ||
- name: Run tests | ||
run: pub run test/organisationsnummer.dart |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.dart_tool/ | ||
.packages | ||
pubspec.lock | ||
/*.dart |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## 1.0.0 | ||
|
||
- Initial version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
MIT License | ||
|
||
Copyright (c) Organisationsnummer and Contributors | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# organisationsnummer [![Build Status](https://github.com/organisationsnummer/dart/workflows/test/badge.svg)](https://github.com/organisationsnummer/dart/actions) | ||
|
||
Validate Swedish organization numbers. Follows version 1.1 of the [specification](https://github.com/organisationsnummer/meta#package-specification-v11). | ||
|
||
## Example | ||
|
||
```dart | ||
import 'package:organisationsnummer/organisationsnummer.dart'; | ||
Organisationsnummer.valid('202100-5489'); | ||
//=> true | ||
``` | ||
|
||
See [test/organisationsnummer.dart](test/organisationsnummer.dart) for more examples. | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import 'package:organisationsnummer/organisationsnummer.dart'; | ||
|
||
void main() { | ||
print(Organisationsnummer.valid('INPUT')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import 'package:personnummer/personnummer.dart'; | ||
|
||
class OrganisationsnummerException implements Exception { | ||
String cause; | ||
OrganisationsnummerException( | ||
[this.cause = 'Invalid Swedish organization number']); | ||
} | ||
|
||
class Organisationsnummer { | ||
/// The organization number. | ||
String number = ''; | ||
|
||
/// Personnummer instance. | ||
Personnummer? _personnummer = null; | ||
|
||
/// Organisationsnummer constructor. | ||
Organisationsnummer(String input) { | ||
_parse(input); | ||
} | ||
|
||
/// Luhn/mod10 algorithm. Used to calculate a checksum from the passed value | ||
/// The checksum is returned and tested against the control number | ||
/// in the organization number to make sure that it is a valid number. | ||
bool _luhn(String str) { | ||
var v = 0; | ||
var sum = 0; | ||
|
||
for (var i = 0, l = str.length; i < l; i++) { | ||
v = int.parse(str[i]); | ||
v *= 2 - (i % 2); | ||
if (v > 9) { | ||
v -= 9; | ||
} | ||
sum += v; | ||
} | ||
|
||
return sum % 10 == 0; | ||
} | ||
|
||
/// Parse Swedish organization numbers and set properties. | ||
void _parse(String input) { | ||
var reg = RegExp( | ||
r'^(\d{2}){0,1}(\d{2})(\d{2})(\d{2})([\+\-]?)((?!000)\d{3})(\d)$'); | ||
var match; | ||
|
||
try { | ||
number = input.replaceAll('-', ''); | ||
match = reg.firstMatch(number); | ||
|
||
if (match == null) { | ||
throw OrganisationsnummerException(); | ||
} | ||
|
||
// May only be prefixed with 16. | ||
if (match[1] != null && int.parse(match[1]) != 16) { | ||
throw OrganisationsnummerException(); | ||
} | ||
|
||
// Third digit bust be more than 20. | ||
if (int.parse(match[3]) < 20) { | ||
throw OrganisationsnummerException(); | ||
} | ||
|
||
// May not start with leading 0. | ||
if (int.parse(match[2]) < 10) { | ||
throw OrganisationsnummerException(); | ||
} | ||
|
||
if (!_luhn(number)) { | ||
throw OrganisationsnummerException(); | ||
} | ||
} catch (e) { | ||
try { | ||
this._personnummer = Personnummer.parse(input); | ||
} catch (_) { | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
/// Format Swedish organization numbers to official format. | ||
String format([bool separator = true]) { | ||
var _number = number; | ||
|
||
if (this.isPersonnummer()) { | ||
_number = this._personnummer!.format(true).substring(2, 12); | ||
} | ||
|
||
return separator | ||
? _number.substring(0, 6) + '-' + _number.substring(6) | ||
: _number; | ||
} | ||
|
||
/// Get Personnummer instance. | ||
Personnummer? personnummer() { | ||
return this._personnummer; | ||
} | ||
|
||
/// Determine if personnummer or not. | ||
bool isPersonnummer() { | ||
return this._personnummer != null; | ||
} | ||
|
||
/// Get the organization type. | ||
String type() { | ||
if (this.isPersonnummer()) { | ||
return 'Enskild firma'; | ||
} | ||
|
||
var unkown = 'Okänt'; | ||
var types = { | ||
1: 'Dödsbon', | ||
2: 'Stat, landsting, kommun eller församling', | ||
3: 'Utländska företag som bedriver näringsverksamhet eller äger fastigheter i Sverige', | ||
5: 'Aktiebolag', | ||
6: 'Enkelt bolag', | ||
7: 'Ekonomisk förening eller bostadsrättsförening', | ||
8: 'Ideella förening och stiftelse', | ||
9: 'Handelsbolag, kommanditbolag och enkelt bolag', | ||
}; | ||
|
||
return types[int.parse(this.number[0])] ?? unkown; | ||
} | ||
|
||
/// Get vat number for a organization number. | ||
String vatNumber() { | ||
return "SE${format(false)}01"; | ||
} | ||
|
||
/// Parse Swedish organization numbers. | ||
/// Returns `Organisationsnummer` class. | ||
static Organisationsnummer parse(String input) { | ||
return Organisationsnummer(input); | ||
} | ||
|
||
/// Validates Swedish organization numbers. | ||
/// Returns `true` if the input value is a valid Swedish organization number | ||
static bool valid(String input) { | ||
try { | ||
parse(input); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
name: organisationsnummer | ||
version: 1.0.0 | ||
description: Validate Swedish organization numbers with Dart and Flutter. | ||
homepage: https://github.com/organisationsnummer/dart | ||
|
||
environment: | ||
sdk: '>=2.12.0 <3.0.0' | ||
|
||
dev_dependencies: | ||
test: ^1.20.1 | ||
dependencies: | ||
personnummer: ^3.0.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import 'package:test/test.dart'; | ||
import 'package:organisationsnummer/organisationsnummer.dart'; | ||
import 'package:personnummer/personnummer.dart'; | ||
|
||
void main() { | ||
test('should validate valid organization numbers', () { | ||
var numbers = ['556016-0680', '556103-4249', '5561034249']; | ||
|
||
numbers | ||
.forEach((number) => expect(Organisationsnummer.valid(number), true)); | ||
}); | ||
|
||
test('should validate invalid organization numbers', () { | ||
var numbers = ['556016-0681', '556103-4250', '5561034250']; | ||
|
||
numbers | ||
.forEach((number) => expect(Organisationsnummer.valid(number), false)); | ||
}); | ||
|
||
test('should format organization numbers without separator', () { | ||
var numbers = { | ||
'556016-0680': '5560160680', | ||
'556103-4249': '5561034249', | ||
'5561034249': '5561034249', | ||
}; | ||
|
||
numbers.forEach((input, output) => | ||
expect(Organisationsnummer.parse(input).format(false), output)); | ||
}); | ||
|
||
test('should format organization numbers with separator', () { | ||
var numbers = { | ||
'556016-0680': '556016-0680', | ||
'556103-4249': '556103-4249', | ||
'5561034249': '556103-4249', | ||
}; | ||
|
||
numbers.forEach((input, output) => | ||
expect(Organisationsnummer.parse(input).format(), output)); | ||
}); | ||
|
||
test('should get type from organization numbers', () { | ||
var numbers = { | ||
'556016-0680': 'Aktiebolag', | ||
'556103-4249': 'Aktiebolag', | ||
'5561034249': 'Aktiebolag', | ||
}; | ||
|
||
numbers.forEach((input, output) => | ||
expect(Organisationsnummer.parse(input).type(), output)); | ||
}); | ||
|
||
test('should get vat number for organization numbers', () { | ||
var numbers = { | ||
'556016-0680': 'SE556016068001', | ||
'556103-4249': 'SE556103424901', | ||
'5561034249': 'SE556103424901', | ||
}; | ||
|
||
numbers.forEach((input, output) => | ||
expect(Organisationsnummer.parse(input).vatNumber(), output)); | ||
}); | ||
|
||
test('should work with personal identity numbers', () { | ||
const type = 'Enskild firma'; | ||
const numbers = {'121212121212': '121212-1212'}; | ||
|
||
numbers.forEach((input, output) { | ||
expect(Organisationsnummer.valid(output), true); | ||
expect(Organisationsnummer.valid(input), true); | ||
var org = Organisationsnummer.parse(input); | ||
expect(org.format(false), output.replaceAll('-', '')); | ||
expect(org.format(true), output); | ||
expect(org.type(), type); | ||
expect(org.isPersonnummer(), true); | ||
expect(org.personnummer().runtimeType, Personnummer); | ||
}); | ||
}); | ||
|
||
test('should get vat number for personal identity numbers', () { | ||
const numbers = { | ||
'121212121212': 'SE121212121201', | ||
'12121212-1212': 'SE121212121201', | ||
}; | ||
|
||
numbers.forEach((input, output) { | ||
expect(Organisationsnummer.parse(input).vatNumber(), output); | ||
}); | ||
}); | ||
} |