Skip to content

Instantly share code, notes, and snippets.

View WebReflection's full-sized avatar
🎯
Focusing

Andrea Giammarchi WebReflection

🎯
Focusing
View GitHub Profile
@WebReflection
WebReflection / icho.sh
Last active December 17, 2015 13:25
A simple bash/sh utility to write console text with some color and bold
# slightly enriched echo
# - - - - - - - - - - - - - -
# by Andrea Giammarchi
icho() {
# [*][g]✔[/] [*][r]x[/] [*][y]![/]
# resets
local reset_all=$(tput sgr0) # [/] usable as reset for each style
local reset_color=$(tput setaf 9) # [/(d|r|g|y|b|m|c|w)]
@WebReflection
WebReflection / opkg-upgrade-all
Created August 4, 2015 21:26
A simple way to upgrade all packages in an Open WRT distribution
#!/usr/bin/env sh
for p in $(opkg list-upgradable); do
if [ "$p" != "-" ]; then
if [ "$(echo ${p:0:1} | sed -e 's/[0-9]//')" != "" ]; then
echo "upgrading $p"
opkg upgrade $p
fi
fi
done
@WebReflection
WebReflection / document.ready.js
Created June 23, 2015 10:35
Inspired by jQuery, an inline function to solve forever any problem related to DOMContentLoaded (with or without defer)
!function(r,e,a,d,y){function $(f){d?f():r.push(f)}e.ready=$;e.addEventListener(a,y=function(){d=!e.removeEventListener(a,y,d);while(y=r.shift())y()},d)}([],document,'DOMContentLoaded',!1);
@WebReflection
WebReflection / es7-bind-for-es5.md
Created May 15, 2015 23:10
ES7 bind proposal directly in ES5

Using the Object.prototype to reach all JS objects but those inheriting null it is possible to somehow simulate this proposed ES7 Function Bind Syntax.

Object.defineProperty(
  Object.prototype,
  '::',
  {
    configurable: true,
 writable: true,
@WebReflection
WebReflection / svg2png.js
Created April 29, 2015 21:19
A worry-free function that grabs all SVGs images and replace them with PNGs if errors occurred
function svg2png(list) {
/*! (C) Andrea Giammarchi - MIT Style License */
for (var
error = 'error',
complete = 'complete',
naturalWidth = 'naturalWidth',
convert = function (image) {
image.src = image.src.replace(/\.svg$/,'.png');
},
onerror = function () {
@WebReflection
WebReflection / JSON.isJSON.js
Created April 29, 2015 11:34
A probably pointless attempt to have an `isJSON` method without using `try/catch`
if (!JSON.isJSON) JSON.isJSON = (function (RegExp) {
/*! (C) Andrea Giammarchi - MIT Style License */
var
reStart = '^',
reEnd = '$',
valArrayEnd = '(,|\\])',
valObjectEnd = '(,|\\})',
@WebReflection
WebReflection / gist:15fc0a2bbdd5afc7a669
Last active January 16, 2024 01:03
Disable multile submits

Coming from this post, the solution is very simple and doesn't need jQuery.

This is the function that will disable multiple submits to any form that has such attribute

function disableMultipleSubmits() { // by Andrea Giammarchi - WTFPL
  Array.prototype.forEach.call(
    document.querySelectorAll('form[disablemultiplesubmits]'),
    function (form) {
      form.addEventListener('submit', this, true);
@WebReflection
WebReflection / WeakMap.js
Last active October 8, 2015 01:01
Symbol based WeakMap
var WeakMap = WeakMap || (function (s, dP, hOP) {'use strict';
function WeakMap() { // by Andrea Giammarchi - WTFPL
dP(this, s, {value: Symbol('WeakMap')});
}
WeakMap.prototype = {
'delete': function del(o) {
delete o[this[s]];
},
get: function get(o) {
return o[this[s]];
@WebReflection
WebReflection / Symbol.md
Last active July 11, 2018 10:31
An attempt to polyfill Symbol for ES5 compatible engines
@WebReflection
WebReflection / Promise-reject-no-chain.js
Last active August 29, 2015 14:18
An alternative playground with cancel-able Promises without chainability
(function (Object, Original) {'use strict';
// (C) Andrea Giammarchi - WTFPL
if ('cancelable' in Original.prototype) return;
var
defineProperties = Object.defineProperties,
error = function () {
return new Error('Operation not permitted');