Skip to content

Instantly share code, notes, and snippets.

View AJamesPhillips's full-sized avatar

Alexander James Phillips AJamesPhillips

View GitHub Profile
// Similar to Ruby array `last` method
Object.defineProperty(Array.prototype, "last", {
get: function(){
return this.length && this[this.length-1];
}
});
// Adapted from: https://nodejs.org/docs/latest/api/cluster.html
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running on ${numCPUs} numCPUs`);
// Fork workers.
@AJamesPhillips
AJamesPhillips / logger.js
Last active February 4, 2019 13:19
fake logger for tests
const LOG_LEVELS = {
DEBUG: 1000,
INFO: 2000,
WARN: 3000,
ERROR: 4000,
NONE: 5000
}
export function makeFakeLogger (log_level_override) {
@AJamesPhillips
AJamesPhillips / lightweight_npm_dependency_scanner.py
Created January 4, 2019 00:42
Rough scan of node dependencies for lesser used packages
import requests
import time
THRESHOLD = 1000000
packages = ["babel-core","babel-loader","babel-plugin-transform-flow-strip-types","babel-plugin-transform-object-rest-spread","babel-preset-es2015","babel-preset-react","babel-register","chai","chai-enzyme","cheerio","codecov","css-loader","enzyme","expect","file-loader","flow-bin","json-loader","mocha","node-sass","nyc","react-addons-test-utils","sass-loader","style-loader","url-loader"]
for package in packages:
url = "https://api.npmjs.org/downloads/range/2018-11-12:2018-12-09/" + package
resp = requests.get(url)
data = resp.json()
@AJamesPhillips
AJamesPhillips / reducer.js
Last active May 3, 2019 11:58
Small function for combining redux selectors
function wrap_selectors (selectors, state_path) {
const wrapped_selectors = {}
Object.keys(selectors).forEach(selector_key => {
wrappedSelectors[selectorKey] = (state, ...args) => selectors[selectorKey](state[statePath], ...args)
})
return wrapped_selectors
}
@AJamesPhillips
AJamesPhillips / killads.js
Created July 21, 2019 09:50
Kills some ads
// Based off: https://gist.github.com/jasonleonhard/177b67f135c443719fe8
function removeElement (element) {
element.parentNode.removeChild(element);
}
function removeElements (elements) {
for (var i = 0; i < elements.length; ++i) {
removeElement(elements[i]);
}
@AJamesPhillips
AJamesPhillips / type safe nested redux reducers.ts
Last active January 10, 2020 17:28
Type safe nested redux reducers
// state/reducers/schema.ts or spread through sub reducers/schema.ts files.
interface SettingsState {
favouriteColor: string
}
interface UsersState {
count: number
settings: SettingsState
}
@AJamesPhillips
AJamesPhillips / chrome_history_by_date.py
Created August 23, 2019 23:47
Search google chrome history by date
# Your database is probably in: ~/Library/Application Support/Google/Chrome/Default/History
# Copy it to ./chrome_history
import sqlite3
conn = sqlite3.connect("./chrome_history")
# Manually exploring the db and foreign keys etc
# print(conn.execute("SELECT name FROM sqlite_master WHERE type='table';").fetchall())
@AJamesPhillips
AJamesPhillips / redux in c sharp.cs
Last active August 29, 2019 23:07
C# Redux replica
using System;
//using System.Web.Extensions;
//using System.Web.Script.Serialization;
/************************************************
* State
***********************************************/
public abstract class State<SubClassState>
{
# Takes text with the format:
#
# 00:05
# Some subtitles
# 00:12
# Some more text
# ...
#
# 61:20
# More subtitle at 61 minutes, 20 seconds