-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Operators): implement template tag based operators
- Loading branch information
1 parent
321ae82
commit 77a6ae3
Showing
9 changed files
with
110 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,15 @@ | ||
import parseScheme from 'cerebral-scheme-parser' | ||
import populateInputAndStateSchemes from './helpers/populateInputAndStateSchemes' | ||
|
||
export default function (passedPath, filterFunc) { | ||
const pathScheme = parseScheme(passedPath) | ||
|
||
export default function (template, filterFunc) { | ||
const filterValue = typeof filterFunc === 'function' | ||
? filterFunc | ||
: (value) => value === filterFunc | ||
|
||
if (pathScheme.target !== 'state' && pathScheme.target !== 'input') { | ||
throw new Error('Cerebral operator FILTER - The path: "' + passedPath + '" does not target "state" or "input"') | ||
} | ||
|
||
// define the action | ||
const filter = function ({input, state, path}) { | ||
const pathValue = pathScheme.getValue(populateInputAndStateSchemes(input, state)) | ||
let value | ||
|
||
if (pathScheme.target === 'input') { | ||
value = input[pathValue] | ||
} else if (pathScheme.target === 'state') { | ||
value = state.get(pathValue) | ||
} | ||
function filter (context) { | ||
const value = template(context).toValue() | ||
|
||
return filterValue(value) ? path.accepted() : path.discarded() | ||
return filterValue(value) ? context.path.accepted() : context.path.discarded() | ||
} | ||
|
||
filter.displayName = 'operator FILTER' | ||
filter.displayName = 'operator.filter' | ||
|
||
return filter | ||
} |
11 changes: 0 additions & 11 deletions
11
packages/cerebral/src/operators/helpers/populateInputAndStateSchemes.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export default function populatePath (context, strings, values) { | ||
return strings.reduce((currentPath, string) => { | ||
if (!string) { | ||
return currentPath + values.shift()(context).toValue() | ||
} | ||
|
||
return currentPath + string | ||
}, '') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import populatePath from './helpers/populatePath' | ||
|
||
export default function input (strings, ...values) { | ||
return (context) => { | ||
const target = 'input' | ||
const path = populatePath(context, strings, values) | ||
|
||
return { | ||
target, | ||
path, | ||
toValue () { | ||
return path.split('.').reduce((currentValue, key) => { | ||
return currentValue[key] | ||
}, context.input) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,20 @@ | ||
import parseScheme from 'cerebral-scheme-parser' | ||
import populateInputAndStateSchemes from './helpers/populateInputAndStateSchemes' | ||
|
||
export default function (path, value) { | ||
const pathScheme = parseScheme(path) | ||
const valueScheme = typeof value === 'string' ? parseScheme(value) : value | ||
|
||
if (pathScheme.target !== 'state') { | ||
throw new Error('Cerebral operator SET - The path: "' + path + '" does not target "state"') | ||
export default function (fromTemplate, valueTemplate) { | ||
if (typeof fromTemplate !== 'function') { | ||
throw new Error('Cerebral operator.set: You have to use a state template tag as first argument') | ||
} | ||
|
||
if ( | ||
valueScheme && | ||
valueScheme.target && | ||
valueScheme.target !== 'input' && | ||
valueScheme.target !== 'state' | ||
) { | ||
throw new Error('Cerebral operator SET - The value: "' + path + '" does not target "input" or "state"') | ||
} | ||
const set = function set ({input, state}) { | ||
const pathSchemeValue = pathScheme.getValue(populateInputAndStateSchemes(input, state)) | ||
let valueSchemeValue = value | ||
function set (context) { | ||
const from = fromTemplate(context) | ||
const value = typeof valueTemplate === 'function' ? valueTemplate(context).toValue() : valueTemplate | ||
|
||
if (valueScheme && valueScheme.target && valueScheme.target === 'input') { | ||
valueSchemeValue = input[valueScheme.getValue(populateInputAndStateSchemes(input, state))] | ||
} else if (valueScheme && valueScheme.target && valueScheme.target === 'state') { | ||
valueSchemeValue = state.get(valueScheme.getValue(populateInputAndStateSchemes(input, state))) | ||
if (from.target !== 'state') { | ||
throw new Error('Cerebral operator.set: You have to use a state template tag as first argument') | ||
} | ||
|
||
state.set(pathSchemeValue, valueSchemeValue) | ||
context.state.set(from.path, value) | ||
} | ||
|
||
set.displayName = 'operator SET' | ||
set.displayName = 'operator.set' | ||
|
||
return set | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import populatePath from './helpers/populatePath' | ||
|
||
export default function state (strings, ...values) { | ||
return (context) => { | ||
const target = 'state' | ||
const path = populatePath(context, strings, values) | ||
|
||
return { | ||
target, | ||
path, | ||
toValue () { | ||
return context.state.get(path) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,15 @@ | ||
import parseScheme from 'cerebral-scheme-parser' | ||
import populateInputAndStateSchemes from './helpers/populateInputAndStateSchemes' | ||
export default function (template) { | ||
function toggle ({input, state}) { | ||
const {target, path} = template() | ||
|
||
export default function (path, onValue = true, offValue = false) { | ||
const pathScheme = parseScheme(path) | ||
if (target !== 'state') { | ||
throw new Error('Cerebral operator.toggle: You have to use a state template tag as first argument') | ||
} | ||
|
||
if (pathScheme.target !== 'state') { | ||
throw new Error('Cerebral operator TOGGLE - The path: "' + path + '" does not target "state"') | ||
state.set(path, !state.get(path)) | ||
} | ||
|
||
const toggle = function toggleRead ({input, state}) { | ||
const pathValue = pathScheme.getValue(populateInputAndStateSchemes(input, state)) | ||
const currentValue = state.get(pathValue) | ||
|
||
state.set(pathValue, currentValue === onValue ? offValue : onValue) | ||
} | ||
|
||
toggle.displayName = 'operator TOGGLE' | ||
toggle.displayName = 'operator.toggle' | ||
|
||
return toggle | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters