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

not sytax es6 #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
62 changes: 62 additions & 0 deletions lib/es6_codemeli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @typedef {Object} CodeMeli
* @property code
* @property parity
* @property city_code
* @property uid
*/

/**
*
* @param {String|Number} code - Input national number value. it can be string or number
* @param {Boolean} [returnObj] - If returnObj is true (defaults to `false`) function will return an object.
* @returns {CodeMeli|Object}
*/
function codemeli(code, returnObj) {
if (!code || code === '') return null;
code = String(code);
// Try to parse it as a number
let num = parseInt(code.trim());
if (!num) return null;
// Convert back to string
let _code = String(num);
if (_code.length < 8) {
return null;
}
while (_code.length < 10)
_code = '0' + _code;
// xxxxxxxxxx is invalid pattern!
let same = true;
for (let i = 0; i < 10; i++)
if (_code[i] !== _code[0]) {
same = false;
break;
}
if (same) {
return null;
}
// Validate
let sum = 0;
for (let i = 10; i > 0; i--)
sum += parseInt(_code[10-i])*i;
if ((sum = sum % 11)>= 2) {sum = 11 - sum};

if (_code.substr(9, 1) !== sum + '') {
return null;
}
if (returnObj) {
return {
code: _code,
parity: _code[9],
city_code: _code.substr(0, 2),
uid: _code.substr(2, 7)
}
}

return _code;
}
if (typeof module !== 'undefined') {
module.exports = codemeli;
} else if (typeof window !== 'undefined') {
window.codemeli = codemeli;
}