Skip to content

Instantly share code, notes, and snippets.

View josefaidt's full-sized avatar
🦉

josef josefaidt

🦉
View GitHub Profile
function _create_react_component
if test (count $argv) -lt 1
read -l -P "Name of component? " component
_create_react_component $component
else
set cmp $argv[1]
mkdir -p components/$cmp
echo "
import React from 'react'
import styles from './$cmp.module.css'
@josefaidt
josefaidt / parseWorkbook.js
Created July 13, 2020 19:58
parse xlsx workbook
const xlsx = require('xlsx')
module.exports = function parseWorkbook(file) {
const result = {}
console.info(`Parsing workbook ${file}`)
const wb = xlsx.readFile(file)
if (!wb.SheetNames.length) throw new Error('Invalid workbook, no sheets available.')
const extractDataWithHeaders = (data = {}) => {
const result = []
@josefaidt
josefaidt / binary-gap.js
Created July 30, 2020 17:13
find the longest binary gap from an integer input
function solution(N) {
// write your code in JavaScript (Node.js 8.9.4)
return parseInt(N >>> 0).toString(2).split('1').reduce((acc, value, index, self) => value.length > acc ? (typeof self[index + 1] === 'string' ? value.length : acc) : acc, 0)
}
// src/pages/_app.js
import { Amplify, Auth } from 'aws-amplify'
import {
AmplifyAuthenticator,
AmplifySignUp,
AmplifySignIn,
AmplifySignOut,
} from '@aws-amplify/ui-react'
import config from '../aws-exports.js'
@josefaidt
josefaidt / post-checkout
Created November 9, 2021 21:16
Git post-checkout hook to change AWS Amplify environments upon branch checkout
#!/bin/bash
# .git/hooks/post-checkout
# Utility to change AWS Amplify environments on branch checkout
# helper to get path to aws-exports.js
function get_aws_exports_path {
local AMPLIFY_PROJECT_CONFIG_PATH="amplify/.config/project-config.json"
local AMPLIFY_EXPORTS_PATH=$(jq -r '.javascript.config.SourceDir' $AMPLIFY_PROJECT_CONFIG_PATH)
if [[ "$AMPLIFY_EXPORTS_PATH" == */ ]]
import { LambdaClient, InvokeCommand } from '@aws-sdk/client-lambda'
/**
* Invoke our Lambda function with a payload
* @param {Object.<string, any>} payload
* @returns {Promise<import('@aws-sdk/client-lambda').InvokeCommandOutput>}
*/
async function invoke(payload) {
/** @type {import('@aws-sdk/client-lambda').LambdaClient} */
const client = new LambdaClient({ region: process.env.REGION })
@josefaidt
josefaidt / amplify-init.sh
Last active December 8, 2022 22:31
Minimal Amplify CLI headless init script
#!/bin/bash
set -e
IFS='|'
AMPLIFY_ENVIRONMENT='dev'
AWS_REGION='us-east-1'
AWSCLOUDFORMATIONCONFIG="{\
\"configLevel\":\"project\",\
\"useProfile\":true,\
@josefaidt
josefaidt / get-date-from-epoch.js
Created April 20, 2022 21:38
gets date from epoch
const getDateFromEpoch = epoch => {
let date = new Date(0)
date.setUTCSeconds(epoch)
return date
}
@josefaidt
josefaidt / appsync-from-lambda.js
Created April 26, 2022 13:48
Sample AppSync IAM call from Node.js Lambda using AWS SDK v3
import crypto from '@aws-crypto/sha256-js'
import { defaultProvider } from '@aws-sdk/credential-provider-node'
import { SignatureV4 } from '@aws-sdk/signature-v4'
import { HttpRequest } from '@aws-sdk/protocol-http'
import { default as fetch, Request } from 'node-fetch'
const { Sha256 } = crypto
const AWS_REGION = process.env.AWS_REGION || 'us-east-1'
const QUERY_LIST_USERS = /* GraphQL */ `
@josefaidt
josefaidt / lambda-sms.js
Created April 26, 2022 15:18
sends SMS message from Lambda using AWS SDK v3
import { SNSClient, PublishCommand } from '@aws-sdk/client-sns'
const client = new SNSClient()
/**
* Publish a message to a phone number
* @param {string} number - phone number of recipient
* @param {string} [message] - message to send to phone number
* @returns {import('@aws-sdk/client-sns').PublishCommandOutput}
*/
async function publish(number, message) {