Skip to content

Instantly share code, notes, and snippets.

@eneko89
eneko89 / anchorify.js
Created March 8, 2016 10:30
Looks for URLs in the source text and replaces them with anchor tags. If there are no links in the text, it does nothing.
/**
* Looks for URLs in the source text and replaces them with anchor
* tags. If there are no links in the text, it does nothing.
*
* @param {String} text Source text.
*
* @return {String} New string with URLs in the source
* text replaced with anchor tags.
*/
function anchorify(text) {
@eneko89
eneko89 / httpGet.js
Last active March 9, 2016 09:25
Fetches data from a JSON API through HTTP GET (XMLHttpRequest). It does not make a request to the same URL if the previous has not finished. Those requests are coalesced and their callbacks receive the same result.
// Variable to track requests and their callbacks so that httpGet()
// does not make a request to the same URL if the previous has not
// finished.
var requests = {};
/**
* Fetches data from a JSON API through HTTP GET (XMLHttpRequest).
*
* Requests to the same URL before the previous one completes are
* coalesced and their callbacks receive the same result.
@eneko89
eneko89 / rewrite.js
Last active March 30, 2016 10:36
Rewrite 'el' element periodically with a string composed by an always static 'base' string and slices of 'sentence' in order to create a console prompt writing effect.
/**
* Check the following example in jsfiddle.
*
* <https://jsfiddle.net/qg9k3pmo/1/>
*/
/**
* Rewrite 'el' element periodically with a string composed by an
* always static 'base' string and slices of 'sentence' in order
* to create a console prompt writing effect. For example, if:
@eneko89
eneko89 / useAsyncEffect.tsx
Last active October 27, 2022 17:23
Custom hook for that provides an "ignore" object to avoid race conditions or memory leaks in async operations
import { useEffect } from 'react';
type EffectReturn = Promise<(() => void) | void>;
type EffectFunction = (ignore: { current: boolean }) => EffectReturn;
type DependencyArray = unknown[];
/**
* Works like useEffect, but accepts an async function as the effect
* function and provides an ignore object with a current attribute
* that tells if you should ignore the result.