Skip to content

Instantly share code, notes, and snippets.

View gixxerblade's full-sized avatar
💭
Cycling

Stephen Clark gixxerblade

💭
Cycling
View GitHub Profile
// 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)}-`
@gixxerblade
gixxerblade / calculateGPA.tsx
Created September 19, 2022 12:38
cassidoo Interview question of the week 9-19-2022
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,
// 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
@gixxerblade
gixxerblade / gist:345e6b7810a1c0a7e5b77662905a52f4
Created June 9, 2022 14:06 — forked from crspiccin/gist:790796a68e7178404de4
Node.js convert an image to Base 64
//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');
}
@gixxerblade
gixxerblade / meetup.ts
Last active August 23, 2021 19:57
Converts ical ics file to json for a Meetup Google subscribed calendar
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,
@gixxerblade
gixxerblade / meetup.ts
Last active August 23, 2021 19:02
Get public events from a subscribed Meetup Google calendar ics ical file
import fetch from 'node-fetch';
interface Events {
startTime: Date;
endTime: Date;
url: string;
description: string;
location: string;
title: string;
id: string;
@gixxerblade
gixxerblade / ip.js
Last active August 19, 2021 20:57
Get local ip address
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;
export class SplitTimer {
private startTime: number;
private lastTime: number;
start () {
this.startTime = Date.now();
this.lastTime = this.startTime;
}
split (message: string) {
@gixxerblade
gixxerblade / createNewShipment.js
Created May 29, 2020 19:14
EasyPost Stripe lambda function written for Node
// 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);