Skip to content

Instantly share code, notes, and snippets.

View twhite96's full-sized avatar
☢️
Cookin

tiff twhite96

☢️
Cookin
View GitHub Profile
@twhite96
twhite96 / error-handling-with-fetch.md
Created February 2, 2020 00:32 — forked from odewahn/error-handling-with-fetch.md
Processing errors with Fetch API

I really liked @tjvantoll article Handling Failed HTTP Responses With fetch(). The one thing I found annoying with it, though, is that response.statusText always returns the generic error message associated with the error code. Most APIs, however, will generally return some kind of useful, more human friendly message in the body.

Here's a modification that will capture this message. The key is that rather than throwing an error, you just throw the response and then process it in the catch block to extract the message in the body:

fetch("/api/foo")
  .then( response => {
    if (!response.ok) { throw response }
    return response.json()  //we only get here if there is no error
 })
@twhite96
twhite96 / example.js
Created January 22, 2020 15:03 — forked from hwangbible/example.js
Scriptablify - lets you require('modules') in Scriptable app!
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: gray; icon-glyph: magic;
// Defaults to the latest version and no automatic update; if the file exists, just use it
const moment = await require('moment');
// Use any SemVer options to specify a version you want
// Refer to the calculator here: https://semver.npmjs.com/
const lodash = await require('lodash@^3.9.1');
@twhite96
twhite96 / clear-font-cache.md
Created July 31, 2019 00:59 — forked from jaredhowland/clear-font-cache.md
Clear Mac OS X Font Caches
@twhite96
twhite96 / git_newrepo
Created June 27, 2019 00:33 — forked from c0ldlimit/git_newrepo
Git: Push a new or existing repo to Github
# Create a new repository on the command line
touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/c0ldlimit/vimcolors.git
git push -u origin master
# Push an existing repository from the command line

Download Audio from YouTube

-i - ignore errors

-c - continue

-t - use video title as file name

--extract-audio - extract audio track

// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: green; icon-glyph: file-code;
const getModule = importModule("getModule");
const documentDirectory = FileManager.iCloud().documentsDirectory();
module.exports = async ({ moduleName, url, forceDownload = false }) => {
if (moduleExists(moduleName) && !forceDownload) {
return importModule(moduleName);
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: green; icon-glyph: file-code;
const getString = importModule('getString');
const documentDirectory = FileManager.iCloud().documentsDirectory();
const header = `// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-gray; icon-glyph: file-code;`;
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: green; icon-glyph: file-code;
module.exports = async ({ url, headers = {} }) => {
const request = new Request(url);
request.method = methods.get;
request.headers = {
...headers
};
return await request.loadString();
@twhite96
twhite96 / requests.js
Created May 5, 2019 03:31 — forked from yoav-lavi/requests.js
request module for Scriptable
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: green; icon-glyph: file-code;
module.exports = {
post: async ({ url, body, headers = {} }) => {
const request = new Request(url);
request.body = JSON.stringify(body);
request.method = methods.post;
request.headers = {
...defaultHeaders,
@twhite96
twhite96 / data-structures.md
Last active March 22, 2019 01:38 — forked from alyssaq/data-structures.md
Data Structure descriptions: Dictionaries, priority queues, sets, graphs, trees

Dictionaries

A set of n records, each identified by one or more key fields.

Problem: Efficiently locate, insert and delete the record associated with any query key q.

Data structures include hash tables, skip lists and balanced/unbalanced binary search trees.

Representations