Skip to content

Instantly share code, notes, and snippets.

View JosePedroDias's full-sized avatar

José Pedro Dias JosePedroDias

View GitHub Profile
@JosePedroDias
JosePedroDias / getMyIPs.js
Created July 2, 2014 13:24
returns object with network interface name -> IP address
var os = require('os');
var getMyIPs = function() {
var ips = {};
var ifaces = os.networkInterfaces();
var onDetails = function(details) {
if (details.family === 'IPv4') {
ips[ dev ] = details.address;
}
@JosePedroDias
JosePedroDias / removeCycles.js
Created July 2, 2014 14:17
receives a JS object which may have cycles and returns another one with repeated cycles replaced by '[removed]' string. Use return instead if you don't mind them not being signalled.
var removeCycles = function(o) {
var seen = [];
var s = JSON.stringify(o, function(k, v) {
if (v !== null && typeof v === 'object') {
if (seen.indexOf(v) !== -1) {
//return;
v = '[removed]';
}
else {
@JosePedroDias
JosePedroDias / argumentsSnippet.js
Last active August 29, 2015 14:03
performant arguments handling
var ctx = this; // this can be skipped if you don't care about the context
var argsL = arguments.length;
var args = new Array(argsL); // optimizable arguments-to-array
for(var i = 0; i < argsL; ++i) {
args[i] = arguments[i];
}
@JosePedroDias
JosePedroDias / listToArr.js
Last active August 29, 2015 14:03
use this for nodeList to array conversion
var listToArr = function(lst) {
var l = lst.length;
var arr = new Array(l); // optimizable *list-to-array
for (var i = 0; i < l; ++i) {
arr[i] = lst[i];
}
return arr;
};
@JosePedroDias
JosePedroDias / README.md
Created December 5, 2014 18:58
debug function call (js)

if you want to log the call parameters for function xyz, add this is the first line of xyz:

function xyz(p1, p2) {
    debugFunctionCall('xyz', arguments);
    ...
}

then if you call

@vslinko
vslinko / 01_readme.md
Last active August 29, 2015 14:25
Experimental Relay Implementation

Experimental Relay Implementation

Features:

  • Relay.fetch(graphqlQuery) returns subscribtion that could change over time by mutation queries.
  • Relay.update(m: UpdateMutation) optimistically updates resource in all previous queries that contains updated resource.
  • Relay.update(m: DeleteMutation) optimistically deletes resource from all previous queries that contains deleted resource.
  • Relay.update(m: CreateMutation) pessimistically creates resource and executes again all previous queries.
  • All objects with id key in graphql response explained as resources. Arrays, objects without id and scalars explained as static properties.
/* For Node.js - resolve a chain of HTTP redirects
Uses getResponse() from http://gist.github.com/399276
Example: resolveHttpRedirects('http://ow.ly/1Kn4j', function(url) { sys.puts(url) });
*/
function resolveHttpRedirects(url, callback, maxnum) {
maxnum = maxnum || 3;
var count = 0;
function next(url) {
getResponse(url, function(response) {
/* For Node.js - deal with some of the boilerplate involved in HTTP GET requests */
var http = require('http'),
urlmod = require('url');
function getResponse(url, callback) {
var parsed = urlmod.parse(url);
var port = parseInt(parsed.port || '80', 10);
var client = http.createClient(port, parsed.hostname);
var path = parsed.pathname;
@thetallweeks
thetallweeks / memoize.js
Created August 22, 2014 17:59
Memoize functions
http://jsperf.com/another-memoization-comparison
// underscore.js memoize
function memoize1(func) {
"use strict";
var memo = {};
var slice = Array.prototype.slice;
return function() {
var key = "" + slice.call(arguments);
return (key in memo) ? memo[key] : (memo[key] = func.apply(this, arguments));
@devongovett
devongovett / translate.js
Created February 2, 2014 23:14
Use PhantomJS to translate stdin to english using Google Translate. Useful as a textmate command or just a command line tool.
#!/usr/bin/env phantomjs
var system = require('system');
var text = encodeURIComponent(system.stdin.read());
var url = "http://translate.google.com/#auto/en/" + text;
var page = require('webpage').create();
page.settings.userAgent = 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:13.0) Gecko/20100101 Firefox/13.0';
page.onConsoleMessage = function (msg) {