Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cyclejs-group

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cyclejs-group - npm Package Compare versions

Comparing version 0.4.0 to 0.5.0

8

dist/create-group.js

@@ -32,2 +32,8 @@ 'use strict';

function _removeUnderscores(string) {
var result = /^_*(.*[^_])_*$/.exec(string);
return result ? result[1] : string;
}
function _makeInjectFn(streamWithDependencies) {

@@ -135,3 +141,3 @@ return function inject() {

return {
dependencies: (0, _getParameterNames2['default'])(streamFn),
dependencies: (0, _getParameterNames2['default'])(streamFn).map(_removeUnderscores),
stream: (0, _cyclejsStream2['default'])(streamFn)

@@ -138,0 +144,0 @@ };

2

package.json
{
"name": "cyclejs-group",
"version": "0.4.0",
"version": "0.5.0",
"author": "Eryk Napierala",

@@ -5,0 +5,0 @@ "description": "Utility for CycleJS framework for reducing boilerplate when creating groups of streams",

# cyclejs-group
Utility for the [Cycle.js framework](https://github.com/staltz/cycle) for reducing boilerplate when creating groups of streams.
Utility for the [Cycle.js framework](https://github.com/staltz/cycle) for reducing
boilerplate when creating many dependent streams.
## Why may I need it?
Usually in a Cycle.js application or component you want to create more than one stream, especially for intent and model parts. It's 100% possible to do it with pure JS, but it requires a lot of boilerplate code. This utility covers common case and makes creating complicated programs easier.
Usually in a Cycle.js application or component you want to create more than one stream,
especially for intent and model parts. It's 100% possible to do it with pure JS,
but it requires a lot of boilerplate code when different streams depend on each other.
This utility covers common case and makes creating complicated programs easier.
## Example usage
Let's say, you want to create simple application, that allows you to add two numbers. With pure Cycle.js you can do it like this:
Let's say, you want to create simple application, that allows you to add two numbers.
With pure Cycle.js and [cyclejs-stream](https://github.com/erykpiast/cyclejs-stream) you can do it like this:
```javascript
import { createStream, render, h, Rx } from 'cyclejs';
import { applyToDOM, h, Rx } from 'cyclejs';
import createStream from 'cyclejs-stream';
let a$ = createStream((changeA$) => changeA$
let a$ = createStream((changeA$) =>
changeA$
.map(value => parseInt(value, 10))

@@ -21,3 +28,4 @@ .filter(value => !isNaN(value))

let b$ = createStream((changeB$) => changeB$
let b$ = createStream((changeB$) =>
changeB$
.map(value => parseInt(value, 10))

@@ -29,72 +37,87 @@ .filter(value => !isNaN(value))

let c = createStream((a$, b$) => Rx.Observable.combineLatest(
let c$ = createStream((a$, b$) =>
Rx.Observable.combineLatest(
a$, b$,
(a, b) => a + b
(a, b) =>
a + b
));
let vtree$ = createStream((a$, b$, c$) =>
Rx.Observable.combineLatest(a$, b$, c$, (a, b, c) =>
h('form',
h('fieldset', [
h('legend', 'Add two numbers'),
h('input#a', {
type: 'number',
value: a,
}),
h('input#b', {
type: 'number',
value: b,
}),
h('output', {
value: c,
htmlFor: 'a,b'
})
])
)
Rx.Observable.combineLatest(
a$, b$, c$,
(a, b, c) =>
h('form',
h('fieldset', [
h('legend', 'Add two numbers'),
h('input#a', {
type: 'number',
value: a,
}),
h('input#b', {
type: 'number',
value: b,
}),
h('output', {
value: c,
htmlFor: 'a,b'
})
])
)
)
);
let changeA$ = createStream((interaction$) =>
interaction$
.choose('#a', 'input')
.map(({ target }) => target.value)
let changeA$ = createStream((interactions) =>
interactions
.get('#a', 'input')
.map(({ target }) => target.value)
);
let changeA$ = createStream((interaction$) =>
interaction$
.choose('#b', 'input')
.map(({ target }) => target.value)
let changeA$ = createStream((interactions) =>
interactions
.get('#b', 'input')
.map(({ target }) => target.value)
);
let interaction$ = createStream((vtree$) => render(vtree$, document.body).interaction$);
applyToDOM('.js-container', (interactions) => {
a$.inject(changeA$);
b$.inject(changeB$);
c$.inject(a$, b$);
vtree$.inject(a$, b$, c$);
interactions.inject(vtree$);
changeA$.inject(interactions);
changeB$.inject(interactions);
a$.inject(changeA$);
b$.inject(changeB$);
c$.inject(a$, b$);
vtree$.inject(a$, b$, c$);
interaction$.inject(vtree$);
changeA$.inject(interaction$);
changeB$.inject(interaction$);
return vtree$;
});
```
Seems easy for now, but when streams number grows, amount of boilerplate will grow proportionally. With `createGroup` you can achieve the same effect in more compact way and create batch of streams from plain functions. Thanks to `inject` method of the group, you can make streams form one group available for streams from another one. Connection is detected based on names of function parameters and keys of the group object.
Seems easy for now, but when streams number grows, amount of boilerplate will grow proportionally.
With `createGroup` you can achieve the same effect in more compact way and create batch of streams from plain functions.
Thanks to `inject` method of the group, you can make streams form one group available for streams from another one.
Connection is detected based on names of function parameters and keys of the group object. The whole concept of grouping
streams can help with separation of concerns and increase readability of your code.
```javascript
import { createStream, render, h, Rx } from 'cyclejs';
import { applyToDOM, h, Rx } from 'cyclejs';
import createGroup from 'cyclejs-group';
let model = createGroup({
a$: (changeA$) => changeA$
.map(value => parseInt(value, 10))
.filter(value => !isNaN(value))
.startWith(1)
.distinctUntilChanged()
b$: (changeB$) => changeB$
.map(value => parseInt(value, 10))
.filter(value => !isNaN(value))
.startWith(1)
.distinctUntilChanged(),
c$: (a$, b$) => Rx.Observable.combineLatest(
a$, b$,
(a, b) => a + b
a$: (changeA$) =>
changeA$
.map(value => parseInt(value, 10))
.filter(value => !isNaN(value))
.startWith(1)
.distinctUntilChanged()
b$: (changeB$) =>
changeB$
.map(value => parseInt(value, 10))
.filter(value => !isNaN(value))
.startWith(1)
.distinctUntilChanged(),
c$: (a$, b$) =>
Rx.Observable.combineLatest(
a$, b$,
(a, b) =>
a + b
)

@@ -104,31 +127,34 @@ });

let intent = createGroup({
changeA$: (interaction$) => interaction$
.choose('#a', 'input')
.map(({ target }) => target.value),
changeB$: (interaction$) => interaction$
.choose('#b', 'input')
.map(({ target }) => target.value)
changeA$: (interactions) =>
interactions
.get('#a', 'input')
.map(({ target }) => target.value),
changeB$: (interactions) =>
interactions
.get('#b', 'input')
.map(({ target }) => target.value)
});
let view = createGroup({
vtree$: (a$, b$, c$) => Rx.Observable.combineLatest(
a$, b$, c$,
(a, b, c) =>
vtree$: (a$, b$, c$) =>
Rx.Observable.combineLatest(
a$, b$, c$,
(a, b, c) =>
h('form',
h('fieldset', [
h('legend', 'Add two numbers'),
h('input#a', {
type: 'number',
value: a,
}),
h('input#b', {
type: 'number',
value: b,
}),
h('output', {
value: c,
htmlFor: 'a,b'
})
]
)
h('fieldset', [
h('legend', 'Add two numbers'),
h('input#a', {
type: 'number',
value: a,
}),
h('input#b', {
type: 'number',
value: b,
}),
h('output', {
value: c,
htmlFor: 'a,b'
})
]
)
)

@@ -138,10 +164,9 @@ )

let user = createGroup({
interaction$: (vtree$) => render(vtree$, document.body).interaction$
applyToDOM('.js-container', (interactions) => {
model.inject(intent, model); // self-injection to make a$ and b$ available for c$
view.inject(model);
intent.inject({ interactions });
return view.vtree$;
});
model.inject(intent, model); // self-injection to make a$ and b$ available for c$
view.inject(model);
user.inject(view);
intent.inject(user);
```

@@ -11,2 +11,9 @@ 'use strict';

function _removeUnderscores(string) {
let result = /^_*(.*[^_])_*$/.exec(string);
return result ? result[1] : string;
}
function _makeInjectFn(streamWithDependencies) {

@@ -68,3 +75,4 @@ return function inject(...inputObjects) {

let streamsWithDeps = mapValues(streamsDefs, (streamFn) => ({
dependencies: getParametersNames(streamFn),
dependencies: getParametersNames(streamFn)
.map(_removeUnderscores),
stream: createStream(streamFn)

@@ -71,0 +79,0 @@ }));

@@ -75,2 +75,36 @@ /* global suite, test */

test('should ignore sorrounding underscores in function parameter name when injecting dependencies', (done) => {
let group = createGroup({
foo$: (asd$_) => asd$_.map(x => 3 * x),
bar$: (_lol$) => _lol$.map(x => 5 * x)
});
let inputs = {
asd$: Rx.Observable.just(2),
lol$: Rx.Observable.just(4)
};
Rx.Observable.combineLatest(group.foo$, group.bar$, (foo, bar) => [foo, bar])
.subscribe(([foo, bar]) => {
assert.strictEqual(foo, 6);
assert.strictEqual(bar, 20);
done();
});
group.inject(inputs);
});
test('should not ignore underscores inside function parameter name when injecting dependencies', () => {
let group = createGroup({
foo$: (a_s_d$) => a_s_d$.map(x => 3 * x),
bar$: (_lol$_) => _lol$_.map(x => 5 * x)
});
let inputs = {
asd$: Rx.Observable.just(2),
lol$: Rx.Observable.just(4)
};
assert.throws(() => {
group.inject(inputs)
}, /dependency "a_s_d\$" is not available/i);
});
test('should be injectable with another Group', (done) => {

@@ -77,0 +111,0 @@ let group1 = createGroup({

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc