From 461f85d64421eff33855d8bca64b42163c61dd0b Mon Sep 17 00:00:00 2001 From: cloudratha Date: Thu, 27 Jun 2019 22:06:42 +0100 Subject: [PATCH 1/2] Fix `SliceString` with range [stop, start]. --- src/slice.js | 2 +- test/test-slice-string.js | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/slice.js b/src/slice.js index a777c51..0b8b772 100644 --- a/src/slice.js +++ b/src/slice.js @@ -90,7 +90,7 @@ class Slice { if (array.slice && (this.step == null || this.step === 1)) { const start = this.start == null ? undefined : this.start; const stop = this.stop == null ? undefined : this.stop; - extracted = array.slice(start, stop); + extracted = array.slice(start, stop).split(''); } else { extracted = this.indices(array) .map(index => array[index]); diff --git a/test/test-slice-string.js b/test/test-slice-string.js index 0435f2a..7c36d77 100644 --- a/test/test-slice-string.js +++ b/test/test-slice-string.js @@ -65,10 +65,17 @@ describe('SliceString', () => { assert(output == expectedOutput); }); - it('should extract the odd elements from an array', () => { + it('should extract the odd elements from a string', () => { const input = new SliceString('hello'); const expectedOutput = 'el'; const output = input[[1,,2]]; assert(output == expectedOutput); }); + + it('should extract the range from a string', () => { + const input = new SliceString('hello'); + const expectedOutput = 'ell'; + const output = input[[1,4]]; + assert(output == expectedOutput); + }); }); From e850c3e57f75c8e18292a0ff3e9f490cef035d19 Mon Sep 17 00:00:00 2001 From: cloudratha Date: Fri, 28 Jun 2019 00:15:39 +0100 Subject: [PATCH 2/2] Ensure extracted value is an array. --- src/slice.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/slice.js b/src/slice.js index 0b8b772..9470b3b 100644 --- a/src/slice.js +++ b/src/slice.js @@ -90,7 +90,8 @@ class Slice { if (array.slice && (this.step == null || this.step === 1)) { const start = this.start == null ? undefined : this.start; const stop = this.stop == null ? undefined : this.stop; - extracted = array.slice(start, stop).split(''); + extracted = array.slice(start, stop); + extracted = !Array.isArray(extracted) ? extracted.split('') : extracted; } else { extracted = this.indices(array) .map(index => array[index]);