Skip to content

Instantly share code, notes, and snippets.

View davidgarsan's full-sized avatar
💩
entropy

David davidgarsan

💩
entropy
View GitHub Profile
@davidgarsan
davidgarsan / infiniteArrayPagination.js
Last active August 8, 2018 06:12
Infinite Array Pagination
/**
* Takes an array, a given window size and
* a starting index and returns a fragment of the
* array with infinite pagination.
* If the window size if bigger than teh array size,
* it shouldn't be paginated.
**/
const a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
javascript:(function() {
function textNodesUnder(el){ var n, a=[], walk=document.createTreeWalker(el,NodeFilter.SHOW_TEXT,null,false); while(n=walk.nextNode()) a.push(n); return a; }
Array.prototype.forEach.call(textNodesUnder(document.body), function(elem){
const t = elem.textContent.trim();
if(t) {
fetch("https://ninini-cvyphndqrn.now.sh/ninini/"+t)
.then(resp => resp.text())
.then(text => elem.textContent = text) } });
})()
var memoizeNewValue = function() {
var old = null;
return function(newVal){
if(newVal === old) {
return false;
} else {
old = newVal;
return true;
}
}
function attributeChangeDetector(element, callback) {
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type == 'attributes') {
console.log('Attribute ' + mutation.attributeName + ' changed');
!!callback && callback(mutation);
}
});
function spyObjectProperty(obj, key) {
var val = obj[key];
Object.defineProperty(obj, key, {
get() {
return val;
},
set(newVal) {
val = newVal;
console.log(key + ' is now ' + val);
var xhr = function() {
var xhr = new XMLHttpRequest();
var mapper = function(data) {
return Object.keys(data).map(function(k) {
return encodeURIComponent(k) + '=' + encodeURIComponent(data[k])
}).join('&');
};
return function(method, url, data, success, error, isJSON, headers) {
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
/**
* @desc Returns a template function builder.
* @param template - template string.
* @param params - Substitution params.
* @return {function} - Template function.
*
* @example
* var template = buildTemplate("Hello, my name is ${params}");
* template('David'); // Hello, my name is David
*
@davidgarsan
davidgarsan / IE_base_bug.html
Created July 14, 2017 06:40
Fix for IE8 and IE9 base tag bug
<head>
<base href="/">
<!--[if IE]><script type="text/javascript">
// Fix for IE ignoring relative base tags.
// See http://stackoverflow.com/questions/3926197/html-base-tag-and-local-folder-path-with-internet-explorer
(function() {
var baseTag = document.getElementsByTagName('base')[0];
baseTag.href = baseTag.href;
})();
</script><![endif]-->
@davidgarsan
davidgarsan / .gitlab-ci.yml
Created October 16, 2016 08:34 — forked from b0bbywan/.gitlab-ci.yml
Build and Deploy a javascript app with Gitlab-Ci
image: node:argon
before_script:
- apt-get -qq update
- apt-get -qq install -y python2.7 python2.7-dev build-essential make gcc g++ libicu-dev
- npm -g install npm --silent
- "echo -e \"export default {CLIENT_ID: '$CLIENT_ID'}\" > app/scripts/settings.js"
- npm set progress=false
- npm install --silent

Keybase proof

I hereby claim:

  • I am davidgarsan on github.
  • I am davidgarsan (https://keybase.io/davidgarsan) on keybase.
  • I have a public key whose fingerprint is DD0B 6620 AB30 D2CB 1E47 7206 F7B7 2F6E 08CC 0955

To claim this, I am signing this object: