index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.Utils = exports.AREA = exports.ParseAddress = undefined;
  6. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  7. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /**
  8. * address-parse
  9. * MIT License
  10. * By www.asseek.com
  11. */
  12. var _area = require('../area');
  13. var _area2 = _interopRequireDefault(_area);
  14. var _utils = require('./utils');
  15. var _utils2 = _interopRequireDefault(_utils);
  16. var _parseArea = require('./parse-area');
  17. var _parseArea2 = _interopRequireDefault(_parseArea);
  18. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  19. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  20. var ParseAddress = function () {
  21. function ParseAddress(address) {
  22. _classCallCheck(this, ParseAddress);
  23. if (address) {
  24. return this.parse(address);
  25. }
  26. }
  27. /**
  28. * 开始解析
  29. * @param address string 地址
  30. * @param parseAll boolean 是否完全解析
  31. * @returns {Array}
  32. */
  33. _createClass(ParseAddress, [{
  34. key: 'parse',
  35. value: function parse(address, parseAll) {
  36. var results = [];
  37. if (address) {
  38. this.result = {
  39. mobile: '',
  40. zip_code: '',
  41. phone: ''
  42. };
  43. this.address = address;
  44. this.replace();
  45. this.parseMobile();
  46. this.parsePhone();
  47. this.parseZipCode();
  48. this.address = this.address.replace(/ {2,}/, ' ');
  49. var firstName = ParseAddress.parseName({ details: this.address });
  50. results = ParseAddress.ParseArea.parse(this.address, parseAll);
  51. var _iteratorNormalCompletion = true;
  52. var _didIteratorError = false;
  53. var _iteratorError = undefined;
  54. try {
  55. for (var _iterator = results[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  56. var _result = _step.value;
  57. Object.assign(_result, this.result);
  58. _result.name = _result.name.trim();
  59. ParseAddress.parseName(_result, { firstName: firstName });
  60. ParseAddress.handlerDetail(_result);
  61. }
  62. } catch (err) {
  63. _didIteratorError = true;
  64. _iteratorError = err;
  65. } finally {
  66. try {
  67. if (!_iteratorNormalCompletion && _iterator.return) {
  68. _iterator.return();
  69. }
  70. } finally {
  71. if (_didIteratorError) {
  72. throw _iteratorError;
  73. }
  74. }
  75. }
  76. if (!results.length) {
  77. var result = Object.assign(this.result, {
  78. province: '',
  79. city: '',
  80. area: '',
  81. details: this.address,
  82. name: '',
  83. code: '',
  84. __type: ''
  85. });
  86. ParseAddress.parseName(result);
  87. results.push(result);
  88. }
  89. }
  90. return results;
  91. }
  92. /**
  93. * 替换无效字符
  94. */
  95. }, {
  96. key: 'replace',
  97. value: function replace() {
  98. var address = this.address;
  99. var _iteratorNormalCompletion2 = true;
  100. var _didIteratorError2 = false;
  101. var _iteratorError2 = undefined;
  102. try {
  103. for (var _iterator2 = ParseAddress.ExcludeKeys[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
  104. var key = _step2.value;
  105. address = address.replace(new RegExp(key, 'g'), ' ');
  106. }
  107. } catch (err) {
  108. _didIteratorError2 = true;
  109. _iteratorError2 = err;
  110. } finally {
  111. try {
  112. if (!_iteratorNormalCompletion2 && _iterator2.return) {
  113. _iterator2.return();
  114. }
  115. } finally {
  116. if (_didIteratorError2) {
  117. throw _iteratorError2;
  118. }
  119. }
  120. }
  121. this.address = address.replace(/\r\n/g, ' ').replace(/\n/g, ' ').replace(/\t/g, ' ').replace(/ {2,}/g, ' ').replace(/(\d{3})-(\d{4})-(\d{4})/g, '$1$2$3').replace(/(\d{3}) (\d{4}) (\d{4})/g, '$1$2$3');
  122. }
  123. /**
  124. * 提取手机号码
  125. */
  126. }, {
  127. key: 'parseMobile',
  128. value: function parseMobile() {
  129. ParseAddress.Reg.mobile.lastIndex = 0;
  130. var mobile = ParseAddress.Reg.mobile.exec(this.address);
  131. if (mobile) {
  132. this.result.mobile = mobile[0];
  133. this.address = this.address.replace(mobile[0], ' ');
  134. }
  135. }
  136. /**
  137. * 提取电话号码
  138. */
  139. }, {
  140. key: 'parsePhone',
  141. value: function parsePhone() {
  142. ParseAddress.Reg.phone.lastIndex = 0;
  143. var phone = ParseAddress.Reg.phone.exec(this.address);
  144. if (phone) {
  145. this.result.phone = phone[0];
  146. this.address = this.address.replace(phone[0], ' ');
  147. }
  148. }
  149. /**
  150. * 提取邮编
  151. */
  152. }, {
  153. key: 'parseZipCode',
  154. value: function parseZipCode() {
  155. ParseAddress.Reg.zipCode.lastIndex = 0;
  156. var zip = ParseAddress.Reg.zipCode.exec(this.address);
  157. if (zip) {
  158. this.result.zip_code = zip[0];
  159. this.address = this.address.replace(zip[0], '');
  160. }
  161. }
  162. /**
  163. * 提取姓名
  164. * @param result
  165. * @param maxLen 字符串占位 比这个数值短才识别为姓名 汉字2位英文1位
  166. * @param firstName 最初切分地址识别到的name
  167. */
  168. }], [{
  169. key: 'parseName',
  170. value: function parseName(result) {
  171. var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
  172. _ref$maxLen = _ref.maxLen,
  173. maxLen = _ref$maxLen === undefined ? 11 : _ref$maxLen,
  174. firstName = _ref.firstName;
  175. if (!result.name || _utils2.default.strLen(result.name) > 15) {
  176. var list = result.details.split(' ');
  177. var name = {
  178. value: '',
  179. index: -1
  180. };
  181. if (list.length > 1) {
  182. var index = 0;
  183. var _iteratorNormalCompletion3 = true;
  184. var _didIteratorError3 = false;
  185. var _iteratorError3 = undefined;
  186. try {
  187. for (var _iterator3 = list[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
  188. var v = _step3.value;
  189. if (v && !name.value || v && _utils2.default.strLen(name.value) > _utils2.default.strLen(v) || firstName && v === firstName) {
  190. name.value = v;
  191. name.index = index;
  192. if (firstName && v === firstName) break;
  193. }
  194. index += 1;
  195. }
  196. } catch (err) {
  197. _didIteratorError3 = true;
  198. _iteratorError3 = err;
  199. } finally {
  200. try {
  201. if (!_iteratorNormalCompletion3 && _iterator3.return) {
  202. _iterator3.return();
  203. }
  204. } finally {
  205. if (_didIteratorError3) {
  206. throw _iteratorError3;
  207. }
  208. }
  209. }
  210. }
  211. if (name.value) {
  212. result.name = name.value;
  213. list.splice(name.index, 1);
  214. result.details = list.join(' ').trim();
  215. }
  216. }
  217. return result.name;
  218. }
  219. /**
  220. * 清洗地址详情内的省市区
  221. * @param result
  222. */
  223. }, {
  224. key: 'handlerDetail',
  225. value: function handlerDetail(result) {
  226. if (result.details.length > 5) {
  227. var ary = ['province', 'city', 'area'];
  228. var _iteratorNormalCompletion4 = true;
  229. var _didIteratorError4 = false;
  230. var _iteratorError4 = undefined;
  231. try {
  232. for (var _iterator4 = ary[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
  233. var key = _step4.value;
  234. var index = result.details.indexOf(result[key]);
  235. if (index !== 0) continue;
  236. result.details = result.details.substr(result[key].length);
  237. }
  238. } catch (err) {
  239. _didIteratorError4 = true;
  240. _iteratorError4 = err;
  241. } finally {
  242. try {
  243. if (!_iteratorNormalCompletion4 && _iterator4.return) {
  244. _iterator4.return();
  245. }
  246. } finally {
  247. if (_didIteratorError4) {
  248. throw _iteratorError4;
  249. }
  250. }
  251. }
  252. }
  253. }
  254. }]);
  255. return ParseAddress;
  256. }();
  257. ParseAddress.ExcludeKeys = ['发件人', '收货地址', '收货人', '收件人', '收货', '手机号码', '邮编', '电话', '所在地区', '详细地址', '地址', ':', ':', ';', ';', ',', ',', '。', '、'];
  258. ParseAddress.ParseArea = new _parseArea2.default();
  259. ParseAddress.Reg = _extends({}, _utils2.default.Reg);
  260. exports.ParseAddress = ParseAddress;
  261. exports.AREA = _area2.default;
  262. exports.Utils = _utils2.default;
  263. exports.default = new ParseAddress();