From 9f1912e8c86d62b606c5784f966fe709584927ef Mon Sep 17 00:00:00 2001 From: Edgardo Date: Sun, 26 May 2019 21:16:02 -0500 Subject: [PATCH 1/5] Adds support for msg_id & thread_id --- lib/wit.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/wit.js b/lib/wit.js index 73a6c10..dac6674 100644 --- a/lib/wit.js +++ b/lib/wit.js @@ -42,10 +42,20 @@ function Wit(opts) { this._sessions = {}; this.message = (message, context, n, verbose, junk) => { + if (typeof message === 'object') { + // User can pass an object with options, this will override arguments + ({message, context, n, verbose, junk, msgId, threadId} = message) + } let qs = 'q=' + encodeURIComponent(message); if (context) { qs += '&context=' + encodeURIComponent(JSON.stringify(context)); } + if (msgId) { + qs += '&msg_id=' + encodeURIComponent(msgId); + } + if (threadId) { + qs += '&thread_id=' + encodeURIComponent(threadId); + } if (typeof n === 'number') { qs += '&n=' + encodeURIComponent(JSON.stringify(n)); } From 71978f4821276827ca0711a577c9a994cb2caa87 Mon Sep 17 00:00:00 2001 From: Edgardo Date: Tue, 4 Jun 2019 21:08:27 -0500 Subject: [PATCH 2/5] New implementation --- lib/wit.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/wit.js b/lib/wit.js index dac6674..4130a09 100644 --- a/lib/wit.js +++ b/lib/wit.js @@ -42,11 +42,18 @@ function Wit(opts) { this._sessions = {}; this.message = (message, context, n, verbose, junk) => { + let qs = '?'; if (typeof message === 'object') { - // User can pass an object with options, this will override arguments - ({message, context, n, verbose, junk, msgId, threadId} = message) + Object.keys(message).reduce( + (qs, field) => { + qs += field + '=' + message[field]; + return qs; + }, + qs + ); } - let qs = 'q=' + encodeURIComponent(message); + + qs += 'q=' + encodeURIComponent(message); if (context) { qs += '&context=' + encodeURIComponent(JSON.stringify(context)); } From c2cef209c5e7ce96a01511c2042fdc46c7d0ed40 Mon Sep 17 00:00:00 2001 From: Edgardo Date: Tue, 4 Jun 2019 21:11:10 -0500 Subject: [PATCH 3/5] Using map instead of reduce --- lib/wit.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/wit.js b/lib/wit.js index 4130a09..550b9d7 100644 --- a/lib/wit.js +++ b/lib/wit.js @@ -42,15 +42,11 @@ function Wit(opts) { this._sessions = {}; this.message = (message, context, n, verbose, junk) => { - let qs = '?'; + let qs = ''; if (typeof message === 'object') { - Object.keys(message).reduce( - (qs, field) => { - qs += field + '=' + message[field]; - return qs; - }, - qs - ); + Object.keys(message).map( + (field) => field + '=' + message[field] + ).join('&'); } qs += 'q=' + encodeURIComponent(message); From 2621088a223f3dd4c7aaed0581f701db54fb7963 Mon Sep 17 00:00:00 2001 From: Edgardo Date: Tue, 4 Jun 2019 21:12:05 -0500 Subject: [PATCH 4/5] Concatenation of qs variable --- lib/wit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/wit.js b/lib/wit.js index 550b9d7..1b40ec2 100644 --- a/lib/wit.js +++ b/lib/wit.js @@ -44,7 +44,7 @@ function Wit(opts) { this.message = (message, context, n, verbose, junk) => { let qs = ''; if (typeof message === 'object') { - Object.keys(message).map( + qs += Object.keys(message).map( (field) => field + '=' + message[field] ).join('&'); } From 14fcb50c0e06aa492a785cf52b5233a201f3785c Mon Sep 17 00:00:00 2001 From: Edgardo Date: Wed, 5 Jun 2019 10:06:23 -0500 Subject: [PATCH 5/5] Removing not defined values --- lib/wit.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/wit.js b/lib/wit.js index 1b40ec2..73350fd 100644 --- a/lib/wit.js +++ b/lib/wit.js @@ -48,17 +48,13 @@ function Wit(opts) { (field) => field + '=' + message[field] ).join('&'); } + else { + qs += 'q=' + encodeURIComponent(message); + } - qs += 'q=' + encodeURIComponent(message); if (context) { qs += '&context=' + encodeURIComponent(JSON.stringify(context)); } - if (msgId) { - qs += '&msg_id=' + encodeURIComponent(msgId); - } - if (threadId) { - qs += '&thread_id=' + encodeURIComponent(threadId); - } if (typeof n === 'number') { qs += '&n=' + encodeURIComponent(JSON.stringify(n)); }