Skip to content

Instantly share code, notes, and snippets.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>minimum-webrtc-demo</title>
</head>
<body>
function compose(middlewares) {
return function () {
return dispatch(0)
function dispatch(i) {
let fn = middlewares[i]
if (!fn) {
return Promise.resolve()
}
return Promise.resolve(
fn(function next() {
@Rainsho
Rainsho / genHashedIds.js
Last active June 18, 2019 03:51
generate hashed Ids with hash collision check, use Node.js `crypto` module
import { createHash } from 'crypto';
/**
* @type {Object} HASH_POOL segment_hash -> str
* @type {Object} HASH_CACHE str -> full_hash
*/
const HASH_POOL = {};
const HASH_CACHE = {};
const DEFAULT_LENGTH = 6;
const MAX_LENGTH = 32;
@Rainsho
Rainsho / basic.js
Last active December 9, 2018 12:06
basic js concepts
/**
* 1. deepClone
*/
function deepClone(obj) {
var copy;
// Handle the 3 simple types, and null or undefined
if (null == obj || 'object' != typeof obj) return obj;
@Rainsho
Rainsho / utils.js
Last active March 2, 2019 07:14
Node.js && Javascript useful utils
/**
* 1. bytesToSize
* 2. getAllFiles
* 3. createWorker
*/
function bytesToSize(bytes, prec = 2) {
if (Number.isNaN(parseInt(bytes, 10))) return bytes;
if (bytes < 1024) return `${parseInt(bytes, 10)} B`;