Skip to content

Instantly share code, notes, and snippets.

// 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
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);
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
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);
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 }) => {
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) => {
@bunnyhawk
bunnyhawk / submit.js
Created October 30, 2020 18:13
aws nodemailer example submit
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();
@bunnyhawk
bunnyhawk / handler.js
Created October 30, 2020 18:12
aws nodemailer handler
"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
@bunnyhawk
bunnyhawk / secrets.yaml
Created October 30, 2020 18:07
aws nodemailer secrets
default: &default # Shared variables
<<: *default
EXAMPLE_DEFAULT_KEY: "EXAMPLE_VALUE"
dev: # Environment specific variables
<<: *default
MAIL_ACCESS: "YOUR_PASSWORD_HERE"
@bunnyhawk
bunnyhawk / serverless.yml
Created October 30, 2020 18:06
aws nodemailer yaml
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}}