Skip to content

Instantly share code, notes, and snippets.

Avatar

aronanda

View GitHub Profile
@aronanda
aronanda / bookmark-convert.js
Created Sep 16, 2020
Convert Netscape bookmark export files from HTML to JSON using Node.js
View bookmark-convert.js
const fs = require('fs')
const path = require('path')
const cheerio = require('cheerio')
const inputFile = process.env.INPUT || process.argv[2] || 'bookmarks.html'
const outputFile = process.env.OUTPUT || process.argv[3] || 'bookmarks.json'
const inputFilePath = path.resolve(inputFile)
const outputFilePath = path.resolve(outputFile)
fs.readFile(inputFilePath, { encoding: 'utf8' }, (error, data) => {
if (error)
@aronanda
aronanda / Middleware.js
Last active Mar 13, 2021
A minimal Javascript Middleware Pattern Implementation with support for custom parameters, chainable use() method and a definable object. Based on https://gist.github.com/darrenscerri/5c3b3dcbe4d370435cfa
View Middleware.js
class Middleware {
constructor(obj) {
obj = obj || this
Object.defineProperty(this, '__obj', { value: obj })
Object.defineProperty(this, 'go', { value: function go(...args) {
args[args.length - 1].apply(obj, args.slice(0, -1))
}, writable: true })
}
use(fn) {
this.go = (stack => (...args) => stack(...args.slice(0, -1), () =>
View uuid.js
// https://stackoverflow.com/questions/105034/how-to-create-guid-uuid
// https://gist.github.com/jed/982883
function uuid(rng) {
rng = rng || Math.random.bind(Math)
function gen(a) {
return a
? (a ^ rng() * 16 >> a/4).toString(16)
: ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, gen)
}
View Q 4437.ruby
def visit_twitter_and_log_in
visit 'https://cards-dev.twitter.com/validator'📁
find('input.js-username-field').set(ENV['TWITTER_USERNAME'])
find('input.js-password-field').set(ENV['TWITTER_PASSWORD'])
click_on('Log in')
end
def enter_url_and_click_preview(url)
find('input.FormControl').set(url)
click_on('Preview card')
result = has_content?('Page fetched successfully')
@aronanda
aronanda / fine-state-machine.js
Last active Apr 16, 2020
Fine State Machine
View fine-state-machine.js
const EventEmitter = require('events').EventEmitter
class PrivateStateMachine {
constructor(sm, opts = {}) {
this.state = opts.state
Object.defineProperty(this, 'sm', { value: sm })
Object.defineProperty(this, 'stateKey', { value: opts.stateKey || 'state' })
this._setData(opts.data)
this._setStates(opts)
Object.defineProperty(this.sm, this.stateKey, { value: this.state, enumerable: true, configurable: true })
@aronanda
aronanda / Event.js
Last active Jun 30, 2020
Event Emitter
View Event.js
function assign(source, target) {
const fields = Object.getOwnPropertyNames(source.constructor.prototype).filter(f => f !== 'constructor')
for (const key of fields) {
if (key.substr(0, 1) === '_')
continue
const info = Object.getOwnPropertyDescriptor(source.constructor.prototype, key)
if (info.value) {
Object.defineProperty(target, key, { value: source[key].bind(source), configurable: true, writable: true })
} else {
const field = { configurable: true }
@aronanda
aronanda / server.js
Last active Dec 2, 2018
Node.js Server
View server.js
const http = require('http'),
url = require('url'),
fs = require('fs'),
path = require('path'),
port = process.env.PORT || 3000,
docroot = process.argv[2] || process.env.DOCROOT || '.',
log = console.log.bind(console)
http.createServer(function (req, res) {
log(`${ req.method } ${ req.url }`)
View logger.js
const DEBUG = true
// const DEBUG = process.env.NODE_ENV !== 'production'
const LOG_DATE = true
const LOG_LOCALE = "en-US"
const LOG_DATE_OPTS = { year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit' }
const log = function (level, ...args) {
if (DEBUG) {
let prefix = ''
if (typeof level === 'string' && [ 'log', 'warn', 'error' ].includes(level)) {