Skip to content

Commit 061e4e3

Browse files
authored
improve performance, remove dependencies, code refactoring (#70)
* refactor, improve performance, remove dependencies * code refactoring * remove node versions < 8 closes #63
1 parent 9af5c72 commit 061e4e3

14 files changed

+1109
-681
lines changed

Diff for: .eslintignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
node_modules/
22
coverage/
3-
test/**/*.js

Diff for: .eslintrc

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"extends": [
3-
"airbnb-base"
3+
"eslint:recommended",
4+
"airbnb-base",
5+
"plugin:prettier/recommended"
46
],
57
"parserOptions": {
68
"ecmaVersion": 6
@@ -10,13 +12,9 @@
1012
"mocha": true
1113
},
1214
"rules": {
13-
"strict": 0,
14-
"no-param-reassign": 0,
15-
"max-len": [2, 200, 4, {
16-
"ignoreComments": true,
17-
"ignoreUrls": true
18-
}],
15+
"prettier/prettier": ["error", { "singleQuote": true, "printWidth": 100 }],
1916
"no-underscore-dangle": 0,
17+
"no-param-reassign": 0,
2018
"class-methods-use-this": 0
2119
}
2220
}

Diff for: .travis.yml

-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ node_js:
33
- "stable"
44
- "10"
55
- "8"
6-
- "6"
7-
- "5"
8-
- "4"
96
script:
107
- npm run lint
118
- npm test

Diff for: benchmark/index.js

+46-38
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,32 @@ const data = [
2323
lastName: 'Maggio',
2424
2525
age: '80',
26-
gender: 'male',
26+
gender: 'male'
2727
},
2828
tags: ['1', '2'],
2929
photos: [
3030
'ed70cf44-9a34-4878-84e6-0c0e4a450cfe',
3131
'24ba3666-a593-498c-9f5d-55a4ee08c72e',
32-
'f386492d-df61-4573-b4e3-54f6f5d08acf',
32+
'f386492d-df61-4573-b4e3-54f6f5d08acf'
3333
],
3434
comments: [
3535
{
3636
_id: '1',
3737
body: 'First !',
38-
created: '2015-08-14T18:42:16.475Z',
38+
created: '2015-08-14T18:42:16.475Z'
3939
},
4040
{
4141
_id: '2',
4242
body: 'I Like !',
43-
created: '2015-09-14T18:42:12.475Z',
43+
created: '2015-09-14T18:42:12.475Z'
4444
},
4545
{
4646
_id: '3',
4747
body: 'Awesome',
48-
created: '2015-09-15T18:42:12.475Z',
49-
},
50-
],
51-
},
48+
created: '2015-09-15T18:42:12.475Z'
49+
}
50+
]
51+
}
5252
];
5353

5454
serializer.register('article', {
@@ -58,7 +58,7 @@ serializer.register('article', {
5858
self(d) {
5959
// Can be a function or a string value ex: { self: '/articles/1'}
6060
return `/articles/${d.id}`;
61-
},
61+
}
6262
},
6363
relationships: {
6464
// An object defining some relationships.
@@ -68,32 +68,32 @@ serializer.register('article', {
6868
// An object or a function that describes Relationships links
6969
return {
7070
self: `/articles/${d.id}/relationships/author`,
71-
related: `/articles/${d.id}/author`,
71+
related: `/articles/${d.id}/author`
7272
};
73-
},
73+
}
7474
},
7575
tags: {
76-
type: 'tag',
76+
type: 'tag'
7777
},
7878
photos: {
79-
type: 'photo',
79+
type: 'photo'
8080
},
8181
comments: {
8282
type: 'comment',
83-
schema: 'only-body', // A custom schema
84-
},
83+
schema: 'only-body' // A custom schema
84+
}
8585
},
8686
topLevelMeta(d, extraData) {
8787
// An object or a function that describes top level meta.
8888
return {
8989
count: extraData.count,
90-
total: d.length,
90+
total: d.length
9191
};
9292
},
9393
topLevelLinks: {
9494
// An object or a function that describes top level links.
95-
self: '/articles', // Can be a function (with extra data argument) or a string value
96-
},
95+
self: '/articles' // Can be a function (with extra data argument) or a string value
96+
}
9797
});
9898

9999
// Register 'people' type
@@ -102,23 +102,23 @@ serializer.register('people', {
102102
links: {
103103
self(d) {
104104
return `/peoples/${d.id}`;
105-
},
106-
},
105+
}
106+
}
107107
});
108108

109109
// Register 'tag' type
110110
serializer.register('tag', {
111-
id: 'id',
111+
id: 'id'
112112
});
113113

114114
// Register 'photo' type
115115
serializer.register('photo', {
116-
id: 'id',
116+
id: 'id'
117117
});
118118

119119
// Register 'comment' type with a custom schema
120120
serializer.register('comment', 'only-body', {
121-
id: '_id',
121+
id: '_id'
122122
});
123123

124124
let serialized;
@@ -131,13 +131,18 @@ console.log(`${os.type()} ${os.release()} ${os.arch()}`);
131131
console.log('Node.JS:', process.versions.node);
132132
console.log('V8:', process.versions.v8);
133133

134-
let cpus = os.cpus().map(cpu => cpu.model).reduce((o, model) => {
135-
if (!o[model]) o[model] = 0;
136-
o[model] += 1;
137-
return o;
138-
}, {});
134+
let cpus = os
135+
.cpus()
136+
.map(cpu => cpu.model)
137+
.reduce((o, model) => {
138+
if (!o[model]) o[model] = 0;
139+
o[model] += 1;
140+
return o;
141+
}, {});
139142

140-
cpus = Object.keys(cpus).map(key => `${key} \u00d7 ${cpus[key]}`).join('\n');
143+
cpus = Object.keys(cpus)
144+
.map(key => `${key} \u00d7 ${cpus[key]}`)
145+
.join('\n');
141146

142147
console.info(cpus);
143148

@@ -147,17 +152,21 @@ suite
147152
.add('serializeAsync', {
148153
defer: true,
149154
fn(deferred) {
150-
serializer.serializeAsync('article', data, { count: 2 }).then(() => { deferred.resolve(); });
151-
},
155+
serializer.serializeAsync('article', data, { count: 2 }).then(() => {
156+
deferred.resolve();
157+
});
158+
}
152159
})
153160
.add('serialize', () => {
154161
serialized = serializer.serialize('article', data, { count: 2 });
155162
})
156163
.add('deserializeAsync', {
157164
defer: true,
158165
fn(deferred) {
159-
serializer.deserializeAsync('article', serialized).then(() => { deferred.resolve(); });
160-
},
166+
serializer.deserializeAsync('article', serialized).then(() => {
167+
deferred.resolve();
168+
});
169+
}
161170
})
162171
.add('deserialize', () => {
163172
serializer.deserialize('article', serialized);
@@ -174,16 +183,15 @@ suite
174183
status: '422',
175184
source: { pointer: '/data/attributes/error' },
176185
title: 'Error',
177-
detail: 'An error occured',
186+
detail: 'An error occured'
178187
};
179188

180189
serializer.serializeError(jsonapiError);
181190
})
182191
// add listeners
183-
.on('cycle', (event) => {
192+
.on('cycle', event => {
184193
console.log(String(event.target));
185194
})
186-
.on('complete', () => {
187-
})
188-
// run async
195+
.on('complete', () => {})
196+
// run async
189197
.run({ async: false });

0 commit comments

Comments
 (0)