Skip to content

Instantly share code, notes, and snippets.

View VanDalkvist's full-sized avatar

Ivan VanDalkvist

  • Yandex
View GitHub Profile
const express = require("express");
module.exports = (app, { base = "" } = {}) => ({
applyApi: api => app.use(base + api.base, buildRouter(api)),
applyRoute: route => applyRoute(app, { base })(route),
applyMiddleware: middleware => app.use(awaitHandlerFactory(middleware))
});
function buildRouter(api) {
const routeReducer = (router, key) => applyRoute(router)(api.methods[key]);
@VanDalkvist
VanDalkvist / fetch.middleware.js
Created December 17, 2018 11:45
Configurable fetch middleware implementation
import buildAsyncStages from "../stages/async-stages";
import { handleFetchError } from "../errors";
const ASYNC_FETCH = "ASYNC_FETCH";
const baseRequestOptions = { credentials: "same-origin" };
const fetchMiddleware = fetchImplementation => store => next => action => {
next(action);
@VanDalkvist
VanDalkvist / strict-storage.js
Created December 17, 2018 11:42
Strict storage with errors throwing when property is not define and already exists.
function buildStrictStorage() {
const storage = {};
return new Proxy(storage, {
get: (target, property) => {
if (!Reflect.has(target, property)) {
throw new Error(`Label "${property}" is missing.`);
}
return Reflect.get(target, property);
@VanDalkvist
VanDalkvist / run-pipeline.js
Created December 17, 2018 11:38
Run a pipeline of build scripts
const path = require("path");
const packageJson = require("./package");
const buildConfig = packageJson.build;
if (!buildConfig) {
throw new Error("There is no configuration for build.");
}
const pipeline = buildConfig.pipeline;
if (!pipeline) {
@VanDalkvist
VanDalkvist / form-builder.js
Last active December 17, 2018 11:30
FormBuilder initialization
(function (module, bus, _, logger, Q) {
module.createInstance = _createInstance;
function _createInstance(context, instanceConfig, settings) {
var initSequence = [
_initState.bind(this, instanceConfig, settings, context),
_initViewModel,
_initApp,
@VanDalkvist
VanDalkvist / search.service.js
Created December 17, 2018 11:24
Angularjs Search service
(function () {
angular
.module('projectName')
.service('searchService', _searchService);
function _searchService($log, $q, _, lunr, reportsService, userService) {
var state;
var reindexPromise = $q.when("");
@VanDalkvist
VanDalkvist / event-loop.md
Created September 7, 2018 08:31 — forked from jesstelford/event-loop.md
What is the JS Event Loop and Call Stack?

Regular Event Loop

This shows the execution order given JavaScript's Call Stack, Event Loop, and any asynchronous APIs provided in the JS execution environment (in this example; Web APIs in a Browser environment)


Given the code

import React from "react";
const renderDefinition = definition => props =>
definition && definition.Component ? (
<definition.Component {...props} />
) : null;
const renderFirst = definitions => props => {
const definition = definitions.find(definition =>
definition.condition(props)
One always has, at every stage, in the process, a working system. I
find that teams can grow much more complex entities in four months
than they can build.
-- Fred Brooks, "No Silver Bullet"
%
I made this letter longer than usual because I lack the time to make it shorter.
@VanDalkvist
VanDalkvist / builder.js
Created August 9, 2017 10:22
Build objects tree from string with properties separating by dot.
module.exports = {
construct: _construct
};
function _construct(path) {
var parts = path.split('.');
var result = {};
var last = parts.slice(0, parts.length - 1).reduce(function (prev, part) {
prev[part] = {};