Skip to content

Commit

Permalink
fix: after, before and first helpers can process strings now (#15)
Browse files Browse the repository at this point in the history
* feat: switch to github packages

* fix: package.json changes for github packages

* fix: package.json name under @oneflow/

* fix: after, before and first helpers fixes

* fix: pr feedback

* fix: after, before and first helpers with checking slice function

* fix: after, before and first helpers with checking if slice is function
  • Loading branch information
Sergio-svb authored Sep 15, 2021
1 parent ba220e3 commit 7d19459
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 21 deletions.
10 changes: 7 additions & 3 deletions lib/array.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const _ = require('lodash');
const utils = require('./utils');
const mathHelpers = require('./math');

Expand All @@ -22,7 +23,8 @@ const helpers = module.exports;
*/

helpers.after = function(array, n) {
if (!Array.isArray(array)) return '';
if (utils.isUndefined(array)) return '';
if (!_.isFunction(array.slice)) return '';
return array.slice(n);
};

Expand Down Expand Up @@ -56,7 +58,8 @@ helpers.arrayify = utils.arrayify;
*/

helpers.before = function(array, n) {
if (!Array.isArray(array)) return '';
if (utils.isUndefined(array)) return '';
if (!_.isFunction(array.slice)) return '';
return array.slice(0, n);
};

Expand Down Expand Up @@ -140,7 +143,8 @@ helpers.filter = function(array, value, options) {
*/

helpers.first = function(array, n) {
if (!Array.isArray(array)) return '';
if (utils.isUndefined(array)) return '';
if (!_.isFunction(array.slice)) return '';
if (!utils.isNumber(n)) {
return array[0];
}
Expand Down
5 changes: 5 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const handlebars = require('handlebars');
const _ = require('lodash');

const utils = {};

Expand Down Expand Up @@ -33,6 +34,10 @@ utils.falsey = value => {
return false;
};

utils.isOptions = val => _.isObject(val) && _.isObject(val.hash);

utils.isUndefined = val => val == null || (utils.isOptions(val) && val.hash != null);

utils.get = (value, path, defaultValue) => {
return String(path).split('.').reduce((acc, v) => {
try {
Expand Down
50 changes: 34 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@
"docs": "jsdoc -c jsdoc.js",
"release": "semantic-release"
},
"dependencies": {},
"dependencies": {
"lodash": "4.17.21"
},
"peerDependencies": {
"handlebars": "4.x",
"moment": "2.x"
Expand Down
17 changes: 16 additions & 1 deletion test/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ helpers.string({ handlebars: hbs });
helpers.array({ handlebars: hbs });
helpers.math({ handlebars: hbs });

const context = { array: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], duplicate: [ 'a', 'b', 'b', 'c', 'd', 'b', 'f', 'a', 'g'] };
const context = { str: 'abcdefgh', array: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], duplicate: [ 'a', 'b', 'b', 'c', 'd', 'b', 'f', 'a', 'g'] };

describe('array', function() {
describe('after', function() {
Expand All @@ -19,6 +19,11 @@ describe('array', function() {
assert.equal(fn(context), 'f,g,h');
});

it('should return all of the items in a string after the given index', function() {
const fn = hbs.compile('{{after str 5}}');
assert.equal(fn(context), 'fgh');
});

it('should return all of the items in an array after the specified count', function() {
const fn = hbs.compile('{{after array 5}}');
assert.equal(fn(context), 'f,g,h');
Expand All @@ -41,6 +46,11 @@ describe('array', function() {
assert.equal(fn(context), 'a,b,c');
});

it('should return all of the items in a string before the given index', function() {
const fn = hbs.compile('{{before str 3}}');
assert.equal(fn(context), 'abc');
});

it('should return all of the items in an array before the specified count', function() {
const fn = hbs.compile('{{before array 3}}');
assert.equal(fn(context), 'a,b,c');
Expand Down Expand Up @@ -85,6 +95,11 @@ describe('array', function() {
const fn = hbs.compile('{{first foo 2}}');
assert.equal(fn({foo: ['a', 'b', 'c']}), 'a,b');
});

it('should return an string with the first two items in a string', function() {
const fn = hbs.compile('{{first foo 2}}');
assert.equal(fn({foo: 'abcde' }), 'ab');
});
});

describe('filter', function() {
Expand Down

0 comments on commit 7d19459

Please sign in to comment.