npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sifodyas/fp-di

v3.0.0

Published

Provide a way to have DI from Sifodyas container into Functional Programing (or functional components)

Downloads

4

Readme

@sifodyas/fp-di

Provide a way to have DI from Sifodyas container into Functional Programing (or functional components)

Installation

yarn

$ yarn add @sifodyas/fp-di

npm

$ npm install --save @sifodyas/fp-di

About

This Sifodyas plugin helps you using the container and the dependency injection in functional programing situations.
It can be used in React functional components or other non class based component from other frameworks or architecture choices.

Usage

Init

// setup the plugin in your kernel

import { Container, Kernel } from '@sifodyas/sifodyas';
import { FunctionalDepencencyInjectorPass } from '@sifodyas/fp-di';

class MyKernel extends Kernel {
    public build(container: Container) {
        container.addCompilerPass(new FunctionalDepencencyInjectorPass());
    }
}

Registering this compiler pass will setup an internal glbal container for the plugin to work.

Example

Use case: we want to display a red outline if we are in debug mode.

import React from 'react';
import { withParameter } from '@sifodyas/fp-di';

const Comp = withParameter('kernel.debug')(({ parameter: isDebug }) => { // "isDebug" will be typed as boolean
    const style = isDebug ? { style: { border: '1px solid red' } } : {};

    return (<div {...style}>{ props.children }</div>)
});
import { injectParameter } from '@sifodyas/fp-di';

// in the injected function, every other arguments after the first one (where container's values are injected)
// are infer back into the type of the returned function.
const f = injectParameter((isDebug, something: string) => {
    // use isDebug and render
}, 'kernel.debug');

// f will be now typed as: (something: string) => void

f('Rendered!');

API

The plugin provides 3 ways of using the container: get, inject, with. Each "way" works with parameters or services. Also, what you request will be typed if your mapping is properly set in you code (by augmenting Sifodyas).

Setup the container:

// for example purpose, we setup our own services and parameters
// into Sifodyas.
// In next code, Container boot phase is skipped, only type augmentation is shown
type MyService = { run(): void };
declare module '@sifodyas/sifodyas' {
    // augment param key list with associated type
    interface KernelParametersKeyType {
        foo: number;
        bar: string;
    }

    // augment service key list with associated type
    interface ServicesKeyType {
        baz: MyService;
        qux: MyService
    }
}

get

Get will just get what you need from the container and return it as is. It's more a locator/retriever than an injection.

const foo = getParameter('foo'); // typed as number
const [foo, bar] = getParameters('foo', 'bar'); // typed as number and string

getService('baz').run(); // because returned value is typed as MyService
getServices('baz', 'qux').forEach(s => s.run()); // because returned value is typed as a tuple of [MyService, MyService]

inject

Inject will bind a function to inject what you need in its first argument. The returned bound function will have every other arguments typed and extracted from the original function.

injectParameter(foo => {}, 'foo')(); // foo, inside the function, is typed as number
injectParameters(([foo, bar]) => {}, 'foo', 'bar')(); // foo ans bar, inside the function, are typed as number and string

injectService(baz => { baz.run() }, 'baz')();
injectServices(([baz, qux]) => { baz.run(); qux.run(); }, 'baz', 'qux')();

with

With will act like an high order function and will hydrate the first argument object with a property containing what you requested.

withParameter('foo')(({parameter: foo}) => {})(); // the "parameter" property will be provided with the string value from foo
withParameters('foo', 'bar')(props => { const [foo, bar] = props.parameters; })(); // for multiple things, the "parameters" (plural) property will be stuffed

interface Props extends WithService {
    data: string;
}
withService('baz')((props: Props) => {
    props.service.run();
    console.log(props.data);
})({ data: 'Hello World!' });

interface Props extends WithServices {
    data?: number;
}
withServices('baz', 'qux')<Props>(props => {
    const [baz, qux] = props.parameters;
    baz.run();
    qux.run();
})();