helpers.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. // These methods let you build a transform function from a transformComponent function
  15. // for OT types like text and JSON in which operations are lists of components
  16. // and transforming them requires N^2 work.
  17. // Add transform and transformX functions for an OT type which has transformComponent defined.
  18. // transformComponent(destination array, component, other component, side)
  19. let bootstrapTransform
  20. exports._bt = bootstrapTransform = function (
  21. type,
  22. transformComponent,
  23. checkValidOp,
  24. append
  25. ) {
  26. let transformX
  27. const transformComponentX = function (left, right, destLeft, destRight) {
  28. transformComponent(destLeft, left, right, 'left')
  29. return transformComponent(destRight, right, left, 'right')
  30. }
  31. // Transforms rightOp by leftOp. Returns ['rightOp', clientOp']
  32. type.transformX =
  33. type.transformX =
  34. transformX =
  35. function (leftOp, rightOp) {
  36. checkValidOp(leftOp)
  37. checkValidOp(rightOp)
  38. const newRightOp = []
  39. for (let rightComponent of Array.from(rightOp)) {
  40. // Generate newLeftOp by composing leftOp by rightComponent
  41. const newLeftOp = []
  42. let k = 0
  43. while (k < leftOp.length) {
  44. let l
  45. const nextC = []
  46. transformComponentX(leftOp[k], rightComponent, newLeftOp, nextC)
  47. k++
  48. if (nextC.length === 1) {
  49. rightComponent = nextC[0]
  50. } else if (nextC.length === 0) {
  51. for (l of Array.from(leftOp.slice(k))) {
  52. append(newLeftOp, l)
  53. }
  54. rightComponent = null
  55. break
  56. } else {
  57. // Recurse.
  58. const [l_, r_] = Array.from(transformX(leftOp.slice(k), nextC))
  59. for (l of Array.from(l_)) {
  60. append(newLeftOp, l)
  61. }
  62. for (const r of Array.from(r_)) {
  63. append(newRightOp, r)
  64. }
  65. rightComponent = null
  66. break
  67. }
  68. }
  69. if (rightComponent != null) {
  70. append(newRightOp, rightComponent)
  71. }
  72. leftOp = newLeftOp
  73. }
  74. return [leftOp, newRightOp]
  75. }
  76. // Transforms op with specified type ('left' or 'right') by otherOp.
  77. return (type.transform = type.transform =
  78. function (op, otherOp, type) {
  79. let _
  80. if (type !== 'left' && type !== 'right') {
  81. throw new Error("type must be 'left' or 'right'")
  82. }
  83. if (otherOp.length === 0) {
  84. return op
  85. }
  86. // TODO: Benchmark with and without this line. I _think_ it'll make a big difference...?
  87. if (op.length === 1 && otherOp.length === 1) {
  88. return transformComponent([], op[0], otherOp[0], type)
  89. }
  90. if (type === 'left') {
  91. let left
  92. ;[left, _] = Array.from(transformX(op, otherOp))
  93. return left
  94. } else {
  95. let right
  96. ;[_, right] = Array.from(transformX(otherOp, op))
  97. return right
  98. }
  99. })
  100. }
  101. if (typeof WEB === 'undefined') {
  102. exports.bootstrapTransform = bootstrapTransform
  103. }