Архитектурный шаблон "Мрак в моделях" на нескольких языках и платформах
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.

1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/env node
  2. if (process.argv.length < 4) {
  3. console.error("Usage: toSwift SRC DST");
  4. process.exit(1);
  5. }
  6. var fs = require("fs");
  7. let fileSrc = process.argv[2];
  8. let fileDst = process.argv[3];
  9. let globalReplacements = {
  10. "function": "func",
  11. "number": "Float",
  12. "Number": "Float",
  13. "):": ") ->",
  14. "interface": "protocol",
  15. "})": "}", // forEach
  16. ".push(": ".append(", // array
  17. };
  18. console.log(`Converting '${fileSrc}' to '${fileDst}'`);
  19. var linesDst = [];
  20. let linesSrc = fs.readFileSync(fileSrc).toString().split(/\r?\n/);
  21. for (let i in linesSrc) {
  22. let ln = linesSrc[i];
  23. linesDst.push(convert(ln));
  24. }
  25. let contentsDst = linesDst.join("\r\n");
  26. fs.writeFileSync(fileDst, contentsDst);
  27. // Functions making decisions.
  28. function convert(line) {
  29. var result = line;
  30. for (let src in globalReplacements) {
  31. let dst = globalReplacements[src];
  32. result = result.replaceAll(src, dst);
  33. }
  34. result = protocolReplace(result);
  35. result = typeArrayReplace(result);
  36. result = forEachReplace(result);
  37. result = recordReplace(result);
  38. return result;
  39. }
  40. // x.forEach(y => { -> x.forEach { y in
  41. function forEachReplace(line) {
  42. if (!line.includes("forEach")) {
  43. return line;
  44. }
  45. let partsFE = line.split("forEach");
  46. let identifier = partsFE[0];
  47. let closure = partsFE[1];
  48. let partsC = closure.split(" => ");
  49. let bracketAndVar = partsC[0];
  50. let bracket = partsC[1];
  51. let variable = bracketAndVar.substring(1);
  52. let result = `${identifier}forEach { ${variable} in`;
  53. return result;
  54. }
  55. var isProtocol = false;
  56. // interface -> protocol
  57. function protocolReplace(line) {
  58. if (line.includes("protocol")) {
  59. isProtocol = true;
  60. }
  61. if (line == "}") {
  62. isProtocol = false;
  63. }
  64. if (!isProtocol) {
  65. return line;
  66. }
  67. var result = line;
  68. if (isProtocol) {
  69. result = protocolReplaceVariable(result);
  70. }
  71. return result;
  72. }
  73. // a: Number -> var a: Float { get }
  74. function protocolReplaceVariable(line) {
  75. let parts = line.split(": ");
  76. if (parts.length == 2) {
  77. let type = parts[1];
  78. let name = parts[0].trim();
  79. let spaceLength = parts[0].length - name.length;
  80. let spaces = " ".repeat(spaceLength);
  81. return `${spaces}var ${name}: ${type} { get }`;
  82. }
  83. return line;
  84. }
  85. // Record<TypeA, TypeB> -> [TypeA: TypeB]
  86. // Record<TypeA, TypeB> = {} -> [TypeA: TypeB] = [:]
  87. function recordReplace(line) {
  88. if (!line.includes("Record")) {
  89. return line;
  90. }
  91. let partsR = line.split("Record<");
  92. let typesAndEnding = partsR[1];
  93. let partsT = typesAndEnding.split(">");
  94. let types = partsT[0];
  95. let ending = partsT[1];
  96. let swiftTypes = types.split(", ");
  97. let was = `Record<${swiftTypes[0]}, ${swiftTypes[1]}>`;
  98. let now = `[${swiftTypes[0]}: ${swiftTypes[1]}]`;
  99. var result = line;
  100. result = result.replace(was, now);
  101. result = result.replace("] = {};", "] = [:]");
  102. return result;
  103. }
  104. // Type[] -> [Type]
  105. function typeArrayReplace(line) {
  106. let parts = line.split(" ");
  107. for (var i in parts) {
  108. let part = parts[i];
  109. if (
  110. part != "[]" &&
  111. part.endsWith("[]")
  112. ) {
  113. let type = part.substring(0, part.length - 2)
  114. let swiftType = `[${type}]`;
  115. return line.replace(part, swiftType);
  116. }
  117. }
  118. return line;
  119. }