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
// Standard | |
[1, 2, 3, 4, 5, 6, 7].reduce((total, num) => total + num); | |
// Reusable functional style | |
const add = (...nums) => nums.reduce((total, num) => total + num); | |
add(1, 2, 3, 4, 5, 6, 7); | |
// Returns: 28 |
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
arr.reduce((accumulator, currentValue, currentIndex, sourceArray) => { | |
// accumulator: the current running "total" | |
// currentValue: the current value of the arr (we are looping through them, one by one) | |
// currentIndex: where you are in the loop (0 based, as usual) | |
// sourceArray: access to the unmodified original arr array | |
// Return a function here leveraging the accumulator or currentValue | |
}, initialValue); |
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 pipe = (...fns) => x => fns.reduce((y, fn) => fn(y), x); | |
const addOne = num => num + 1; | |
const subtractTwo = num => num - 2; | |
const square = num => num * num; | |
const quickMaths = pipe(addOne, subtractTwo, square); | |
quickMaths(20); | |
// Returns: 361 |
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 getUser = (email) => fetch(`/api/${email}`).then(res => res.json()); | |
const promiseQueue = (values, func) => { | |
const reducer = (promises, value) => | |
promises.then((promise) => func(value).then(res => promise.push(res) && res)); | |
return values.reduce(reducer, Promise.result([])); | |
}; | |
promiseQueue(["test@example.com", "another@example.com", "thirdOne@example.com"], getUser); |
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 counties = [ | |
{ name: 'Clackamas', state: 'Oregon', population: 380000 }, | |
{ name: 'King', state: 'Washington', population: 2000000 }, | |
{ name: 'Kitsap', state: 'Washington', population: 250000 }, | |
{ name: 'Multinomah', state: 'Oregon', population: 750000 }, | |
{ name: 'Snohomish', state: 'Washington', population: 720000 }, | |
{ name: 'Wasco', state: 'Oregon', population: 25000 }, | |
]; | |
const highPopulousCounties = counties.reduce((acc, { name, state, population }) => { |
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 counties = [ | |
{ name: 'Clackamas', state: 'Oregon', population: 380000 }, | |
{ name: 'King', state: 'Washington', population: 2000000 }, | |
{ name: 'Kitsap', state: 'Washington', population: 250000 }, | |
{ name: 'Multinomah', state: 'Oregon', population: 750000 }, | |
{ name: 'Snohomish', state: 'Washington', population: 720000 }, | |
{ name: 'Wasco', state: 'Oregon', population: 25000 }, | |
]; | |
const oldStateCountyMap = counties.reduce((acc, county) => { |
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 API_URL = 'https://XXXXXXXXX.execute-api.us-west-2.amazonaws.com/dev/send-mail'; | |
const onSubmit = async (event) => { | |
event.preventDefault(); | |
submitButton.disabled = true; | |
const { submissionProof, ...rest } = Object.fromEntries(new FormData(event.target).entries()); | |
const body = new FormData(); |
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
"use strict"; | |
// Express is our server here. It will host any routes we need to leverage (in this case, just the one) | |
const express = require("express"); | |
const sls = require("serverless-http"); // Serverless wrapper | |
const bodyParser = require("body-parser"); // Middleware that parses our incoming requests | |
const Busboy = require("busboy"); // Busboy is used to parse HTML data from our form | |
const nodemailer = require("nodemailer"); | |
const { Logger } = require("lambda-logger-node"); // Optional: Provides cleaner error logging in AWS |
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
default: &default # Shared variables | |
<<: *default | |
EXAMPLE_DEFAULT_KEY: "EXAMPLE_VALUE" | |
dev: # Environment specific variables | |
<<: *default | |
MAIL_ACCESS: "YOUR_PASSWORD_HERE" |
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
service: covid-api # The name of our service in AWS | |
package: | |
exclude: | |
- secrets.yml | |
custom: | |
stage: ${opt:stage, self:provider.stage} | |
secrets: ${file(secrets.yml):${self:custom.stage}} |
NewerOlder