Skip to content

Instantly share code, notes, and snippets.

View JosePedroDias's full-sized avatar

José Pedro Dias JosePedroDias

View GitHub Profile
@JosePedroDias
JosePedroDias / wc.html
Created June 14, 2019 13:14
custom element
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>webcomponents test</title>
<link
href="data:image/x-icon;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQEAYAAABPYyMiAAAABmJLR0T///////8JWPfcAAAACXBIWXMAAABIAAAASABGyWs+AAAAF0lEQVRIx2NgGAWjYBSMglEwCkbBSAcACBAAAeaR9cIAAAAASUVORK5CYII="
rel="icon"
type="image/x-icon"
/>
@JosePedroDias
JosePedroDias / log.js
Created January 18, 2019 14:04
basic logging to document
function log(...args) {
// console.log.apply(console, args);
args = args.map((arg) =>
typeof arg === 'object' ? JSON.stringify(arg) : arg
);
const preEl = document.createElement('pre');
const codeEl = document.createElement('code');
codeEl.appendChild(document.createTextNode(args.join(' ')));
preEl.appendChild(codeEl);
document.body.appendChild(preEl);
@JosePedroDias
JosePedroDias / deepTransformValues.js
Created June 18, 2018 14:24
transforms values from json file (simple version)
const O = {
a: 2,
b: '3',
c: [5, [6, 'a'], {e:'c', f:2}],
d: {z:[3, 'd']}
};
function deepTransformValues(mapFn, o) {
if (o instanceof Array) {
@JosePedroDias
JosePedroDias / reactHooks.js
Created March 13, 2018 12:32
expose react hook internals
(function() {
var noop = function() {};
var h = __REACT_DEVTOOLS_GLOBAL_HOOK__;
var helpers = h.helpers[ Object.keys(h.helpers)[0] ];
window.helpers = helpers;
/*helpers.walkTree(
noop,
function(r) {
window.root = r;
}
@JosePedroDias
JosePedroDias / cors.py
Created February 20, 2018 00:50
mitmproxy CORS
# based on https://gist.github.com/jhass/652dd780d23c1e236ff913e8a2b77eb2
# http://jsbin.com/wonitaqode/edit?js,output
# mitmproxy -s cors.py
# mitmdump -s cors.py
from mitmproxy import http
def response(flow):
h = flow.request.headers
@JosePedroDias
JosePedroDias / ajax.js
Created February 6, 2018 17:26
ajax promise
function ajax(url, {
method = 'GET',
headers = {},
body = null,
}) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open(method, url, true);
@JosePedroDias
JosePedroDias / README.md
Last active December 1, 2017 20:05
svg path to polygon (doesn't support curve segments, only LlMmHhVvZz, assumes commands are space separated!)
const d =
  "M 12.773125,113.36602 221.3132,95.992053 l 12.11088,1.651482 10.58724,6.605925 7.48176,8.10713 2.84979,11.98594 -1.51508,13.41407 -4.62098,9.90131 -9.00254,8.33802 -9.58241,3.43426 -12.70975,0.58031 -203.961053,-17.77707 -5.9080342,-2.37991 -3.4503706,-4.50127 -1.9267305,-6.60592 1.1009889,-7.00904 3.5114276,-5.52164 z";
const p = svgDToPoly(d);

p would then be

[
@JosePedroDias
JosePedroDias / bootstrap.js
Created October 25, 2017 22:58
You want to serve a browser bundle with Reach, superagent and parse jsx scripts with type="text/jsx"? Install and run `npm run bundle`. You can expose additional content in the window too.
window.React = require('react');
window.ReactDOM = require('react-dom');
window.superagent = require('superagent');
(function() {
'use strict';
const jsx = require('jsx-transform');
const jsxScriptEls = Array.prototype.slice.apply( document.body.querySelectorAll('script[type="text/jsx"]') );
@JosePedroDias
JosePedroDias / EL.js
Created October 5, 2017 22:40
create elements in a snabbdom-like way, without any virtual thingie. small and opinionated.
function EL(_nodeName, _attrs, _children) {
let attrs = {}, children = [];
const argLen = arguments.length;
if (argLen === 1) { return document.createTextNode(_nodeName); }
else if (argLen === 2) {
if (_attrs instanceof Array) { children = _attrs; }
else if (_attrs instanceof Object) { attrs = _attrs; }
else { throw new Error('2nd arg must be either an object (attrs) or an array (children)!'); }
}
else if (argLen === 3) { attrs = _attrs; children = _children; }
@JosePedroDias
JosePedroDias / mapLimit.js
Last active October 4, 2017 22:13
map limit with promises. play with it here: https://jsbin.com/fazihoc
function eachN(arr, n) {
const arr0 = arr.slice();
const arrs = [];
while (arr0.length > 0) {
arrs.push( arr0.splice(0, n) );
}
return arrs;
}
function mapLimit(arr, promFn, limit) {