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.
- package/index.d.ts +7 -0
- package/index.js +13 -10
- package/package.json +17 -3
- package/readme.md +19 -0
package/index.d.ts
ADDED
package/index.js
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
|
-
let uniqueArr = [];
|
|
2
|
-
|
|
3
1
|
function isValid(code) {
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
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.
|
|
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": "
|
|
8
|
+
"test": "node -e \"const v=require('./index'); console.log(v.isValid('541449200002202157'));\""
|
|
8
9
|
},
|
|
9
|
-
"
|
|
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
|
+
```
|