Skip to content

Commit

Permalink
feat(Operators): add string template tag
Browse files Browse the repository at this point in the history
  • Loading branch information
christianalfoni committed Nov 9, 2016
1 parent b039f2b commit 7d18eaa
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/cerebral/src/operators/index.js
Expand Up @@ -5,6 +5,7 @@ export {default as wait} from './wait'

export {default as state} from './state'
export {default as input} from './input'
export {default as string} from './string'

export {default as concat} from './concat'
export {default as merge} from './merge'
Expand Down
12 changes: 5 additions & 7 deletions packages/cerebral/src/operators/input.test.js
@@ -1,21 +1,19 @@
/* eslint-env mocha */
import Controller from '../Controller'
import assert from 'assert'
import {input, set, state} from './'
import {input, string} from './'

describe('operator.input', () => {
it('should read value from input', () => {
const controller = new Controller({
state: {
foo: 'bar'
},
signals: {
test: [
set(state`foo`, input`foo`)
(context) => {
assert.deepEqual(string`foo.${input`foo`}`(context).toValue(), 'foo.bar')
}
]
}
})
controller.getSignal('test')({foo: 'baz'})
assert.deepEqual(controller.getState(), {foo: 'baz'})
controller.getSignal('test')({foo: 'bar'})
})
})
6 changes: 3 additions & 3 deletions packages/cerebral/src/operators/state.test.js
@@ -1,7 +1,7 @@
/* eslint-env mocha */
import Controller from '../Controller'
import assert from 'assert'
import {input, state} from './'
import {state, string} from './'

describe('operator.state', () => {
it('should build path', () => {
Expand All @@ -11,8 +11,8 @@ describe('operator.state', () => {
},
signals: {
test: [
() => {
assert.deepEqual(state`foo.${input`ref`}`({input: {ref: 'bar'}}).path, 'foo.bar')
(context) => {
assert.deepEqual(string`foo.${state`foo`}`(context).toValue(), 'foo.bar')
}
]
}
Expand Down
16 changes: 16 additions & 0 deletions packages/cerebral/src/operators/string.js
@@ -0,0 +1,16 @@
import populatePath from './helpers/populatePath'

export default function string (strings, ...values) {
return (context) => {
const target = 'string'
const path = populatePath(context, strings, values)

return {
target,
path,
toValue () {
return path
}
}
}
}
19 changes: 19 additions & 0 deletions packages/cerebral/src/operators/string.test.js
@@ -0,0 +1,19 @@
/* eslint-env mocha */
import Controller from '../Controller'
import assert from 'assert'
import {input, string} from './'

describe('operator.string', () => {
it('should build path', () => {
const controller = new Controller({
signals: {
test: [
(context) => {
assert.deepEqual(string`foo.${input`ref`}`(context).toValue(), 'foo.bar')
}
]
}
})
controller.getSignal('test')({ref: 'bar'})
})
})

0 comments on commit 7d18eaa

Please sign in to comment.