Skip to content

Instantly share code, notes, and snippets.

View hardboiled's full-sized avatar

Daniel Ege hardboiled

View GitHub Profile
@hardboiled
hardboiled / cd-command-unix.js
Last active June 13, 2018 21:36
Implements the cd command in unix
function changeDir(pwd, cd) {
let targetDir = (cd[0] === '/') ? "" : pwd;
let matches = [];
while ((matches = cd.match(/^\/*([^\/]+)/)) && matches.length > 1) {
const dir = matches[1];
cd = cd.substring(matches[0].length);
switch(dir) {
case ".":
@hardboiled
hardboiled / get-phone-number-combos.js
Last active June 15, 2018 21:08
Returns an array of all possible combinations of letters that can correspond to a phone number
// example call, with 0 and 1 assigned to empty strings
// getPhoneNumberCombinations('9382234567', [ '', '', 'ABC', 'DEF', 'GHI', 'JKL', 'MNO', 'PQRS', 'TUV', 'WXYZ' ])
function getPhoneNumberCombinations(digits, mapping) {
// populate results array so that we have something to append to
const results = [ '' ];
for (const digit of digits) {
const curLength = results.length;
const currentMapping = mapping[digit];
if (currentMapping == null) continue;
/*
* Quick Sort implementation in JS, using stack instead of recursion
*/
function quickSort(arr) {
let stack = [[0, Math.floor(Math.random() * arr.length), arr.length - 1]];
while(stack.length > 0) {
let vals = stack.pop();
let startingIndex = vals[0];
let pivot = vals[1];
function serializeParamsIntoUrl(baseUrl: string, params: any) {
let queryParams = []
for (let key in params) {
queryParams.push(`${key}=${params[key]}`)
}
baseUrl += (baseUrl.indexOf('?') < 0) ? '?' : ''
return baseUrl + queryParams.join('&')
}
@hardboiled
hardboiled / example-tslint.ts
Last active June 7, 2019 21:06
Derivative of Google's tslint file
{
"rules": {
"array-type": [true, "array-simple"],
"arrow-return-shorthand": true,
"ban": [true,
{"name": ["it", "skip"]},
{"name": ["it", "only"]},
{"name": ["it", "async", "skip"]},
{"name": ["it", "async", "only"]},
{"name": ["describe", "skip"]},
{
"name": "example-package-json",
"version": "1.0.0",
"description": "",
"dependencies": {
"csv-parse": "^4.4.1",
"typescript": "^3.4.4"
},
"devDependencies": {
"@types/chai": "^4.1.7",
{
"include": [
"src/**/*"
],
"exclude": [
"node_modules"
],
"compilerOptions": {
"outDir": "dist"
}
@hardboiled
hardboiled / single-file-pr-example-using-github-api.md
Last active February 18, 2020 22:19
An example of how to use github's api to create a PR with a single file change.

How to make a single file PR using github API

(Note this is using github's v3 API. They have a new v4 version that uses graphql. Haven't tried that at the time of writing.)

Using github API is a bit weird at first, because uses the same lower-level git commands that are abstracted away to the normal user. However, this is actually a good thing to some degree, because it means you can do things like changing a single file without copying out an entire branch or repo to your disk.

I will be referencing git objects (blobs, trees, refs, etc.) in this short tutorial, but not covering them in gory detail. You can learn more about them here.

Step 1 Upload a Blob

> npm i -g jwt-decode
> JWT=<your_token> NODE_PATH=$(npm get prefix)/lib/node_modules node
> require('jwt-decode')(process.env.JWT)
// claims will print
/**
* Convenience method for creating not rigorously random uuids to help with testing staging and development APIs
* Definition based off this answer: https://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid#answer-105074
*/
function createFakeUuid () {
return 'xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx'.split('').map(x => {
const chars = '0123456789abcdef';
const seed = Math.random();
switch (x) {
case 'M':