Skip to content

Commit cbc869c

Browse files
committed
Remove ramda dependency
1 parent 12f2b14 commit cbc869c

File tree

5 files changed

+73
-67
lines changed

5 files changed

+73
-67
lines changed

package-lock.json

Lines changed: 0 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
"firebase-admin": "12.1.1",
5656
"node-adm": "0.9.1",
5757
"node-gcm": "1.1.4",
58-
"ramda": "0.32.0",
5958
"web-push": "3.6.7",
6059
"wns": "0.5.4"
6160
},

src/sendAPN.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
const apn = require('@parse/node-apn');
2-
const R = require('ramda');
32
const { APN_METHOD } = require('./constants');
43
const { buildApnsMessage } = require('./utils/tools');
54

6-
const getDeviceTokenOrSelf = R.ifElse(
7-
R.has('device'),
8-
R.prop('device'),
9-
R.identity
10-
);
5+
const getDeviceTokenOrSelf = (token) =>
6+
token && typeof token === 'object' && 'device' in token
7+
? token.device
8+
: token;
119

1210
class APN {
1311
constructor(settings) {

src/sendWeb.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
const webPush = require('web-push');
2-
const { is, unless, assoc } = require('ramda');
32
const { WEB_METHOD } = require('./constants');
43

5-
const stringify = unless(is(String), JSON.stringify);
4+
const stringify = (data) => typeof data === 'string' ? data : JSON.stringify(data);
65

76
const sendWebPush = async (regIds, data, settings) => {
87
const payload = stringify(data);
@@ -39,7 +38,7 @@ const sendWebPush = async (regIds, data, settings) => {
3938
failure: acc.failure + current.failure,
4039
message: [...acc.message, ...current.message],
4140
}));
42-
return assoc('method', WEB_METHOD, reduced);
41+
return { ...reduced, method: WEB_METHOD };
4342
};
4443

4544
module.exports = sendWebPush;

src/utils/tools.js

Lines changed: 67 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
1-
const R = require('ramda');
21
const { Notification: ApnsMessage } = require('@parse/node-apn');
32
const { Message: GcmMessage } = require('node-gcm');
43

54
const { DEFAULT_TTL, GCM_MAX_TTL } = require('../constants');
65

7-
const ttlFromExpiry = R.compose(
8-
R.min(GCM_MAX_TTL),
9-
R.max(0),
10-
(expiry) => expiry - Math.floor(Date.now() / 1000)
11-
);
6+
const ttlFromExpiry = (expiry) => {
7+
const ttl = expiry - Math.floor(Date.now() / 1000);
8+
return Math.min(Math.max(ttl, 0), GCM_MAX_TTL);
9+
};
1210

13-
const extractTimeToLive = R.cond([
14-
[R.propIs(Number, 'expiry'), ({ expiry }) => ttlFromExpiry(expiry)],
15-
[R.propIs(Number, 'timeToLive'), R.prop('timeToLive')],
16-
[R.T, R.always(DEFAULT_TTL)],
17-
]);
11+
const extractTimeToLive = (data) => {
12+
if (typeof data.expiry === 'number') {
13+
return ttlFromExpiry(data.expiry);
14+
}
15+
if (typeof data.timeToLive === 'number') {
16+
return data.timeToLive;
17+
}
18+
return DEFAULT_TTL;
19+
};
1820

1921
const expiryFromTtl = (ttl) => ttl + Math.floor(Date.now() / 1000);
2022

21-
const extractExpiry = R.cond([
22-
[R.propIs(Number, 'expiry'), R.prop('expiry')],
23-
[
24-
R.propIs(Number, 'timeToLive'),
25-
({ timeToLive }) => expiryFromTtl(timeToLive),
26-
],
27-
[R.T, () => expiryFromTtl(DEFAULT_TTL)],
28-
]);
23+
const extractExpiry = (data) => {
24+
if (typeof data.expiry === 'number') {
25+
return data.expiry;
26+
}
27+
if (typeof data.timeToLive === 'number') {
28+
return expiryFromTtl(data.timeToLive);
29+
}
30+
return expiryFromTtl(DEFAULT_TTL);
31+
};
2932

3033
const getPropValueOrUndefinedIfIsSilent = (propName, data) => {
3134
if (data.silent) {
@@ -34,17 +37,28 @@ const getPropValueOrUndefinedIfIsSilent = (propName, data) => {
3437
return data[propName];
3538
};
3639

37-
const toJSONorUndefined = R.when(
38-
R.is(String),
39-
R.tryCatch(JSON.parse, R.always(undefined))
40-
);
40+
const toJSONorUndefined = (value) => {
41+
if (typeof value !== 'string') {
42+
return value;
43+
}
44+
try {
45+
return JSON.parse(value);
46+
} catch (e) {
47+
return undefined;
48+
}
49+
};
4150

42-
const alertLocArgsToJSON = R.evolve({
43-
alert: {
44-
'title-loc-args': toJSONorUndefined,
45-
'loc-args': toJSONorUndefined,
46-
},
47-
});
51+
const alertLocArgsToJSON = (data) => {
52+
const alert = data.alert || {};
53+
return {
54+
...data,
55+
alert: {
56+
...alert,
57+
'title-loc-args': toJSONorUndefined(alert['title-loc-args']),
58+
'loc-args': toJSONorUndefined(alert['loc-args']),
59+
},
60+
};
61+
};
4862

4963
const getDefaultAlert = (data) => ({
5064
title: data.title,
@@ -57,21 +71,33 @@ const getDefaultAlert = (data) => ({
5771
action: data.action,
5872
});
5973

60-
const alertOrDefault = (data) =>
61-
R.when(
62-
R.propSatisfies(R.isNil, 'alert'),
63-
R.assoc('alert', getDefaultAlert(data))
64-
);
74+
const alertOrDefault = (data) => {
75+
if (data.alert !== null && data.alert !== undefined) {
76+
return data;
77+
}
78+
return { ...data, alert: getDefaultAlert(data) };
79+
};
6580

66-
const getParsedAlertOrDefault = (data) =>
67-
R.pipe(alertOrDefault(data), alertLocArgsToJSON)(data);
81+
const getParsedAlertOrDefault = (data) => {
82+
const withAlert = alertOrDefault(data);
83+
return alertLocArgsToJSON(withAlert);
84+
};
6885

69-
const pathIsString = R.pathSatisfies(R.is(String));
86+
const pathIsString = (path) => (val) => {
87+
const current = path.reduce((acc, key) => {
88+
if (acc && typeof acc === 'object') {
89+
return acc[key];
90+
}
91+
return null;
92+
}, val);
93+
return typeof current === 'string';
94+
};
7095

71-
const containsValidRecipients = R.either(
72-
pathIsString(['recipients', 'to']),
73-
pathIsString(['recipients', 'condition'])
74-
);
96+
const containsValidRecipients = (obj) => {
97+
const checkTo = pathIsString(['recipients', 'to'])(obj);
98+
const checkCondition = pathIsString(['recipients', 'condition'])(obj);
99+
return checkTo || checkCondition;
100+
};
75101

76102
const buildGcmNotification = (data) => {
77103
const notification = data.fcm_notification || {

0 commit comments

Comments
 (0)