Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Changing representation of operations to a string where the first cha…

…racter is the operation type
  • Loading branch information...
commit fafde5034c4a3260fe119629b9d3816ad971ee1e 1 parent 30e05ac
Nick Fitzgerald authored April 21, 2011

Showing 1 changed file with 16 additions and 5 deletions. Show diff stats Hide diff stats

  1. 21  operations.js
21  operations.js
@@ -14,23 +14,34 @@ define(function () {
14 14
     // Simple change constructors.
15 15
 
16 16
     function insert (chars) {
17  
-        return ["insert", chars];
  17
+        return "i" + chars;
18 18
     }
19 19
 
20 20
     function del (chars) {
21  
-        return ["delete", chars];
  21
+        return "d" + chars;
22 22
     }
23 23
 
24 24
     function retain (n) {
25  
-        return ["retain", n];
  25
+        return "r" + String(n);
26 26
     }
27 27
 
28 28
     function type (change) {
29  
-        return change[0];
  29
+        switch ( change.charAt(0) ) {
  30
+        case "r":
  31
+            return "retain";
  32
+        case "d":
  33
+            return "delete";
  34
+        case "i":
  35
+            return "insert";
  36
+        default:
  37
+            throw new TypeError("Unknown type of change: ", change);
  38
+        }
30 39
     }
31 40
 
32 41
     function val (change) {
33  
-        return change[1];
  42
+        return type(change) === "r"
  43
+            ? Number(change.slice(1))
  44
+            : change.slice(1);
34 45
     }
35 46
 
36 47
     // We don't want to copy arrays all the time, aren't mutating lists, and

0 notes on commit fafde50

Please sign in to comment.
Something went wrong with that request. Please try again.