Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

cloneNode.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = cloneNode;
  6. var _definitions = require("../definitions");
  7. var _generated = require("../validators/generated");
  8. const has = Function.call.bind(Object.prototype.hasOwnProperty);
  9. function cloneIfNode(obj, deep, withoutLoc) {
  10. if (obj && typeof obj.type === "string") {
  11. return cloneNode(obj, deep, withoutLoc);
  12. }
  13. return obj;
  14. }
  15. function cloneIfNodeOrArray(obj, deep, withoutLoc) {
  16. if (Array.isArray(obj)) {
  17. return obj.map(node => cloneIfNode(node, deep, withoutLoc));
  18. }
  19. return cloneIfNode(obj, deep, withoutLoc);
  20. }
  21. function cloneNode(node, deep = true, withoutLoc = false) {
  22. if (!node) return node;
  23. const {
  24. type
  25. } = node;
  26. const newNode = {
  27. type: node.type
  28. };
  29. if ((0, _generated.isIdentifier)(node)) {
  30. newNode.name = node.name;
  31. if (has(node, "optional") && typeof node.optional === "boolean") {
  32. newNode.optional = node.optional;
  33. }
  34. if (has(node, "typeAnnotation")) {
  35. newNode.typeAnnotation = deep ? cloneIfNodeOrArray(node.typeAnnotation, true, withoutLoc) : node.typeAnnotation;
  36. }
  37. } else if (!has(_definitions.NODE_FIELDS, type)) {
  38. throw new Error(`Unknown node type: "${type}"`);
  39. } else {
  40. for (const field of Object.keys(_definitions.NODE_FIELDS[type])) {
  41. if (has(node, field)) {
  42. if (deep) {
  43. newNode[field] = (0, _generated.isFile)(node) && field === "comments" ? maybeCloneComments(node.comments, deep, withoutLoc) : cloneIfNodeOrArray(node[field], true, withoutLoc);
  44. } else {
  45. newNode[field] = node[field];
  46. }
  47. }
  48. }
  49. }
  50. if (has(node, "loc")) {
  51. if (withoutLoc) {
  52. newNode.loc = null;
  53. } else {
  54. newNode.loc = node.loc;
  55. }
  56. }
  57. if (has(node, "leadingComments")) {
  58. newNode.leadingComments = maybeCloneComments(node.leadingComments, deep, withoutLoc);
  59. }
  60. if (has(node, "innerComments")) {
  61. newNode.innerComments = maybeCloneComments(node.innerComments, deep, withoutLoc);
  62. }
  63. if (has(node, "trailingComments")) {
  64. newNode.trailingComments = maybeCloneComments(node.trailingComments, deep, withoutLoc);
  65. }
  66. if (has(node, "extra")) {
  67. newNode.extra = Object.assign({}, node.extra);
  68. }
  69. return newNode;
  70. }
  71. function maybeCloneComments(comments, deep, withoutLoc) {
  72. if (!comments || !deep) {
  73. return comments;
  74. }
  75. return comments.map(({
  76. type,
  77. value,
  78. loc
  79. }) => {
  80. if (withoutLoc) {
  81. return {
  82. type,
  83. value,
  84. loc: null
  85. };
  86. }
  87. return {
  88. type,
  89. value,
  90. loc
  91. };
  92. });
  93. }