Skip to content

Commit 8974b41

Browse files
committed
Merge pull request #11 from thomsbg/master
Fix transformComponent to handle subtypes that do not return arrays
2 parents 7940c8e + 6c03d6a commit 8974b41

2 files changed

Lines changed: 22 additions & 12 deletions

File tree

lib/json0.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -444,20 +444,18 @@ json.transformComponent = function(dest, c, otherC, type) {
444444
if (c.t && c.t === oc.t) {
445445
var res = subtypes[c.t].transform(c.o, oc.o, type);
446446

447-
if (res.length > 0) {
448-
// convert back to old string ops
449-
if (c.si != null || c.sd != null) {
450-
var p = c.p;
451-
for (var i = 0; i < res.length; i++) {
452-
c.o = [res[i]];
453-
c.p = p.slice();
454-
convertToText(c);
455-
json.append(dest, c);
456-
}
457-
} else {
458-
c.o = res;
447+
// convert back to old string ops
448+
if (c.si != null || c.sd != null) {
449+
var p = c.p;
450+
for (var i = 0; i < res.length; i++) {
451+
c.o = [res[i]];
452+
c.p = p.slice();
453+
convertToText(c);
459454
json.append(dest, c);
460455
}
456+
} else if (!isArray(res) || res.length > 0) {
457+
c.o = res;
458+
json.append(dest, c);
461459
}
462460

463461
return dest;

test/json0.coffee

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ nativetype = require '../lib/json0'
55

66
fuzzer = require 'ot-fuzzer'
77

8+
nativetype.registerSubtype
9+
name: 'mock'
10+
transform: (a, b, side) ->
11+
return { mock: true }
12+
813
# Cross-transform helper function. Transform server by client and client by
914
# server. Returns [server, client].
1015
transformX = (type, left, right) ->
@@ -96,6 +101,13 @@ genTests = (type) ->
96101
it 'does not throw errors with blank inserts', ->
97102
assert.deepEqual type.transform([{p:['k'], t:'text0', o:[{p:5, i:''}]}], [{p:['k'], t:'text0', o:[{p:3, i:'a'}]}], 'left'), []
98103

104+
describe 'subtype with non-array operation', ->
105+
describe '#transform()', ->
106+
it 'works', ->
107+
a = [{p:[], t:'mock', o:'foo'}]
108+
b = [{p:[], t:'mock', o:'bar'}]
109+
assert.deepEqual type.transform(a, b, 'left'), [{p:[], t:'mock', o:{mock:true}}]
110+
99111
describe 'list', ->
100112
describe 'apply', ->
101113
it 'inserts', ->

0 commit comments

Comments
 (0)