ean-validator 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +16 -0
  2. package/package.json +12 -0
package/index.js ADDED
@@ -0,0 +1,16 @@
1
+ let uniqueArr = [];
2
+
3
+ function isValid(code) {
4
+ // Validate length & starting with 54
5
+ if(!code.match(/^[5]{1}[4]{1}[0-9]{16}$/)) return false;
6
+
7
+ // Validate logic
8
+ const sumPart = code.slice(0, -1).split('');
9
+ const sumResult = sumPart.reduce((r, n, i) => i % 2 ? r + parseInt(n) : r + parseInt((n * 3)), 0);
10
+ const nextTenFold = Math.ceil(sumResult/10) * 10;
11
+ const checkingPart = parseInt(code.slice(-1));
12
+
13
+ return (nextTenFold - sumResult) === checkingPart;
14
+ }
15
+
16
+ exports.isValid = isValid;
package/package.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "ean-validator",
3
+ "version": "1.0.0",
4
+ "description": "Validator for Belgium EAN numbers",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": ["ean", "ean number", "validate", "validator", "energy", "gas", "electricity"],
10
+ "author": "Anthony Magnus <anthony.magnus@gmail.com>",
11
+ "license": "ISC"
12
+ }