Skip to content

Instantly share code, notes, and snippets.

View felixzapata's full-sized avatar

Félix Zapata felixzapata

View GitHub Profile
@felixzapata
felixzapata / gist:1f886b46109fbaa97969
Created June 17, 2015 09:57
cómo sacar el valor más cercano a un array dado uno proporcionado
var pos = [0,20,40,60,80,100];
var valor = 15;
var resultado = pos.map(function (val){ return {val:val, dif: Math.abs(val-valor)};}).reduce(function(acc, pair){return (acc && acc.dif < pair.dif) ? acc : pair}).val;
@felixzapata
felixzapata / gist:f26a2b843b67cb53f0f5
Created May 11, 2015 07:20
call and apply examples
function warrior(speed, strength){
console.log(
"Warrior: " + this.kind +
", weapon: " + this.weapon +
", speed: " + speed +
", strength: " + strength
);
}
var warrior1 = {
@felixzapata
felixzapata / gist:4b52de061977fc9040fe
Created May 8, 2015 09:13
'without' function for Ramda
R.without = function (){
var args = Array.prototype.slice.call(arguments);
var originalArray = args.pop();
return R.reject(function (el){ return args.indexOf(el) != -1}, originalArray);
}
@felixzapata
felixzapata / gist:7f587826495748447910
Created May 4, 2015 07:38
Helper functions, inspired from the Command Line API
// Returns first element that matches CSS selector {expr}.
// Querying can optionally be restricted to {container}’s descendants
function $(expr, container) {
return typeof expr === "string"? (container || document).querySelector(expr) : expr || null;
}
// Returns all elements that match CSS selector {expr} as an array.
// Querying can optionally be restricted to {container}’s descendants
function $$(expr, container) {
return [].slice.call((container || document).querySelectorAll(expr));
@felixzapata
felixzapata / gist:1c945182f8c2ba47b1a5
Created April 15, 2015 16:18
" Hello world " example with factory / service / provider:
// http://stackoverflow.com/questions/15666048/service-vs-provider-vs-factory
var myApp = angular.module('myApp', []);
//Service style, probably the simplest one
myApp.service('helloWorldFromService', function() {
this.sayHello = function() {
return "Hello, World!"
};
});
@felixzapata
felixzapata / gist:c1255e9599e859dad7d8
Created April 8, 2015 07:57
Javascript: concat to string at beginning?
// http://stackoverflow.com/questions/6094117/javascript-concat-to-string-at-beginning
mystr = mystr.replace (/^/,'John ');
@felixzapata
felixzapata / gist:ccd99e2a069a2410a37c
Created April 1, 2015 14:44
iPhone 6 and 6 Plus Media Queries
http://stackoverflow.com/questions/25759046/iphone-6-and-6-plus-media-queries
/* 6 */
@media only screen and (max-device-width: 667px)
and (-webkit-device-pixel-ratio: 2) {}
/* 6+ */
@media screen and (min-device-width : 414px)
and (-webkit-device-pixel-ratio: 3) {}
@felixzapata
felixzapata / gist:912151a47e0c3d4f402f
Created March 24, 2015 17:48
How to trigger ng-change in directive test in AngularJS
// http://stackoverflow.com/questions/17626604/how-to-trigger-ng-change-in-directive-test-in-angularjs
changeInputValueTo = function(value) {
inputElm.val(value);
browserTrigger(inputElm, $sniffer.hasEvent('input') ? 'input' : 'change');
};
@felixzapata
felixzapata / gist:1f640a182c8f5e24ff33
Created January 24, 2015 01:04
Freeze / Stub Time in Jasmine tests
// http://grosser.it/2012/03/23/freeze-stub-time-in-jasmine-tests/
withTimeFrozenAt("2012-01-01", function(){
it("is frozen", function(){
// do something useful with time
})
})
var withTimeFrozenAt = function(time, fn){
describe('with time frozen at ' + time, function() {
console.log('jasmine-version:' + jasmine.getEnv().versionString());