utils.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.equals = exports.isVanillaObject = exports.isFunction = exports.isObject = exports.isArray = exports.comparable = exports.typeChecker = void 0;
  4. const typeChecker = (type) => {
  5. const typeString = "[object " + type + "]";
  6. return function (value) {
  7. return getClassName(value) === typeString;
  8. };
  9. };
  10. exports.typeChecker = typeChecker;
  11. const getClassName = value => Object.prototype.toString.call(value);
  12. const comparable = (value) => {
  13. if (value instanceof Date) {
  14. return value.getTime();
  15. }
  16. else if (exports.isArray(value)) {
  17. return value.map(exports.comparable);
  18. }
  19. else if (value && typeof value.toJSON === "function") {
  20. return value.toJSON();
  21. }
  22. return value;
  23. };
  24. exports.comparable = comparable;
  25. exports.isArray = exports.typeChecker("Array");
  26. exports.isObject = exports.typeChecker("Object");
  27. exports.isFunction = exports.typeChecker("Function");
  28. const isVanillaObject = value => {
  29. return (value &&
  30. (value.constructor === Object ||
  31. value.constructor === Array ||
  32. value.constructor.toString() === "function Object() { [native code] }" ||
  33. value.constructor.toString() === "function Array() { [native code] }") &&
  34. !value.toJSON);
  35. };
  36. exports.isVanillaObject = isVanillaObject;
  37. const equals = (a, b) => {
  38. if (a == null && a == b) {
  39. return true;
  40. }
  41. if (a === b) {
  42. return true;
  43. }
  44. if (Object.prototype.toString.call(a) !== Object.prototype.toString.call(b)) {
  45. return false;
  46. }
  47. if (exports.isArray(a)) {
  48. if (a.length !== b.length) {
  49. return false;
  50. }
  51. for (let i = 0, { length } = a; i < length; i++) {
  52. if (!exports.equals(a[i], b[i]))
  53. return false;
  54. }
  55. return true;
  56. }
  57. else if (exports.isObject(a)) {
  58. if (Object.keys(a).length !== Object.keys(b).length) {
  59. return false;
  60. }
  61. for (const key in a) {
  62. if (!exports.equals(a[key], b[key]))
  63. return false;
  64. }
  65. return true;
  66. }
  67. return false;
  68. };
  69. exports.equals = equals;
  70. //# sourceMappingURL=utils.js.map