Skip to content

Commit

Permalink
Merge branch 'release/1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
spikeheap committed Sep 8, 2015
2 parents 0d26f5a + 0f3ed2d commit 28d3efc
Show file tree
Hide file tree
Showing 8 changed files with 470 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules/
77 changes: 77 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
// JSHint Default Configuration File (as on JSHint website)
// See http://jshint.com/docs/ for more details

"maxerr" : 50, // {int} Maximum error before stopping

// Enforcing
"bitwise" : true, // true: Prohibit bitwise operators (&, |, ^, etc.)
"camelcase" : false, // true: Identifiers must be in camelCase
"curly" : true, // true: Require {} for every new block or scope
"eqeqeq" : true, // true: Require triple equals (===) for comparison
"forin" : true, // true: Require filtering for..in loops with obj.hasOwnProperty()
"freeze" : true, // true: prohibits overwriting prototypes of native objects such as Array, Date etc.
"immed" : false, // true: Require immediate invocations to be wrapped in parens e.g. `(function () { } ());`
"indent" : 4, // {int} Number of spaces to use for indentation
"latedef" : false, // true: Require variables/functions to be defined before being used
"newcap" : false, // true: Require capitalization of all constructor functions e.g. `new F()`
"noarg" : true, // true: Prohibit use of `arguments.caller` and `arguments.callee`
"noempty" : true, // true: Prohibit use of empty blocks
"nonbsp" : true, // true: Prohibit "non-breaking whitespace" characters.
"nonew" : false, // true: Prohibit use of constructors for side-effects (without assignment)
"plusplus" : false, // true: Prohibit use of `++` & `--`
"quotmark" : false, // Quotation mark consistency:
// false : do nothing (default)
// true : ensure whatever is used is consistent
// "single" : require single quotes
// "double" : require double quotes
"undef" : true, // true: Require all non-global variables to be declared (prevents global leaks)
"unused" : true, // Unused variables:
// true : all variables, last function parameter
// "vars" : all variables only
// "strict" : all variables, all function parameters
"strict" : true, // true: Requires all functions run in ES5 Strict Mode
"maxparams" : false, // {int} Max number of formal params allowed per function
"maxdepth" : false, // {int} Max depth of nested blocks (within functions)
"maxstatements" : false, // {int} Max number statements per function
"maxcomplexity" : false, // {int} Max cyclomatic complexity per function
"maxlen" : false, // {int} Max number of characters per line
"varstmt" : false, // true: Disallow any var statements. Only `let` and `const` are allowed.

// Relaxing
"asi" : false, // true: Tolerate Automatic Semicolon Insertion (no semicolons)
"boss" : false, // true: Tolerate assignments where comparisons would be expected
"debug" : false, // true: Allow debugger statements e.g. browser breakpoints.
"eqnull" : false, // true: Tolerate use of `== null`
"es5" : false, // true: Allow ES5 syntax (ex: getters and setters)
"esnext" : false, // true: Allow ES.next (ES6) syntax (ex: `const`)
"moz" : false, // true: Allow Mozilla specific syntax (extends and overrides esnext features)
// (ex: `for each`, multiple try/catch, function expression…)
"evil" : false, // true: Tolerate use of `eval` and `new Function()`
"expr" : false, // true: Tolerate `ExpressionStatement` as Programs
"funcscope" : false, // true: Tolerate defining variables inside control statements
"globalstrict" : false, // true: Allow global "use strict" (also enables 'strict')
"iterator" : false, // true: Tolerate using the `__iterator__` property
"lastsemic" : false, // true: Tolerate omitting a semicolon for the last statement of a 1-line block
"laxbreak" : false, // true: Tolerate possibly unsafe line breakings
"laxcomma" : false, // true: Tolerate comma-first style coding
"loopfunc" : false, // true: Tolerate functions being defined in loops
"multistr" : false, // true: Tolerate multi-line strings
"noyield" : false, // true: Tolerate generator functions with no yield statement in them.
"notypeof" : false, // true: Tolerate invalid typeof operator values
"proto" : false, // true: Tolerate using the `__proto__` property
"scripturl" : false, // true: Tolerate script-targeted URLs
"shadow" : false, // true: Allows re-define variables later in code e.g. `var x=1; x=2;`
"sub" : false, // true: Tolerate using `[]` notation when it can still be expressed in dot notation
"supernew" : false, // true: Tolerate `new function () { ... };` and `new Object;`
"validthis" : false, // true: Tolerate using this in a non-constructor function

// Environments
"browser" : false, // Web Browser (window, document, etc)
"browserify" : false, // Browserify (node.js code in the browser)
"jasmine" : true, // Jasmine
"node" : true, // Node.js

// Custom Globals
"globals" : {} // additional predefined global variables
}
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
This software is released under the MIT License (MIT)

Copyright (c) 2015 Ryan Brooks

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.
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# NHS number validator (JS)

A simple NHS number validator, following the process described in the [NHS Data Dictionary](http://www.datadictionary.nhs.uk/data_dictionary/attributes/n/nhs/nhs_number_de.asp?shownav=0?query=%22nhs+number%22&rank=100&shownav=1).

## Installation

##### NPM

```bash
npm install --save nhs-number-validator
```

##### Bower

```bash
bower install --save nhs-number-validator
```

## Usage

```javascript
var validator = require('nhs-number-validator');

validator.validate('2983396339') // => true
validator.validate('test') // => false
```


## Contributing

Pull requests, issues, and questions are all welcome.

Some useful commands:

* `npm run`: run the validator CLI.
* `npm test`: run the test suite.
* `npm run test-watch --silent`: watch for changes and run the test suite each time.
* `npm run lint --silent`: run the code linter.

NPM is overly verbose [at the moment](https://github.com/npm/npm/issues/5452), hence the additional `--silent` to suppress the unnecessary noise.

You can run the tests on every change with:

```bash
karma start
```

### Creating a release

Making a release is pretty straightforward:

```bash
VERSION_NUMBER=1.0.0

git checkout master && git pull
git checkout develop && git pull

git flow release start $VERSION_NUMBER

# Bump the version in package.json now!
jq ".version = \"${VERSION_NUMBER}\"" package.json > package.json.new && mv package.json.new package.json
git add package.json
git commit -m "Update package version number to $VERSION_NUMBER"

git flow release finish $VERSION_NUMBER

git checkout master && git push
git checkout develop && git push
git push --tags

git checkout master
hub release create -a labking_$VERSION_NUMBER.zip -m "$VERSION_NUMBER" $VERSION_NUMBER
npm publish
# Bower is published automatically using Git tags, so don't worry about that
git checkout develop
```
27 changes: 27 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "nhs-number-validator",
"main": "index.js",
"version": "1.0.0",
"homepage": "https://github.com/spikeheap/nhs-number-validator",
"authors": [
"Ryan Brooks <ryan@ryanbrooks.co.uk>"
],
"description": "A simple NHS number validator",
"moduleType": [
"node",
"globals"
],
"keywords": [
"nhs",
"validator",
"validation"
],
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
]
}
74 changes: 74 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"use strict";

// Only export where a Node/Browerserify-esque environment is present
if(typeof exports !== 'undefined'){
exports.validate = validate;
}

// Only attach to `window` if it's present
if(typeof window !== 'undefined'){
window.nhsNumberValidator = {validate: validate};
}

/**
* Validate an NHS number
* @param {Number, String} nhsNumber The NHS number to validate. This may be a String or a number.
* @returns {Boolean} `true` IFF the NHS number validates, else `false`
**/
function validate(nhsNumber){
// pre-flight checks
if(
nhsNumber === undefined ||
nhsNumber === null ||
isNaN(Number(nhsNumber)) ||
nhsNumber.toString().length !== 10
){
return false;
}

// convert numbers to strings, for internal consistency
if(Number.isInteger(nhsNumber)){
nhsNumber = nhsNumber.toString();
}

// Step 1: Multiply each of the first 9 numbers by (11 - position indexed from 1)
// Step 2: Add the results together
// Step 3: Divide the total by 11 to get the remainder
var nhsNumberAsArray = nhsNumber.split('');
var remainder = nhsNumberAsArray.slice(0,9)
.map(multiplyByPosition)
.reduce(addTogether, 0) % 11;

var checkDigit = 11 - remainder;

// replace 11 for 0
if(checkDigit === 11){
checkDigit = 0;
}

var providedCheckDigit = nhsNumberAsArray[9];

// Do the check digits match?
return checkDigit === Number(providedCheckDigit);
}

/**
* Multiply a value by its position, using the NHS number strategy
* @param {Number} digit the single-digit portion of the number
* @param {Number} index The 0-indexed position of `digit` within the NHS number
* @returns {Number} the result of the 'multiply by (11-position)' calculation
**/
function multiplyByPosition(digit, index) {
// multiple each digit by 11 minus its position (indexed from 1)
return digit * (11 - (index+1));
}

/**
* Add two values together. Useful for use in `reduce` calls
* @param {Number} previousValue the initial value
* @param {Number} currentValue the value to add to the initial value
* @returns {Number} the sum of the two parameters
**/
function addTogether(previousValue, currentValue){
return previousValue + currentValue;
}
Loading

0 comments on commit 28d3efc

Please sign in to comment.