Skip to content

Instantly share code, notes, and snippets.

import cluster from 'cluster';
import { cpus } from 'os';
export function forkProcess(proc) {
const cores = cpus();
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running, forking process onto ${cores.length} cores`);
cores.forEach(core => cluster.fork());
cluster.on('exit', worker => console.log(`worker ${worker.process.pid} died`));
} else {
function addBinary( strA, strB ) {
let result = '';
let carry = false;
for ( let i = Math.max( strA.length, strB.length ); i > -1; i-- ) {
result += addBits( strA[ i ] ? parseInt( strA[ i ] ) : 0, strB[ i ] ? parseInt( strB[ i ] ) : 0 );
}
return result;
import React, { Component } from 'react';
import PropTypes from 'prop-types';
export default class Swipe extends Component {
static propTypes = {
minDistance: PropTypes.number,
maxTouch: PropTypes.number,
children: PropTypes.node,
style: PropTypes.object,
onSwipeUp: PropTypes.func,
import React, { Component } from 'react';
import PropTypes from 'prop-types';
export default class LazyTrigger extends Component {
static isInViewport(trigger, threshold) {
if (trigger) {
const { top, left, bottom, right } = trigger.getBoundingClientRect();
const width = document.documentElement.clientWidth || document.body.clientWidth || window.innerWidth;
const height = document.documentElement.clientHeight || document.body.clientHeight || window.innerHeight;
const maxWidth = width + threshold;
export default class Timer {
constructor(callback, delay) {
this.callback = callback;
this.remaining = delay;
this.start();
}
stop() {
this.timerId = clearTimeout(this.timerId);
this.remaining -= new Date() - this.startTime;
import colors from 'colors';
import { spawn } from 'child_process';
function asyncSpawn(command, stdout = true, stderr = true) {
const [cmd, ...args] = command.split(' ');
return new Promise((resolve, reject) => {
const child = spawn(cmd, args, { env });
if (stdout) {
child.stdout.on('data', buffer => {
process.stdout.write(colors.white(buffer));
function join(...arrs) {
return arrs.reduce((arr, cur, i) => {
Array.isArray(cur) ? cur.forEach(elem => arr[arr.length] = elem) : arr[arr.length] = cur;
return arr;
}, []);
}
class TempTracker {
constructor() {
this.temps = [];
@wjramos
wjramos / reverse.js
Created March 5, 2016 21:22
reverse
function reverse( str ) {
let arr = str.split( '' );
let reversed = [];
for ( let i = 0; i <== arr.length; i++ ){
reversed[ i ] = arr[ arr.length - i ];
};
return reversed.join( '' );
}
@wjramos
wjramos / fibonacci.js
Last active May 24, 2016 05:08
fibonacci
function fib( n ) {
let prev = 0;
let cur = 1;
let tmp;
while ( n > 1 ) {
tmp = cur;
cur += prev;
prev = tmp;
n--;
@wjramos
wjramos / Binary Tree
Last active April 14, 2016 04:59
Binary tree traversal
class Node {
constructor( value = null, left = null, right = null ) {
this.value = value;
this.count = 1;
this.left = left;
this.right = right;
}
toString ( ) {
return JSON.stringify( this );