iyuo/context


The TypeScript ecosystem for an object. It allows to add processing plugins to it.

https://iyuo.github.io/context/

License: Apache-2.0

Language: TypeScript

Keywords: context, typescript


context

NPM version License Twitter

The TypeScript ecosystem for an object. It allows to add processing plugins to it.

Install

Node:

https://nodei.co/npm/@iyuo/context.png?downloads=true&downloadRank=true&stars=true

npm install @iyuo/context

Browser

<script src="//iyuo.github.io/context/dist/context.min.js"></script>

Import package to the project

TypeScript:

import { Context, pluginize, lsh, rsh } from "@iyuo/context";

or JavaScript:

var iyuo = require("@iyuo/context");
var Context = iyuo.Context;
var pluginize = iyuo.pluginize;
var lsh = iyuo.lsh;
var rsh = iyuo.rsh;

Documentation

Link: https://iyuo.github.io/context/docs/index.html

Demo

See, how it's working: https://runkit.com/lopatnov/context-demo

Test it with a runkit: https://npm.runkit.com/@iyuo/context

Plugins

Example

function sum(this: number[]): number {
   return this.reduce((previousValue: number, currentValue: number): number => {
     return previousValue + currentValue;
   }, 0);
}

var numbers = new Context([1, 2, 3, 4, 5]);
var takeSum = numbers.make(sum);
console.log(takeSum); // 15

Use cases

class Context

console.info('A sample of a Context<TContext> class, the object Context<string>')
console.log(new Context("A sample context"));

context(): TContext

console.info('context(): TContext');
console.info('Gets the context from a wrapper');

var sys = new Context({ hello: 'world' });
console.log(sys.context());

var arr = new Context([1, 2, 3]);
console.log(arr.context());

change(context: TContext): Context

console.info('change(context: TContext): Context<TContext>');
console.info('Change current context to a new one');

var arr = new Context([1, 2, 3]);
var changed = arr.change([4,5,6]);
console.log( arr === changed ); // true
console.log( arr.context() ); // [4,5,6]

var sys = new Context({ hello: 'world' });
console.log(sys.change({hello: 'friends'}).context()); //{hello: "friends"}

use(...args: any[]): Context

console.info('use(...args: any[]): Context<TContext>');
console.info('Add arguments of plugins execution');

function dialog(useQ, useA) {
   this.q(`${useQ} Hi, how are you doing?`);
   this.a(`${useA} Fine.`);
   this.q(`${useA} How's it going?`);
   this.a(`${useQ} Good`);
}

var space = new Context({
   q: (msg) => { console.log(`Q: ${msg}`) },
   a: (msg) => { console.log(`A: ${msg}`) }
});

space.use('Tom', 'Lisa').make(dialog);

useArray(use: any[]): Context

console.info('useArray(use: any[]): Context<TContext>');
console.info('Uses "use" parameter for arguments of plugins execution instead of current Context class property _use.');

function strategy(teamA, teamB) {
    return function(behavior1, behavior2, behaviorDefault) {
        switch(this(teamA,teamB)) {
            case 1:
                return behavior1();
            case -1:
                return behavior2();
            default:
                return behaviorDefault();
        }
    }
}

function strategy1() {
    console.log('This is strategy1');
    return 1;
}

function strategy2() {
    console.log('This is strategy2');
    return 2;
}

function strategy3() {
    console.log('This is strategy3');
    return 3;
}

var space = new Context(function compare(a,b){
  var diff = a - b;
  return diff === 0 ? 0 : (diff > 0 ? 1 : -1);
});

space.useArray([strategy1, strategy2, strategy3]).make(strategy(3,4))

tasks(...plugins: IPlugin<TContext, void>[]): Context

console.info('tasks(...plugins: IPlugin<TContext, void>[]): Context<TContext>');
console.info('Execute plugins functions the tasks for a context');

function or() {
 this.or = this.a | this.b;
}

function and() {
 this.and = this.a & this.b;
}

function xor() {
 this.xor = this.a ^ this.b;
}

var tasksDemo = new Context({a: true, b: false});
tasksDemo.tasks(or, and, xor);
console.log(tasksDemo.context())

task(plugin: IPlugin<TContext, void>, ...use: any[]): Context

console.info('task(plugin: IPlugin<TContext, void>, ...use: any[]): Context<TContext>');
console.info('Execute plugin with params');

function not(comment) {
 console.log(`${comment}: ${!this.value}`);
 this.not = !this.value;
}

var taskDemo = new Context({value: false});
taskDemo.task(not, 'not this equals');
console.log(taskDemo.context())

make(plugin: IPlugin<TContext, TResult>, ...use: any[]): TResult

console.info('make<TResult>(plugin: IPlugin<TContext, TResult>, ...use: any[]): TResult');
console.info('Execute plugin function and return the result of plugin processing');

function sum() {
   return this.reduce((previousValue, currentValue) => {
     return previousValue + currentValue;
   }, 0);
}

var numbers = new Context([1, 2, 3, 4, 5]);
var takeSum = numbers.make(sum);
console.log(takeSum); // 15

map(plugin: IPlugin<TContext, TMappedContext>, ...use: any[]): Context

console.info('map<TMappedContext>(plugin: IPlugin<TContext, TMappedContext>, ...use: any[]): Context<TMappedContext>');
console.info('Executes plugin and make new context, based on the plugin result');

function increment() {
 return this + 1;
}

var num = new Context(10);
console.log(num
    .map(increment)
    .map(increment)
    .map(increment)
    .map(increment)
    .context());

scope(plugin: IScope<TContext, TResult>, ...use: any[]): TResult

console.info('scope<TResult>(plugin: IScope<TContext, TResult>, ...use: any[]): TResult');
console.info('Execute IScope processing plugin');
console.info('IScope = (this: Context<TContext>, context: TContext, use: any[]): TResult');

function switchContextUse(context, use) {
    return new Context(use).use(context);
}

function eachFunc() {
    for (var index in this) {
        var f = this[index];
        if (f instanceof Function) {
            f.apply(this, arguments);
        }
    }
}

var c = new Context([1,2,3,4,5]);
c
.use(function(){ console.log('A: ', this, arguments) })
.use(function(){ console.log('B: ', this, arguments) })
.scope(switchContextUse)
.task(eachFunc);

console.log(c);

pluginize<TContext, TResult>(plugin: IPlugin<TContext, TResult>): IPlugin<TContext, TResult>

console.info('pluginize<TContext, TResult>');
console.info('Converts a function to a context ecosystem plugin.');

function addFunction(n) {
    var arr = [];
    for (var i = 0; i < this.length; i++) {
        arr.push(this[i] + n);
    }
    return arr;
}

var add = pluginize(addFunction);

var x = new Context([1,2,3,4,5]);

//You can use
console.log(x.map(addFunction, 10).context());

//But better looks
console.log(x.map(add(10)).context());

lsh<TContext, TResult>(func: (context: TContext, ...args: any[]) => TResult): IPlugin<TContext, TResult>

console.info('lsh<TContext, TResult>(func: (context: TContext, ...args: any[]) => TResult): IPlugin<TContext, TResult>');
console.info('Moves a first argument to a function context. This is conversion from a function to context based function.');

function sub(a,b) {
  return a - b;
}

var s = lsh(sub);
console.log(s.call(10, 5));

rsh<TContext, TResult>(plugin: IPlugin<TContext, TResult>): (context: TContext, ...args: any[]) => TResult

console.info('rsh<TContext, TResult>(plugin: IPlugin<TContext, TResult>): (context: TContext, ...args: any[]) => TResult');
console.info('Moves a function context to a first argument. This is conversion from context based function to a function.');

function stringify() {
  return '' + this;
}

var s = rsh(stringify);
console.log(s(12345));

Rights and Agreements

License Apache-2.0

Copyright 2019 Oleksandr Lopatnov

Project Statistics

Sourcerank 3
Repository Size 268 KB
Stars 0
Forks 0
Watchers 1
Open issues 0
Dependencies 456
Contributors 1
Tags 4
Created
Last updated
Last pushed

Top Contributors See all

Oleksandr

Packages Referencing this Repo

@iyuo/context
The TypeScript ecosystem for an object. It allows to add processing plugins to it.
Latest release 1.3.3 - Updated
@iyuo/namespace
The TypeScript plugin Namespace for @iyuo/context ecosystem.
Latest release 1.3.2 - Updated

Recent Tags See all

1.3.3 December 15, 2019
1.3.2 December 02, 2019
1.3.1 December 02, 2019
1.3.0 December 02, 2019

Something wrong with this page? Make a suggestion

Last synced: 2019-12-15 18:33:43 UTC

Login to resync this repository