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
// Function for input mask on phone number field | |
// Formats XXXXXXXXXX as XXX-XXX-XXXX as you type | |
const normalizePhone = (value, previousValue) => { | |
if (!value) return value | |
const onlyNums = value.replace(/[^\d]/g, '') // only allows 0-9 | |
if (!previousValue || value.length > previousValue.length) { | |
if (onlyNums.length === 3) return `${onlyNums}` | |
if (onlyNums.length === 6) return `${onlyNums.slice(0, 3)}-${onlyNums.slice(3)}-` |
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 lookup = { | |
A: 4, | |
'A-': 3.7, | |
'B+': 3.3, | |
B: 3, | |
'B-': 2.7, | |
'C+': 2.3, | |
C: 2, | |
'C-': 1.7, | |
'D+': 1.3, |
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
// https://replit.com/@gixxerblade/previous-fibonacci-number#index.ts | |
/** | |
* Given a Fibonacci number, give the previous Fibonacci number. | |
* If the number given is not a Fibonacci number, return -1. | |
*/ | |
const isPerfectSquare = (num: number): boolean => { | |
// Perfect square: Floor square root then square number. Are they equal? | |
const factor = Math.floor(Math.sqrt(num)) | |
return factor * factor === num |
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
//http://www.hacksparrow.com/base64-encoding-decoding-in-node-js.html | |
var fs = require('fs'); | |
// function to encode file data to base64 encoded string | |
function base64_encode(file) { | |
// read binary data | |
var bitmap = fs.readFileSync(file); | |
// convert binary data to base64 encoded string | |
return new Buffer(bitmap).toString('base64'); | |
} |
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 nodeFetch = require("node-fetch") | |
const ical = require("ical2json") | |
const main = async (fileLocation) => { | |
const icsRes = await nodeFetch(fileLocation) | |
const icsData = await icsRes.text() | |
const data = ical.convert(icsData) | |
const myEvents = data.VCALENDAR[0].VEVENT.map(event => ({ | |
startTime: event.DTSTART, | |
endTime: event.DTEND, |
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
import fetch from 'node-fetch'; | |
interface Events { | |
startTime: Date; | |
endTime: Date; | |
url: string; | |
description: string; | |
location: string; | |
title: string; | |
id: string; |
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
import { networkInterfaces } from 'os' | |
const getLocalExternalIP = () => [].concat(...Object.values(networkInterfaces())) | |
.find((details) => details.family === 'IPv4' && !details.internal) | |
.address | |
// or | |
import { networkInterfaces } from 'os'; | |
const ip = Object.values(networkInterfaces()).flat().find(i => i.family == 'IPv4' && !i.internal).address; |
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
export class SplitTimer { | |
private startTime: number; | |
private lastTime: number; | |
start () { | |
this.startTime = Date.now(); | |
this.lastTime = this.startTime; | |
} | |
split (message: string) { |
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
// Function to purchase shipping from Easypost | |
// API doc here => https://www.easypost.com/stripe-relay | |
// Originally written in Ruby but converted to node | |
// and https://www.easypost.com/docs/api/node#shipments | |
const EasyPost = require("@easypost/api"); | |
const apiKey = process.env.GATSBY_EASYPOST_APIKEY; | |
const api = new EasyPost(apiKey); | |
const stripe = require("stripe")(process.env.GATSBY_STRIPE_SECRET_KEY); |