Skip to content

Commit 7ef0314

Browse files
committed
Minor code modernisation
1 parent cbc869c commit 7ef0314

File tree

11 files changed

+32
-84
lines changed

11 files changed

+32
-84
lines changed

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"@babel/preset-env",
55
{
66
"targets": {
7-
"node": "6"
7+
"node": "14"
88
}
99
}
1010
]

.eslintrc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
{
22
"extends": ["airbnb-base", "plugin:prettier/recommended"],
33
"env": {
4-
"node": true
4+
"node": true,
5+
"es2020": true
6+
},
7+
"parserOptions": {
8+
"ecmaVersion": 2020,
9+
"sourceType": "module"
510
}
611
}

src/push-notifications.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ class PN {
3737
sendWith(method, regIds, data, cb) {
3838
return method(regIds, data, this.settings)
3939
.then((results) => {
40-
(cb || ((noop) => noop))(null, results);
40+
cb?.(null, results);
4141
return results;
4242
})
4343
.catch((error) => {
44-
(cb || ((noop) => noop))(error);
44+
cb?.(error);
4545
return Promise.reject(error);
4646
});
4747
}

src/sendADM.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,7 @@ const sendADM = (regIds, _data, settings) => {
1010
};
1111
const promises = [];
1212
const admSender = new adm.Sender(settings.adm);
13-
const data = { ..._data };
14-
const { consolidationKey, expiry, timeToLive, custom } = data;
15-
16-
delete data.consolidationKey;
17-
delete data.expiry;
18-
delete data.timeToLive;
19-
delete data.custom;
13+
const { consolidationKey, expiry, timeToLive, custom, ...data } = _data;
2014

2115
const message = {
2216
expiresAfter:

src/sendAPN.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ const apn = require('@parse/node-apn');
22
const { APN_METHOD } = require('./constants');
33
const { buildApnsMessage } = require('./utils/tools');
44

5-
const getDeviceTokenOrSelf = (token) =>
6-
token && typeof token === 'object' && 'device' in token
7-
? token.device
8-
: token;
5+
const getDeviceTokenOrSelf = (token) => token?.device ?? token;
96

107
class APN {
118
constructor(settings) {

src/sendFCM.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,8 @@ const { FCM_METHOD } = require('./constants');
44
const FcmMessage = require('./utils/fcmMessage');
55
const { containsValidRecipients } = require('./utils/tools');
66

7-
const getRecipientList = (obj) => {
8-
if (obj.tokens) {
9-
return obj.tokens;
10-
}
11-
if (obj.token) {
12-
return [obj.token];
13-
}
14-
if (obj.condition) {
15-
return [obj.condition];
16-
}
17-
if (obj.topic) {
18-
return [obj.topic];
19-
}
20-
return [];
21-
};
7+
const getRecipientList = (obj) =>
8+
obj.tokens ?? [obj.token, obj.condition, obj.topic].filter(Boolean);
229

2310
const sendChunk = (firebaseApp, recipients, message) => {
2411
const firebaseMessage = message.buildWithRecipients(recipients);

src/sendGCM.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,8 @@ const gcm = require('node-gcm');
22
const { GCM_METHOD } = require('./constants');
33
const { containsValidRecipients, buildGcmMessage } = require('./utils/tools');
44

5-
const getRecipientList = (obj) => {
6-
if (obj.registrationTokens) {
7-
return obj.registrationTokens;
8-
}
9-
if (obj.to) {
10-
return [obj.to];
11-
}
12-
if (obj.condition) {
13-
return [obj.condition];
14-
}
15-
return [];
16-
};
5+
const getRecipientList = (obj) =>
6+
obj.registrationTokens ?? [obj.to, obj.condition].filter(Boolean);
177

188
const sendChunk = (GCMSender, recipients, message, retries) =>
199
new Promise((resolve) => {

src/sendWNS.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,8 @@ const wns = require('wns');
22
const { WNS_METHOD } = require('./constants');
33

44
const parseErrorMessage = (err) => (err instanceof Error ? err.message : err);
5-
const parseError = (err) => {
6-
if (err instanceof Error) {
7-
return err;
8-
}
9-
if (err) {
10-
return new Error(err);
11-
}
12-
return null;
13-
};
5+
const parseError = (err) =>
6+
err instanceof Error ? err : err ? new Error(err) : null;
147

158
let resumed;
169

src/sendWeb.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const webPush = require('web-push');
22
const { WEB_METHOD } = require('./constants');
33

4-
const stringify = (data) => typeof data === 'string' ? data : JSON.stringify(data);
4+
const stringify = (data) =>
5+
typeof data === 'string' ? data : JSON.stringify(data);
56

67
const sendWebPush = async (regIds, data, settings) => {
78
const payload = stringify(data);

src/utils/fcmMessage.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,10 @@ class FcmMessage {
2020
static normalizeDataParams(data) {
2121
if (!data) return {};
2222
return Object.entries(data).reduce((normalized, [key, value]) => {
23-
if (value === undefined || value === null) {
24-
return normalized;
25-
}
26-
23+
if (value == null) return normalized;
2724
const stringifyValue =
2825
typeof value === 'string' ? value : JSON.stringify(value);
29-
30-
Object.assign(normalized, { [key]: stringifyValue });
31-
32-
return normalized;
26+
return { ...normalized, [key]: stringifyValue };
3327
}, {});
3428
}
3529

0 commit comments

Comments
 (0)