From db633ed8081ea31ba1c6ba4d179c61a1b0926336 Mon Sep 17 00:00:00 2001 From: Mohamed Amin Boubaker Date: Sat, 4 Apr 2020 10:04:40 +0100 Subject: [PATCH] feat(isIdentityCard): added support for Tunisia (#1112) * added support for Tunisia National Identity Card test Successfully passed Signed-off-by: Mohamed Amin Boubaker * package script update clean.win : clean script for windows buildtest : one script to build and test * don't need to add a buildtest command since pretest will be called automatically by npm test Signed-off-by: Mohamed Amin Boubaker * use `yarn clean` command with `rimraf` for cross platform support Signed-off-by: Mohamed Amin Boubaker --- README.md | 2 +- package.json | 7 ++++--- src/lib/isIdentityCard.js | 12 ++++++++++++ test/validators.js | 29 +++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 239b3a8e1..d19ec3b48 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,7 @@ Validator | Description **isHexColor(str)** | check if the string is a hexadecimal color. **isHSL(str)** | check if the string is an HSL (hue, saturation, lightness, optional alpha) color based on [CSS Colors Level 4 specification](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value).

Comma-separated format supported. Space-separated format supported with the exception of a few edge cases (ex: `hsl(200grad+.1%62%/1)`). **isRgbColor(str, [, includePercentValues])** | check if the string is a rgb or rgba color.

`includePercentValues` defaults to `true`. If you don't want to allow to set `rgb` or `rgba` values with percents, like `rgb(5%,5%,5%)`, or `rgba(90%,90%,90%,.3)`, then set it to false. -**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.

`locale` is one of `['ES', 'zh-TW', 'he-IL']` OR `'any'`. If 'any' is used, function will check if any of the locals match.

Defaults to 'any'. +**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.

`locale` is one of `['ES', 'zh-TW', 'he-IL', 'ar-TN']` OR `'any'`. If 'any' is used, function will check if any of the locals match.

Defaults to 'any'. **isIn(str, values)** | check if the string is in a array of allowed values. **isInt(str [, options])** | check if the string is an integer.

`options` is an object which can contain the keys `min` and/or `max` to check the integer is within boundaries (e.g. `{ min: 10, max: 99 }`). `options` can also contain the key `allow_leading_zeroes`, which when set to false will disallow integer values with leading zeroes (e.g. `{ allow_leading_zeroes: false }`). Finally, `options` can contain the keys `gt` and/or `lt` which will enforce integers being greater than or less than, respectively, the value provided (e.g. `{gt: 1, lt: 4}` for a number between 1 and 4). **isIP(str [, version])** | check if the string is an IP (version 4 or 6). diff --git a/package.json b/package.json index b9d0f46bf..ef9a80309 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "eslint-plugin-import": "^2.11.0", "mocha": "^5.1.1", "nyc": "^14.1.0", + "rimraf": "^3.0.0", "rollup": "^0.43.0", "rollup-plugin-babel": "^4.0.1", "uglify-js": "^3.0.19" @@ -57,9 +58,9 @@ "scripts": { "lint": "eslint src test", "lint:fix": "eslint --fix src test", - "clean:node": "rm -rf index.js lib", - "clean:es": "rm -rf es", - "clean:browser": "rm -rf validator*.js", + "clean:node": "rimraf index.js lib", + "clean:es": "rimraf es", + "clean:browser": "rimraf validator*.js", "clean": "npm run clean:node && npm run clean:browser && npm run clean:es", "minify": "uglifyjs validator.js -o validator.min.js --compress --mangle --comments /Copyright/", "build:browser": "node --require @babel/register build-browser && npm run minify", diff --git a/src/lib/isIdentityCard.js b/src/lib/isIdentityCard.js index c74acde2a..6e7d29a5a 100644 --- a/src/lib/isIdentityCard.js +++ b/src/lib/isIdentityCard.js @@ -51,6 +51,18 @@ const validators = { } return sum % 10 === 0; }, + 'ar-TN': (str) => { + const DNI = /^\d{8}$/; + + // sanitize user input + const sanitized = str.trim(); + + // validate the data structure + if (!DNI.test(sanitized)) { + return false; + } + return true; + }, 'zh-TW': (str) => { const ALPHABET_CODES = { A: 10, diff --git a/test/validators.js b/test/validators.js index 2a76438a1..7b82560fe 100644 --- a/test/validators.js +++ b/test/validators.js @@ -3977,6 +3977,35 @@ describe('Validators', () => { '336000842', ], }, + { + locale: 'ar-TN', + valid: [ + '09958092', + '09151092', + '65126506', + '79378815', + '58994407', + '73089789', + '73260311', + ], + invalid: [ + '123456789546', + '123456789', + '023456789', + '12345678A', + '12345', + '1234578A', + '123 578A', + '12345 678Z', + '12345678-Z', + '1234*6789', + '1234*678Z', + 'GE9800as98', + 'X231071922', + '1234*678Z', + '12345678!', + ], + }, { locale: 'zh-TW', valid: [