Skip to content

Commit

Permalink
fix: STRF-10494 last and first helpers works with strings (#254)
Browse files Browse the repository at this point in the history
* fix: STRF-10494 last and first helpers works with strings

* fix: STRF-10494 PR fixes
  • Loading branch information
rafa-avila-bc authored Mar 15, 2023
1 parent 1c8376e commit 51f341e
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 3 deletions.
7 changes: 4 additions & 3 deletions helpers/3p/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ helpers.first = function(array, n) {

if (utils.isString(array)) {
const chars = array.split('');
return arrayAlt(chars, n);
const result = arrayAlt(chars, n);
return Array.isArray(result) ? result.join('') : result;
}

return [];
Expand Down Expand Up @@ -308,8 +309,8 @@ helpers.last = function(array, n) {

function stringAlt(str, n) {
const chars = str.split('');
const arr = arrayAlt(chars, n);
return arr.join('');
const result = arrayAlt(chars, n);
return Array.isArray(result) ? result.join('') : result;
}

if (Array.isArray(array)) {
Expand Down
85 changes: 85 additions & 0 deletions spec/helpers/3p/first.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const Lab = require('lab'),
lab = exports.lab = Lab.script(),
describe = lab.experiment,
it = lab.it;
const {testRunner} = require("../../spec-helpers");

describe('first', () => {
describe('string', () => {

const context = {
smallString: 'abc',
empty: '',
myString: 'BigCommerce'
};

const runner = testRunner({context});

it('should extract the first char when no "n" is given.', done => {
runner([{
input: '{{first myString}}',
output: 'B'
}], done);
});

it('should return the whole string when "n" is bigger that string size', done => {
runner([{
input: '{{first smallString 5}}',
output: context.smallString
}], done);
});

it('should return the expected string', done => {
runner([{
input: '{{first myString 3}}',
output: 'Big'
}], done);
});

it('should return empty string when empty string is provided', done => {
runner([{
input: '{{first empty 3}}',
output: ''
}], done);
});
});

describe('array', () => {

const context = {
small: [1,2,3],
empty:[],
arrayYay: [1,2,3,4,5,6,7,8,9,0]
};

const runner = testRunner({context});

it('should extract the first elem when no "n" is given.', done => {
runner([{
input: '{{first arrayYay}}',
output: '1'
}], done);
});

it('should return the whole array when "n" is bigger that array size', done => {
runner([{
input: '{{first small 5}}',
output: context.small.join()
}], done);
});

it('should return the expected elems', done => {
runner([{
input: '{{first arrayYay 3}}',
output: [1,2,3].join()
}], done);
});

it('should return empty string when empty array is provided', done => {
runner([{
input: '{{first empty 3}}',
output: ''
}], done);
});
});
});
85 changes: 85 additions & 0 deletions spec/helpers/3p/last.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const Lab = require('lab'),
lab = exports.lab = Lab.script(),
describe = lab.experiment,
it = lab.it;
const {testRunner} = require("../../spec-helpers");

describe('last', () => {
describe('string', () => {

const context = {
smallString: 'abc',
empty: '',
myString: 'BigCommerce'
};

const runner = testRunner({context});

it('should extract the last char when no "n" is given.', done => {
runner([{
input: '{{last myString}}',
output: 'e'
}], done);
});

it('should return the whole string when "n" is bigger that string size', done => {
runner([{
input: '{{last smallString 5}}',
output: context.smallString
}], done);
});

it('should return the expected string', done => {
runner([{
input: '{{last myString 3}}',
output: 'rce'
}], done);
});

it('should return empty string when empty string is provided', done => {
runner([{
input: '{{last empty 3}}',
output: ''
}], done);
});
});

describe('array', () => {

const context = {
small: [1, 2, 3],
empty: [],
arrayYay: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
};

const runner = testRunner({context});

it('should extract the last elem when no "n" is given.', done => {
runner([{
input: '{{last arrayYay}}',
output: '0'
}], done);
});

it('should return the whole array when "n" is bigger that array size', done => {
runner([{
input: '{{last small 5}}',
output: context.small.join()
}], done);
});

it('should return the expected elems', done => {
runner([{
input: '{{last arrayYay 3}}',
output: [8, 9, 0].join()
}], done);
});

it('should return empty string when empty array is provided', done => {
runner([{
input: '{{last empty 3}}',
output: ''
}], done);
});
});
});

0 comments on commit 51f341e

Please sign in to comment.