Skip to content

Instantly share code, notes, and snippets.

View englishextra's full-sized avatar
💜
the beat goes on

englishextra englishextra

💜
the beat goes on
View GitHub Profile
@englishextra
englishextra / htmlentities.js
Last active May 12, 2016 11:21
A minimal html entities decoder/encoder using DOM
/*!
* A minimal html entities decoder/encoder using DOM.
* gist.github.com/englishextra/737e35d19bcb4ee62ab85c9f9b292842
* modified github.com/jussi-kalliokoski/htmlentities.js
* see issue github.com/jussi-kalliokoski/htmlentities.js/issues/1
* htmlentities.encode('<&>'); returns '&lt;&amp;&gt;';
* htmlentities.decode('&lt;&amp;&gt;'); returns '<&>';
* htmlentities is a shorthand for htmlentities.encode
*/
var htmlentities = (function (a) {
/*!
* Escape strings for use as JavaScript string literals
* gist.github.com/englishextra/3053a4dc18c2de3c80ce7d26207681e0
* modified github.com/joliss/js-string-escape
*/
jsStringEscape = function (b) {
return ("" + b).replace(/["'\\\n\r\u2028\u2029]/g, function (a) {
switch (a) {
case '"':
case "'":
@englishextra
englishextra / interval.js
Last active May 17, 2016 09:41 — forked from manast/interval.js
Accurate Javascript setInterval replacement
/*!
* Accurate Javascript setInterval replacement
* gist.github.com/manast/1185904
* gist.github.com/englishextra/f721a0c4d12aa30f74c2e089370e09eb
* minified with closure-compiler.appspot.com/home
* var timer = new interval(50, function(){ if(1===1){timer.stop(), timer = 0;}}); timer.run();
* The handle will be a number that isn't equal to 0; therefore, 0 makes a handy flag value for "no timer set".
* stackoverflow.com/questions/5978519/setinterval-and-how-to-use-clearinterval
*/
function interval(d,f){this.baseline=void 0;this.run=function(){void 0===this.baseline&&(this.baseline=(new Date).getTime());f();var c=(new Date).getTime();this.baseline+=d;var b=d-(c-this.baseline);0>b&&(b=0);(function(d){d.timer=setTimeout(function(){d.run(c)},b)}(this))};this.stop=function(){clearTimeout(this.timer)}};
@englishextra
englishextra / setAsap.js
Last active May 17, 2016 19:45
setImmediate polyfill for the browser and node
/*!
* setImmediate polyfill for the browser and node
* modified github.com/taylorhakes/setAsap
* added: !window.setImmediate&&(window.setImmediate=window.setAsap);
* gist.github.com/englishextra/f4a5b2f47573af15d1ed206ac240c8d7
* This is an alternative to setTimeout(fn, 0), which behaves like setTimeout(fn, 4).
* setAsap will execute immediately. It has unmatched performance and is extremely lightweight.
* nczonline.net/blog/2011/09/19/script-yielding-with-setimmediate/
* setAsap(function() {
* // do something async
@englishextra
englishextra / promises.md
Created May 17, 2016 21:19 — forked from domenic/promises.md
You're Missing the Point of Promises

This article has been given a more permanent home on my blog. Also, since it was first written, the development of the Promises/A+ specification has made the original emphasis on Promises/A seem somewhat outdated.

You're Missing the Point of Promises

Promises are a software abstraction that makes working with asynchronous operations much more pleasant. In the most basic definition, your code will move from continuation-passing style:

getTweetsFor("domenic", function (err, results) {
 // the rest of your code goes here.
@englishextra
englishextra / JSON with JS.md
Created May 22, 2016 13:11
Access local JSON data with Javascript

What

An updated guide/collection of guides on how to access JSON data with JavaScript

Original Question on Stack Exchange.


Example 1

For reading the external Local JSON file (data.json) using java script

/*!
* truncate a text or line with ellipsis
* stackoverflow.com/questions/4700226/i-want-to-truncate-a-text-or-line-with-ellipsis-using-javascript
* gist.github.com/englishextra/b4939b3430da4b55d731201460d3decb
*/
function truncString(str, max, add) {
add = add || "\u2026";
return (typeof str === "string" && str.length > max ? str.substring(0, max) + add : str);
}
@englishextra
englishextra / plain.js
Created May 23, 2016 13:09 — forked from kkga/plain.js
snippets of plain js
// document.ready
document.addEventListener("DOMContentLoaded", function() {
// your code
}, false);
// select div
var element = document.querySelector("div");
/*!
* return an array of values that match on a certain key
* techslides.com/how-to-parse-and-search-json-in-javascript
* gist.github.com/englishextra/872269c30d7cb2d10e3c3babdefc37b4
* var jpr = JSON.parse(response);
* for (var i = 0; i < jpr.length; i++) {
* var o = jpr[i],
* t = getJsonKeyValues(o, "label"),
* p = getJsonKeyValues(o, "link");
* crel(select, crel("option", {
/*!
* escape string for HTML attributes and titles
* gist.github.com/englishextra/214582c0f3ba64be19c655f57f11241d
*/
function escapeHtml(s) {
return s = s.replace(/[<!="'\/>&]/g, function (s) {
return {
"<" : "&lt;",
"!" : "&#033;",
"=" : "&#061;",