Skip to content

Instantly share code, notes, and snippets.

/* eslint-disable */
let id = 0;
function retryAsync(fn) {
const guid = arguments[1] || {};
return new Promise(resolve => {
resolve(fn(() => guid));
})
.then(value => value === guid ?
console.clear();
var intervalIds = {
nextId: 1,
};
/**
* Invoke a function callback on an interval delay. This is better than setInterval, as it...
* 1. Waits for the function to complete before starting the next interval
* 2. Waits for the optional returned Promise to resolve before starting the next interval
// URL: https://github.com/wildpeaks/package-snapshot-dom/blob/master/src/snapshot.js
console.clear();
function snapshotDOM(node, nodes, path) {
nodes = nodes || [];
if (!isObject(node)) {
return nodes;
}
@softwarespot
softwarespot / evaluate.js
Last active July 28, 2023 12:56
Pass context to eval()
function evaluate(code, args = {}) {
// Call is used to define where "this" within the evaluated code should reference.
// eval does not accept the likes of eval.call(...) or eval.apply(...) and cannot
// be an arrow function
return function evaluateEval() {
// Create an args definition list e.g. "arg1 = this.arg1, arg2 = this.arg2"
const argsStr = Object.keys(args)
.map(key => `${key} = this.${key}`)
.join(',');
const argsDef = argsStr ? `let ${argsStr};` : '';
@softwarespot
softwarespot / Promise.if.js
Last active November 5, 2017 21:23
Promise.if implementation
/**
* Resolve or reject a {@link Promise|Promise} based on the return value of a function.
* If a truthy value is returned, then the {@link Promise|Promise} is
* resolved with the truthy value; otherwise, if a falsy value, it's rejected with the reason
* defined as an instance of Error
*
* @memberof Promise
* @param {Function} fn Function to invoke and compare the return value of
* @param {*} [comparison] Optional comparison value to compare with the return value of the function;
* otherwise, default to truthy/falsy comparison
@softwarespot
softwarespot / Subject.js
Last active May 3, 2017 06:09
Subject implementation
// Both Subject and ReplaySubject
function Subject() {
if (!(this instanceof Subject)) {
throw new TypeError('"Subject" cannot be called as a function. Use the "new" keyword to construct a new "Subject" object');
}
var _this = this;
['unsubscribe', 'next', 'error', 'complete']
@softwarespot
softwarespot / Feed.js
Last active November 4, 2017 06:50
Observable implementation aka Feed
function Feed(subscriber) {
if (!(this instanceof Feed)) {
throw new TypeError('"Feed" cannot be called as a function. Use the "new" keyword to construct a new "Feed" object');
}
if (typeof subscriber !== 'function') {
throw new TypeError('Invalid argument, expected "subscriber" to be a function data type');
}
this._subscriber = subscriber
@softwarespot
softwarespot / Chainer.js
Last active April 23, 2017 14:07
Chainer implementation
// Create an object for checking if an internal object error/value combination or a simple value
var _internal = {
name: 'Chainer'
};
function Chainer(initial) {
var internal = _isInternal(initial) ?
initial :
_createInternal(null, initial);
@softwarespot
softwarespot / Queue_Stack.js
Last active April 23, 2017 13:25
Queue & Stack implementation
function Queue(iterable) {
if (!(this instanceof Queue)) {
throw new TypeError('"Queue" cannot be called as a function. Use the "new" keyword to construct a new "Queue" object');
}
this._queue = [];
if (!_isArrayLike(iterable)) {
return;
}