util.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. function isValidUrl(url) {
  2. return /(ht|f)tp(s?):\/\/([^ \\/]*\.)+[^ \\/]*(:[0-9]+)?\/?/.test(url);
  3. }
  4. /**
  5. * 深度对比两个对象是否一致
  6. * from: https://github.com/epoberezkin/fast-deep-equal
  7. * @param {Object} a 对象a
  8. * @param {Object} b 对象b
  9. * @return {Boolean} 是否相同
  10. */
  11. /* eslint-disable */
  12. function equal(a, b) {
  13. if (a === b) return true;
  14. if (a && b && typeof a == 'object' && typeof b == 'object') {
  15. var arrA = Array.isArray(a)
  16. , arrB = Array.isArray(b)
  17. , i
  18. , length
  19. , key;
  20. if (arrA && arrB) {
  21. length = a.length;
  22. if (length != b.length) return false;
  23. for (i = length; i-- !== 0;)
  24. if (!equal(a[i], b[i])) return false;
  25. return true;
  26. }
  27. if (arrA != arrB) return false;
  28. var dateA = a instanceof Date
  29. , dateB = b instanceof Date;
  30. if (dateA != dateB) return false;
  31. if (dateA && dateB) return a.getTime() == b.getTime();
  32. var regexpA = a instanceof RegExp
  33. , regexpB = b instanceof RegExp;
  34. if (regexpA != regexpB) return false;
  35. if (regexpA && regexpB) return a.toString() == b.toString();
  36. var keys = Object.keys(a);
  37. length = keys.length;
  38. if (length !== Object.keys(b).length)
  39. return false;
  40. for (i = length; i-- !== 0;)
  41. if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;
  42. for (i = length; i-- !== 0;) {
  43. key = keys[i];
  44. if (!equal(a[key], b[key])) return false;
  45. }
  46. return true;
  47. }
  48. return a !== a && b !== b;
  49. }
  50. module.exports = {
  51. isValidUrl,
  52. equal
  53. };