util.js 4.3 KB

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