Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added imei #1346

Merged
merged 4 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ Validator | Description
**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).<br/><br/>Comma-separated format supported. Space-separated format supported with the exception of a few edge cases (ex: `hsl(200grad+.1%62%/1)`).
**isIBAN(str)** | check if a string is a IBAN (International Bank Account Number).
**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.<br/><br/>`locale` is one of `['ES', 'IN', 'NO', 'zh-TW', 'he-IL', 'ar-TN', 'zh-CN']` OR `'any'`. If 'any' is used, function will check if any of the locals match.<br/><br/>Defaults to 'any'.
**isIMEI(str)** | check if the string is a valid IMEI number.
**isIn(str, values)** | check if the string is in a array of allowed values.
**isInt(str [, options])** | check if the string is an integer.<br/><br/>`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).
Expand Down
36 changes: 36 additions & 0 deletions src/lib/isIMEI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import assertString from './util/assertString';


let imeiRegex = /^[0-9]{15}$/;

export default function isIMEI(str) {
assertString(str);

if (!imeiRegex.test(str)) {
return false;
}

let sum = 0,
mul = 2,
l = 14;

for (let i = 0; i < l; i++) {
const digit = str.substring(l - i - 1, l - i);
const tp = parseInt(digit, 10) * mul;
if (tp >= 10) {
sum += (tp % 10) + 1;
} else {
sum += tp;
}
if (mul === 1) {
mul += 1;
} else {
mul -= 1;
}
}
const chk = ((10 - (sum % 10)) % 10);
if (chk !== parseInt(str.substring(14, 15), 10)) {
return false;
}
return true;
}
20 changes: 20 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2755,6 +2755,26 @@ describe('Validators', () => {
});
});


it('should validate imei strings', () => {
test({
validator: 'isIMEI',
valid: [
'352099001761481',
'868932036356090',
'490154203237518',
'546918475942169',
'998227667144730',
'532729766805999',
],
invalid: [
'490154203237517',
'3568680000414120',
'3520990017614823',
],
});
});

it('should validate uppercase strings', () => {
test({
validator: 'isUppercase',
Expand Down