@xprmntl/kompose
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

kompose

tiny functional composition utilities (educational project)

Examples

01 login url

Configuration file:

// config.ts
export default {
   baseUrl: 'https://example.com',
   loginPath: '/login/path',

};

Providers are data accessors, factories, getter etc. All providers get context as argument.

// provider.ts
import {qs} from './qs'; // arbitrary query string library

// data accessors providers
export const getBaseUrl = (ctx: {baseUrl: string}) => ctx.baseUrl;
export const getLoginPath = (ctx: {loginPath: string}) => ctx.loginPath;

// factory provider
export const queryStringFactory = (ctx?: any) => (params: object) => qs(params);

Factory is where composition start:

// factory function
// each provider represent argument
const loginUrlFactory = (baseUrl: string, loginPath: string, queryString: Function) =>
    // return the composed function with optional arguments
    // the composed function has access to local arguments and to factory arguments
    (params: object) => `${baseUrl}${loginPath}?` +  queryString(params);

Composition and usage:

import {factory, kompose, provide} from '../src';
import config from './share/config';
import {getBaseUrl, getLoginPath, queryStringFactory} from './share/providers';

const loginUrlFactory = (baseUrl: string, loginPath: string, queryString: Function) =>
    (params: object) => `${baseUrl}${loginPath}?` +  queryString(params);

// "provide" and "factory" are helpers function
const composition = kompose(
    provide(getBaseUrl, getLoginPath, queryStringFactory),
    factory(loginUrlFactory)
);

// when context is available...
const loginUrl = composition(config);

// ...the function is ready to use
console.log( loginUrl({foo: 'bar'}) );
// https://example.com/login/path?foo=bar

Enjoy :)

Readme

Keywords

none

Package Sidebar

Install

npm i @xprmntl/kompose

Weekly Downloads

1

Version

0.0.1

License

MIT

Unpacked Size

173 kB

Total Files

30

Last publish

Collaborators

  • xprmntl