Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/fromRdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ api.fromRDF = async (
{
useRdfType = false,
useNativeTypes = false,
rdfDirection = null
rdfDirection = null,
preserveOrder = false,
}
) => {
const defaultGraph = {};
Expand Down Expand Up @@ -246,13 +247,13 @@ api.fromRDF = async (
}

const result = [];
const subjects = Object.keys(defaultGraph).sort();
const subjects = preserveOrder ? Object.keys(defaultGraph): Object.keys(defaultGraph).sort();
for(const subject of subjects) {
const node = defaultGraph[subject];
if(subject in graphMap) {
const graph = node['@graph'] = [];
const graphObject = graphMap[subject];
const graphSubjects = Object.keys(graphObject).sort();
const graphSubjects = preserveOrder ? Object.keys(graphObject) : Object.keys(graphObject).sort();
for(const graphSubject of graphSubjects) {
const node = graphObject[graphSubject];
// only add full subjects to top-level
Expand Down
150 changes: 150 additions & 0 deletions tests/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,156 @@ describe('other fromRDF tests', () => {
done();
});
});

it('should preserve object and property order when specified', done => {
const nq = `
<http://example.com/Subject3/Property3> <http://example.com/value> "3" <http://example.com/Subject3> .\n
<http://example.com/Subject3/Property1> <http://example.com/value> "1" <http://example.com/Subject3> .\n
<http://example.com/Subject3/Property2> <http://example.com/value> "2" <http://example.com/Subject3> .\n

<http://example.com/Subject1/Property3> <http://example.com/value> "3" <http://example.com/Subject1> .\n
<http://example.com/Subject1/Property1> <http://example.com/value> "1" <http://example.com/Subject1> .\n
<http://example.com/Subject1/Property2> <http://example.com/value> "2" <http://example.com/Subject1> .\n

<http://example.com/Subject2/Property3> <http://example.com/value> "3" <http://example.com/Subject2> .\n
<http://example.com/Subject2/Property1> <http://example.com/value> "1" <http://example.com/Subject2> .\n
<http://example.com/Subject2/Property2> <http://example.com/value> "2" <http://example.com/Subject2> .\n
`;
const p = jsonld.fromRDF(nq, {preserveOrder: true});
assert(p instanceof Promise);
p.catch(e => {
assert.ifError(e);
}).then(output => {
assert.deepEqual(
output,
[
{"@id": "http://example.com/Subject3",
"@graph": [
{
"@id": "http://example.com/Subject3/Property3",
"http://example.com/value": [{"@value": "3"}]
},
{
"@id": "http://example.com/Subject3/Property1",
"http://example.com/value": [{"@value": "1"}]
},
{
"@id": "http://example.com/Subject3/Property2",
"http://example.com/value": [{"@value": "2"}]
}
]
},
{"@id": "http://example.com/Subject1",
"@graph": [
{
"@id": "http://example.com/Subject1/Property3",
"http://example.com/value": [{"@value": "3"}]
},
{
"@id": "http://example.com/Subject1/Property1",
"http://example.com/value": [{"@value": "1"}]
},
{
"@id": "http://example.com/Subject1/Property2",
"http://example.com/value": [{"@value": "2"}]
}
]
},
{"@id": "http://example.com/Subject2",
"@graph": [
{
"@id": "http://example.com/Subject2/Property3",
"http://example.com/value": [{"@value": "3"}]
},
{
"@id": "http://example.com/Subject2/Property1",
"http://example.com/value": [{"@value": "1"}]
},
{
"@id": "http://example.com/Subject2/Property2",
"http://example.com/value": [{"@value": "2"}]
}
]
}
]);
done();
});
});

it('orders objects and properties by default or when specified', done => {
const nq = `
<http://example.com/Subject3/Property3> <http://example.com/value> "3" <http://example.com/Subject3> .\n
<http://example.com/Subject3/Property1> <http://example.com/value> "1" <http://example.com/Subject3> .\n
<http://example.com/Subject3/Property2> <http://example.com/value> "2" <http://example.com/Subject3> .\n

<http://example.com/Subject1/Property3> <http://example.com/value> "3" <http://example.com/Subject1> .\n
<http://example.com/Subject1/Property1> <http://example.com/value> "1" <http://example.com/Subject1> .\n
<http://example.com/Subject1/Property2> <http://example.com/value> "2" <http://example.com/Subject1> .\n

<http://example.com/Subject2/Property3> <http://example.com/value> "3" <http://example.com/Subject2> .\n
<http://example.com/Subject2/Property1> <http://example.com/value> "1" <http://example.com/Subject2> .\n
<http://example.com/Subject2/Property2> <http://example.com/value> "2" <http://example.com/Subject2> .\n
`;
const p = jsonld.fromRDF(nq);
assert(p instanceof Promise);
p.catch(e => {
assert.ifError(e);
}).then(output => {
assert.deepEqual(
output,
[
{"@id": "http://example.com/Subject1",
"@graph": [
{
"@id": "http://example.com/Subject1/Property1",
"http://example.com/value": [{"@value": "1"}]
},
{
"@id": "http://example.com/Subject1/Property2",
"http://example.com/value": [{"@value": "2"}]
},
{
"@id": "http://example.com/Subject1/Property3",
"http://example.com/value": [{"@value": "3"}]
}
]
},
{"@id": "http://example.com/Subject2",
"@graph": [
{
"@id": "http://example.com/Subject2/Property1",
"http://example.com/value": [{"@value": "1"}]
},
{
"@id": "http://example.com/Subject2/Property2",
"http://example.com/value": [{"@value": "2"}]
},
{
"@id": "http://example.com/Subject2/Property3",
"http://example.com/value": [{"@value": "3"}]
}
]
},
{"@id": "http://example.com/Subject3",
"@graph": [
{
"@id": "http://example.com/Subject3/Property1",
"http://example.com/value": [{"@value": "1"}]
},
{
"@id": "http://example.com/Subject3/Property2",
"http://example.com/value": [{"@value": "2"}]
},
{
"@id": "http://example.com/Subject3/Property3",
"http://example.com/value": [{"@value": "3"}]
}
]
}
]);
done();
});
});
});

describe('loading multiple levels of contexts', () => {
Expand Down