helpers.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 = type.transformX = transformX = function (leftOp, rightOp) {
  33. checkValidOp(leftOp)
  34. checkValidOp(rightOp)
  35. const newRightOp = []
  36. for (let rightComponent of Array.from(rightOp)) {
  37. // Generate newLeftOp by composing leftOp by rightComponent
  38. const newLeftOp = []
  39. let k = 0
  40. while (k < leftOp.length) {
  41. var l
  42. const nextC = []
  43. transformComponentX(leftOp[k], rightComponent, newLeftOp, nextC)
  44. k++
  45. if (nextC.length === 1) {
  46. rightComponent = nextC[0]
  47. } else if (nextC.length === 0) {
  48. for (l of Array.from(leftOp.slice(k))) {
  49. append(newLeftOp, l)
  50. }
  51. rightComponent = null
  52. break
  53. } else {
  54. // Recurse.
  55. const [l_, r_] = Array.from(transformX(leftOp.slice(k), nextC))
  56. for (l of Array.from(l_)) {
  57. append(newLeftOp, l)
  58. }
  59. for (const r of Array.from(r_)) {
  60. append(newRightOp, r)
  61. }
  62. rightComponent = null
  63. break
  64. }
  65. }
  66. if (rightComponent != null) {
  67. append(newRightOp, rightComponent)
  68. }
  69. leftOp = newLeftOp
  70. }
  71. return [leftOp, newRightOp]
  72. }
  73. // Transforms op with specified type ('left' or 'right') by otherOp.
  74. return (type.transform = type.transform = function (op, otherOp, type) {
  75. let _
  76. if (type !== 'left' && type !== 'right') {
  77. throw new Error("type must be 'left' or 'right'")
  78. }
  79. if (otherOp.length === 0) {
  80. return op
  81. }
  82. // TODO: Benchmark with and without this line. I _think_ it'll make a big difference...?
  83. if (op.length === 1 && otherOp.length === 1) {
  84. return transformComponent([], op[0], otherOp[0], type)
  85. }
  86. if (type === 'left') {
  87. let left
  88. ;[left, _] = Array.from(transformX(op, otherOp))
  89. return left
  90. } else {
  91. let right
  92. ;[_, right] = Array.from(transformX(otherOp, op))
  93. return right
  94. }
  95. })
  96. }
  97. if (typeof WEB === 'undefined') {
  98. exports.bootstrapTransform = bootstrapTransform
  99. }