This file contains hidden or 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 mapToJson = (map) => JSON.stringify(Object.fromEntries(map)); | |
const map = new Map([ | |
['user1', 'Alison'], | |
['user2', 'Kevin'], | |
['user3', 'James'], | |
]); | |
const json = mapToJson(map); | |
// {"user1":"Alison","user2":"Kevin","user3":"James"} | |
console.log(json); |
This file contains hidden or 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 jsonToMap = (json) => new Map(Object.entries(JSON.parse(json))); | |
const json = '{"user1":"Alison","user2":"Kevin","user3":"James"}'; | |
const map = jsonToMap(json); | |
// Kevin | |
console.log(map.get('user2')); | |
// Map(3) { 'user1' => 'Alison', 'user2' => 'Kevin', 'user3' => 'James' } | |
console.log(map); |
This file contains hidden or 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 snakeToCamelCase = (s) => | |
s.toLowerCase().replace(/(_\w)/g, (w) => w.toUpperCase().substr(1)); | |
const str1 = 'string_manipulation'; | |
const str2 = 'one_liner'; | |
console.log(snakeToCamelCase(str1)); // stringManipulation | |
console.log(snakeToCamelCase(str2)); // oneLiner |
This file contains hidden or 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
/* 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 |
This file contains hidden or 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
/* 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'; |
This file contains hidden or 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 { | |
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. |
This file contains hidden or 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
// 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 |
This file contains hidden or 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 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 |
This file contains hidden or 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
# 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 |
This file contains hidden or 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
#!/bin/bash | |
mycommand & | |
child_pid=$! | |
while kill -0 $child_pid >/dev/null 2>&1; do | |
echo "Child process is still running" | |
sleep 1 | |
done |