Skip to content
This repository was archived by the owner on Apr 25, 2022. It is now read-only.

Commit

Permalink
Add business number method (#4)
Browse files Browse the repository at this point in the history
* Add business number method

* Use latest node; yarn
  • Loading branch information
abejfehr authored Mar 26, 2019
1 parent 5bcd0ab commit 3b6ef21
Show file tree
Hide file tree
Showing 8 changed files with 3,428 additions and 118 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
language: node_js
node_js:
- "0.10"
- "11"
cache: yarn
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ sin.isTemporary();
// Returns: false
```

Use `.isBusinessNumber()` to determine if the SIN is a Business Number.

```javascript
var sin = new SocialInsuranceNumber("817640897");
sin.isBusinessNumber();
// Returns: true

sin = new SocialInsuranceNumber("130692544");
sin.isBusinessNumber();
// Returns: false
```

Use `.provinces()` to get the Canadian provinces associated with the SIN.

```javascript
Expand Down
82 changes: 0 additions & 82 deletions karma.conf.js

This file was deleted.

14 changes: 3 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
{
"name": "social-insurance-number",
"version": "0.0.2",
"version": "0.0.3",
"description": "Canadian SIN (Social Insurance Number) parser and generator",
"main": "social-insurance-number.js",
"scripts": {
"test": "node_modules/karma/bin/karma start --single-run --browsers PhantomJS"
"test": "jest"
},
"repository": {
"type": "git",
"url": "git://github.com/wealthsimple/social-insurance-number.git"
},
"engines": {
"node": "0.10.x"
},
"license": "MIT",
"devDependencies": {
"chai": "1.9.1",
"karma": "0.12.21",
"karma-mocha": "0.1.6",
"karma-phantomjs-launcher": "0.1.4",
"karma-spec-reporter": "0.0.13",
"underscore": "1.6.0"
"jest": "^24.5.0"
}
}
5 changes: 5 additions & 0 deletions social-insurance-number.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(function(global) {
var SIN_LENGTH = 9;
var TEMPORARY_RESIDENT_FIRST_DIGIT = 9;
var BUSINESS_NUMBER_FIRST_DIGIT = 8;
// Map Canadian provinces to associated first SIN digits
var PROVINCES = {
"AB": [6],
Expand Down Expand Up @@ -51,6 +52,10 @@
return this.firstDigit() === TEMPORARY_RESIDENT_FIRST_DIGIT;
};

SocialInsuranceNumber.prototype.isBusinessNumber = function() {
return this.firstDigit() === BUSINESS_NUMBER_FIRST_DIGIT;
};

SocialInsuranceNumber.prototype.provinces = function() {
var provinces = [];
for(var province in PROVINCES) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var expect = chai.expect;
var { SocialInsuranceNumber } = require('../social-insurance-number');

var invalidSins = [
"1a3456789",
Expand All @@ -25,92 +25,102 @@ describe('SocialInsuranceNumber', function() {
describe(".generate", function() {
it("generates a valid SIN number", function() {
var sin = SocialInsuranceNumber.generate();
expect(sin.length).to.equal(SocialInsuranceNumber.SIN_LENGTH);
expect(new SocialInsuranceNumber(sin).isValid()).to.equal(true);
expect(sin.length).toEqual(SocialInsuranceNumber.SIN_LENGTH);
expect(new SocialInsuranceNumber(sin).isValid()).toEqual(true);
});

describe("with `province` set", function() {
beforeEach(function() {
this.sin = SocialInsuranceNumber.generate({province: "ON"});
sin = SocialInsuranceNumber.generate({province: "ON"});
});

it("uses that province", function() {
var ontarioProvinceNumbers = ["4", "5"];
expect(ontarioProvinceNumbers).to.contain(this.sin.substring(0, 1));
expect(ontarioProvinceNumbers).toContain(sin.substring(0, 1));
});

it("generates a valid SIN number", function() {
expect(new SocialInsuranceNumber(this.sin).isValid()).to.equal(true);;
expect(new SocialInsuranceNumber(sin).isValid()).toEqual(true);
});
});

describe("with `startsWith` set to a single digit", function() {
beforeEach(function() {
this.startsWith = 8;
this.sin = SocialInsuranceNumber.generate({startsWith: this.startsWith});
startsWith = 8;
sin = SocialInsuranceNumber.generate({startsWith: startsWith});
});

it("uses that as the first digit", function() {
expect(this.sin.substring(0, 1)).to.equal(String(this.startsWith));
expect(sin.substring(0, 1)).toEqual(String(startsWith));
});

it("generates a valid SIN number", function() {
expect(new SocialInsuranceNumber(this.sin).isValid()).to.equal(true);;
expect(new SocialInsuranceNumber(sin).isValid()).toEqual(true);
});
});

describe("with `startsWith` set to multiple digits", function() {
beforeEach(function() {
this.startsWith = "12345";
this.sin = SocialInsuranceNumber.generate({startsWith: this.startsWith});
startsWith = "12345";
sin = SocialInsuranceNumber.generate({startsWith: startsWith});
});

it("uses that as the first digit", function() {
expect(this.sin.substring(0, 5)).to.equal(String(this.startsWith));
expect(sin.substring(0, 5)).toEqual(String(startsWith));
});

it("generates a valid SIN number", function() {
expect(new SocialInsuranceNumber(this.sin).isValid()).to.equal(true);;
expect(new SocialInsuranceNumber(sin).isValid()).toEqual(true);
});
});
});

describe("#normalizedValue", function() {
it("normalizes the SIN input value", function() {
_.each(validSins, function(input) {
expect(new SocialInsuranceNumber(input).normalizedValue()).to.equal(normalizedValidSin);
validSins.forEach(function(input) {
expect(new SocialInsuranceNumber(input).normalizedValue()).toEqual(normalizedValidSin);
});
});
});

describe("#isValid", function() {
it("returns false for an invalid input", function() {
_.each(invalidSins, function(input) {
expect(new SocialInsuranceNumber(input).isValid()).to.equal(false);
invalidSins.forEach(function(input) {
expect(new SocialInsuranceNumber(input).isValid()).toEqual(false);
});
});

it("returns true for a valid format", function() {
_.each(validSins, function(input) {
expect(new SocialInsuranceNumber(input).isValid()).to.equal(true);
validSins.forEach(function(input) {
expect(new SocialInsuranceNumber(input).isValid()).toEqual(true);
});
});
});

describe("#isTemporary", function() {
it("returns true for a temporary resident SIN", function() {
expect(new SocialInsuranceNumber("918640897").isTemporary()).to.equal(true);
expect(new SocialInsuranceNumber("918640897").isTemporary()).toEqual(true);
});

it("returns false for a non-temporary resident SIN", function() {
expect(new SocialInsuranceNumber("130692544").isTemporary()).to.equal(false);
expect(new SocialInsuranceNumber("130692544").isTemporary()).toEqual(false);
});
});

describe("#isBusinessNumber", function() {
it("returns true for a BN", function() {
expect(new SocialInsuranceNumber("817640897").isBusinessNumber()).toEqual(true);
});

it("returns false for a SIN", function() {
expect(new SocialInsuranceNumber("130692544").isBusinessNumber()).toEqual(false);
});
});

describe("#provinces", function() {
it("returns the associated provinces for the SIN", function() {
expect(new SocialInsuranceNumber("918640897").provinces()).to.eql([]);
expect(new SocialInsuranceNumber("130692544").provinces()).to.eql(['NB', 'NF', 'NS', 'PE']);
expect(new SocialInsuranceNumber("918640897").provinces()).toEqual([]);
expect(new SocialInsuranceNumber("130692544").provinces()).toEqual(['NB', 'NF', 'NS', 'PE']);
});
});
});
Loading

0 comments on commit 3b6ef21

Please sign in to comment.