Skip to content

Instantly share code, notes, and snippets.

@shannonmoeller
shannonmoeller / sprinkle.js
Last active August 29, 2015 14:09
sprinkle
var a = [
'a', 'aa', 'aaa', 'aaaa', 'aaaaa',
'b', 'bb', 'bbb', 'bbbb', 'bbbbb',
'c', 'cc', 'ccc', 'cccc', 'ccccc'
];
var b = [
'Z', 'ZZ', 'ZZZ'
];
@shannonmoeller
shannonmoeller / mocket.js
Last active August 29, 2015 14:18
Mock Socket
var es = require('event-stream'),
reader = es.readArray([mockDataIn]),
writer = es.writeArray(function (err, mockDataOut){
assert(mockDataOut);
}),
mockSocket = es.duplex(writer, reader);
mockSocket.pipe(...).pipe(mockSocket);
@shannonmoeller
shannonmoeller / many-to-one-gulpfile.js
Last active August 29, 2015 14:19
Many data sources to one template
var gulp = require('gulp');
var hb = require('gulp-hb');
var rename = require('gulp-rename');
var through = require('through2');
gulp.task('build', function () {
return gulp
.src('./data/**/*.json')
.pipe(through.obj(function(file, enc, cb) {
var data = JSON.parse(String(file.contents));
@shannonmoeller
shannonmoeller / container.js
Last active September 8, 2018 18:09
Dependency-injection container example using vanilla JS.
let uid = 0;
class Foo {
constructor({ bar }, id) {
this.uid = uid++;
this.id = id;
this.bar = bar;
}
}
const tests = new Set();
const t = {
total: 0,
passed: 0,
failed: 0,
comment(message) {
console.log(`# ${message}`);
},
root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = tab
insert_final_newline = true
trim_trailing_whitespace = true
function createCascade() {
const middleware = [];
async function run(ctx, next = () => {}) {
// Reverse list to .pop() instead of .shift() for performance
const handlers = [...middleware, next].reverse();
async function runNext() {
if (handlers.length) {
const handler = handlers.pop();
@shannonmoeller
shannonmoeller / store.md
Last active May 8, 2020 23:00
JavaScript stores for devs who like stores.

Arbitrary Values

export function createStore(state) {
  const listeners = new Set();

  return {
    get() {
      return state;
    },
#!/usr/bin/env bash
JS_FILES="$(git diff --name-only --staged --diff-filter=ACM | grep '.js$')"
if [ -n "$JS_FILES" ]; then
set -e
echo 'eslint' && ./node_modules/.bin/eslint --fix $JS_FILES
echo 'prettier' && ./node_modules/.bin/prettier --write $JS_FILES
echo 'git add' && git add $JS_FILES
fi