v1.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.deepEqualCodegen = void 0;
  4. const codegenValue = (doc, code, r) => {
  5. let rr = r;
  6. const type = typeof doc;
  7. const isPrimitive = doc === null || type === 'boolean' || type === 'string' || type === 'number';
  8. if (isPrimitive) {
  9. if (doc === Infinity) {
  10. code.push(`if(r${r} !== Infinity)return false;`);
  11. }
  12. else if (doc === -Infinity) {
  13. code.push(`if(r${r} !== -Infinity)return false;`);
  14. }
  15. else {
  16. code.push(`if(r${r} !== ${JSON.stringify(doc)})return false;`);
  17. }
  18. return rr;
  19. }
  20. if (Array.isArray(doc)) {
  21. code.push(`if(!Array.isArray(r${r}) || r${r}.length !== ${doc.length})return false;`);
  22. for (let i = 0; i < doc.length; i++) {
  23. rr++;
  24. code.push(`var r${rr}=r${r}[${i}];`);
  25. rr = codegenValue(doc[i], code, rr);
  26. }
  27. return rr;
  28. }
  29. if (doc instanceof Uint8Array) {
  30. const length = doc.length;
  31. code.push(`if(!(r${r} instanceof Uint8Array) || r${r}.length !== ${length})return false;`);
  32. let condition = '';
  33. for (let i = 0; i < length; i++)
  34. condition += (condition ? '||' : '') + `(r${r}[${i}]!==${doc[i]})`;
  35. if (condition)
  36. code.push(`if(${condition})return false;`);
  37. return rr;
  38. }
  39. if (type === 'object' && doc) {
  40. const obj = doc;
  41. const keys = Object.keys(obj);
  42. code.push(`if(!r${r} || typeof r${r} !== "object" || Array.isArray(r${r}) || Object.keys(r${r}).length !== ${keys.length})return false;`);
  43. for (const key of keys) {
  44. rr++;
  45. code.push(`var r${rr}=r${r}[${JSON.stringify(key)}];`);
  46. rr = codegenValue(obj[key], code, rr);
  47. }
  48. return rr;
  49. }
  50. if (doc === undefined) {
  51. code.push(`if(r${r} !== undefined)return false;`);
  52. return rr;
  53. }
  54. return rr;
  55. };
  56. const deepEqualCodegen = (a) => {
  57. const code = [];
  58. codegenValue(a, code, 0);
  59. const fn = ['(function(r0){', ...code, 'return true;', '})'];
  60. return fn.join('');
  61. };
  62. exports.deepEqualCodegen = deepEqualCodegen;
  63. //# sourceMappingURL=v1.js.map