From 0a9e14c460d87e70da56c53f92bc08daa39c0ca5 Mon Sep 17 00:00:00 2001 From: Leifer Mendez Date: Wed, 2 Nov 2022 21:10:18 +0100 Subject: [PATCH] toJson --- packages/io/methods/addAnswer.js | 15 ++++++++++++--- packages/io/methods/addKeyword.js | 12 +++++++++--- packages/io/methods/index.js | 3 ++- packages/io/methods/toJson.js | 6 ++++++ packages/io/tests/methods.test.js | 16 ++++++++++++++++ 5 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 packages/io/methods/toJson.js diff --git a/packages/io/methods/addAnswer.js b/packages/io/methods/addAnswer.js index 99bc117..143c6fd 100644 --- a/packages/io/methods/addAnswer.js +++ b/packages/io/methods/addAnswer.js @@ -1,5 +1,5 @@ const { generateRef } = require('../utils') - +const { toJson } = require('./toJson') /** * * @param answer string @@ -14,7 +14,15 @@ const addAnswer = (inCtx) => (answer, options) => { const lastCtx = inCtx.hasOwnProperty('ctx') ? inCtx.ctx : inCtx const ctxAnswer = () => { - const ref = generateRef() + const ref = `ans_${generateRef()}` + + const json = [].concat(inCtx.json).concat([ + { + ref, + keyword: lastCtx.ref, + answer, + }, + ]) /** * Se guarda en db */ @@ -24,7 +32,7 @@ const addAnswer = (inCtx) => (answer, options) => { keyword: {}, } - return { ...lastCtx, ref, answer, options } + return { ...lastCtx, ref, answer, json, options } } const ctx = ctxAnswer() @@ -33,6 +41,7 @@ const addAnswer = (inCtx) => (answer, options) => { ctx, ref: ctx.ref, addAnswer: addAnswer(ctx), + toJson: toJson(ctx), } } diff --git a/packages/io/methods/addKeyword.js b/packages/io/methods/addKeyword.js index 7fc88f1..2522538 100644 --- a/packages/io/methods/addKeyword.js +++ b/packages/io/methods/addKeyword.js @@ -10,7 +10,7 @@ const { addAnswer } = require('./addAnswer') * @param {*} message `string | string[]` * @param {*} options {sensitive:boolean} default */ -const addKeyword = (message, options) => { +const addKeyword = (keyword, options) => { /** * Esta funcion deberia parsear y validar las opciones * del keyword @@ -25,13 +25,19 @@ const addKeyword = (message, options) => { } const ctxAddKeyword = () => { - const ref = generateRef() + const ref = `key_${generateRef()}` const options = parseOptions() + const json = [ + { + ref, + keyword, + }, + ] /** * Se guarda en db */ - return { ref, keyword: message, options } + return { ref, keyword, options, json } } const ctx = ctxAddKeyword() diff --git a/packages/io/methods/index.js b/packages/io/methods/index.js index 278fbcc..8d53bf1 100644 --- a/packages/io/methods/index.js +++ b/packages/io/methods/index.js @@ -1,4 +1,5 @@ const { addAnswer } = require('./addAnswer') const { addKeyword } = require('./addKeyword') +const { toJson } = require('./toJson') -module.exports = { addAnswer, addKeyword } +module.exports = { addAnswer, addKeyword, toJson } diff --git a/packages/io/methods/toJson.js b/packages/io/methods/toJson.js new file mode 100644 index 0000000..121748e --- /dev/null +++ b/packages/io/methods/toJson.js @@ -0,0 +1,6 @@ +const toJson = (inCtx) => () => { + const lastCtx = inCtx.hasOwnProperty('ctx') ? inCtx.ctx : inCtx + return lastCtx.json +} + +module.exports = { toJson } diff --git a/packages/io/tests/methods.test.js b/packages/io/tests/methods.test.js index 51d7af9..5afb807 100644 --- a/packages/io/tests/methods.test.js +++ b/packages/io/tests/methods.test.js @@ -75,4 +75,20 @@ test('Debere probar error las addAnswer', () => { assert.is(MAIN_CTX.ctx.options.answer.buttons.length, 0) }) +test('Obtener toJson', () => { + const [ctxA, ctxB, ctxC] = addKeyword('hola') + .addAnswer('pera!') + .addAnswer('chao') + .toJson() + + assert.is(ctxA.keyword, 'hola') + assert.match(ctxA.ref, /^key_/) + + assert.is(ctxB.answer, 'pera!') + assert.match(ctxB.ref, /^ans_/) + + assert.is(ctxC.answer, 'chao') + assert.match(ctxC.ref, /^ans_/) +}) + test.run()