This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = []; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( '' ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fib( n ) { | |
let prev = 0; | |
let cur = 1; | |
let tmp; | |
while ( n > 1 ) { | |
tmp = cur; | |
cur += prev; | |
prev = tmp; | |
n--; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |