Skip to content

Instantly share code, notes, and snippets.

View ancms2600's full-sized avatar
💭
Toot! Toot!

Bobby Johnson ancms2600

💭
Toot! Toot!
View GitHub Profile
@ancms2600
ancms2600 / rng-sort.js
Created December 22, 2018 19:33
Score and sort a list of hex strings by most entropy
/**
* Sort a list of hexadecimal strings
* by the most unique bytes per line (ie. most entropy).
* Useful when searching a list for RNG values, such as cryptographic keys.
*/
const fs = require('fs');
const [, , file] = process.argv;
const content = fs.readFileSync(file);
const lines = content.toString().split(/\n/g);
@ancms2600
ancms2600 / sql-parser.js
Created July 10, 2018 18:01
SQL Parser in Javascript
'use strict';
(exports => {
// console.clear();
//
// let SAMPLE = ``+
// `SELECT\n`+
// ` Instance.Instance.Id AS Name,\n`+
// ` Instance.Instance.PrivateIpAddress AS "IP Address",\n`+
// ` Instance.Instance.LaunchTime AS Created,\n`+
@ancms2600
ancms2600 / iget_iunset.js
Created May 31, 2018 16:16
Case-insensitive Lodash-esque _.get() and _.unset()
// like lodash _.get() but key lookup is case-insensitive
const iget = (value, prop, defaultValue) => {
if (_.isPlainObject(value)) {
if (_.isString(prop) && prop !== '') {
return iget(value, prop.split('.'), defaultValue);
} else if (_.isArray(prop) && prop.length) {
const key = _.toLower(prop.shift()),
val = Object.keys(value).reduce((a, k) => {
if (a !== undefined) {
return a;
@ancms2600
ancms2600 / sortby-multi.js
Created May 18, 2018 17:01
Javascript sortBy multi-dimensional (e.g., `SORT BY state ASC, city ASC`)
const sortBy = (...k) => (a,b) => ((k)=> (null==k || a[k]===b[k]) ? 0 : a[k]<=b[k] ? -1 : 1 )(k.find(_k=>a[_k]!==b[_k]));
@ancms2600
ancms2600 / cross-join.js
Created May 13, 2018 18:35
CROSS JOIN implementation in Javascript
var crossJoin = (...args) => {
const m = args.reduce((acc,a)=>{
acc.push(0 === acc.length ?
a.length :
(acc[acc.length-1] * a.length));
return acc;
},[]);
const o = [];
for (const [i,letters] of args.entries()) {
@ancms2600
ancms2600 / es6-template-tags.js
Created May 13, 2018 17:54
ES6 Template Tags exampple
// Template Tag function replaces string references to variable names with eval()'ed variable's value,
// and rpads the value string with spaces so that it takes up exactly the same space that the variable name used to.
// Makes it easier to maintain block column formatting across rows of text.
var apples = 1, bananas = 2, cucumbers = 3,
result = ((apples) * bananas) - cucumbers;
console.log(vpad`((apples) * bananas) - cucumbers = result`);
console.log(vpad`((${'apples'}) * ${'bananas'}) - ${'cucumbers'} = ${'result'}`);
@ancms2600
ancms2600 / array-flatmap.js
Created May 12, 2018 20:00
Array .flatMap() Javascript shim (as per draft proposal)
Array.prototype.flatMap || (Array.prototype.flatMap = function () { // shim for draft proposal
return this.reduce(((acc,v)=>(acc.push(...v),acc)),[]); });
@ancms2600
ancms2600 / single-line-variable-declaration-cache-reuse.js
Last active May 12, 2018 20:09
Sneaky single-line temporary scoping of variable declarations and cache-reuse (Javascript trivia)
// e.g., when you want parameters passed in a pre-sorted order, but you don't want to impose on the developer
{ // shadow to avoid mutating input
// three examples computing Math.min() just once, though two assignments:
const toZero = Math.min(a,c), a = a - toZero, c = c - toZero; // toZero still in scope by EOL (wasteful?)
with({toZero:Math.min(a,c)}) const [a,c] = [a-toZero,c-toZero]; // toZero out of scope by EOL (discouraged in "use strict" mode)
const [a,c] = [a,c].map((toZero=>v=>v-toZero)(Math.min(a,c))); // toZero out of scope by EOL
const [a,c] = (()=>{ const toZero = Math.min(a,c); return [a-toZero,c-toZero]; })(); // toZero out of scope by EOL (long-winded)
}
@ancms2600
ancms2600 / server-client-browserify-shim.js
Last active May 19, 2018 15:36
Minimalist CommonJS module.exports compatibility shim for Node.js Server + Browser Client
'use strict';
(exports=>{ // browser-client reuse shim
// ...
})(typeof exports === 'undefined' ? window : exports);
@ancms2600
ancms2600 / routes.js
Created April 18, 2018 04:00
Express.js render Stylus .styl to .css every request; skips output file to local disk
// render .styl to .css every request; skips output file to local disk
app.use((req, res, next) => {
const stylus = require('stylus');
const join = require('path').join;
const src = join(__dirname, '/../public');
const url = require('url');
const fs = require('fs');
if ('GET' !== req.method && 'HEAD' !== req.method) return next();
const path = url.parse(req.url).pathname;
if (!/\.css$/.test(path)) return next();