├── test.js ├── nong.js ├── .gitignore ├── package.json ├── LICENSE ├── README.md └── idCard.js /test.js: -------------------------------------------------------------------------------- 1 | idCard = require('./idCard') 2 | 3 | console.log(idCard.all('110226198501272116')) 4 | console.log(idCard.birthDay('110226198501272116')) 5 | // console.log(idCard.all('110226198501272116')) 6 | // console.log(idCard.all('110226198501272116')) 7 | -------------------------------------------------------------------------------- /nong.js: -------------------------------------------------------------------------------- 1 | var chineseLunar = require('chinese-lunar'); 2 | 3 | function aa(ba){ 4 | ba = ba.slice(0,4)+'/'+ba.slice(4,6)+'/'+ba.slice(6,8) 5 | a = new Date(ba) 6 | try { 7 | var lunar = chineseLunar.solarToLunar(a) 8 | } catch (err) { 9 | return '时间错误' 10 | } 11 | return lunar.year+'/'+lunar.month+'/'+lunar.day 12 | } 13 | // console.log(aa('09840715')) 14 | 15 | module.exports = aa 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | bak 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "js-idcard", 3 | "version": "1.0.6", 4 | "description": "身份证解析", 5 | "main": "idCard.js", 6 | "dependencies": { 7 | "chinese-lunar": "^0.1.4", 8 | "node-constellation": "^0.0.5" 9 | }, 10 | "devDependencies": {}, 11 | "scripts": { 12 | "test": "echo \"Error: no test specified\" && exit 1" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "git+https://github.com/NoBey/IdCard.git" 17 | }, 18 | "keywords": [ 19 | "身份证", 20 | "id-card" 21 | ], 22 | "author": "NoBey", 23 | "contributors": [ 24 | "NoBey", 25 | "Labi Kyo (github.com/labikyo)" 26 | ], 27 | "license": "ISC", 28 | "bugs": { 29 | "url": "https://github.com/NoBey/IdCard/issues" 30 | }, 31 | "homepage": "https://github.com/NoBey/IdCard#readme" 32 | } 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 NoBey 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # IdCard - 身份证的工具库 2 | ---- 3 | 本来是不想重新造轮子的可是找了找github上的仓库,看了看感觉都有bug而且很别扭,所以还是自己开刀搞,说说跟别的库有什么不同吧 4 | - 对地区数据上不准备直接用网上的一些数据因为那些数据在计算到县和区的时候会漏掉市级的一些数据所以准备先对数据进行二次处理在引入 5 | 6 | 7 | 8 | ## 安装 9 | `npm install js-idcard` 10 | 11 | ## 使用 12 | ```js 13 | var IdCard = require('js-idcard') 14 | ``` 15 | 16 | ## 方法列表 17 | ---- 18 | ### IdCard.EndNum(IdCard) 19 | 返回根据前17位数算出来的第18位 20 | 21 | ##### 参数说明 22 | - @param {String} IdCard 身份证号码 23 | 24 | ##### 返回数据 25 | `{Number}` 26 | 27 | ---- 28 | 29 | ### IdCard.birthDay(IdCard) 30 | 返回计算出来的星期几,星座,生肖 31 | 32 | ##### 参数说明 33 | - @param {String} IdCard 身份证号码 34 | 35 | ##### 返回数据 36 | ```js 37 | { 38 | date: '2014/2/2', 39 | nong: '2013/3/3', 40 | year: 2001, 41 | month: 01, 42 | day: 01, 43 | week: '星期一', // 星期几 44 | zodiac: '天秤座', // 星座 45 | zodiac_zh: '龙' // 生肖 46 | } 47 | ``` 48 | ---- 49 | 50 | ### IdCard.checkIdCard(IdCard) 51 | 返回验证身份证号是否正确 52 | 53 | 54 | ##### 参数说明 55 | - @param {String} IdCard 身份证号码 56 | 57 | ##### 返回数据 58 | `{Boolean}` 59 | 60 | --- 61 | 62 | ### IdCard.repairIdCard(IdCard) 63 | 返回补全身份证号 64 | 65 | 66 | ##### 参数说明 67 | - @param {String} IdCard 身份证号码 18位活着残缺的17位 68 | 69 | ##### 返回数据 70 | `{Number}` 71 | 72 | --- 73 | 74 | ### IdCard.num15to18(IdCard) 75 | 返回15位转换18位后的身份证号码 76 | 77 | 78 | ##### 参数说明 79 | - @param {String} IdCard 身份证号码 15位 80 | 81 | ##### 返回数据 82 | `{Number}` 83 | 84 | --- 85 | 86 | ### IdCard.sex(IdCard) 87 | 返回性别 男或女 88 | 89 | 90 | ##### 参数说明 91 | - @param {String} IdCard 身份证号码 92 | 93 | ##### 返回数据 94 | `{String}男或女` 95 | 96 | --- 97 | 98 | ### IdCard.nong(date) 99 | 返回性别 农历日期 100 | 101 | 102 | ##### 参数说明 103 | - @param {String} date 日期 2016/01/01 104 | 105 | ##### 返回数据 106 | `{String}农历日期` 107 | 108 | --- 109 | 110 | ### IdCard.address(IdCard) 111 | 返回 地址信息 112 | 113 | 114 | ##### 参数说明 115 | - @param {String} IdCard 身份证号码 116 | 117 | ##### 返回数据 118 | ```json 119 | { 120 | "address": "地址", 121 | "provinces": "省/直辖市", 122 | "citiy": "市", 123 | "areas": "县/区", 124 | "all": "省-市-县" 125 | } 126 | 127 | ``` 128 | 129 | --- 130 | 131 | ### IdCard.all(IdCard) 132 | 返回 全部解析的数据 133 | 134 | 135 | ##### 参数说明 136 | - @param {String} IdCard 身份证号码 137 | 138 | ##### 返回数据 139 | ```js 140 | { 141 | endNum: 6, 142 | birthDay: { 143 | date: '2014/2/2', 144 | nong: '2013/3/3', 145 | year: '1985', 146 | month: '01', 147 | week: '星期天', 148 | zodiac: '水瓶座', 149 | zodiac_zh: '牛' 150 | }, 151 | checkIdCard: true, 152 | address: { 153 | address: '北京市平谷县', 154 | provinces: '北京市', 155 | citiy: '无', 156 | areas: '平谷县', 157 | all: '北京市--平谷县' 158 | }, 159 | sex: '男' 160 | } 161 | 162 | ``` 163 | -------------------------------------------------------------------------------- /idCard.js: -------------------------------------------------------------------------------- 1 | var node_constellation = require('node-constellation'); 2 | var chineseLunar = require('chinese-lunar'); 3 | 4 | var dataAddress = require('./data/data.json') 5 | // 字典 6 | var dict = { 7 | week: function(year, month, date) { 8 | var i = new Date(year, month - 1, date).getUTCDay() 9 | var day = { 10 | 0: '星期一', 11 | 1: '星期二', 12 | 2: '星期三', 13 | 3: '星期四', 14 | 4: '星期五', 15 | 5: '星期六', 16 | 6: '星期天', 17 | } 18 | return day[i] 19 | }, 20 | zodiac_zh: function(year) { 21 | var arr = '鼠牛虎兔龙蛇马羊猴鸡狗猪' 22 | year = year % 12 - 4 23 | if (year < 0) year += 12 24 | return arr[year] 25 | }, 26 | zodiac: function(month, date) { 27 | try{ 28 | if (month != undefined && date != undefined) return node_constellation(month, date, 'zh-cn') 29 | if (date == undefined && month != undefined) { 30 | if (month.length == 4) return node_constellation(month.substr(0, 2), month.substr(2, 2), 'zh-cn') 31 | month = month.split(/\/|\\|-/) 32 | return node_constellation(month[0], month[1], 'zh-cn') 33 | } 34 | return 35 | } 36 | catch(err){ 37 | return '出错'+month+date 38 | } 39 | } 40 | } 41 | 42 | // 计算最后一位应该是多少 43 | function idCardEndNum(idCard) { 44 | idCard = idCard.toString() 45 | var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; 46 | var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2]; 47 | var sum = 0; 48 | var ai = 0; 49 | var wi = 0; 50 | for (var i = 0; i < 17; i++) { 51 | ai = idCard[i]; 52 | wi = factor[i]; 53 | sum += ai * wi; 54 | } 55 | var last = parity[sum % 11]; 56 | return last; 57 | } 58 | 59 | // 农历转换 60 | function Nong(birthday){ 61 | var birthday = birthday.split(/\/|\\|-/) 62 | birthday = birthday.slice(0,4)+'/'+birthday.slice(4,6)+'/'+birthday.slice(6,8) 63 | nong = new Date(birthday) 64 | try { 65 | var lunar = chineseLunar.solarToLunar(nong) 66 | } catch (err) { 67 | return '时间错误' 68 | } 69 | return lunar.year+'/'+lunar.month+'/'+lunar.day 70 | } 71 | 72 | // 解析生日信息 73 | function birthDay(idCard) { 74 | idCard = idCard.toString() 75 | var birthday, month, day, nong; 76 | year = idCard.substr(6, 4); 77 | month = idCard.substr(10, 2); 78 | day = idCard.substr(12, 2); 79 | birthday = year + '/' + month + '/' + day; 80 | nong = Nong(birthday); 81 | nongyear = nong.substr(0, 4) 82 | return { 83 | date: birthday, 84 | nong: nong, 85 | year: year, 86 | month: month, 87 | day: day, 88 | week: dict.week(year, month, day), // 星期几 89 | zodiac: dict.zodiac(month, day), // 星座 90 | zodiac_zh: dict.zodiac_zh(nongyear) // 生肖 91 | }; 92 | } 93 | 94 | // 验证身份证号是否正确 95 | function checkIdCard(idCard) { 96 | idCard = idCard.toString() 97 | if (/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(idCard) && idCardEndNum(idCard) == idCard[17].toUpperCase()) return true 98 | return false 99 | } 100 | 101 | // 补全身份证号 102 | function repairIdCard(idCard) { 103 | idCard = idCard.toString() 104 | if (/(^\d{17}$)/.test(idCard)) return idCard + idCardEndNum(idCard) 105 | if (/(^\d{18}$)/.test(idCard)) return idCard.slice(0, 17) + idCardEndNum(idCard) 106 | } 107 | 108 | // 15位转换18位 109 | function num15to18(idCard) { 110 | idCard = idCard.toString() 111 | if (/(^\d{15}$)/.test(idCard)) return repairIdCard(idCard.slice(0, 6) + '19' + idCard.slice(6, 15)) 112 | } 113 | 114 | // 地址信息解析 115 | function address(idCard) { 116 | idCard = idCard.toString() 117 | var addressId = idCard.slice(0, 6) 118 | var data = dataAddress[addressId] 119 | if(data == undefined){ 120 | console.log(idCard) 121 | return '未找到' 122 | } 123 | data.all = (data.provinces + '-' + data.citiy + '-' + data.areas).replace('无', '') 124 | return data 125 | } 126 | 127 | /* 地址信息返回格式 128 | { 129 | "address": "地址", 130 | "provinces": "省/直辖市", 131 | "citiy": "市", 132 | "areas": "县/区", 133 | "all": "省-市-县" 134 | } 135 | */ 136 | 137 | 138 | // 性别解析 139 | function sex(idCard) { 140 | idCard = idCard.toString() 141 | if (idCard[16] % 2) return '男' 142 | return '女' 143 | } 144 | 145 | module.exports = { 146 | endNum: idCardEndNum, 147 | birthDay: birthDay, 148 | checkIdCard: checkIdCard, 149 | repairIdCard: repairIdCard, 150 | num15to18: num15to18, 151 | sex: sex, 152 | address: address, 153 | nong: Nong, 154 | all: (idCard) => { 155 | return { 156 | endNum: idCardEndNum(idCard), 157 | birthDay: birthDay(idCard), 158 | checkIdCard: checkIdCard(idCard), 159 | address: address(idCard), 160 | sex: sex(idCard) 161 | } 162 | } 163 | } 164 | 165 | 166 | // console.log(num15to18('411403960314001')) 167 | 168 | // console.log(checkIdCard('411403199603140010')) 169 | --------------------------------------------------------------------------------