ean-validator 1.0.0 → 1.0.2

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 (4) hide show
  1. package/index.d.ts +7 -0
  2. package/index.js +13 -10
  3. package/package.json +17 -3
  4. package/readme.md +19 -0
package/index.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ export interface EanValidator {
2
+ isValid(code: string): boolean;
3
+ }
4
+
5
+ declare const eanValidator: EanValidator;
6
+
7
+ export = eanValidator;
package/index.js CHANGED
@@ -1,16 +1,19 @@
1
- let uniqueArr = [];
2
-
3
1
  function isValid(code) {
4
- // Validate length & starting with 54
5
- if(!code.match(/^[5]{1}[4]{1}[0-9]{16}$/)) return false;
2
+ if (typeof code !== "string") return false;
3
+
4
+ // Validate length & starting with 54
5
+ if (!code.match(/^[5]{1}[4]{1}[0-9]{16}$/)) return false;
6
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));
7
+ // Validate logic
8
+ const sumPart = code.slice(0, -1).split("");
9
+ const sumResult = sumPart.reduce(
10
+ (r, n, i) => (i % 2 ? r + Number(n) : r + Number(n) * 3),
11
+ 0,
12
+ );
13
+ const nextTenFold = Math.ceil(sumResult / 10) * 10;
14
+ const checkingPart = Number(code.slice(-1));
12
15
 
13
- return (nextTenFold - sumResult) === checkingPart;
16
+ return nextTenFold - sumResult === checkingPart;
14
17
  }
15
18
 
16
19
  exports.isValid = isValid;
package/package.json CHANGED
@@ -1,12 +1,26 @@
1
1
  {
2
2
  "name": "ean-validator",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Validator for Belgium EAN numbers",
5
5
  "main": "index.js",
6
+ "types": "index.d.ts",
6
7
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
8
+ "test": "node -e \"const v=require('./index'); console.log(v.isValid('541449200002202157'));\""
8
9
  },
9
- "keywords": ["ean", "ean number", "validate", "validator", "energy", "gas", "electricity"],
10
+ "files": [
11
+ "index.js",
12
+ "index.d.ts",
13
+ "readme.md"
14
+ ],
15
+ "keywords": [
16
+ "ean",
17
+ "ean number",
18
+ "validate",
19
+ "validator",
20
+ "energy",
21
+ "gas",
22
+ "electricity"
23
+ ],
10
24
  "author": "Anthony Magnus <anthony.magnus@gmail.com>",
11
25
  "license": "ISC"
12
26
  }
package/readme.md ADDED
@@ -0,0 +1,19 @@
1
+ # EAN validator
2
+
3
+ Simple Belgium EAN validator to check if an energy or electricity meter is valid
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ npm i ean-validator
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```sh
14
+ const eanValidator = require('ean-validator');
15
+ // or in TypeScript
16
+ import eanValidator = require('ean-validator');
17
+
18
+ eanValidator.isValid('541449200002202157'); // true or false
19
+ ```