提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.9 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.setDeep = exports.getDeep = void 0;
  4. const is_js_1 = require("./is.cjs");
  5. const util_js_1 = require("./util.cjs");
  6. const getNthKey = (value, n) => {
  7. if (n > value.size)
  8. throw new Error('index out of bounds');
  9. const keys = value.keys();
  10. while (n > 0) {
  11. keys.next();
  12. n--;
  13. }
  14. return keys.next().value;
  15. };
  16. function validatePath(path) {
  17. if (util_js_1.includes(path, '__proto__')) {
  18. throw new Error('__proto__ is not allowed as a property');
  19. }
  20. if (util_js_1.includes(path, 'prototype')) {
  21. throw new Error('prototype is not allowed as a property');
  22. }
  23. if (util_js_1.includes(path, 'constructor')) {
  24. throw new Error('constructor is not allowed as a property');
  25. }
  26. }
  27. const getDeep = (object, path) => {
  28. validatePath(path);
  29. for (let i = 0; i < path.length; i++) {
  30. const key = path[i];
  31. if (is_js_1.isSet(object)) {
  32. object = getNthKey(object, +key);
  33. }
  34. else if (is_js_1.isMap(object)) {
  35. const row = +key;
  36. const type = +path[++i] === 0 ? 'key' : 'value';
  37. const keyOfRow = getNthKey(object, row);
  38. switch (type) {
  39. case 'key':
  40. object = keyOfRow;
  41. break;
  42. case 'value':
  43. object = object.get(keyOfRow);
  44. break;
  45. }
  46. }
  47. else {
  48. object = object[key];
  49. }
  50. }
  51. return object;
  52. };
  53. exports.getDeep = getDeep;
  54. const setDeep = (object, path, mapper) => {
  55. validatePath(path);
  56. if (path.length === 0) {
  57. return mapper(object);
  58. }
  59. let parent = object;
  60. for (let i = 0; i < path.length - 1; i++) {
  61. const key = path[i];
  62. if (is_js_1.isArray(parent)) {
  63. const index = +key;
  64. parent = parent[index];
  65. }
  66. else if (is_js_1.isPlainObject(parent)) {
  67. parent = parent[key];
  68. }
  69. else if (is_js_1.isSet(parent)) {
  70. const row = +key;
  71. parent = getNthKey(parent, row);
  72. }
  73. else if (is_js_1.isMap(parent)) {
  74. const isEnd = i === path.length - 2;
  75. if (isEnd) {
  76. break;
  77. }
  78. const row = +key;
  79. const type = +path[++i] === 0 ? 'key' : 'value';
  80. const keyOfRow = getNthKey(parent, row);
  81. switch (type) {
  82. case 'key':
  83. parent = keyOfRow;
  84. break;
  85. case 'value':
  86. parent = parent.get(keyOfRow);
  87. break;
  88. }
  89. }
  90. }
  91. const lastKey = path[path.length - 1];
  92. if (is_js_1.isArray(parent)) {
  93. parent[+lastKey] = mapper(parent[+lastKey]);
  94. }
  95. else if (is_js_1.isPlainObject(parent)) {
  96. parent[lastKey] = mapper(parent[lastKey]);
  97. }
  98. if (is_js_1.isSet(parent)) {
  99. const oldValue = getNthKey(parent, +lastKey);
  100. const newValue = mapper(oldValue);
  101. if (oldValue !== newValue) {
  102. parent.delete(oldValue);
  103. parent.add(newValue);
  104. }
  105. }
  106. if (is_js_1.isMap(parent)) {
  107. const row = +path[path.length - 2];
  108. const keyToRow = getNthKey(parent, row);
  109. const type = +lastKey === 0 ? 'key' : 'value';
  110. switch (type) {
  111. case 'key': {
  112. const newKey = mapper(keyToRow);
  113. parent.set(newKey, parent.get(keyToRow));
  114. if (newKey !== keyToRow) {
  115. parent.delete(keyToRow);
  116. }
  117. break;
  118. }
  119. case 'value': {
  120. parent.set(keyToRow, mapper(parent.get(keyToRow)));
  121. break;
  122. }
  123. }
  124. }
  125. return object;
  126. };
  127. exports.setDeep = setDeep;
  128. //# sourceMappingURL=accessDeep.js.map