karka
Lead Maintainer: Matt Edelman
A simple rule parsing module. Given a rule in a specified format, the context (in which the rule matching is invoked), this module will resolve a key to a value if the context satisifes the rules.
A simple usage:
var karka = require('karka'),
config = {
'foo' : [
{
'is': 'bar',
'when': {
'hakunamatata': 'It-Means-No-Worries'
}
}
],
'ying': [
{
'is': 'yang',
'when': {
'secret': 'There-Is-None'
}
}
]
},
context = {
'hakunamatata': 'It-Means-No-Worries',
'secret': 'There-Is-None'
},
spcl = karka.create(config),
mappedName = spcl.resolve('foo', context),
maps = spcl.resolveAll(context);
As you can see a single key like 'foo' can be mapped to an array of possible matches, each with their own rule set.
The first spec that has all rules satisfied the context will be picked. Simply put it matches with a swtich case mentality. SO it is important to remember to place the one with highest specifity on the top of the array.
Different ways to specify the config resolution:
The above example assumes that the rule values are directly available in the context object.
But fear not there are multiple ways the rules can be resolved.
- If you would like to specify a module/file that custom implements the rule resolution (that can be resolved using require):
var config = {
'foo' : [
{
'is': 'bar',
'when': {
'hakunamatata': 'It-Means-No-Worries'
},
'use': 'require:./I/am/a/file'
}
]
};
var config = {
'foo' : [
{
'is': 'bar',
'when': {
'hakunamatata': 'It-Means-No-Worries'
},
'use': 'require:I-am-a-module'
}
]
};
- If you would like to specify a factory method in a module/file that does the rule resolution:
var config = {
'foo' : [
{
'is': 'bar',
'when': {
'hakunamatata': 'It-Means-No-Worries'
},
'use':'exec:./I/am/a/file#ruleEvaluator'
}
]
};
Different ways to specify the context in config:
- To specify the context in a nested object under the context object:
var config = {
'ying' : [
{
'is': 'yang',
'when': {
'state.of.mind.is': 'peaceful'
}
}
]
},
context = {
'state': {
'of': {
'mind': {
'is': 'peaceful'
}
}
}
}
- To specify multiple rules to be satisfied
var config = {
'ying' : [
{
'is': 'yang',
'when': {
'state.of.mind.is': 'peaceful',
'state.of.body.is': 'active',
'mood.is': 'happy'
}
}
]
};
- To specify multiple values for a rule in the config and want at least one value matched in your context
var config = {
'ying' : [
{
'is': 'yang',
'when': {
'state.of.mind': 'peaceful',
'state.of.body': 'active'
'mood': ['happy', 'elated', 'ecstatic', 'jubilant']
}
}
]
},
context = {
'state': {
'of': {
'mind': 'peaceful'
}
},
'state': {
'of': {
'body': 'active'
}
},
'mood': 'jubilant'
};
- To have multiple values for a rule in the config and you want to specify a complex and/or rule to match against your context
var config = {
'ying' : [
{
'is': 'yang',
'when': {
'state.of.mind': 'peaceful',
'state.of.body': 'active',
'mood': [['happy', 'calm'], 'joyous']
}
},
{
'is': 'bong',
'when' : {
'state.of.mind': 'peaceful',
'state.of.body': 'active'
'mood': [['jubilant', 'outrageous'],['ecstatic', 'crazy']]
}
}
]
},
context = {
'state': {
'of': {
'mind': 'peaceful'
}
},
'state': {
'of': {
'body': 'active'
}
},
'mood': ['jubilant', 'outrageous']
};