Skip to content

Instantly share code, notes, and snippets.

View c4x1's full-sized avatar
๐ŸŒ€
home->work->home->work;

CSpornov c4x1

๐ŸŒ€
home->work->home->work;
  • Russian
View GitHub Profile
@c4x1
c4x1 / Node.js clustered HTTP server example.js
Last active August 3, 2019 10:37 — forked from dsibilly/gist:2992412
Node.js clustered HTTP server example
(function () {
'use strict';
var cluster = require('cluster'),
http = require('http'),
os = require('os'),
/*
* ClusterServer object
@c4x1
c4x1 / promise-dot-all.js
Created July 24, 2019 01:16 — forked from indiesquidge/promise-dot-all.js
Recreating Promise.all with async/await
/*
Let us re-create `Promise.all`
`Promise.all` method returns a promise that resolves when all of the promises in the iterable argument have resolved,
or rejects with the reason of the first passed promise that rejects.
Read more about `Promise.all` on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
A basic example would be something like this:
@c4x1
c4x1 / async-examples.js
Created July 24, 2019 00:32 — forked from developit/async-examples.js
Async Array utilities in async/await. Now available as an npm package: https://github.com/developit/asyncro
/** Async version of Array.prototype.reduce()
* await reduce(['/foo', '/bar', '/baz'], async (acc, v) => {
* acc[v] = await (await fetch(v)).json();
* return acc;
* }, {});
*/
export async function reduce(arr, fn, val, pure) {
for (let i=0; i<arr.length; i++) {
let v = await fn(val, arr[i], i, arr);
if (pure!==false) val = v;
'use restrict';
/**
* Module dependences.
*/
var mongoose = require('mongoose');
/**
* load up the material model.
*/
@c4x1
c4x1 / es7-async-await.js
Created June 27, 2019 10:37
Javascript fetch JSON with ES7 Async Await
// Async/Await requirements: Latest Chrome/FF browser or Babel: https://babeljs.io/docs/plugins/transform-async-to-generator/
// Fetch requirements: Latest Chrome/FF browser or Github fetch polyfill: https://github.com/github/fetch
// async function
async function fetchAsync () {
// await response of fetch call
let response = await fetch('https://api.github.com');
// only proceed once promise is resolved
let data = await response.json();
// only proceed once second promise is resolved
@c4x1
c4x1 / searchtrie.js
Created June 15, 2019 19:44 — forked from achadha235/searchtrie.js
Simple Search Tries in ES6
/*
TRIES
A trie (or radix tree) is a simple data structure that represents a set of values stored against a
tokenizable key (usually strings). Each edge in a Trie represents a token in a key. A key is present in the trie if
(1) There exists a path (e1..en) from the root such that the key = <token(e1), token(e2)...token(en)> and
(2) there is some value at the path's terminal vertex vn.
Let k = length(key). Then time complexity for lookup, insertion and deletion for a Trie is O(k).
@c4x1
c4x1 / Trie.js
Created June 15, 2019 19:42 — forked from deadlocked247/Trie.js
Trie implementation in ES6, good for string autocomplete
/* Class representing a Trie data structure */
export default class Trie {
/**
* Creates a Trie
* @return {Object} Trie
*/
constructor() {
this.words = 0;
this.prefixes = 0;
@c4x1
c4x1 / observer.js
Created June 12, 2019 08:00 — forked from Komock/observer.js
Simple ES6 Observer Class
class Observer {
subscribe(subscriber) {
this.subscribers.push(subscriber);
}
publish(event, data) {
let query = this.subscribers.filter(subscriber => subscriber.event === event);
if (query.length) query.forEach(subscriber => subscriber.action(data));
}
constructor() {
this.subscribers = [];
@c4x1
c4x1 / createStore-no-comments.js
Created June 11, 2019 04:29 — forked from markerikson/createStore-no-comments.js
Redux mini createStore example
function createStore(reducer) {
var state;
var listeners = []
function getState() {
return state
}
function subscribe(listener) {
listeners.push(listener)
@c4x1
c4x1 / compose.js
Created May 20, 2019 11:25 — forked from WaldoJeffers/compose.js
JavaScript one-line compose (ES6)
const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)))
// Usage : compose functions right to left
// compose(minus8, add10, multiply10)(4) === 42
//
// The resulting function can accept as many arguments as the first function does
// compose(add2, multiply)(4, 10) === 42