Skip to content

Instantly share code, notes, and snippets.

View arch1t3ct's full-sized avatar

Andrius Virbičianskas arch1t3ct

View GitHub Profile
@arch1t3ct
arch1t3ct / cluster_worker_persistent_data.js
Last active December 19, 2015 20:08
A demonstration on how to pass data from a killed worker to a new one when using Node.js cluster module. More information at http://a.ndri.us/blog/node-js-cluster-worker-persistent-data
var cluster = require('cluster');
if (true === cluster.isMaster) {
var map = {};
function forkWorker(data) {
var worker = cluster.fork({data: data});
map[worker.id] = data;
}
@arch1t3ct
arch1t3ct / big_number_to_string_in_json_string.js
Last active December 19, 2015 07:09
Javascripts supports precise numbers up to 9 007 199 254 740 992. Everything beyond that looses precision. I found myself loosing this precision when I had to parse a JSON with big numbers. A solution (although hacky) is to manually convert numbers to strings before parsing. Note: this will convert numbers to strings from 1 000 000 000 000 000 a…
var json_string = '{"big_number": 3754019163539080706}';
var json_string_converted = json_string
.replace(/\s*:\s*(\d{16,})\s*\}/mg, ':"$1"}')
.replace(/\s*:\s*(\d{16,})\s*\,/mg, ':"$1",');
var json = JSON.parse(json_string);
var json_converted = JSON.parse(json_string_converted);
console.log(json); // Precision lost
@arch1t3ct
arch1t3ct / last_business_day_of_month.js
Created November 30, 2012 10:17
Javascript (Node.js) function for getting last working/business day of the month.
/**
* Finds last working/business day of the month.
*
* Not tested for cross-browser compability. Works in Node.js though.
*
* EXAMPLES:
*
* 1. Returns last day of the last month of the current year:
*
* var result = lastBusinessDayOfMonth();