feat: Add config to framework, examples

This commit is contained in:
1ambda 2017-02-07 13:18:15 +09:00
parent 70ebe29fee
commit 4d3c2c7d2f
3 changed files with 36 additions and 8 deletions

View file

@ -26,7 +26,28 @@ export default class EchoSpell extends SpellBase {
super("%echo");
}
interpret(paragraphText) {
return new SpellResult(paragraphText);
/**
* Consumes text and return `SpellResult`.
*
* @param paragraphText {string} which doesn't include magic
* @param config {Object}
* @return {SpellResult}
*/
interpret(paragraphText, config) {
let repeat = 1;
try {
repeat = parseFloat(config.repeat);
} catch (error) {
/** ignore, use default value */
}
let repeated = "";
for (let i = 0; i < repeat; i++) {
repeated += `${paragraphText}\n`;
}
return new SpellResult(repeated);
}
}

View file

@ -28,11 +28,18 @@ export default class TranslatorSpell extends SpellBase {
super("%translator");
}
interpret(paragraphText) {
/**
* Consumes text and return `SpellResult`.
*
* @param paragraphText {string} which doesn't include magic
* @param config {Object}
* @return {SpellResult}
*/
interpret(paragraphText, config) {
const parsed = this.parseConfig(paragraphText);
const auth = config['access-token'];
const source = parsed.source;
const target = parsed.target;
const auth = parsed.auth;
const text = parsed.text;
/**
@ -49,7 +56,7 @@ export default class TranslatorSpell extends SpellBase {
}
parseConfig(text) {
const pattern = /^\s*(\S+)-(\S+)\s*(\S+)([\S\s]*)/g;
const pattern = /^\s*(\S+)-(\S+)\s*([\S\s]*)/g;
const match = pattern.exec(text);
if (!match) {
@ -59,8 +66,7 @@ export default class TranslatorSpell extends SpellBase {
return {
source: match[1],
target: match[2],
auth: match[3],
text: match[4],
text: match[3],
}
}

View file

@ -31,9 +31,10 @@ export class SpellBase {
* Consumes text and return `SpellResult`.
*
* @param paragraphText {string} which doesn't include magic
* @param config {Object}
* @return {SpellResult}
*/
interpret(paragraphText) {
interpret(paragraphText, config) {
throw new Error('SpellBase.interpret() should be overrided');
}