Permalink
Browse files

Added test case and code for merging same-subjects in normalize.

  • Loading branch information...
dlongley committed Aug 7, 2011
1 parent 4bdef91 commit 6f92eb7bf3127e462f24abc2a2de1c6c38cb6ef2
Showing with 24 additions and 7 deletions.
  1. +11 −7 js/jsonld.js
  2. +7 −0 tests/jsonld/normalize-0051-in.json
  3. +1 −0 tests/jsonld/normalize-0051-out.json
  4. +5 −0 tests/jsonld/normalize.test
View
@@ -1029,19 +1029,23 @@ var _flatten = function(parent, parentProperty, value, subjects)
// drop null values
if(v !== null)
{
- if(v.constructor === Array)
+ if(key in subject)
{
- subject[key] = [];
- _flatten(subject[key], null, v, subjects);
- if(subject[key].length === 1)
+ if(subject[key].constructor !== Array)
{
- // convert subject[key] to object if it has only 1
- subject[key] = subject[key][0];
+ subject[key] = [subject[key]];
}
}
else
{
- _flatten(subject, key, v, subjects);
+ subject[key] = [];
+ }
+
+ _flatten(subject[key], null, v, subjects);
+ if(subject[key].length === 1)
+ {
+ // convert subject[key] to object if it has only 1
+ subject[key] = subject[key][0];
}
}
}
@@ -0,0 +1,7 @@
+[{
+ "@subject": "http://example.org/test#example",
+ "http://example.org/test#property": "object1"
+}, {
+ "@subject": "http://example.org/test#example",
+ "http://example.org/test#property": "object2"
+}]
@@ -0,0 +1 @@
+[{"@subject":{"@iri":"http://example.org/test#example"},"http://example.org/test#property":["object1","object2"]}]
@@ -250,5 +250,10 @@
"type": "normalize",
"input": "normalize-0050-in.json",
"expect": "normalize-0050-out.json"
+ }, {
+ "name": "merging subjects",
+ "type": "normalize",
+ "input": "normalize-0051-in.json",
+ "expect": "normalize-0051-out.json"
}]
}

0 comments on commit 6f92eb7

Please sign in to comment.