Skip to content

Instantly share code, notes, and snippets.

View kevboutin's full-sized avatar

Kevin Boutin kevboutin

View GitHub Profile
@kevboutin
kevboutin / conditionalFlowControl.js
Created October 17, 2023 15:29
Conditional Flow Control
/* Instead of using this:
const getNumWord = (num) => {
if (num === 1) {
return 'one';
} else if (num === 2) {
return 'two';
} else if (num === 3) {
return 'three';
} else if (num === 4) {
return 'four';
@kevboutin
kevboutin / randomUuid.js
Created October 17, 2023 15:31
Generate a random UUID
/* This can be done by the uuid package, which is probably better than using Math.random(), but this is great for unit tests. */
const generateRandomUUID = (a) =>
a
? (a ^ ((Math.random() * 16) >> (a / 4))).toString(16)
: ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(
/[018]/g,
generateRandomUUID
);
console.log(generateRandomUUID()); // f138f635-acbd-4f78-9be5-ca3198c4cf34
console.log(generateRandomUUID()); // 8935bb0d-6503-441f-bb25-7bc685b5b5bc
@kevboutin
kevboutin / generateSasTokenUrl.js
Last active December 25, 2022 19:54
Generates a SAS token URL for a particular file in an Azure storage account
const {
BlobSASPermissions, generateBlobSASQueryParameters, StorageSharedKeyCredential,
} = require('@azure/storage-blob');
/**
* Generate a secure token URL for a particular file in a storage account.
*
* @param {string} accountName The name of the storage account.
* @param {string} accountKey The account key.
* @param {string} container The container.
@kevboutin
kevboutin / enum_in_javascript.js
Last active December 11, 2021 07:24
Best way to do enum in JavaScript
// Season enums can be grouped as static members of a class
class Season {
// Create new instances of the same class as static attributes
static Summer = new Season("summer")
static Autumn = new Season("autumn")
static Winter = new Season("winter")
static Spring = new Season("spring")
constructor(name) {
this.name = name
@kevboutin
kevboutin / clonetest.ts
Last active March 22, 2021 18:35
cloning objects in javascript
function cloneObject(obj: any): any {
switch (obj["constructor"]) {
case Date: return new Date(obj);
case Object: return Object.keys(obj).reduce<{ [key: string]: any }>((newObj, key) => (newObj[key] = cloneObject(obj[key]), newObj), {});
case Array: return obj.map(cloneObject);
}
return obj;
}
// Test
@kevboutin
kevboutin / Solarin.bgptheme
Last active September 16, 2020 23:57
Solarin theme for git prompt (brew install bash-git-prompt)
# This is a custom theme template for gitprompt.sh and is used here: https://github.com/magicmonty/bash-git-prompt
# Install this package using: brew install bash-git-prompt
# Copy this file to $(brew --prefix)/opt/bash-git-prompt/share/themes/
# Add the following code to your ~/.bash_profile
# if [ -f "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh" ]; then
# __GIT_PROMPT_DIR=$(brew --prefix)/opt/bash-git-prompt/share
# GIT_PROMPT_ONLY_IN_REPO=1
# GIT_PROMPT_THEME=Solarin
# source "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh"
# fi
@kevboutin
kevboutin / bash-async.sh
Created March 29, 2020 22:26
This demonstrates how a bash script can invoke a child process to do some work asynchronously and monitor for finish
#!/bin/bash
mycommand &
child_pid=$!
while kill -0 $child_pid >/dev/null 2>&1; do
echo "Child process is still running"
sleep 1
done
@kevboutin
kevboutin / processMessages.js
Created March 10, 2020 20:47
Processing messages from a queue using Azure Service Bus
const { ServiceBusClient } = require('@azure/service-bus');
const moment = require('moment');
const connectionString = process.env.SB_CONNECTION;
const queueName = process.env.SB_QUEUE_NAME;
/**
* Processing messages from service bus queue.
*
* @param {object} context The context.
@kevboutin
kevboutin / sendMessages.js
Created March 10, 2020 20:45
Sending messages to a queue using Azure Service Bus
const { ServiceBusClient } = require('@azure/service-bus');
const moment = require('moment');
const connectionString = process.env.SB_CONNECTION;
const queueName = process.env.SB_QUEUE_NAME;
const listOfScientists = [
{
id: 1,
name: "Einstein",
@kevboutin
kevboutin / scheduleEvent.js
Last active March 6, 2020 05:08
Calculate available time windows
// Designed for Azure functions
module.exports = function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
const scheduledEvents = [];
//Add all scheduled events into an array
req.body.forEach(function (calendar) {
calendar.items.forEach(function (items) {
scheduledEvents.push({
start: new Date(items['start']).toLocaleTimeString('en-US', { hour12: false }),