Skip to content

Instantly share code, notes, and snippets.

🎯
Focusing

Dan Levy justsml

🎯
Focusing
Block or report user

Report or block justsml

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View custom-headers.js
fetch('https://api.github.com/orgs/nodejs', {
headers: new Headers({
'User-agent': 'Mozilla/4.0 Custom User Agent'
})
})
.then(response => response.json())
.then(data => {
console.log(data)
})
.catch(error => console.error(error))
View basic-example.js
fetch('https://api.github.com/orgs/nodejs')
.then(response => response.json())
.then(data => {
console.log(data) // Prints result from `response.json()` in getRequest
})
.catch(error => console.error(error))
@justsml
justsml / progressHelper.js
Last active Feb 10, 2019
Adds an onProgress mechanism to track Download Progress when using the `fetch` API.
View progressHelper.js
function progressHelper(onProgress) {
return (response) => {
if (!response.body) return response
let loaded = 0
const contentLength = response.headers.get('content-length')
const total = !contentLength ? -1 : parseInt(contentLength, 10)
return new Response(
new ReadableStream({
@justsml
justsml / auth-with-FP.js
Last active Nov 10, 2018
Using Functional Promises library
View auth-with-FP.js
// Functional River Pattern
// Original: https://github.com/justsml/escape-from-callback-mountain/blob/master/src/auth.fp.js
const FP = require('functional-promises')
const {hashString} = require('./lib/crypto')
const users = require('./lib/users')
function auth({username = '', password = ''}) {
return FP
.resolve({username, password})
.then(_checkArgs)
@justsml
justsml / auth-with-admin.js
Last active Nov 8, 2018
Functional River pattern
View auth-with-admin.js
// Functional River Pattern
// Original: https://github.com/justsml/escape-from-callback-mountain/blob/master/src/auth.fp.js
const {hashString} = require('./lib/crypto')
const users = require('./lib/users')
function auth({username = '', password = ''}) {
return Promise.resolve({username, password})
.then(_checkArgs)
.then(_loginUser)
View auth-complex.js
// Easier to follow. Equally easy to test -- and with one fewer function in need of testing.
import { userInfo } from "os";
const Promise = require("bluebird");
const { hashStringAsync } = Promise.promisifyAll(require("./lib/crypto"));
const { logEventAsync } = Promise.promisifyAll(require("./lib/log"));
const { openAsync } = Promise.promisifyAll(require("./lib/db"));
const { TimeoutError, ValidationError, NotFoundError } = require("./errors");
@justsml
justsml / sorting-algos.md
Last active Jun 19, 2019
Passing the Tech Screen: Big-O & Sorting algorithms
View sorting-algos.md

Sorting algorithms are hard to memorize.

So here's a template for tracking the important differences.

Algo. Name Big-O (worst case) Suitable for large sets? Good for partially/mostly sorted data? Totally random data? Notes
Bubble O(n^2) 👎 😞 😐 Simple/not-ideal for large data sets
View index.js
runTests()
function runTests() {
const Mocha = require('mocha')
require('sinon')
const mocha = new Mocha()
@justsml
justsml / AsyncObjectStore.js
Last active Mar 28, 2019
Add auto JSON encoding to `localStorage` & `AsyncStorage` (from React Native)
View AsyncObjectStore.js
/**
Utility for React Native's AsyncStorage module.
Auto encodes/decodes using `JSON`.
Essentially data is saved as key/value to available flash/SD storage.
*/
import { AsyncStorage } from "react-native"
export default {
setItem(key, value) {
return AsyncStorage.setItem(key, JSON.stringify(value))
View detect-and-install-npm-packages.sh
#!/bin/bash
PACKAGES=$(cat "${1:-/dev/stdin}" | \
grep "require([\"'][^'\"]*.)" | \
sed 's/^[^(]*(.//g' | \
sed 's/.[\)]*[a-zA-Z;]*$//' | \
sed "s/.[)].*$//" | \
sort | uniq)
# Remove NodeJS built-ins
You can’t perform that action at this time.