Skip to content

Commit 69c1942

Browse files
committed
Allow a max depth to be passed to Resource#toJSON
1 parent a4aaa2b commit 69c1942

File tree

2 files changed

+134
-4
lines changed

2 files changed

+134
-4
lines changed

lib/Resource.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,21 +100,21 @@ export class Resource {
100100
/**
101101
* Create a convenient JSON representation of a Resource.
102102
*/
103-
public toJSON(): any {
103+
public toJSON(maxDepth = -1): any {
104104
if (Object.keys(this.properties).length === 0 && !this.list) {
105105
return termToString(this.term);
106106
}
107107
return {
108108
'@id': termToString(this.term),
109-
...Object.keys(this.properties).length > 0 ?
109+
...maxDepth !== 0 && Object.keys(this.properties).length > 0 ?
110110
{
111111
properties: Object.keys(this.properties).reduce((acc: any, key) => {
112-
acc[key] = this.properties[key].map(resource => resource.toJSON());
112+
acc[key] = this.properties[key].map(resource => resource.toJSON(maxDepth - 1));
113113
return acc;
114114
}, {}),
115115
} :
116116
{},
117-
...this.list ? { list: this.list.map(resource => resource.toJSON()) } : {},
117+
...maxDepth !== 0 && this.list ? { list: this.list.map(resource => resource.toJSON(maxDepth - 1)) } : {},
118118
};
119119
}
120120

test/Resource-test.ts

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,136 @@ describe('Resource', () => {
151151
],
152152
});
153153
});
154+
155+
it('with nested list and properties and no max depth', () => {
156+
const value1 = new Resource({ term: DF.namedNode('ex:value1') });
157+
const value1_1 = new Resource({ term: DF.namedNode('ex:value1.1') });
158+
const value1_1_1 = new Resource({ term: DF.namedNode('ex:value1.1.1') });
159+
value1_1.properties['ex:a.1.1'].push(value1_1_1);
160+
value1.properties['ex:a.1'].push(value1_1);
161+
resource.properties['ex:a'].push(value1);
162+
resource.properties['ex:b'].push(new Resource({ term: DF.literal('value2') }));
163+
resource.list = [];
164+
resource.list.push(value1);
165+
resource.list.push(new Resource({ term: DF.literal('value2') }));
166+
expect(resource.toJSON()).toEqual({
167+
'@id': 'http://example.org/resource1',
168+
properties: {
169+
'http://example.org/a': [
170+
{
171+
'@id': 'ex:value1',
172+
properties: {
173+
'ex:a.1': [
174+
{
175+
'@id': 'ex:value1.1',
176+
properties: {
177+
'ex:a.1.1': [
178+
'ex:value1.1.1',
179+
],
180+
},
181+
},
182+
],
183+
},
184+
},
185+
],
186+
'http://example.org/b': [
187+
'"value2"',
188+
],
189+
},
190+
list: [
191+
{
192+
'@id': 'ex:value1',
193+
properties: {
194+
'ex:a.1': [
195+
{
196+
'@id': 'ex:value1.1',
197+
properties: {
198+
'ex:a.1.1': [
199+
'ex:value1.1.1',
200+
],
201+
},
202+
},
203+
],
204+
},
205+
},
206+
'"value2"',
207+
],
208+
});
209+
});
210+
211+
it('with nested list and properties and a max depth of 1', () => {
212+
const value1 = new Resource({ term: DF.namedNode('ex:value1') });
213+
const value1_1 = new Resource({ term: DF.namedNode('ex:value1.1') });
214+
const value1_1_1 = new Resource({ term: DF.namedNode('ex:value1.1.1') });
215+
value1_1.properties['ex:a.1.1'].push(value1_1_1);
216+
value1.properties['ex:a.1'].push(value1_1);
217+
resource.properties['ex:a'].push(value1);
218+
resource.properties['ex:b'].push(new Resource({ term: DF.literal('value2') }));
219+
resource.list = [];
220+
resource.list.push(value1);
221+
resource.list.push(new Resource({ term: DF.literal('value2') }));
222+
expect(resource.toJSON(1)).toEqual({
223+
'@id': 'http://example.org/resource1',
224+
properties: {
225+
'http://example.org/a': [
226+
{ '@id': 'ex:value1' },
227+
],
228+
'http://example.org/b': [
229+
'"value2"',
230+
],
231+
},
232+
list: [
233+
{ '@id': 'ex:value1' },
234+
'"value2"',
235+
],
236+
});
237+
});
238+
239+
it('with nested list and properties and a max depth of 2', () => {
240+
const value1 = new Resource({ term: DF.namedNode('ex:value1') });
241+
const value1_1 = new Resource({ term: DF.namedNode('ex:value1.1') });
242+
const value1_1_1 = new Resource({ term: DF.namedNode('ex:value1.1.1') });
243+
value1_1.properties['ex:a.1.1'].push(value1_1_1);
244+
value1.properties['ex:a.1'].push(value1_1);
245+
resource.properties['ex:a'].push(value1);
246+
resource.properties['ex:b'].push(new Resource({ term: DF.literal('value2') }));
247+
resource.list = [];
248+
resource.list.push(value1);
249+
resource.list.push(new Resource({ term: DF.literal('value2') }));
250+
expect(resource.toJSON(2)).toEqual({
251+
'@id': 'http://example.org/resource1',
252+
properties: {
253+
'http://example.org/a': [
254+
{
255+
'@id': 'ex:value1',
256+
properties: {
257+
'ex:a.1': [
258+
{
259+
'@id': 'ex:value1.1',
260+
},
261+
],
262+
},
263+
},
264+
],
265+
'http://example.org/b': [
266+
'"value2"',
267+
],
268+
},
269+
list: [
270+
{
271+
'@id': 'ex:value1',
272+
properties: {
273+
'ex:a.1': [
274+
{
275+
'@id': 'ex:value1.1',
276+
},
277+
],
278+
},
279+
},
280+
'"value2"',
281+
],
282+
});
283+
});
154284
});
155285
});
156286

0 commit comments

Comments
 (0)