Skip to content

Instantly share code, notes, and snippets.

View sylvainpolletvillard's full-sized avatar

Sylvain Pollet-Villard sylvainpolletvillard

View GitHub Profile
if(!("valueAsDate" in HTMLInputElement.prototype)){
Object.defineProperty(HTMLInputElement.prototype, "valueAsDate", {
get: function(){
var d = this.value.split(/\D/);
return new Date(d[0], --d[1], d[2]);
},
set: function(d){
var day = ("0" + d.getDate()).slice(-2),
month = ("0" + (d.getMonth() + 1)).slice(-2),
datestr = d.getFullYear()+"-"+month+"-"+day;
@sylvainpolletvillard
sylvainpolletvillard / observe-sham.js
Created October 27, 2015 14:21
Cant wait for Object.observe, so I wrote my own change detection system: simplistic, but I dont need anything else
(function (globals, factory) {
if (typeof define === 'function' && define.amd) define(factory); // AMD
else if (typeof exports === 'object') module.exports = factory(); // Node
else globals['observe'] = factory(); // globals
}(this, function () {
var ARRAY_MUTATOR_METHODS = ["pop", "push", "reverse", "shift", "sort", "splice", "unshift"];
return function observe(o, notify, signature) {
@sylvainpolletvillard
sylvainpolletvillard / gist:fcdd71c6af9dd151a3d4
Last active November 30, 2015 00:13
Problems with constructors in JS
// 1) it is dangerous to have a `constructor` property, but not forbidden
function Car(model){
this.model = model;
this.speed = 0;
}
function Car2(model, constructor){
this.model = model;
this.constructor = constructor;
@sylvainpolletvillard
sylvainpolletvillard / gist:05da2047e74442522ee9
Last active February 14, 2016 00:47
array permutations
function getPermutations(arr){
var permutations = [];
(function _(stack, rest){
rest.length
? rest.map((el, i) => _([...stack, el], rest.filter((x,j) => i!=j)))
: permutations.push(stack)
})([], arr);
return permutations
}
@sylvainpolletvillard
sylvainpolletvillard / details-polyfill.js
Last active March 8, 2016 16:04
<details> polyfill
/**
* FORKED FROM: https://gist.githubusercontent.com/remy/370590/raw/0ade67d1b83b31c8eab1acc1f354ce425d290377/details.js
*/
(function (window, document) {
if ('open' in document.createElement('details')) return; // no polyfill necessary
var addEvent = function (el, type, fn) {
if (el && el.nodeName || el === window) {
if (document.addEventListener) {
el.addEventListener(type, fn, false);
function formatDate(value, format) {
var date = value && value instanceof Date ? value : new Date(value);
return format
.replace("YYYY", date.getFullYear().toString())
.replace("YY", date.getFullYear().toString().substr(2, 2))
.replace("MM", (date.getMonth() + 1).toString().replace(/^(\d)$/, '0$1'))
.replace("M", (date.getMonth() + 1).toString())
.replace("DD", date.getDate().toString().replace(/^(\d)$/, '0$1'))
.replace("D", date.getDate().toString())
function getAllPropertyNames(o){ return o ? Object.getOwnPropertyNames(o).concat(getAllPropertyNames(Object.getPrototypeOf(o))) : [] }
Object.prototype.getAllPropertyNames = function(){ return getAllPropertyNames(this) }
@sylvainpolletvillard
sylvainpolletvillard / chess.js
Last active December 25, 2021 15:17
Óscar Toledo G.Nanochess writen in 6chars.js
[][[[]+[][+[]]][+[]][++[++[++[++[[]][+[]]][+[]]][+[]]][+[]]]+[[]+[][+[]]][+[]][++[++[++[++[++[[]][+[]]][+[]]][+[]]][+[]]][+[]]]+[[]+[][+[]]][+[]][++[[]][+[]]]+[[]+[][+[]]][+[]][++[++[[]][+[]]][+[]]]][[[][[[]+[][+[]]][+[]][++[++[++[++[[]][+[]]][+[]]][+[]]][+[]]]+[[]+[][+[]]][+[]][++[++[++[++[++[[]][+[]]][+[]]][+[]]][+[]]][+[]]]+[[]+[][+[]]][+[]][++[[]][+[]]]+[[]+[][+[]]][+[]][++[++[[]][+[]]][+[]]]]+[]][+[]][++[++[++[[]][+[]]][+[]]][+[]]]+[[][[[]+[][+[]]][+[]][++[++[++[++[[]][+[]]][+[]]][+[]]][+[]]]+[[]+[][+[]]][+[]][++[++[++[++[++[[]][+[]]][+[]]][+[]]][+[]]][+[]]]+[[]+[][+[]]][+[]][++[[]][+[]]]+[[]+[][+[]]][+[]][++[++[[]][+[]]][+[]]]]+[]][+[]][++[++[++[++[++[++[[]][+[]]][+[]]][+[]]][+[]]][+[]]][+[]]]+[[]+[][+[]]][+[]][++[[]][+[]]]+[[![]][+[]]+[]][+[]][++[++[++[[]][+[]]][+[]]][+[]]]+[+[++[+[]][+[]]+[]+[[]+[][+[]]][+[]][++[++[++[+[]][+[]]][+[]]][+[]]]+[++[+[]][+[]]+[]][+[]]+[+[]]+[+[]]+[+[]]][+[]]+[]][+[]][++[++[++[++[++[++[[]][+[]]][+[]]][+[]]][+[]]][+[]]][+[]]]+[[!![]][+[]]+[]][+[]][++[+[]][+[]]]+[[]+[][+[]]][
@sylvainpolletvillard
sylvainpolletvillard / 666.js
Created September 8, 2016 22:02
currently thinking of the most obscure way to declare factorial function in JS
f=_=>!_||_*f(~-_)
@sylvainpolletvillard
sylvainpolletvillard / hurray.js
Created December 19, 2016 23:48
Hurray; Array subclass with hash destructuring for method callbacks, and a few helpers
class Hurray extends Array {
forEach(callback){
super.forEach.call(this, (...args) => callback(Hurray.iterator(...args)));
return this; // make forEach chainable
}
every(callback){ return super.every.call(this, (...args) => callback(Hurray.iterator(...args))) }
filter(callback){ return super.filter.call(this, (...args) => callback(Hurray.iterator(...args))) }
find(callback){ return super.find.call(this, (...args) => callback(Hurray.iterator(...args))) }
findIndex(callback){ return super.findIndex.call(this, (...args) => callback(Hurray.iterator(...args))) }