util.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.warning = undefined;
  6. var _extends2 = require('babel-runtime/helpers/extends');
  7. var _extends3 = _interopRequireDefault(_extends2);
  8. var _typeof2 = require('babel-runtime/helpers/typeof');
  9. var _typeof3 = _interopRequireDefault(_typeof2);
  10. exports.format = format;
  11. exports.isEmptyValue = isEmptyValue;
  12. exports.isEmptyObject = isEmptyObject;
  13. exports.asyncMap = asyncMap;
  14. exports.complementError = complementError;
  15. exports.deepMerge = deepMerge;
  16. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  17. var formatRegExp = /%[sdj%]/g;
  18. var warning = exports.warning = function warning() {};
  19. // don't print warning message when in production env or node runtime
  20. if (process.env.NODE_ENV !== 'production' && typeof window !== 'undefined' && typeof document !== 'undefined') {
  21. exports.warning = warning = function warning(type, errors) {
  22. if (typeof console !== 'undefined' && console.warn) {
  23. if (errors.every(function (e) {
  24. return typeof e === 'string';
  25. })) {
  26. console.warn(type, errors);
  27. }
  28. }
  29. };
  30. }
  31. function format() {
  32. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  33. args[_key] = arguments[_key];
  34. }
  35. var i = 1;
  36. var f = args[0];
  37. var len = args.length;
  38. if (typeof f === 'function') {
  39. return f.apply(null, args.slice(1));
  40. }
  41. if (typeof f === 'string') {
  42. var str = String(f).replace(formatRegExp, function (x) {
  43. if (x === '%%') {
  44. return '%';
  45. }
  46. if (i >= len) {
  47. return x;
  48. }
  49. switch (x) {
  50. case '%s':
  51. return String(args[i++]);
  52. case '%d':
  53. return Number(args[i++]);
  54. case '%j':
  55. try {
  56. return JSON.stringify(args[i++]);
  57. } catch (_) {
  58. return '[Circular]';
  59. }
  60. break;
  61. default:
  62. return x;
  63. }
  64. });
  65. for (var arg = args[i]; i < len; arg = args[++i]) {
  66. str += ' ' + arg;
  67. }
  68. return str;
  69. }
  70. return f;
  71. }
  72. function isNativeStringType(type) {
  73. return type === 'string' || type === 'url' || type === 'hex' || type === 'email' || type === 'pattern';
  74. }
  75. function isEmptyValue(value, type) {
  76. if (value === undefined || value === null) {
  77. return true;
  78. }
  79. if (type === 'array' && Array.isArray(value) && !value.length) {
  80. return true;
  81. }
  82. if (isNativeStringType(type) && typeof value === 'string' && !value) {
  83. return true;
  84. }
  85. return false;
  86. }
  87. function isEmptyObject(obj) {
  88. return Object.keys(obj).length === 0;
  89. }
  90. function asyncParallelArray(arr, func, callback) {
  91. var results = [];
  92. var total = 0;
  93. var arrLength = arr.length;
  94. function count(errors) {
  95. results.push.apply(results, errors);
  96. total++;
  97. if (total === arrLength) {
  98. callback(results);
  99. }
  100. }
  101. arr.forEach(function (a) {
  102. func(a, count);
  103. });
  104. }
  105. function asyncSerialArray(arr, func, callback) {
  106. var index = 0;
  107. var arrLength = arr.length;
  108. function next(errors) {
  109. if (errors && errors.length) {
  110. callback(errors);
  111. return;
  112. }
  113. var original = index;
  114. index = index + 1;
  115. if (original < arrLength) {
  116. func(arr[original], next);
  117. } else {
  118. callback([]);
  119. }
  120. }
  121. next([]);
  122. }
  123. function flattenObjArr(objArr) {
  124. var ret = [];
  125. Object.keys(objArr).forEach(function (k) {
  126. ret.push.apply(ret, objArr[k]);
  127. });
  128. return ret;
  129. }
  130. function asyncMap(objArr, option, func, callback) {
  131. if (option.first) {
  132. var flattenArr = flattenObjArr(objArr);
  133. return asyncSerialArray(flattenArr, func, callback);
  134. }
  135. var firstFields = option.firstFields || [];
  136. if (firstFields === true) {
  137. firstFields = Object.keys(objArr);
  138. }
  139. var objArrKeys = Object.keys(objArr);
  140. var objArrLength = objArrKeys.length;
  141. var total = 0;
  142. var results = [];
  143. var next = function next(errors) {
  144. results.push.apply(results, errors);
  145. total++;
  146. if (total === objArrLength) {
  147. callback(results);
  148. }
  149. };
  150. objArrKeys.forEach(function (key) {
  151. var arr = objArr[key];
  152. if (firstFields.indexOf(key) !== -1) {
  153. asyncSerialArray(arr, func, next);
  154. } else {
  155. asyncParallelArray(arr, func, next);
  156. }
  157. });
  158. }
  159. function complementError(rule) {
  160. return function (oe) {
  161. if (oe && oe.message) {
  162. oe.field = oe.field || rule.fullField;
  163. return oe;
  164. }
  165. return {
  166. message: oe,
  167. field: oe.field || rule.fullField
  168. };
  169. };
  170. }
  171. function deepMerge(target, source) {
  172. if (source) {
  173. for (var s in source) {
  174. if (source.hasOwnProperty(s)) {
  175. var value = source[s];
  176. if ((typeof value === 'undefined' ? 'undefined' : (0, _typeof3['default'])(value)) === 'object' && (0, _typeof3['default'])(target[s]) === 'object') {
  177. target[s] = (0, _extends3['default'])({}, target[s], value);
  178. } else {
  179. target[s] = value;
  180. }
  181. }
  182. }
  183. }
  184. return target;
  185. }