add withLocation
This commit is contained in:
parent
ed50da52f7
commit
cf5ae93638
1 changed files with 15 additions and 0 deletions
15
strudel.mjs
15
strudel.mjs
|
|
@ -275,6 +275,14 @@ class Pattern {
|
||||||
return new Pattern(span => func(this.query(span)))
|
return new Pattern(span => func(this.query(span)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
withLocation(location) {
|
||||||
|
return this.fmap(value => {
|
||||||
|
value = typeof value === 'object' && !Array.isArray(value) ? value : { value };
|
||||||
|
const locations = (value.locations || []).concat([location]);
|
||||||
|
return {...value, locations }
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
withValue(func) {
|
withValue(func) {
|
||||||
// Returns a new pattern, with the function applied to the value of
|
// Returns a new pattern, with the function applied to the value of
|
||||||
// each event. It has the alias 'fmap'.
|
// each event. It has the alias 'fmap'.
|
||||||
|
|
@ -455,7 +463,14 @@ class Pattern {
|
||||||
_patternify(func) {
|
_patternify(func) {
|
||||||
const pat = this
|
const pat = this
|
||||||
const patterned = function (...args) {
|
const patterned = function (...args) {
|
||||||
|
// the problem here: args could a pattern that has been turned into an object to add location
|
||||||
|
// to avoid object checking for every pattern method, we can remove it here...
|
||||||
|
// in the future, patternified args should be marked as well + some better object handling
|
||||||
|
args = args.map((arg) =>
|
||||||
|
arg.constructor?.name === 'Pattern' ? arg.fmap((value) => value.value || value) : arg
|
||||||
|
);
|
||||||
const pat_arg = sequence(...args)
|
const pat_arg = sequence(...args)
|
||||||
|
// arg.locations has to go somewhere..
|
||||||
return pat_arg.fmap(arg => func.call(pat,arg)).outerJoin()
|
return pat_arg.fmap(arg => func.call(pat,arg)).outerJoin()
|
||||||
}
|
}
|
||||||
return patterned
|
return patterned
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue