Skip to content

Instantly share code, notes, and snippets.

View timoxley's full-sized avatar

Tim Kevin Oxley timoxley

View GitHub Profile
@timoxley
timoxley / Readme.md
Last active June 7, 2016 07:27
JS Pop Quiz: How well do you know your functions?

JS Pop Quiz: How well do you know your functions?

Given an Array of Functions fns, what argument(s) can you pass to fns.forEach such that each function in fns will execute, in order, without creating any anonymous (or named) functions or invoking the Function constructor?

Conditions

  • Do not use the function keyword, or arrow functions () => .
  • Do not invoke the Function constructor.
  • Do not use method definitions.
  • Function#bind & friends on the Function.prototype are ok.
@timoxley
timoxley / index.js
Created March 30, 2016 12:51
requirebin sketch
const date = require('date.js')
google.charts.load('current', {packages: ['corechart', 'line']});
google.charts.setOnLoadCallback(() => {
fetch('https://crossorigin.me/https://wakatime.com/leaders', {mode: 'cors', credentials: 'same-origin'})
.then(res => (console.log('res', res), res))
.then(res => res.text())
.then(html => {
const frag = document.createDocumentFragment()
const div = frag.appendChild(document.createElement('div'))
div.innerHTML = html
function (toMatch, items) {
const options = arguments[arguments.length - 1] // options always last arg
if (!Array.isArray(items)) items = [] // items is optional
if (!toMatch) throw new Error('No value given to match')
if (!options || Array.isArray(options)) throw new Error('No internal options object')
}
// cat.js
// cat a.js b.js | node cat.js
var vm = require('vm')
var a = ''
process.stdin.on('data', function(b) {
a += b.toString()
})
process.stdin.on('end', function() {
vm.runInThisContext(a)
@timoxley
timoxley / gist:ae19556a33dd3867a697
Last active August 29, 2015 14:20
tl;dr. 2.5 Dynamic Behavior Changes: Changing An Instance’s Class and Dynamic Inheritance

Organizing Programs Without Classes

Source: https://cs.au.dk/~hosc/local/LaSC-4-3-pp223-242.pdf

Section 2.5 Dynamic Behavior Changes: Changing An Instance’s Class and Dynamic Inheritance

tl;dr

Sometimes the behavior of an instance of a data type can be divided into several different “modes” of behavior or implementation…

@timoxley
timoxley / stillInZero.js
Last active February 18, 2016 22:13
find number of packages in still < 1.0.0 according to search cache
var columnify = require('columnify')
var fs = require('fs')
var results = [];
var packages = JSON.parse(fs.readFileSync(process.env.HOME + '/.npm/registry.npmjs.org/-/all/.cache.json'))
var packageNames = Object.keys(packages)
// only count packages with dist-tags + latest
var validPackages = packageNames.filter(function(name) {
var item = packages[name];
return (
@timoxley
timoxley / Readme.md
Created June 20, 2014 03:29
"non-destructive" injecting of file content into markdown files

Test

This is a test

Examples

An example:

@timoxley
timoxley / index.js
Last active August 29, 2015 13:56
iterate objects polymer. not work.
// paste into console on http://www.polymer-project.org/ and inspect document body
// template appears, but not rendered data
var doc = document.implementation.createHTMLDocument()
doc.body.innerHTML = [
'<template repeat="{{key, value in items}}">',
' {{key}} {{value}}',
'</template>'
].join('\n')
@timoxley
timoxley / ranges.js
Created December 16, 2013 05:20
ranges
var NUM_BUCKETS = 5
var data = [100, 200, 120, 130, 150]
var max = data.reduce(function(max, num) {return Math.max(max, num)}, -Infinity)
var min = data.reduce(function(min, num) {return Math.min(min, num)}, Infinity)
var interval = (max - min) / NUM_BUCKETS
var toBucket = function toBucket(num) {
var bucket = Math.floor((num - min) / interval)
// constrain to 0 -> NUM_BUCKETS - 1
return Math.max(0, Math.min(bucket, NUM_BUCKETS - 1))
}
@@ -1,6 +1,6 @@
@media not print {
.screen-scroll,.screen-scroll>body,.screen-scroll .container {
- overflow: hidden
+ height: 100%;
}
.screen-scroll .surface {
@@ -9,7 +9,6 @@