Coder Social home page Coder Social logo

simpledi's Introduction

A really simple, functional dependency-injection system for ES6. No classes required.

This will allow us to override any module imports, so we'll be able to test a module by itself, without all the baggage of what it imports.

There's a little bit of extra code needed, because all exports must be higher-order functions, but any DI system is going to have some boilerplate. I've tried to keep it as minimal as possible, here.

Note: this is not a code library you need to import. It's just a demo of a coding technique.

Export higher-order functions

Whereas normally we'd export this way:

export const double = (n) => n * 2;

We'll now export this way:

export const double = () => (n) => n * 2;

Import and inject:

Whereas, normally we'd use double this way:

import {double} from './whatever';

export const total = (price) => price * double(price) + (price * .2);

double(5);

Now we'll use it this way:

import {double as _double} from './whatever';

export const total = (double = _double()) => (price) => double(price) + (price * .2);

We can now call total in a couple of different ways:

import {total} from './something';
total()(5); // uses total's default dependencies

or

// ineffective double
const double = () => (n) => n;

import {total} from './something';

total(double())(5); // uses injected version of double

In some cases, the higher-order functions aren't necessary. For instance, in the last example, we could have done this:

// ineffective double
const double => (n) => n;

import {total} from './something';

total(double(5); // uses injected version of double

But if we always use higher-order functions for exports, we don't have to think about when to use them and when not to.

simpledi's People

Watchers

Marcus Geduld avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.