Skip to content

Instantly share code, notes, and snippets.

View ldmarz's full-sized avatar
💯
Dandolo todo mientras me divierto

Lenin martinez ldmarz

💯
Dandolo todo mientras me divierto
View GitHub Profile
@ldmarz
ldmarz / http-interceptor.ts
Last active April 17, 2021 12:00
Http interceptor for ionic 3, this allow us to make actions like obtain a JWT token before send the request. Sample of implementation at http://ldmarz.com/ionic3-http-interceptor/
import {Http, RequestOptionsArgs, Response, RequestOptions, ConnectionBackend, Headers} from '@angular/http';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';
import { Storage } from '@ionic/storage';
export class HttpProvider extends Http {
constructor (connectionBackend: ConnectionBackend, requestOptions: RequestOptions, public storage: Storage ) {
super(connectionBackend, requestOptions);
}
@ldmarz
ldmarz / unit-test-service.js
Created February 5, 2018 19:22
Unit testing service in angularjs
const module = angular.mock.module;
const inject = angular.mock.inject;
let searchService;
describe('Testing some awesome service', () => {
beforeAll(module('yourModule'));
beforeEach(inject($injector => {
searchService = $injector.get('SearchClass');
}));
@ldmarz
ldmarz / index.js
Created June 27, 2018 14:32
Get list of events bound on an element with jQuery
console.info($._data($(document)[0], 'events'));
// Example output
// {
// "mousedown": [
// {
// "type": "mousedown",
// "origType": "mousedown",
// "guid": 1,
// "namespace": ""
@ldmarz
ldmarz / KeydownGist
Last active July 31, 2018 01:10
Capture keydown in multiple divs using JQuery Running sample at: http://jsfiddle.net/gh/gist/jQuery/3.3.1/ed86caa7186b17858f4f200e8b636a1f
Just for name purpose
@ldmarz
ldmarz / Debounce example
Last active August 19, 2018 18:17
Example of how to use debounce from lodash, more information at http://ldmarz.com/mejora-la-velocidad-de-tu-javascript-con-debounce/
Just for naming purpose
@ldmarz
ldmarz / ExampleDebounce.js
Last active August 19, 2018 18:14
Example of how to use debounce from lodash, more information at http://ldmarz.com/improve-the-speed-of-your-javascript-code-with-debounce/
function writeDOM(e) {
// Imagine some slow/hard function
$("#result").text(`${e.clientX} - ${e.clientY}`);
}
const $checkBox = $('#checkbox');
const debouncedWriteDom = _.debounce(writeDOM, 100);
$("#container-result").mousemove(e => {
if ($checkBox.is(':checked')) {
@ldmarz
ldmarz / ThrottleExample.js
Last active August 19, 2018 18:16
Example to Improve your javascript speed with throttle, example at: https://wp.me/pabITX-7j
// find elements
let i = 0;
function writeDOM() {
// Imagine some slow/hard function
$("#result").html(i++);
}
const $checkBox = $('#checkbox');
const throttleWriteDom = _.throttle(writeDOM, 1000);
@ldmarz
ldmarz / find.js
Created August 10, 2018 13:43
Example of use find function from lodash
var users = [
{ firstName: "Ldmarz", lastName: "martinez", age: 28, gender: "male" },
{ firstName: "Suli", lastName: "bern", age: 5, gender: "female" },
{ firstName: "Marianis", lastName: "Carrey", age: 54, gender: "male" },
{ firstName: "Jhon", lastName: "carret", age: 40, gender: "female" }
];
var user = _.find(users, { name: "ldmarz", gender: "male" });
// { firstName: "Ldmarz", lastName: "martinez", age: 28, gender: "male" },
@ldmarz
ldmarz / set.js
Created August 10, 2018 13:56
Set and Get lodash example
var someNiceObject = { hello: { someDeepKey: "hello" } };
_.set(someNiceObject, "hello.items[0]", "An item");
// here items was added safely
// someNiceObject === { hello: { someDeepKey: "hello", items: ["An item"] } }
//Here we can access to a not existing key getting a default value instead
var name = _.get(someNiceObject, "someNotExistingKey", "Ldmarz");
// name => Ldmarz
@ldmarz
ldmarz / uniq.js
Created August 10, 2018 14:03
Uniq example from lodash function
const someArray = [1, 1, 3]
const cleanArray = _.uniq(someArray);
// cleanArray === [1, 3]