Skip to content

Instantly share code, notes, and snippets.

@dhoko
dhoko / debug-property.window.js
Created August 12, 2016 15:08
Watch new property attach to window with a stacktrace
Object.defineProperty(window, 'data', {set: function () {console.trace()}, get: function () {return 1}});
$$('span.value:last-of-type')
.map(node => node.textContent.trim())
.filter(txt => txt.indexOf('EUR') === 0)
.map(txt => txt.split('EUR')[1].trim().replace(',','.'))
.reduce((a,b) => a + +b, 0)
@dhoko
dhoko / listDirectives.js
Last active November 13, 2019 06:33
AngularJs - get list of all registered directives
const listDirectivesApp = () => {
const listDirectives = name => {
return angular
.module(name)
._invokeQueue
.filter(item => 'directive' === item[1])
.map(item => item[2][0]);
};
return angular
@dhoko
dhoko / removeSpacesTemplateStrings.js
Created March 4, 2016 09:49
Template Strings - remove spaces
/**
* From @link {https://gist.github.com/zenparsing/5dffde82d9acef19e43c}
* Usage:
* output = dedent`Monique mange:
* - une pomme
* - une poire
* Voilou`
*
* Output:
* 'Monique mange:
function mockUserAgent(ua) {
'use strict';
var copyNav = navigator;
navigator = {};
navigator.__proto__ = copyNav;
navigator.__defineGetter__('userAgent', function() {
return ua;
});
@dhoko
dhoko / onename.js
Last active February 1, 2016 15:50
var PSEUDO = 'bob'
var service = angular.element(document.body).injector().get('Username')
service.register(PSEUDO, function(data) {
location.href = '/';
}, function(data) {
console.log(data)
});
@dhoko
dhoko / demo.js
Last active January 22, 2016 11:08
Dispatcher
const dispatcherApp = dispatcher('App');
const eventChange = dispatcherApp
.on('change', data => {
console.log('App.change', data);
})
const eventTest = dispatcherApp
.on('test', data => {
console.log('App.test', data);
@dhoko
dhoko / mask.js
Last active July 15, 2016 09:14
Apply a mask to a String
/**
* Format an input from a given mask, key per key
* - XXXX-XXX-XXX-XXX => 1111-222-333-444
* - XXXX XXXX XXXX => 1111 2222 3333
* - XX/XX => 11/22
* @param {String} mask
* @return {Function}
*/
const maskInput = (mask = '') => {
@dhoko
dhoko / anchor.js
Created November 18, 2015 09:58
handle anchor hash linking in AngularJS
/**
* @param String content Html
* @param String state State url -> $state.$current.url
*/
const bindAnchor = (content, state) => {
return content
.replace(/href="#\w+"/g, match => {
const url = match.split('#');
return `${url[0]}#${state}#${url[1]}`;
});
@dhoko
dhoko / getNetworkType.js
Created November 2, 2015 13:00
Détection du type de connexion
/**
* Detect the type of network
* @link http://caniuse.com/#feat=resource-timing
*/
function getNetwork() {
// Default case if the network detection api is not available
if (!window.performance || !window.performance.getEntriesByName) {
return '4G';
}