Skip to content

Instantly share code, notes, and snippets.

We couldn’t find that file to show.
We couldn’t find that file to show.
View distinct.js
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
*
@rkatic
rkatic / re.js
Last active Jan 19, 2021
re - raw, multi-line, auto-escaping, composable, RegExp sexy creations
View re.js
'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, '\\$&')
@rkatic
rkatic / timeout.js
Last active Jan 19, 2021
Simple promise based timeout implementation.
View timeout.js
function timeout (ms, x) {
return new Promise((resolve, reject) => {
let timeoutId = setTimeout(() => {
timeoutId = null
reject('timeout')
}, ms)
const onSettle = () => {
if (timeoutId != null) {
clearTimeout(timeoutId)
View afterAllLoaded.js
// 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: [],
View spawn.js
'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;
@rkatic
rkatic / shallowEquality.js
Last active Nov 10, 2015
shallow equality
View shallowEquality.js
/*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);
@rkatic
rkatic / graph.py
Last active Aug 29, 2015
graph.py
View 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):
View asap.sub.js
var asap = require("asap")
asap.sub = function (onerror) {
var parent = this;
var self = function (task) {
parent(function () {
try {
task();