Skip to content

Instantly share code, notes, and snippets.

View Lokua's full-sized avatar
🐐
 

Joshua Kleckner Lokua

🐐
 
View GitHub Profile
@Lokua
Lokua / random-string.js
Last active January 21, 2016 18:00
random string
export default n => Math.random().toString(35).slice(2, !n || n >= 1097 ? 1099 : n+2)
@Lokua
Lokua / lsf.sh
Last active January 28, 2016 14:56
lsf
#!/bin/bash
alias lsfiles='for f in *; do [[ -f "$f" ]] && ls -- "$f"; done'
alias lsf=lsfiles
@Lokua
Lokua / quick-server.js
Created February 5, 2016 23:56
quickly serve index.html and and other static assets in a directory
'use strict'
const serve = require('koa-static')
new (require('koa'))()
.use(serve(`${__dirname}`))
.use(function* (next) {
return this.body = yield new Promise((resolve, reject) => {
require('fs').readFile(`${__dirname}/index.html`, 'utf8',
(err, body) => resolve(body))
@Lokua
Lokua / index.html
Last active September 8, 2016 04:36
angular 1 pass promise to directive w/out isolate scope
<div ng-app="app" ng-controller="Controller as vm" directive="directive" promise="vm.promise"></div>
@Lokua
Lokua / copy.js
Created February 5, 2017 19:15
browser programmatic text copy
export default function copy(text) {
const textArea = document.createElement(`textarea`)
textArea.value = text
textArea.style.width = `2em`
textArea.style.height = `2em`
textArea.style.background = `transparent`
document.body.appendChild(textArea)
textArea.select()
const success = document.execCommand(`copy`)
document.body.removeChild(textArea)
@Lokua
Lokua / isEventWithin.js
Created February 5, 2017 19:18
Detect if DOM event happened within a bounding box. Useful for outside click detection
export default function isEventWithin(e, element) {
const rect = element.getBoundingClientRect()
return (
rect.top <= e.clientY &&
e.clientY <= rect.top + rect.height &&
rect.left <= e.clientX &&
e.clientX <= rect.left + rect.width
)
}
@Lokua
Lokua / devToolsIgnoreStringRegExp.txt
Created February 6, 2017 22:06
Chrome DevTools regexp to ignore 404s #RegExp #ChromeDevTools
^((?!SOME_STRING).)*$
@Lokua
Lokua / .hyper.js
Created March 11, 2017 05:34
my hyperterm config
const md = mdColors()
const LIGHT = false
module.exports = {
config: {
fontSize: 12,
fontFamily: `"Fira Code", "Droid Sans Mono", "DejaVu Sans Mono", "Lucida Console", monospace`,
// `BEAM` for |, `UNDERLINE` for _, `BLOCK` for █
cursorShape: `BEAM`,
@Lokua
Lokua / quick-benchmark.js
Created April 19, 2017 02:32
quick benchmark.js
const { Suite } = require('benchmark')
const test = (specs = {/*name:fn*/}) => {
const suite = new Suite()
Object.keys(specs).forEach(name => suite.add(name, specs[name]))
suite.on('cycle', e => console.info(String(e.target)))
suite.on('complete', () => console.info(`Fastest is ${suite.filter('fastest').map('name')}`))
suite.run({ async: false })
}
// // EXAMPLE:
@Lokua
Lokua / chunk.js
Created April 20, 2017 05:33
chunk
function chunk(array, chunkSize) {
if (!chunkSize) throw new Error('chunkSize must be greater than 0')
return array.reduce((chunked, value, i) => {
if (!(i % chunkSize)) chunked.push([])
chunked[chunked.length - 1].push(value)
return chunked
}, [])
}