This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const twilio = require('twilio'); | |
const TAG = 'smsUtility'; | |
/** | |
* Send an SMS text message. | |
* | |
* Example response from Twilio: | |
* { | |
* "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sgMail = require('@sendgrid/mail'); | |
sgMail.setTimeout(40000); | |
const TAG = 'emailUtility'; | |
const DEFAULT_SENDER = 'donotreply@somedomain.com'; | |
/** | |
* Send an email. | |
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const groupBy = (arr, groupFn) => { | |
const grouped = {}; | |
for (const obj of arr) { | |
const groupName = groupFn(obj); | |
if (!grouped[groupName]) { | |
grouped[groupName] = []; | |
} | |
grouped[groupName].push(obj); | |
} | |
return grouped; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* The old way: | |
const reverseString = (str) => { | |
let reversed = ''; | |
for (let i = str.length - 1; i >= 0; i--) { | |
const ch = str[i]; | |
reversed += ch; | |
} | |
return reversed; | |
}; | |
const reverse = reverseString('javascript'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const areEqual = (arr1, arr2) => | |
arr1.sort().join(',') === arr2.sort().join(','); | |
const arr1 = [1, 2, 3, 4]; | |
const arr2 = [3, 4, 1, 2]; | |
const arr3 = [1, 2, 3]; | |
console.log(areEqual(arr1, arr2)); // true | |
console.log(areEqual(arr1, arr3)); // false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const removeDuplicates = (arr) => [...new Set(arr)]; | |
const arr = [1, 2, 3, 4, 5, 3, 1, 2, 5]; | |
const distinct = removeDuplicates(arr); | |
console.log(distinct); // [1, 2, 3, 4, 5] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mapToJson = (map) => JSON.stringify(Object.fromEntries(map)); | |
const map = new Map([ | |
['user1', 'Alison'], | |
['user2', 'Kevin'], | |
['user3', 'James'], | |
]); | |
const json = mapToJson(map); | |
// {"user1":"Alison","user2":"Kevin","user3":"James"} | |
console.log(json); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const jsonToMap = (json) => new Map(Object.entries(JSON.parse(json))); | |
const json = '{"user1":"Alison","user2":"Kevin","user3":"James"}'; | |
const map = jsonToMap(json); | |
// Kevin | |
console.log(map.get('user2')); | |
// Map(3) { 'user1' => 'Alison', 'user2' => 'Kevin', 'user3' => 'James' } | |
console.log(map); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const snakeToCamelCase = (s) => | |
s.toLowerCase().replace(/(_\w)/g, (w) => w.toUpperCase().substr(1)); | |
const str1 = 'string_manipulation'; | |
const str2 = 'one_liner'; | |
console.log(snakeToCamelCase(str1)); // stringManipulation | |
console.log(snakeToCamelCase(str2)); // oneLiner |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* This can be done by the uuid package, which is probably better than using Math.random(), but this is great for unit tests. */ | |
const generateRandomUUID = (a) => | |
a | |
? (a ^ ((Math.random() * 16) >> (a / 4))).toString(16) | |
: ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace( | |
/[018]/g, | |
generateRandomUUID | |
); | |
console.log(generateRandomUUID()); // f138f635-acbd-4f78-9be5-ca3198c4cf34 | |
console.log(generateRandomUUID()); // 8935bb0d-6503-441f-bb25-7bc685b5b5bc |
NewerOlder