count.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Sanity-check the conversion and remove this comment.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS101: Remove unnecessary use of Array.from
  6. * DS102: Remove unnecessary code created because of implicit returns
  7. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  8. */
  9. // This is a simple type used for testing other OT code. Each op is [expectedSnapshot, increment]
  10. exports.name = 'count'
  11. exports.create = () => 1
  12. exports.apply = function (snapshot, op) {
  13. const [v, inc] = Array.from(op)
  14. if (snapshot !== v) {
  15. throw new Error(`Op ${v} != snapshot ${snapshot}`)
  16. }
  17. return snapshot + inc
  18. }
  19. // transform op1 by op2. Return transformed version of op1.
  20. exports.transform = function (op1, op2) {
  21. if (op1[0] !== op2[0]) {
  22. throw new Error(`Op1 ${op1[0]} != op2 ${op2[0]}`)
  23. }
  24. return [op1[0] + op2[1], op1[1]]
  25. }
  26. exports.compose = function (op1, op2) {
  27. if (op1[0] + op1[1] !== op2[0]) {
  28. throw new Error(`Op1 ${op1} + 1 != op2 ${op2}`)
  29. }
  30. return [op1[0], op1[1] + op2[1]]
  31. }
  32. exports.generateRandomOp = doc => [[doc, 1], doc + 1]