Skip to content

Commit d9066d3

Browse files
committed
Pull Request cleanup items
Mostly making CollectionProxy more consistent. Was half acting like an array and half acting like a thing that passes on an array when asked. Made it be the latter.
1 parent 93f8e97 commit d9066d3

File tree

6 files changed

+25
-33
lines changed

6 files changed

+25
-33
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ declare module NodeJS {
1313

1414
interface japiDoc {
1515
data: any; // can't do Array | japiResource
16-
included: Array<japiResource>;
16+
included?: Array<japiResource>;
1717
meta?: any;
1818
}
1919

src/collection-proxy.ts

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,26 @@ class CollectionProxy<T> {
44
private _raw_json : japiDoc;
55
private _array : Array<T>;
66

7-
constructor (raw_json : japiDoc = { data: [], included: [] }) {
8-
this.setRaw(raw_json)
7+
constructor (raw_json : japiDoc = { data: [] }) {
8+
this.setRaw(raw_json);
99
}
1010

1111
get raw () : japiDoc {
12-
return this._raw_json
12+
return this._raw_json;
1313
}
1414

1515
get data () : Array<T> {
16-
return this._array
17-
}
18-
19-
get collection () : Array<T> {
20-
return this._array
16+
return this._array;
2117
}
2218

2319
get meta () : Object {
24-
if (this.raw) {
25-
return this.raw.meta || {}
26-
}
27-
28-
return {}
29-
}
30-
31-
map (fn) {
32-
return this.data.map(fn)
20+
return this.raw.meta || {};
3321
}
3422

3523
private setRaw = (json_payload : japiDoc) => {
36-
this._raw_json = json_payload
24+
this._raw_json = json_payload;
3725

38-
this._array = []
26+
this._array = [];
3927

4028
if (this.raw.data) {
4129
this.raw.data.map((datum : japiResource) => {
@@ -45,4 +33,4 @@ class CollectionProxy<T> {
4533
}
4634
}
4735

48-
export default CollectionProxy
36+
export default CollectionProxy;

src/scope.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ export default class Scope {
2222

2323
all() : Promise<CollectionProxy<Model>> {
2424
return this._fetch(this.model.url()).then((json : japiDoc) => {
25-
let collection = new CollectionProxy(json)
25+
let collection = new CollectionProxy(json);
2626

27-
return collection
27+
return collection;
2828
});
2929
}
3030

@@ -104,6 +104,9 @@ export default class Scope {
104104
return this;
105105
}
106106

107+
// The `Model` class has a `scope()` method to return the scope for it.
108+
// This method makes it possible for methods to expect either a model or
109+
// a scope and reliably cast them to a scope for use via `scope()`
107110
scope() : Scope {
108111
return this;
109112
}

test/integration/finders-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ describe('Model finders', function() {
109109
});
110110
});
111111

112-
it('promise collection includes meta payload', function() {
112+
it('includes meta payload in the resulting collection', function() {
113113
return expect(Person.all()).to.eventually
114114
.have.deep.property('meta.stats.total.count', 45)
115115
});

test/unit/collection-proxy-test.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ import { Person } from '../fixtures';
33

44
import CollectionProxy from '../../src/collection-proxy'
55

6-
beforeEach(function() {
7-
});
8-
96
describe('CollectionProxy', function() {
107
let personData = {
118
data: [
@@ -48,12 +45,16 @@ describe('CollectionProxy', function() {
4845
let collection = new CollectionProxy(personData)
4946
expect(collection.meta).to.eq(personData.meta)
5047
})
51-
})
5248

53-
describe('#collection', function() {
54-
it('should alias the data field', function() {
55-
let collection = new CollectionProxy(personData)
56-
expect(collection.collection).to.eq(collection.data)
49+
describe('meta is null', function() {
50+
let personData = {
51+
data: [],
52+
}
53+
54+
it('should return an empty object', function() {
55+
let collection = new CollectionProxy(personData)
56+
expect(collection.meta).to.deep.eq({})
57+
})
5758
})
5859
})
5960
})

test/unit/model-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ describe('Model', function() {
174174

175175
it('skips relationships without data', function() {
176176
let instance = Model.fromJsonapi(doc.data, doc);
177-
expect(instance.tags.length).to.eql(0);
177+
expect(instance.tags).to.eql([]);
178178
});
179179
});
180180
});

0 commit comments

Comments
 (0)