context demo

node v10.24.1
version: 1.3.2
endpointsharetweet
var iyuo = require("@iyuo/context"); var Context = iyuo.Context; var pluginize = iyuo.pluginize; var lsh = iyuo.lsh; var rsh = iyuo.rsh; console.info('A sample of a Context<TContext> class, the object Context<string>') console.log(new Context("A sample context"));
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());
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"}
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);
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)); space.make(strategy(3,4), strategy1, strategy2, strategy3); // the same
console.info('tasks(...plugins: IPlugin<TContext, void>[]): Context<TContext>'); console.info('Execute plugins functions the tasks for a context') console.info('IPlugin = (this: TContext, ...use: any[]): void;'); 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())
console.info('task(plugin: IPlugin<TContext, void>, ...use: any[]): Context<TContext>'); console.info('Execute plugin with params'); console.info('IPlugin = (this: TContext, ...use: any[]): void;'); 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())
console.info('make<TResult>(plugin: IPlugin<TContext, TResult>, ...use: any[]): TResult'); console.info('Execute plugin function and return the result of plugin processing'); console.info('IPlugin = (this: TContext, ...use: any[]): TResult;'); 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
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'); console.info('IPlugin = (this: TContext, ...use: any[]): TMappedContext;'); function increment() { return this + 1; } var num = new Context(10); console.log(num .map(increment) .map(increment) .map(increment) .map(increment) .context());
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);
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());
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));
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));
Loading…

no comments

    sign in to comment