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 / Promise-rejet.js
Last active August 29, 2015 14:18
An alternative playground with cancel-able Promises that rejects through arbitrary value
(function (Object, Original) {'use strict';
// (C) Andrea Giammarchi - WTFPL
OP = Original.prototype;
if ('cancel' in OP) return;
var
OP,
$then = OP.then,
@WebReflection
WebReflection / Promise.js
Last active November 8, 2017 12:51
An attempt to make sense in cancelable Promises world
(function (Object, Original) {'use strict';
// (C) Andrea Giammarchi - WTFPL
if (Original._isPatch) return;
var
$then = Original.prototype.then,
$catch = Original.prototype.catch,
defineProperty = Object.defineProperty,
@WebReflection
WebReflection / WeakMap.js
Created March 27, 2015 13:09
Polymer WeakMap alternative
var WeakMap = WeakMap || (function () {'use strict';
// "memory-safer" alternative to Polymer WeakMap
// probably less performant but it's OK
// since deleting is really a rare case with WM anyway
// (C) Andrea Giammarchi - MIT Style License
function WeakMap() {
defineProperty(this, id, {value: id + i++});
}
var
defineProperty = Object.defineProperty,
@WebReflection
WebReflection / csp
Last active November 11, 2020 00:08
Generates the right sha1 and sha256 for Content Security Policy aware scripts
#!/usr/bin/env bash
echo ''
if [ "$1" = "" ]; then
echo 'csp sha1 and sha256 generatoor'
echo 'csp "code"'
echo 'csp /file-name'
else
if [ -f "$1" ]; then
CONTENT=$(cat $1)
@WebReflection
WebReflection / one.js
Created March 7, 2015 19:42
A simple one file technique to bring JS on the client and the server
/** server */
var
port = 3000,
server = require('http').createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<!DOCTYPE html>'.concat(
'<html>',
'<head>',
'<script>(', client, ').call(this);</script>',
'</head>',
@WebReflection
WebReflection / slice.js
Last active March 8, 2016 11:06
How to slice arguments without leaking them
// Andrea Giammarchi, WTFPL
function slice() {'use strict';
for (var
o = +this, // offset
i = o, // start index
l = arguments.length, // length
n = l - o, // new length
a = Array(n < 0 ? 0 : n); // new Array
i < l; i++
) a[i - o] = arguments[i];
@WebReflection
WebReflection / gist:24c4c475bdeb59405e87
Last active February 15, 2016 15:56
setTimeout and setInterval extra arguments

Every single JavaScript engine supports standard timers *

These accept one or more extra argument by specs

for(var i = 0; i < 2; i++) {
  setTimeout(function (i) {
    console.log(i);
  }, 0, i); // <== see this?
}
@WebReflection
WebReflection / md.css
Last active August 29, 2015 14:15
shows HTML as if it was written in Markdown
/* HTML.md
a silly idea by Andrea Giammarchi */
.md h1:before {
content: '# ';
}
.md h2:before {
content: '## ';
}
.md h3:before {
content: '### ';
@WebReflection
WebReflection / is-arguments.js
Last active August 29, 2015 14:14
an attempt to understand arguments without {}.toString
function isArguments(obj) {
return obj &&
typeof obj === 'object' &&
Object.prototype.hasOwnProperty.call(obj, 'length') &&
!Object.prototype.propertyIsEnumerable.call(obj, 'length') &&
!Array.isArray(obj);
}
// should be more reliable
function isArguments(obj) {
@WebReflection
WebReflection / html-escape.md
Last active August 21, 2022 16:27
How to escape and unescape from a language to another

update

I've created a little repository that simply exposes the final utility as npm module.

It's called html-escaper


there is basically one rule only: do not ever replace one char after another if you are transforming a string into another.