utils.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. var logSwitch = false;
  2. export var setLogSwitch = function (on) {
  3. logSwitch = on;
  4. };
  5. //@ts-expect-error
  6. export var log = function () {
  7. var args = [];
  8. for (var _i = 0; _i < arguments.length; _i++) {
  9. args[_i] = arguments[_i];
  10. }
  11. if (!logSwitch)
  12. return;
  13. console.log.apply(console, args);
  14. };
  15. //@ts-expect-error
  16. export var error = function () {
  17. var args = [];
  18. for (var _i = 0; _i < arguments.length; _i++) {
  19. args[_i] = arguments[_i];
  20. }
  21. console.error.apply(console, args);
  22. };
  23. export var time = function (label) {
  24. if (!logSwitch)
  25. return;
  26. console.time(label);
  27. };
  28. export var timeEnd = function (label) {
  29. if (!logSwitch)
  30. return;
  31. console.timeEnd(label);
  32. };
  33. //@ts-expect-error
  34. export var shallowCompare = function (obj1, obj2, skipKey) {
  35. if (obj1 === obj2)
  36. return true;
  37. if (typeof obj1 !== typeof obj2)
  38. return false;
  39. if (typeof obj1 !== 'object' || typeof obj2 !== 'object')
  40. return false;
  41. if (obj1 === null || obj2 === null)
  42. return false;
  43. if (Array.isArray(obj1) || Array.isArray(obj2))
  44. return false;
  45. if (Object.keys(obj1).length !== Object.keys(obj2).length)
  46. return false;
  47. for (var key in obj1) {
  48. if (obj1[key] !== obj2[key] && (!skipKey || skipKey.indexOf(key) === -1)) {
  49. return false;
  50. }
  51. }
  52. return true;
  53. };
  54. // 这么做是为了测试方便,隔离一些全局变量
  55. export var EComponent2Status;
  56. (function (EComponent2Status) {
  57. EComponent2Status["VALID"] = "VALID";
  58. EComponent2Status["INVALID"] = "INVALID";
  59. EComponent2Status["UNKNOWN"] = "UNKNOWN";
  60. })(EComponent2Status || (EComponent2Status = {}));
  61. var component2Status = EComponent2Status.UNKNOWN;
  62. export var getComponent2Status = function () {
  63. return component2Status;
  64. };
  65. export var updateComponent2Status = function (status) {
  66. if (status) {
  67. component2Status = status;
  68. //@ts-expect-error
  69. }
  70. else if (typeof my !== 'undefined') {
  71. //@ts-expect-error
  72. component2Status = my.canIUse('component2')
  73. ? EComponent2Status.VALID
  74. : EComponent2Status.INVALID;
  75. }
  76. };
  77. var incrementalId = 0;
  78. var instanceKeyId = '_functional_instance_id_';
  79. //@ts-expect-error
  80. export function getIdFromAppxInstance(appxInstance) {
  81. var _a;
  82. if (!appxInstance)
  83. throw new Error('appxInstance param is falsy');
  84. var id;
  85. // 支付宝端
  86. if (appxInstance.$id) {
  87. id = "".concat(appxInstance.$id, "-").concat(((_a = appxInstance.$page) === null || _a === void 0 ? void 0 : _a.$viewId) || '', "-").concat(appxInstance.is || 'page'); /* 页面 */
  88. }
  89. else if (appxInstance[instanceKeyId]) {
  90. id = appxInstance[instanceKeyId];
  91. }
  92. else {
  93. id = "instance-".concat(incrementalId++);
  94. appxInstance[instanceKeyId] = id;
  95. }
  96. if (!id)
  97. throw new Error('failed to identify appxInstance.$id');
  98. return id;
  99. }
  100. export var instanceKeyPropNames = '_functional_instance_prop_names_';
  101. export function mergeObjectKeys() {
  102. var objects = [];
  103. for (var _i = 0; _i < arguments.length; _i++) {
  104. objects[_i] = arguments[_i];
  105. }
  106. var res = [];
  107. for (var _a = 0, objects_1 = objects; _a < objects_1.length; _a++) {
  108. var iterator = objects_1[_a];
  109. res = res.concat(Object.keys(iterator));
  110. }
  111. return res;
  112. }