index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. ParseAddress.parseName(_result, { firstName: firstName });
  59. }
  60. } catch (err) {
  61. _didIteratorError = true;
  62. _iteratorError = err;
  63. } finally {
  64. try {
  65. if (!_iteratorNormalCompletion && _iterator.return) {
  66. _iterator.return();
  67. }
  68. } finally {
  69. if (_didIteratorError) {
  70. throw _iteratorError;
  71. }
  72. }
  73. }
  74. if (!results.length) {
  75. var result = Object.assign(this.result, {
  76. province: '',
  77. city: '',
  78. area: '',
  79. details: this.address,
  80. name: '',
  81. code: '',
  82. __type: ''
  83. });
  84. ParseAddress.parseName(result);
  85. results.push(result);
  86. }
  87. }
  88. return results;
  89. }
  90. /**
  91. * 替换无效字符
  92. */
  93. }, {
  94. key: 'replace',
  95. value: function replace() {
  96. var address = this.address;
  97. var _iteratorNormalCompletion2 = true;
  98. var _didIteratorError2 = false;
  99. var _iteratorError2 = undefined;
  100. try {
  101. for (var _iterator2 = ParseAddress.ExcludeKeys[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
  102. var key = _step2.value;
  103. address = address.replace(new RegExp(key, 'g'), ' ');
  104. }
  105. } catch (err) {
  106. _didIteratorError2 = true;
  107. _iteratorError2 = err;
  108. } finally {
  109. try {
  110. if (!_iteratorNormalCompletion2 && _iterator2.return) {
  111. _iterator2.return();
  112. }
  113. } finally {
  114. if (_didIteratorError2) {
  115. throw _iteratorError2;
  116. }
  117. }
  118. }
  119. 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');
  120. }
  121. /**
  122. * 提取手机号码
  123. */
  124. }, {
  125. key: 'parseMobile',
  126. value: function parseMobile() {
  127. ParseAddress.Reg.mobile.lastIndex = 0;
  128. var mobile = ParseAddress.Reg.mobile.exec(this.address);
  129. if (mobile) {
  130. this.result.mobile = mobile[0];
  131. this.address = this.address.replace(mobile[0], ' ');
  132. }
  133. }
  134. /**
  135. * 提取电话号码
  136. */
  137. }, {
  138. key: 'parsePhone',
  139. value: function parsePhone() {
  140. ParseAddress.Reg.phone.lastIndex = 0;
  141. var phone = ParseAddress.Reg.phone.exec(this.address);
  142. if (phone) {
  143. this.result.phone = phone[0];
  144. this.address = this.address.replace(phone[0], ' ');
  145. }
  146. }
  147. /**
  148. * 提取邮编
  149. */
  150. }, {
  151. key: 'parseZipCode',
  152. value: function parseZipCode() {
  153. ParseAddress.Reg.zipCode.lastIndex = 0;
  154. var zip = ParseAddress.Reg.zipCode.exec(this.address);
  155. if (zip) {
  156. this.result.zip_code = zip[0];
  157. this.address = this.address.replace(zip[0], '');
  158. }
  159. }
  160. /**
  161. * 提取姓名
  162. * @param result
  163. * @param maxLen 字符串占位 比这个数值短才识别为姓名 汉字2位英文1位
  164. * @param firstName 最初切分地址识别到的name
  165. */
  166. }], [{
  167. key: 'parseName',
  168. value: function parseName(result) {
  169. var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
  170. _ref$maxLen = _ref.maxLen,
  171. maxLen = _ref$maxLen === undefined ? 11 : _ref$maxLen,
  172. firstName = _ref.firstName;
  173. if (!result.name) {
  174. var list = result.details.split(' ');
  175. var name = {
  176. value: '',
  177. index: -1
  178. };
  179. if (list.length > 1) {
  180. var index = 0;
  181. var _iteratorNormalCompletion3 = true;
  182. var _didIteratorError3 = false;
  183. var _iteratorError3 = undefined;
  184. try {
  185. for (var _iterator3 = list[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
  186. var v = _step3.value;
  187. if (v || _utils2.default.strLen(name.value) > _utils2.default.strLen(v) || firstName && v === firstName) {
  188. name.value = v;
  189. name.index = index;
  190. if (firstName && v === firstName) break;
  191. }
  192. index += 1;
  193. }
  194. } catch (err) {
  195. _didIteratorError3 = true;
  196. _iteratorError3 = err;
  197. } finally {
  198. try {
  199. if (!_iteratorNormalCompletion3 && _iterator3.return) {
  200. _iterator3.return();
  201. }
  202. } finally {
  203. if (_didIteratorError3) {
  204. throw _iteratorError3;
  205. }
  206. }
  207. }
  208. }
  209. if (name.value) {
  210. result.name = name.value;
  211. list.splice(name.index, 1);
  212. result.details = list.join(' ');
  213. }
  214. }
  215. return result.name;
  216. }
  217. }]);
  218. return ParseAddress;
  219. }();
  220. ParseAddress.ExcludeKeys = ['发件人', '收货地址', '收货人', '收件人', '收货', '手机号码', '邮编', '电话', '所在地区', '详细地址', '地址', ':', ':', ';', ';', ',', ',', '。', '、'];
  221. ParseAddress.ParseArea = new _parseArea2.default();
  222. ParseAddress.Reg = _extends({}, _utils2.default.Reg);
  223. exports.ParseAddress = ParseAddress;
  224. exports.AREA = _area2.default;
  225. exports.Utils = _utils2.default;
  226. exports.default = new ParseAddress();