Skip to content

Instantly share code, notes, and snippets.

View aronanda's full-sized avatar

aronanda

View GitHub Profile
@aronanda
aronanda / bookmark-convert.js
Created September 16, 2020 01:54
Convert Netscape bookmark export files from HTML to JSON using Node.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 November 24, 2022 01:29
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
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), () =>
@aronanda
aronanda / uuid.js
Created August 10, 2020 07:56
UUID
// 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)
}
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 April 16, 2020 05:18
Fine State Machine
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 June 30, 2020 05:49
Event Emitter
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 December 2, 2018 04:23
Node.js Server
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 }`)
@aronanda
aronanda / logger.js
Last active July 29, 2018 19:13
JS Logger
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)) {