Skip to content

Instantly share code, notes, and snippets.

@rkatic
rkatic / gist:5309285
Last active December 15, 2015 19:09
Some utility/example functions for sequential processing with Q.
function loop (fn, startValue) {
var deferred = Q.defer(),
finished = false;
function end (val) {
finished = true;
deferred.resolve(val);
}
var asap = require("asap")
asap.sub = function (onerror) {
var parent = this;
var self = function (task) {
parent(function () {
try {
task();
@rkatic
rkatic / graph.py
Last active August 29, 2015 14:14
graph.py
def edgesFunc(G):
if callable(G):
return G
items = _itemsFunc(G)
return lambda v: items(G[v])
# (list of dictionaries)
def bgraph(x, dicttype=dict):
@rkatic
rkatic / shallowEquality.js
Last active November 10, 2015 22:33
shallow equality
/*eslint-env es6*/
const hasOwn = ({}).hasOwnProperty;
// Check if two objects are equal by comparing own enumerable properties.
const isEqualShallow = (a, b) => {
if (a === b) return true;
if (!a || !b) return false;
const akeys = Object.keys(a);
const bkeys = Object.keys(b);
'use strict';
const spawn = generatorFunction => function() {
const gen = generatorFunction.apply(this, arguments);
return new Promise(resolve => {
const handle = (it) => {
if (it.done) {
return it.value;
// This is a quick example on how we could wait for real loaded state
// by monitoring traffic. If needed, traffic filtering could be added.
var dataByTabId = {}
function getDataByTabId (tabId) {
if (!dataByTabId[tabId]) {
dataByTabId[tabId] = {
reqCounter: 0,
callbacks: [],
@rkatic
rkatic / timeout.js
Last active January 19, 2021 07:33
Simple promise based timeout implementation.
function timeout (ms, x) {
return new Promise((resolve, reject) => {
let timeoutId = setTimeout(() => {
timeoutId = null
reject('timeout')
}, ms)
const onSettle = () => {
if (timeoutId != null) {
clearTimeout(timeoutId)
@rkatic
rkatic / re.js
Last active January 19, 2021 07:29
re - raw, multi-line, auto-escaping, composable, RegExp sexy creations
'use strict'
const objToStringMethod = Object.prototype.toString
const reRegExpChar = /[\\^$.*+?()[\]{}|]/g
const reUnescapedSpaces = /(?<!\\)\s+/g
const reHasSpace = /\s/
const compact = raw => reHasSpace.test(raw) ? raw.replace(reUnescapedSpaces, '') : raw
const escape = val => String(val).replace(reRegExpChar, '\\$&')
const identity = x => x
/**
* Creates a predicate function to check if a value is not seen before
*
* @param {function} [by = x=>x]
* @returns {function} predicate function
*
* @example
*
We couldn’t find that file to show.