Skip to content

Commit 17eeba1

Browse files
committed
Merge pull request #70 from devlucky/project/style-fixes
Project/style fixes
2 parents cda1ca3 + ce726fb commit 17eeba1

File tree

11 files changed

+73
-78
lines changed

11 files changed

+73
-78
lines changed

src/Database/index.js

+24-25
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ const pushToStore = (collectionName, records, store) => {
88
recordFactory(record, collectionName, store));
99
};
1010

11-
const deepMap = (obj, fn) => {
12-
return _.mapValues(obj, (value, key) => {
13-
if (_.isPlainObject(value)) return deepMap(value, fn);
14-
return fn(value);
15-
});
16-
}
11+
const deepMap = (obj, fn) => _.mapValues(obj, (value) => {
12+
if (_.isPlainObject(value)) return deepMap(value, fn);
13+
return fn(value);
14+
});
1715

1816
export class Database {
1917
constructor() {
@@ -23,15 +21,16 @@ export class Database {
2321
all(collectionName, raw = false) {
2422
this.checkFactoryPresence(collectionName);
2523
const records = _.cloneDeep(this.store[collectionName]);
26-
if (raw) {return records;}
24+
if (raw) { return records; }
2725

28-
return this.serialize(records, collectionName)
26+
return this.serialize(records, collectionName);
2927
}
3028

3129
belongsTo(collectionName, predicate) {
32-
return () => predicate ?
33-
this.find(collectionName, predicate) :
34-
this.randomRecord(collectionName);
30+
return () => {
31+
if (predicate) { return this.find(collectionName, predicate); }
32+
return this.randomRecord(collectionName);
33+
};
3534
}
3635

3736
hasMany(collectionName, limit = randomIndex(this.all(collectionName)) + 1) {
@@ -48,11 +47,13 @@ export class Database {
4847
this.checkFactoryPresence(collectionName);
4948

5049
const factory = this.factoryFor(collectionName);
51-
const records = this.store[collectionName] || [];
50+
const records = this.store[collectionName] || [];
5251

53-
while (size--) {
54-
const record = deepMap(factory(faker), field =>
55-
_.isFunction(field) ? field() : field);
52+
for (let idx = 0; idx < size; ++idx) {
53+
const record = deepMap(factory(faker), (field) => {
54+
if (_.isFunction(field)) { return field(); }
55+
return field;
56+
});
5657

5758
records.push(this.decorateRecord(collectionName, record));
5859
}
@@ -62,7 +63,7 @@ export class Database {
6263

6364
decorateRecord(collectionName, record) {
6465
this.checkFactoryPresence(collectionName);
65-
return _.assign({}, record, {id: this.uuid(collectionName)});
66+
return _.assign({}, record, { id: this.uuid(collectionName) });
6667
}
6768

6869
factoryFor(collectionName) {
@@ -101,8 +102,7 @@ export class Database {
101102
push(collectionName, record) {
102103
this.checkFactoryPresence(collectionName);
103104

104-
const factory = this.factoryFor(collectionName);
105-
const records = this.store[collectionName] || [];
105+
const records = this.store[collectionName] || [];
106106
const content = _.castArray(record);
107107

108108
records.push(...content);
@@ -111,14 +111,14 @@ export class Database {
111111
}
112112

113113
register(collectionName, factory, serializer) {
114-
this.factories[collectionName] = {factory, serializer};
114+
this.factories[collectionName] = { factory, serializer };
115115
this.store[collectionName] = [];
116116
}
117117

118118
serialize(record, collectionName) {
119119
const serializer = this.serializerFor(collectionName);
120120
const records = _.castArray(record);
121-
if (!serializer) {return records;}
121+
if (!serializer) { return records; }
122122

123123
return records.map(r => serializer(r, collectionName));
124124
}
@@ -137,9 +137,8 @@ export class Database {
137137
const all = this.all(collectionName);
138138
const records = [];
139139

140-
while (limit) {
140+
for (let idx = 0; idx < limit; ++idx) {
141141
records.push(randomItem(all));
142-
limit--;
143142
}
144143

145144
return records;
@@ -152,14 +151,14 @@ export class Database {
152151
setInitialState() {
153152
this.factories = {};
154153
this.store = {};
155-
this._uuids = {};
154+
this.uuids = {};
156155
}
157156

158157
uuid(collectionName) {
159158
this.checkFactoryPresence(collectionName);
160159

161-
const id = this._uuids[collectionName] || 0;
162-
this._uuids[collectionName] = id + 1;
160+
const id = this.uuids[collectionName] || 0;
161+
this.uuids[collectionName] = id + 1;
163162

164163
return id;
165164
}

src/Database/recordFactory.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import _ from 'lodash';
22

33
const updateRecord = (record, collectionName, store) => {
4-
const originalRecord = _.find(store[collectionName], {id: record.id});
4+
const originalRecord = _.find(store[collectionName], { id: record.id });
55
return _.assign(originalRecord, record);
66
};
77

88
export const recordFactory = (record, collectionName, store) => {
9-
record.save = function() {
10-
return updateRecord(this, collectionName, store);
9+
const recordExtension = {
10+
save() {
11+
return updateRecord(this, collectionName, store);
12+
},
1113
};
1214

13-
return record;
15+
return Object.assign({}, record, recordExtension);
1416
};

src/Router/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import _ from 'lodash';
22
import { interceptors } from '../interceptors';
33

44
const routerDefaultConfig = {
5-
strategies: ['fetch', 'XMLHttpRequest']
5+
strategies: ['fetch', 'XMLHttpRequest'],
66
};
77

88
const interceptorDefaultConfig = {
99
host: '',
1010
requestDelay: 0,
11-
routes: {GET: {}, POST: {}, PUT: {}, DELETE: {}}
11+
routes: { GET: {}, POST: {}, PUT: {}, DELETE: {} },
1212
};
1313

1414
export class Router {

src/helpers/util.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
export const randomIndex = (arr) => {
2-
return Math.floor(Math.random() * arr.length);
3-
}
1+
export const randomIndex = arr => Math.floor(Math.random() * arr.length);
42

5-
export const randomItem = (arr) => {
6-
return arr[randomIndex(arr)];
7-
}
3+
export const randomItem = arr => arr[randomIndex(arr)];
84

9-
export const lastItem = (arr) => {
10-
return arr[arr.length - 1];
11-
}
5+
export const lastItem = arr => arr[arr.length - 1];

src/interceptors/baseInterceptor.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import _ from 'lodash';
2-
import queryString from 'query-string';
32
import pathMatch from 'path-match';
43
import parseUrl from 'parse-url';
54

@@ -15,7 +14,7 @@ export const baseInterceptor = ({ routes, host }, fakeService) => {
1514
const extractUrl = (url, method) => ({
1615
handlers: routes[method],
1716
pathname: parseUrl(url).pathname,
18-
fullpath: parseUrl(url).href
17+
fullpath: parseUrl(url).href,
1918
});
2019

2120
const getHandler = (url, method) => {
@@ -33,5 +32,5 @@ export const baseInterceptor = ({ routes, host }, fakeService) => {
3332
return route ? matchesPathname(route) : null;
3433
};
3534

36-
return fakeService.bind(null, {getHandler, getParams});
35+
return fakeService.bind(null, { getHandler, getParams });
3736
};

src/interceptors/fetchInterceptor.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import queryString from 'query-string';
2-
import pathMatch from 'path-match';
32
import parseUrl from 'parse-url';
43

54
import { baseInterceptor } from './baseInterceptor';
@@ -9,17 +8,16 @@ import { nativeFetch } from '../helpers/nativeServices';
98
export const name = 'fetch';
109
export const reference = nativeFetch;
1110

12-
const fakeResponse = function(response = {}, headers = {}) {
11+
const fakeResponse = (response = {}, headers = {}) => {
1312
const responseStr = JSON.stringify(response);
14-
15-
return new window.Response(responseStr, {headers});
13+
return new window.Response(responseStr, { headers });
1614
};
1715

1816
export const fakeService = config =>
1917
baseInterceptor(config, (helpers, url, options = {}) => {
2018
const body = options.body || '';
2119
const method = options.method || 'GET';
22-
const headers = options.headers || {};
20+
const headers = options.headers || {};
2321

2422
const handler = helpers.getHandler(url, method);
2523
const params = helpers.getParams(url, method);
@@ -29,14 +27,21 @@ export const fakeService = config =>
2927
}
3028

3129
const query = queryString.parse(parseUrl(url).search);
32-
const response = handler({params, query, body, headers});
30+
const response = handler({ params, query, body, headers });
3331

3432
if (!(response instanceof KakapoResponse)) {
35-
return new Promise((resolve, reject) => setTimeout(() =>
36-
resolve(fakeResponse(response)), config.requestDelay));
33+
return new Promise((resolve) => setTimeout(
34+
() => resolve(fakeResponse(response)),
35+
config.requestDelay
36+
));
3737
}
3838

3939
const result = fakeResponse(response.body, response.headers);
40-
return new Promise((resolve, reject) => setTimeout(() =>
41-
response.error ? reject(result) : resolve(result), config.requestDelay));
40+
return new Promise((resolve, reject) => setTimeout(
41+
() => {
42+
if (response.error) { return reject(result); }
43+
return resolve(result);
44+
},
45+
config.requestDelay
46+
));
4247
});

src/interceptors/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import * as xhrInterceptor from './xhrInterceptor';
22
import * as fetchInterceptor from './fetchInterceptor';
33

4-
const interceptor = ({name, reference, fakeService}) => ({
4+
const interceptor = ({ name, reference, fakeService }) => ({
55
enable(config) {
66
window[name] = fakeService(config);
77
},
88
disable() {
99
window[name] = reference;
10-
}
10+
},
1111
});
1212

1313
export const interceptors = {
14-
'XMLHttpRequest': interceptor(xhrInterceptor),
15-
'fetch': interceptor(fetchInterceptor)
14+
XMLHttpRequest: interceptor(xhrInterceptor),
15+
fetch: interceptor(fetchInterceptor),
1616
};

src/interceptors/xhrInterceptor.js

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
import queryString from 'query-string';
2-
import pathMatch from 'path-match';
3-
import parseUrl from 'parse-url';
4-
51
import { baseInterceptor } from './baseInterceptor';
62
import { nativeXHR } from '../helpers/nativeServices';
73

84
export const name = 'XMLHttpRequest';
9-
export const reference = nativeXHR;
5+
export const Reference = nativeXHR;
106

117
export const fakeService = config =>
128
baseInterceptor(config, class fakeXMLHttpRequest {
139
constructor(helpers) {
14-
this.xhr = new reference();
10+
this.xhr = new Reference();
1511
this.getHandler = helpers.getHandler;
1612
this.getParams = helpers.getParams;
1713
}
@@ -28,10 +24,9 @@ export const fakeService = config =>
2824

2925
if (handler && this.onreadystatechange) {
3026
this.readyState = 4;
31-
this.status = 200; //TODO: Support custom status codes
32-
this.responseText = handler({params});
33-
this.onreadystatechange();
34-
return;
27+
this.status = 200; // @TODO (zzarcon): Support custom status codes
28+
this.responseText = handler({ params });
29+
return this.onreadystatechange();
3530
}
3631

3732
this.xhr.onreadystatechange = () => {
@@ -41,6 +36,6 @@ export const fakeService = config =>
4136
this.onreadystatechange.call(this.xhr);
4237
};
4338

44-
this.xhr.send();
39+
return this.xhr.send();
4540
}
4641
});

src/serializers/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './json-api';
1+
export * from './json-api';

src/serializers/json-api.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
//TODO: Implement 'included' support after relationships
1+
import _ from 'lodash';
2+
3+
// @TODO (zzarcon): Implement 'included' support after relationships
24
export const JSONApiSerializer = (record = {}, type = null) => {
35
const id = record.id;
4-
const relationships = {};
56
const included = [];
6-
7-
delete record.id;
7+
const relationships = {};
8+
const serializedRecord = _.pickBy(record, (value, key) => key !== 'id');
89

910
return {
1011
data: {
1112
id,
12-
attributes: record,
13+
attributes: serializedRecord,
1314
relationships,
14-
type
15+
type,
1516
},
16-
included
17+
included,
1718
};
18-
};
19+
};

test/specs/database_spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const databaseSpec = () => {
2323

2424
assert.ok(_.isObject(db.store), 'Sets up initial store object.');
2525
assert.ok(_.isObject(db.factories), 'Sets up initial factorie object.');
26-
assert.ok(_.isObject(db._uuids), 'Sets up initial identifier object.');
26+
assert.ok(_.isObject(db.uuids), 'Sets up initial identifier object.');
2727

2828
assert.end();
2929
});

0 commit comments

Comments
 (0)