line-endings.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. exports.unixToWin = unixToWin;
  15. exports.winToUnix = winToUnix;
  16. exports.isUnix = isUnix;
  17. exports.isWin = isWin;
  18. function unixToWin(patch) {
  19. if (Array.isArray(patch)) {
  20. // It would be cleaner if instead of the line below we could just write
  21. // return patch.map(unixToWin)
  22. // but mysteriously TypeScript (v5.7.3 at the time of writing) does not like this and it will
  23. // refuse to compile, thinking that unixToWin could then return StructuredPatch[][] and the
  24. // result would be incompatible with the overload signatures.
  25. // See bug report at https://github.com/microsoft/TypeScript/issues/61398.
  26. return patch.map(function (p) { return unixToWin(p); });
  27. }
  28. return __assign(__assign({}, patch), { hunks: patch.hunks.map(function (hunk) { return (__assign(__assign({}, hunk), { lines: hunk.lines.map(function (line, i) {
  29. var _a;
  30. return (line.startsWith('\\') || line.endsWith('\r') || ((_a = hunk.lines[i + 1]) === null || _a === void 0 ? void 0 : _a.startsWith('\\')))
  31. ? line
  32. : line + '\r';
  33. }) })); }) });
  34. }
  35. function winToUnix(patch) {
  36. if (Array.isArray(patch)) {
  37. // (See comment above equivalent line in unixToWin)
  38. return patch.map(function (p) { return winToUnix(p); });
  39. }
  40. return __assign(__assign({}, patch), { hunks: patch.hunks.map(function (hunk) { return (__assign(__assign({}, hunk), { lines: hunk.lines.map(function (line) { return line.endsWith('\r') ? line.substring(0, line.length - 1) : line; }) })); }) });
  41. }
  42. /**
  43. * Returns true if the patch consistently uses Unix line endings (or only involves one line and has
  44. * no line endings).
  45. */
  46. function isUnix(patch) {
  47. if (!Array.isArray(patch)) {
  48. patch = [patch];
  49. }
  50. return !patch.some(function (index) { return index.hunks.some(function (hunk) { return hunk.lines.some(function (line) { return !line.startsWith('\\') && line.endsWith('\r'); }); }); });
  51. }
  52. /**
  53. * Returns true if the patch uses Windows line endings and only Windows line endings.
  54. */
  55. function isWin(patch) {
  56. if (!Array.isArray(patch)) {
  57. patch = [patch];
  58. }
  59. return patch.some(function (index) { return index.hunks.some(function (hunk) { return hunk.lines.some(function (line) { return line.endsWith('\r'); }); }); })
  60. && patch.every(function (index) { return index.hunks.every(function (hunk) { return hunk.lines.every(function (line, i) { var _a; return line.startsWith('\\') || line.endsWith('\r') || ((_a = hunk.lines[i + 1]) === null || _a === void 0 ? void 0 : _a.startsWith('\\')); }); }); });
  61. }