Skip to content

Instantly share code, notes, and snippets.

View JakeTheCorn's full-sized avatar

Jake Corn JakeTheCorn

  • Indianapolis, IN
View GitHub Profile
@JakeTheCorn
JakeTheCorn / getDiagonalValuesFromMatrix.js
Last active January 16, 2020 17:16
given a multi dimensional array (matrix) this will return lists of values found in the primary and secondary diagonals
// diagonal starting from top left
function primary(matrix) {
const items = []
for (let i = 0; i < matrix.length; i++) {
items.push(matrix[i][i])
}
return items
}
// diagonal starting from top right
@JakeTheCorn
JakeTheCorn / getLongestArray.js
Last active January 16, 2020 15:53
variadic function that returns the longest array in the list
function getLongestArray(...arrays) {
let longestIndex = 0
let longestLength = arrays[0].length
if (arrays.length === 1) {
return arrays[0]
}
for (let i = 1; i < arrays.length; i++) {
if (arrays[i].length > longestLength) {
longestIndex = i
longestLength = arrays[i].length
const isJSON = subject => {
if (typeof subject !== 'string') {
return false;
}
try {
JSON.parse(subject);
return true;
} catch (error) {
return false;
}
@JakeTheCorn
JakeTheCorn / switch-vs-map.js
Created December 18, 2019 04:08
For the amount of code saved I think python is due some praise for leaving out the switch control structure. It's really a lot less to read.
// SWITCH
function getFriendlySwitch(k) {
switch(k) {
case "0":
return "value1";
case "1":
return "value2";
case "2":
return "value3";
case "3":
@JakeTheCorn
JakeTheCorn / curl-test.sh
Created December 13, 2019 17:40
way to check in curl testing
curl --header "Content-Type: application/json" \
--request POST \
--data '{"clientId":"'"$CLIENT_ID"'","clientSecret":"'"$CLIENT_SECRET"'"}' \
http://localhost:8888/auth
# usage:
# cat curl-test.sh | CLIENT_ID=lksdjflkjsdf CLIENT_SECRET=aoksdjflkjasdf bash
@JakeTheCorn
JakeTheCorn / logic-gates.js
Last active November 27, 2019 19:55
todo: logic gates js
/* todo: write your tests using truth table syntax
test('xor',`
[
[0, 0, 0],
[0, 1, 1],.. etc
]
a b result
0 0 0
0 1 1
current_file_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# usage: put this in the same file as a script that wants to use the current files
@JakeTheCorn
JakeTheCorn / kill-process-by-port.sh
Last active November 26, 2019 03:45
little helper function I use to help kill processes running on certain ports.
kill-process-by-port() {
local port_num="$1"
local process_id=$(lsof -i :$port_num | awk 'END{print $2}')
if [ -z "$process_id" ] ; then
echo "Nothing running on port $port_num"
else
kill -9 "$process_id"
echo "process running on port $port_num killed."
fi
}
@JakeTheCorn
JakeTheCorn / backoff.ts
Last active November 16, 2019 02:18
nice little timing function i use sometimes
const backoff = (ms: number) =>
new Promise(resolve => setTimeout(_ => resolve(), ms))