under-dash.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. const {toString} = Object.prototype;
  2. const escapeHtmlRegex = /["&<>]/;
  3. const _ = {
  4. each: function each(obj, cb) {
  5. if (obj) {
  6. if (Array.isArray(obj)) {
  7. obj.forEach(cb);
  8. } else {
  9. Object.keys(obj).forEach(key => {
  10. cb(obj[key], key);
  11. });
  12. }
  13. }
  14. },
  15. some: function some(obj, cb) {
  16. if (obj) {
  17. if (Array.isArray(obj)) {
  18. return obj.some(cb);
  19. }
  20. return Object.keys(obj).some(key => cb(obj[key], key));
  21. }
  22. return false;
  23. },
  24. every: function every(obj, cb) {
  25. if (obj) {
  26. if (Array.isArray(obj)) {
  27. return obj.every(cb);
  28. }
  29. return Object.keys(obj).every(key => cb(obj[key], key));
  30. }
  31. return true;
  32. },
  33. map: function map(obj, cb) {
  34. if (obj) {
  35. if (Array.isArray(obj)) {
  36. return obj.map(cb);
  37. }
  38. return Object.keys(obj).map(key => cb(obj[key], key));
  39. }
  40. return [];
  41. },
  42. keyBy(a, p) {
  43. return a.reduce((o, v) => {
  44. o[v[p]] = v;
  45. return o;
  46. }, {});
  47. },
  48. isEqual: function isEqual(a, b) {
  49. const aType = typeof a;
  50. const bType = typeof b;
  51. const aArray = Array.isArray(a);
  52. const bArray = Array.isArray(b);
  53. let keys;
  54. if (aType !== bType) {
  55. return false;
  56. }
  57. switch (typeof a) {
  58. case 'object':
  59. if (aArray || bArray) {
  60. if (aArray && bArray) {
  61. return (
  62. a.length === b.length &&
  63. a.every((aValue, index) => {
  64. const bValue = b[index];
  65. return _.isEqual(aValue, bValue);
  66. })
  67. );
  68. }
  69. return false;
  70. }
  71. if (a === null || b === null) {
  72. return a === b;
  73. }
  74. // Compare object keys and values
  75. keys = Object.keys(a);
  76. if (Object.keys(b).length !== keys.length) {
  77. return false;
  78. }
  79. for (const key of keys) {
  80. if (!b.hasOwnProperty(key)) {
  81. return false;
  82. }
  83. }
  84. return _.every(a, (aValue, key) => {
  85. const bValue = b[key];
  86. return _.isEqual(aValue, bValue);
  87. });
  88. default:
  89. return a === b;
  90. }
  91. },
  92. escapeHtml(html) {
  93. const regexResult = escapeHtmlRegex.exec(html);
  94. if (!regexResult) return html;
  95. let result = '';
  96. let escape = '';
  97. let lastIndex = 0;
  98. let i = regexResult.index;
  99. for (; i < html.length; i++) {
  100. switch (html.charAt(i)) {
  101. case '"':
  102. escape = '&quot;';
  103. break;
  104. case '&':
  105. escape = '&amp;';
  106. break;
  107. case '\'':
  108. escape = '&apos;';
  109. break;
  110. case '<':
  111. escape = '&lt;';
  112. break;
  113. case '>':
  114. escape = '&gt;';
  115. break;
  116. default:
  117. continue;
  118. }
  119. if (lastIndex !== i) result += html.substring(lastIndex, i);
  120. lastIndex = i + 1;
  121. result += escape;
  122. }
  123. if (lastIndex !== i) return result + html.substring(lastIndex, i);
  124. return result;
  125. },
  126. strcmp(a, b) {
  127. if (a < b) return -1;
  128. if (a > b) return 1;
  129. return 0;
  130. },
  131. isUndefined(val) {
  132. return toString.call(val) === '[object Undefined]';
  133. },
  134. isObject(val) {
  135. return toString.call(val) === '[object Object]';
  136. },
  137. deepMerge() {
  138. const target = arguments[0] || {};
  139. const {length} = arguments;
  140. // eslint-disable-next-line one-var
  141. let src, clone, copyIsArray;
  142. function assignValue(val, key) {
  143. src = target[key];
  144. copyIsArray = Array.isArray(val);
  145. if (_.isObject(val) || copyIsArray) {
  146. if (copyIsArray) {
  147. copyIsArray = false;
  148. clone = src && Array.isArray(src) ? src : [];
  149. } else {
  150. clone = src && _.isObject(src) ? src : {};
  151. }
  152. target[key] = _.deepMerge(clone, val);
  153. } else if (!_.isUndefined(val)) {
  154. target[key] = val;
  155. }
  156. }
  157. for (let i = 0; i < length; i++) {
  158. _.each(arguments[i], assignValue);
  159. }
  160. return target;
  161. },
  162. };
  163. module.exports = _;