Skip to content

Instantly share code, notes, and snippets.

View amatiasq's full-sized avatar

A. Matías Quezada amatiasq

View GitHub Profile
@amatiasq
amatiasq / new.js
Created December 10, 2013 15:45
A simple .new() method to create instances without constructors. This allow us to rewrite the "new" in order to do other things but create objects.
function $new() {
var obj = Object.create(this);
obj.init.apply(obj, arguments);
return obj;
}
@amatiasq
amatiasq / analyzer.js
Last active December 31, 2015 03:59
Dirty code than recursively analyzes a object for a specific string without using recursive functions because of call stack limit.
function find(term, obj, name) {
var key = '!"·$% &/()';
var visited = Date.now() + Math.random();
var _current = [{}];
var _keys = [[name || 'your_object']];
var _index = [0];
var results = [];
var current = obj;
var keys = Object.keys(obj)
@amatiasq
amatiasq / request-to-curl.js
Last active December 31, 2015 15:09
A node script than opens a server and creates a bash script per each request, containing the curl command to replicate the request it received.
#!/usr/bin/env node
//jshint node:true
'use strict';
var fs = require('fs');
var url = require('url');
var http = require('http');
var port = process.argv[2];
var folder = process.argv[3];
@amatiasq
amatiasq / 1_README.md
Last active August 29, 2015 13:56
Safe extension for native javascript objects

A simple function to extend native object

void extendNative(Function constructor, optional String property, Object methods);

In order to prevent collisions with future standard all extensions are added under a specific property ($ by default), this is configurable by the optional second argument:

extendNative(String, {
  someExtension: function() { }
});

'my string'.$.someExtension();

(function(global) {
'use strict';
function chain(obj, method) {
var original = obj[method];
obj[method] = function() {
original.apply(this, arguments);
return this;
};
}
function listenAll(target) {
var protos = [],
proto = target;
while (proto) {
protos.push(proto);
proto = Object.getPrototypeOf(proto)
}
protos
(function() {
function Deferred() {
if (window.Promise) {
var that = this;
this.promise = new Promise(function(resolve, reject) {
that._resolve = resolve;
that._reject = reject;
});
}
@amatiasq
amatiasq / .jshintrc
Created September 3, 2014 08:28
JSHint config
{
// JSHint Default Configuration File (as on JSHint website)
// See http://jshint.com/docs/ for more details
"maxerr" : 50, // {int} Maximum error before stopping
// Enforcing
"bitwise" : true, // true: Prohibit bitwise operators (&, |, ^, etc.)
"camelcase" : true, // true: Identifiers must be in camelCase
"curly" : false, // true: Require {} for every new block or scope
@amatiasq
amatiasq / throttle-promise.js
Last active November 16, 2018 16:20
This function will prevent a long operation to be executed twice in parallel. If the function is invoked a second time before the first time has completed it will receive the same promise from the first invocation without need to invoke the operation again.
function throttlePromise(operation) {
var promise = null;
return function() {
if (!promise) {
promise = operation.apply(this, arguments).finally(function() {
promise = null;
});
}
@amatiasq
amatiasq / curry.js
Last active March 15, 2019 10:34
Simple way to recursively curry javascript functions http://jsfiddle.net/amatiasq/osrsomq0/
/**
* @param {Function} fn Function to curry.
* @param {Number} lenght The arguments required to invoke the function. Optional. By default is fn.length
* @returns {Function} The currified function.
*/
function curry(fn, length) {
length = length || fn.length;
return function currified() {
var args = [].slice.call(arguments);