/validating-operational-transformations

Trying to write a solution function for this validating operational transformation problem that I found online

Primary LanguageJavaScript

๐Ÿค– Validating Operational Transformations ๐Ÿš€

Introduction ๐Ÿ‘‹

Operational Transformation (OT) is a ๐Ÿง‘โ€๐Ÿคโ€๐Ÿง‘ technique that allows multiple users to concurrently edit the same document in real-time. In this process, every change made by the user is represented as an operation that can be applied to the document. These operations need to be validated to ensure that they can be safely applied to the document. ๐Ÿ’ป

This project is about validating the operational transformations in JavaScript. We have an initial version of the validation function that takes three arguments:

  • staleContents - The initial contents of the document before any operations were applied.
  • latestContents - The contents of the document after all the operations have been applied.
  • operationsJSON - A JSON string that represents an array of operations to be applied to the document.

The Problem ๐Ÿค”

The goal of the project is to update the validation function to handle all possible edge cases that might occur while applying the operations. We have a set of requirements and constraints that should be satisfied:

  • You can't skip past the end of a string. โŒ
  • You can't delete past the end of a string. โŒ
  • Delete operations are applied forward while keeping the cursor in place. ๐Ÿ”ข
  • If the operationsJSON is not a valid JSON string, the function should return false. ๐Ÿ›‘
  • If any operation type other than "insert", "delete", or "skip" is encountered, the function return false. ๐Ÿ›‘
  • If the count of delete or skip operation is negative, the function should return false ๐Ÿ›‘

Solution ๐Ÿ’ก

๐Ÿค– The function first attempts to parse the operationsJSON string into an array of objects using JSON.parse(). If the parsing fails, the function logs an error message and returns false. ๐Ÿ˜ž

๐Ÿ” Next, the function checks whether staleContents and latestContents are both strings. If either of them is not a string, the function logs an error message and returns false. ๐Ÿ˜•

๐Ÿ’ป Then, the function initializes the cursor position to 0 and creates a new array called result that is a copy of staleContents.

๐Ÿ”„ The function then loops through each operation described in operations, and performs the corresponding action on result. If any of the operations go past the end or start of the string, or if an invalid operation type is provided, the function logs an error message and returns false. ๐Ÿšซ

๐Ÿ” Finally, the function checks if result matches latestContents after applying all operations. If they do not match, the function logs an error message and returns false. Otherwise, the function returns true. ๐Ÿ˜ƒ

๐Ÿ‘‰ The isValid function is exported as the default export of the module, which means it can be imported and used in other parts of the application. ๐Ÿ’ช

Tests ๐Ÿงช

We have written a total of nine tests in index.spec.js file that validates our implementation of the function. These tests cover all possible edge cases mentioned in the requirements section.

We are using the Jest testing framework to run the tests. You can run the tests by executing the command npm test in the terminal. ๐Ÿงช

Conclusion ๐ŸŽ‰

Operational Transformation is a powerful technique that enables multiple users to collaborate in real-time. The validation of operations is a critical aspect of the OT algorithm, and it's essential to handle all possible edge cases that might occur while applying the operations. With the validation function and the set of tests, we can ensure that the OT algorithm works as expected and handles all edge cases correctly. ๐ŸŽ“