Skip to content

Instantly share code, notes, and snippets.

View flesch's full-sized avatar

John Flesch flesch

View GitHub Profile
@flesch
flesch / getElementsByClassName.php
Created August 18, 2012 14:49
document::getElementsByClassName("")
<?php
class document {
public static function getElementsByClassName($name) {
global $xpath;
$nodes = array();
$elements = $xpath->query(sprintf("//div[contains(@class, '%s')]", $name));
foreach ($elements as $e) {
array_push($nodes, $e);
}
@flesch
flesch / list_dir.php
Created August 18, 2012 15:00
Recursive Directory Listing in PHP
<?php
function list_dir($resource) {
$files = array();
$scan = glob(rtrim($resource, '/') . '/*');
if (is_file($resource)) {
array_push($files, $resource);
}
if (is_dir($resource)) {
foreach ($scan as $path) {
@flesch
flesch / basic-auth.js
Last active July 27, 2022 12:39
HTTP Basic Authentication with Express, without express.basicAuth.
var express = require("express");
var app = express();
app.get("/restricted", function(req, res, next){
// Grab the "Authorization" header.
var auth = req.get("authorization");
// On the first request, the "Authorization" header won't exist, so we'll set a Response
// header that prompts the browser to ask for a username and password.
@flesch
flesch / README.md
Created July 28, 2012 03:44
Fake SCORM API

Fake SCORM API

Some courses won't work outside of an LMS. That's annoying when you just want to review a course, so drop this in to provide a fake SCORM API implementation - getAPI should then pick up this fake API.

# ~/.config/starship.toml
format = """
[♥](bold red)\
${custom.git_user}\
${custom.ip}\
$ruby${custom.bundler}\
$golang\
$nodejs${custom.npm}${custom.yarn}\
$terraform\
@flesch
flesch / starredRepositories.graphql
Created May 3, 2018 04:01
starredRepositories.graphql
query starredRepositories($repositories: Int = 10, $after: String, $releases: Int = 1) {
viewer {
starredRepositories(first: $repositories, after: $after, ownedByViewer: false, orderBy: {field: STARRED_AT, direction: DESC}) {
pageInfo {
hasNextPage
endCursor
}
edges {
cursor
starredAt
@flesch
flesch / findMissingLetters.js
Created July 24, 2019 17:52
JavaScript Interview Code Challenge Solution
// A "pangram" is a sentence that includes every letter of the alphabet.
// Write a function that will return what letters of the alphabet are
// missing from a sentence (thus meaning it is not a pangram).
// "A quick brown fox jumps over the lazy dog" includes every letter, returning ""
// "Lions, and tigers, and bears, oh my!" does not include every letter, returning "cfjkpquvwxz"
function findMissingLetters(str) {
const alphabet = 'abcdefghijklmnopqrstuvwzyz';
if (str.length) {
const reducer = (memo, letter) => {
@flesch
flesch / README.md
Last active July 6, 2019 12:45
📈 Calculate a “Net Promoter Score” from an array of integers. (Array.reduce)

@flesch/net-promoter-score

Calculate a “Net Promoter Score” from an array of integers.

Install

$ npm install --save gist:34e4f906939ee1bafc74b82e64d4cc48
@flesch
flesch / pre-commit
Created August 21, 2018 12:34
Git pre-commit hook to ensure the version in package.json has changed
#!/bin/bash
set -o nounset
if [ "$(git status -s | wc -l | bc)" -gt "0" ]; then
if [ "$(git diff --cached master -G '"version":' | wc -l | bc)" -eq "0" ]; then
echo -e "\033[31m✘\033[0m Aborting commit! \"package.json\" was not updated with a new version."
fi
fi
@flesch
flesch / pkg.sh
Created July 13, 2018 01:43
Read or create a package.json file
# Add this function to .bashrc or .bash_profile
# $ pkg
# $ pkg dependencies # (to show only the dpendencies)
# Requires `jq`
pkg() {
if [ -f package.json ]; then
cat package.json | jq .$1
else
read -p "package.json doesn't exist. Create one? [Y/n] " -n 1 -r
if [[ $REPLY =~ ^(Y|y| ) ]] || [[ -z $REPLY ]]; then