Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes argument in Lorem to optional #434

Open
wants to merge 159 commits into
base: master
Choose a base branch
from
Open
Changes from 27 commits
Commits
Show all changes
159 commits
Select commit Hold shift + click to select a range
43fec8d
Starting to modify, removed lazy cache from array.
mike12345567 Jan 30, 2021
418a73b
Removing lazy-cache, removing the utils file that breaks rollup and r…
mike12345567 Jan 30, 2021
203803a
Updating package so that action runs to test and publish package on a…
mike12345567 Jan 31, 2021
e7c22bf
Removing use of create-frame as this also made use of lazy cache.
mike12345567 Feb 1, 2021
f12af59
Incrementing version.
mike12345567 Feb 1, 2021
a9d3dba
array examples
Feb 2, 2021
5b6e5a9
number done
Feb 2, 2021
378d554
maths
Feb 2, 2021
965bd67
url done
Feb 2, 2021
7fbe93a
regex done
Feb 2, 2021
fb6205d
string done
Feb 2, 2021
2899de9
comparisons
Feb 2, 2021
db7c4fe
fix comment
Feb 2, 2021
63267d2
0.11.2
Feb 2, 2021
29edbe0
Merge pull request #1 from Budibase/examples
shogunpurple Feb 2, 2021
782aa90
0.11.3
Feb 2, 2021
7b010d7
Updated most packages without breaking code or modifying tests. Updat…
May 16, 2021
2cf5b71
Bumped min node required to 10.12.0 due to package dependencies.
May 16, 2021
462944b
Added timeout for github actions.
May 16, 2021
f19ae7a
Added os matrix for github actions and badges in readme.md
May 17, 2021
da9acde
Only publish if not a PR.
May 17, 2021
8917e23
Only publish on release event
May 17, 2021
c66e909
Update github actions for github master renamed to main and for when …
May 17, 2021
1603aac
Updating length operator to have a little more intelligence allowing …
mike12345567 Jun 10, 2021
a899446
Merge pull request #3 from Budibase/fix/length-op
Jun 10, 2021
77a3b97
0.11.4
mike12345567 Jun 10, 2021
2d55052
Merge branch 'master' into master
acotty Jun 10, 2021
04d3986
Fix merge array length param name stuff up.
Jun 11, 2021
414ad12
Merge pull request #2 from acotty/master
Jun 11, 2021
7f7c6af
Removing the use of the logging utilities directly from codebase, the…
mike12345567 Jun 29, 2021
4a3f2c7
fix example code for avg helper
mslourens Sep 20, 2021
edac2a0
Merge pull request #4 from Budibase/fix_example_avg_helper
mike12345567 Sep 20, 2021
1d107cb
forgot to change the example doc
mslourens Sep 20, 2021
7258ea3
Bumping version.
mike12345567 Sep 21, 2021
8f98116
Removing use of falsey module, which will not build due to programmin…
mike12345567 Oct 7, 2021
3384005
bumping version.
mike12345567 Oct 7, 2021
b46b83c
Updated commands on installation
Sly777 Oct 16, 2021
3c05692
Some minor tweaks.
mike12345567 Jan 12, 2022
3725026
Updating comparisons to be inline with util usage.
mike12345567 Jan 12, 2022
7243aa2
Removing markdown and date helpers, which add nearly 2MB to the bundl…
mike12345567 Jan 31, 2022
c4dd7ae
v0.11.8
mike12345567 Jan 31, 2022
5ba93f5
Linting fix.
mike12345567 Jan 31, 2022
16b7dee
fix(README): fix incorrect syntax for ellipsis length argument
davidpfahler Oct 13, 2022
c3ffbdf
remove unused dependencies
connorskees Jan 11, 2023
5f939b0
Merge pull request #7 from connorskees/feat/unused-deps
mike12345567 May 9, 2023
8851589
Merge pull request #6 from davidpfahler/patch-1
mike12345567 May 9, 2023
9a15565
Merge pull request #5 from Sly777/patch-1
mike12345567 May 9, 2023
8c40ab8
Minor amends - removed duplicated helpers
ConorWebb96 Jun 21, 2023
5594282
Added line break to the end of math.js
ConorWebb96 Jun 21, 2023
0cb05c7
Merge pull request #8 from ConorWebb96/master
mike12345567 Jun 23, 2023
134183a
Removing mac os build checks.
mike12345567 Jun 23, 2023
e3e27c7
Merge pull request #9 from Budibase/conor-amends
mike12345567 Jun 23, 2023
7aa4282
Fixed example error in multiply helper. (#10)
ConorWebb96 Jun 23, 2023
8dc0503
Adding UUID functionality to helpers, so that it can easily be used o…
mike12345567 Dec 13, 2023
dc568b7
Updating example.
mike12345567 Dec 13, 2023
ae4d40e
v0.11.11
mike12345567 Dec 13, 2023
e5174af
Fixing test case.
mike12345567 Dec 13, 2023
2a8ba84
Updating tests to use latest Node versions.
mike12345567 Dec 13, 2023
c267bd5
Removing dangling comma (lint).
mike12345567 Dec 13, 2023
9c8ea54
Remove array-sort usage
adrinr Jan 9, 2024
d980f95
Remove kind-of usage
adrinr Jan 9, 2024
87673f5
Remove array-sort package
adrinr Jan 9, 2024
0a685a4
Remove define-property usages
adrinr Jan 10, 2024
cf69ff2
Remove define-property usages
adrinr Jan 10, 2024
d9a6a64
Lint
adrinr Jan 10, 2024
e6ceb06
Remove define-property package
adrinr Jan 10, 2024
d9b18bd
Remove extend-shallow usages
adrinr Jan 10, 2024
1fea984
Remove extend-shallow package
adrinr Jan 10, 2024
282a291
Replace for-in usages in favour of native implementation
adrinr Jan 10, 2024
ed83eb4
Remove for-in package
adrinr Jan 10, 2024
debd223
Rename test
adrinr Jan 10, 2024
690b67b
Rename ci
adrinr Jan 10, 2024
acc3a18
Remove helper-coverage
adrinr Jan 10, 2024
db2c431
Update micromatch package
adrinr Jan 10, 2024
e3a74d9
Merge pull request #13 from Budibase/chore/update-micromatch
adrinr Jan 10, 2024
9d8a00e
Merge branch 'master' into chore/remove-unnecessary-dependencies
adrinr Jan 10, 2024
ce05df1
Remove es6 usages
adrinr Jan 10, 2024
054d8f5
Lint
adrinr Jan 10, 2024
7918006
Merge pull request #12 from Budibase/chore/remove-unnecessary-depende…
adrinr Jan 10, 2024
3e13176
v0.12.0
adrinr Jan 10, 2024
3dd6c9f
Remove uuid
adrinr Jan 15, 2024
3a334ba
Remove uuid usage
adrinr Jan 15, 2024
f77d32a
Add missing require
adrinr Jan 15, 2024
97f3975
Lint
adrinr Jan 16, 2024
4d062c3
Merge pull request #14 from Budibase/chore/remove-uuid-dependency
adrinr Jan 16, 2024
69bae06
v0.12.1
adrinr Jan 16, 2024
8ec946d
Remove handlebars-utils.identity usage
adrinr Jan 16, 2024
d03ea93
Copy handlebars-utils in
adrinr Jan 17, 2024
9a910c5
Remove handlebars-utils
adrinr Jan 17, 2024
4f5ef39
Tree-shake number
adrinr Jan 17, 2024
e7d8883
Tree-shake array
adrinr Jan 17, 2024
9fd63a8
Lint
adrinr Jan 17, 2024
4bd65d8
Lint
adrinr Jan 17, 2024
18c5f47
Tree-shake string
adrinr Jan 17, 2024
eba6f9a
Fix tests
adrinr Jan 17, 2024
ef95f3e
Tree-shake object
adrinr Jan 17, 2024
a0d9f06
Tree-shake url
adrinr Jan 17, 2024
60f39e5
Tree-shake regex
adrinr Jan 17, 2024
800e364
Tree-shake comparison
adrinr Jan 17, 2024
5bc63aa
Lint
adrinr Jan 17, 2024
be3297c
Lint
adrinr Jan 17, 2024
cb8a1b2
Add uuid back
adrinr Jan 17, 2024
9b61cf3
Lint
adrinr Jan 18, 2024
78c97f5
Revert "Remove UUID dependency"
adrinr Jan 19, 2024
c9e8229
Merge pull request #16 from Budibase/revert-14-chore/remove-uuid-depe…
adrinr Jan 19, 2024
c5b78cc
v0.12.2
adrinr Jan 19, 2024
1b000ab
Merge branch 'master' into optimise-esbuild-bundle
adrinr Jan 19, 2024
c353df4
Update array doc
adrinr Jan 22, 2024
ead77b1
Fix string examples
adrinr Jan 22, 2024
53ebf31
Fix url
adrinr Jan 22, 2024
0c0a6aa
Fix comparasion
adrinr Jan 23, 2024
b356ed6
Create tests
adrinr Jan 23, 2024
3bfcbd7
Lint
adrinr Jan 23, 2024
b614cbe
Fix random test
adrinr Jan 23, 2024
e9df816
Fix toExponential
adrinr Jan 23, 2024
b6a7a89
Fix loading tests
adrinr Jan 23, 2024
f9b4257
Fix test
adrinr Jan 23, 2024
e711d8e
Fix center test
adrinr Jan 23, 2024
1b00f07
Fix before and after examples and code
adrinr Jan 23, 2024
0228980
Fix stripQuerystring docs
adrinr Jan 23, 2024
f119b5c
Fix array examples
adrinr Jan 23, 2024
e614954
Fixing array.before tests and code
adrinr Jan 23, 2024
05ab084
Fix raw example
adrinr Jan 23, 2024
8f173ba
Fix reverse test
adrinr Jan 23, 2024
0364097
Fix raw test
adrinr Jan 23, 2024
948c850
Fix, run all tests
adrinr Jan 23, 2024
f0c1ec6
Fix ifNth description
adrinr Jan 23, 2024
ca27ff4
Fix and add tests
adrinr Jan 23, 2024
9f5f1aa
Fix example
adrinr Jan 23, 2024
064fe89
Expose only collection helpers
adrinr Jan 23, 2024
f97c8fa
Merge pull request #17 from Budibase/test-examples
adrinr Jan 23, 2024
c551cbd
Merge pull request #18 from Budibase/fix-exposing-helpers
adrinr Jan 23, 2024
8db064f
v0.13.0
adrinr Jan 23, 2024
7a8681e
Merge branch 'master' into optimise-esbuild-bundle
adrinr Jan 23, 2024
554f12e
Add missing examples
adrinr Jan 26, 2024
0f93d9a
Fix wrong attributes
adrinr Jan 26, 2024
33968ef
Merge pull request #19 from Budibase/add-extra-examples
mike12345567 Jan 29, 2024
e658a31
Remove block from array.itemAt
adrinr Jan 30, 2024
cd8d0d8
Add @inline decorators
adrinr Jan 30, 2024
7e502b9
Merge pull request #20 from Budibase/fix-block-attributes
adrinr Jan 30, 2024
fab2b5b
v0.13.1
adrinr Jan 30, 2024
10250c9
adds loremIpsum function plus tests
mikesealey Jul 4, 2024
30a6b6a
adds function to write Lorem Ipsum inc tests
mikesealey Jul 4, 2024
6e045f3
removes unused "maxLoremLength" variable
mikesealey Jul 4, 2024
59bf984
fixes casing in example
mikesealey Jul 4, 2024
8bfc34a
prettier
mikesealey Jul 4, 2024
83afe46
Lint
adrinr Jul 4, 2024
a6bda6c
Merge pull request #21 from Budibase/lorem
mikesealey Jul 4, 2024
3f11685
v0.13.2
mike12345567 Jul 4, 2024
ebe5cb6
Move to using non-mutating variants of sort and reverse.
samwho Oct 4, 2024
123a41b
Merge pull request #22 from Budibase/move-to-non-mutating-sort-reverse-2
shogunpurple Oct 4, 2024
8a67b44
v0.14.0
shogunpurple Oct 4, 2024
3db734d
Merge branch 'master' into optimise-esbuild-bundle
adrinr Dec 2, 2024
993c07c
Update to mocha 9.x
adrinr Dec 2, 2024
0eed4b1
Update to mocha 10.x
adrinr Dec 2, 2024
87553ce
Merge pull request #25 from Budibase/update-mocha
adrinr Dec 2, 2024
f3735de
Merge pull request #23 from Budibase/remove-handlebars-utils
adrinr Dec 4, 2024
9377fd2
changes lorem argument to optional
mikesealey Mar 11, 2025
57a1f69
fixes typo
mikesealey Mar 12, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@ for (const key in lib) {
var hbs = options.handlebars || options.hbs || require('handlebars');
module.exports.handlebars = hbs;
hbs.registerHelper(group);
return hbs.helpers;
return group;
};
}

72 changes: 38 additions & 34 deletions lib/array.js
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ const createFrame = require('./utils/createFrame');
* @param {Number} `n` Starting index (number of items to exclude)
* @return {Array} Array exluding `n` items.
* @api public
* @example {{ after [1, 2, 3] 1}} -> [3]
* @example {{ after ['a', 'b', 'c', 'd'] 2}} -> ['c', 'd']
*/

helpers.after = function(array, n) {
@@ -63,20 +63,20 @@ helpers.arrayify = function(value) {
* ```handlebars
* <!-- array: ['a', 'b', 'c'] -->
* {{before array 2}}
* <!-- results in: '['a', 'b']' -->
* <!-- results in: '['a']' -->
* ```
* @param {Array} `array`
* @param {Number} `n`
* @return {Array} Array excluding items after the given number.
* @api public
* @example {{ before [1, 2, 3] 2}} -> [1, 2]
* @example {{ before ['a', 'b', 'c', 'd'] 3}} -> ['a', 'b']
*/

helpers.before = function(array, n) {
if (util.isUndefined(array)) return '';
array = util.result(array);
if (Array.isArray(array)) {
return array.slice(0, -n);
return array.slice(0, n - 1);
}
return '';
};
@@ -102,7 +102,7 @@ helpers.eachIndex = function(array, options) {
array = util.result(array);
if (Array.isArray(array)) {
for (var i = 0; i < array.length; i++) {
result += options.fn({item: array[i], index: i});
result += options.fn({ item: array[i], index: i });
}
}
return result;
@@ -130,7 +130,6 @@ helpers.filter = function(array, value, options) {
if (util.isUndefined(array)) return options.inverse(this);
array = util.result(array);
if (Array.isArray(array)) {

var content = '';
var results = [];

@@ -141,7 +140,6 @@ helpers.filter = function(array, value, options) {
return value === getValue(val, prop);
});
} else {

// filter on a string value
results = array.filter(function(v) {
return value === v;
@@ -169,7 +167,7 @@ helpers.filter = function(array, value, options) {
* @param {Number} `n` Number of items to return, starting at `0`.
* @return {Array}
* @api public
* @example {{first [1, 2, 3, 4] 2}} -> [1, 2]
* @example {{first [1, 2, 3, 4] 2}} -> 1,2
*/

helpers.first = function(array, n) {
@@ -234,8 +232,8 @@ helpers.forEach = function(array, options) {
item.index = i + 1;
item.total = len;
item.isFirst = i === 0;
item.isLast = i === (len - 1);
buffer += options.fn(item, {data: data});
item.isLast = i === len - 1;
buffer += options.fn(item, { data: data });
}
return buffer;
}
@@ -262,7 +260,7 @@ helpers.forEach = function(array, options) {
* @return {String}
* @block
* @api public
* @example {{#inArray [1, 2, 3] 2}} 2 exists {{else}} 2 does not exist {{/inArray}} -> 2 exists
* @example {{#inArray [1, 2, 3] 2}} 2 exists {{else}} 2 does not exist {{/inArray}} -> ' 2 exists '
*/

helpers.inArray = function(array, value, options) {
@@ -342,7 +340,7 @@ helpers.itemAt = function(array, idx) {
* @param {String} `separator` The separator to use. Defaults to `, `.
* @return {String}
* @api public
* @example {{join [1, 2, 3]}} -> '1, 2, 3'
* @example {{join [1, 2, 3]}} -> 1, 2, 3
*/

helpers.join = function(array, separator) {
@@ -366,7 +364,7 @@ helpers.join = function(array, separator) {
* @return {String}
* @block
* @api public
* @example {{equalsLength '[1,2,3]' 3}} -> true
* @example {{equalsLength [1, 2, 3] 3}} -> true
*/

helpers.equalsLength = function(value, length, options) {
@@ -432,7 +430,7 @@ helpers.last = function(array, n) {
* @param {Array|Object|String} `value`
* @return {Number} The length of the value.
* @api public
* @example {{length '[1, 2, 3]'}} -> 3
* @example {{length [1, 2, 3]}} -> 3
*/

helpers.length = function(array) {
@@ -441,7 +439,11 @@ helpers.length = function(array) {
array = Object.keys(array);
}
// this is an inline array, split it
if (typeof array === 'string' && array.startsWith('[') && array.endsWith(']')) {
if (
typeof array === 'string' &&
array.startsWith('[') &&
array.endsWith(']')
) {
return array.split(',').length;
}
if (typeof array === 'string' || Array.isArray(array)) {
@@ -574,7 +576,7 @@ helpers.reverse = function(array) {
* @return {String}
* @block
* @api public
* @example {{#some [1, 'b', 3] isString}} string found {{else}} No string found {{/some}} -> string found
* @example {{#some [1, "b", 3] isString}} string found {{else}} No string found {{/some}} -> ' string found '
*/

helpers.some = function(array, iter, options) {
@@ -635,7 +637,7 @@ helpers.sort = function(array, options) {
* @param {Array} `array` the array to sort.
* @param {String|Function} `props` One or more properties to sort by, or sorting functions to use.
* @api public
* @example {{ sortBy [{a: 'zzz'}, {a: 'aaa'}] 'a' }} -> [{'a':'aaa'}, {'a':'zzz'}]
* @example {{ sortBy [{'a': 'zzz'}, {'a': 'aaa'}] 'a' }} -> [{'a':'aaa'},{'a':'zzz'}]
*/

helpers.sortBy = function(array, prop, options) {
@@ -649,7 +651,7 @@ helpers.sortBy = function(array, prop, options) {
if (!util.isString(prop) && typeof prop !== 'function') {
return array.sort();
}

if (typeof prop === 'function') {
return array.sort(prop);
}
@@ -746,7 +748,7 @@ helpers.withBefore = function(array, idx, options) {
* @return {String}
* @block
* @api public
* @example {{ withFirst [1, 2, 3] }} {{this}} {{/withFirst}}
* @example {{#withFirst [1, 2, 3] }}{{this}}{{/withFirst}} -> 1
*/

helpers.withFirst = function(array, idx, options) {
@@ -780,7 +782,7 @@ helpers.withFirst = function(array, idx, options) {
* {{#withGroup array 4}}
* {{#each this}}
* {{.}}
* {{each}}
* {{/each}}
* <br>
* {{/withGroup}}
* <!-- results in: -->
@@ -793,7 +795,7 @@ helpers.withFirst = function(array, idx, options) {
* @return {String}
* @block
* @api public
* @example {{#withGroup [1, 2, 3, 4] 2}} {{#each this}} {{.}} {{each}} <br> {{/withGroup}} -> 1,2<br> 3,4<br>
* @example {{#withGroup [1, 2, 3, 4] 2}}{{#each this}}{{.}}{{/each}}<br>{{/withGroup}} -> 12<br>34<br>
* */

helpers.withGroup = function(array, size, options) {
@@ -803,7 +805,7 @@ helpers.withGroup = function(array, size, options) {
if (Array.isArray(array) && array.length > 0) {
var subcontext = [];
for (var i = 0; i < array.length; i++) {
if (i > 0 && (i % size) === 0) {
if (i > 0 && i % size === 0) {
result += options.fn(subcontext);
subcontext = [];
}
@@ -830,7 +832,7 @@ helpers.withGroup = function(array, size, options) {
* @param {Object} `options`
* @return {String}
* @block
* @example {{#withLast [1, 2, 3, 4]}} {{this}} {{/withLast}} -> 4
* @example {{#withLast [1, 2, 3, 4]}}{{this}}{{/withLast}} -> 4
*/

helpers.withLast = function(array, idx, options) {
@@ -847,7 +849,8 @@ helpers.withLast = function(array, idx, options) {
}

array = array.slice(-idx);
var len = array.length, i = -1;
var len = array.length,
i = -1;
var result = '';
while (++i < len) {
result += options.fn(array[i]);
@@ -872,7 +875,7 @@ helpers.withLast = function(array, idx, options) {
* @return {String}
* @block
* @api public
* @example {{#withSort ['b', 'a', 'c']}} {{this}} {{/withSort}} -> abc
* @example {{#withSort ['b', 'a', 'c']}}{{this}}{{/withSort}} -> abc
*/

helpers.withSort = function(array, prop, options) {
@@ -883,7 +886,7 @@ helpers.withSort = function(array, prop, options) {

if (util.isUndefined(prop)) {
options = prop;

array = array.sort();
if (getValue(options, 'hash.reverse')) {
array = array.reverse();
@@ -898,14 +901,15 @@ helpers.withSort = function(array, prop, options) {
array.sort(function(a, b) {
a = getValue(a, prop);
b = getValue(b, prop);
return a > b ? 1 : (a < b ? -1 : 0);
return a > b ? 1 : a < b ? -1 : 0;
});

if (getValue(options, 'hash.reverse')) {
array = array.reverse();
}

var alen = array.length, j = -1;
var alen = array.length,
j = -1;
while (++j < alen) {
result += options.fn(array[j]);
}
@@ -918,16 +922,16 @@ helpers.withSort = function(array, prop, options) {
* Block helper that return an array with all duplicate
* values removed. Best used along with a [each](#each) helper.
*
* ```handlebars
* <!-- array: ['a', 'a', 'c', 'b', 'e', 'e'] -->
* {{#each (unique array)}}{{.}}{{/each}}
* <!-- results in: 'acbe' -->
* ```
* ```handlebars
* <!-- array: ['a', 'a', 'c', 'b', 'e', 'e'] -->
* {{#each (unique array)}}{{.}}{{/each}}
* <!-- results in: 'acbe' -->
* ```
* @param {Array} `array`
* @param {Object} `options`
* @return {Array}
* @api public
* @example {{#each (unique ['a', 'a', 'c', 'b', 'e', 'e']) }} {{.}} {{/each}} -> acbe
* @example {{#each (unique ['a', 'a', 'c', 'b', 'e', 'e']) }}{{.}}{{/each}} -> acbe
*/

helpers.unique = function(array, options) {
53 changes: 29 additions & 24 deletions lib/comparison.js
Original file line number Diff line number Diff line change
@@ -62,7 +62,7 @@ helpers.and = function() {
* @return {String} Block, or if specified the inverse block is rendered if falsey.
* @block
* @api public
* @example {{compare 10 '<' 5 }} -> true
* @example {{compare 10 '<' 5 }} -> false
*/

helpers.compare = function(a, operator, b, options) {
@@ -102,7 +102,9 @@ helpers.compare = function(a, operator, b, options) {
result = typeof a === b;
break;
default: {
throw new Error('helper {{compare}}: invalid operator: `' + operator + '`');
throw new Error(
'helper {{compare}}: invalid operator: `' + operator + '`'
);
}
}

@@ -150,7 +152,7 @@ helpers.contains = function(collection, value, startIndex, options) {
* @return {String}
* @alias .or
* @api public
* @example {{default null null 'default'}} -> default
* @example {{default null null 'default'}} -> default
*/

helpers.default = function() {
@@ -173,7 +175,7 @@ helpers.default = function() {
* @alias is
* @block
* @api public
* @example {{#eq 3 3}} equal{{else}} not equal{{/eq}} -> equal
* @example {{#eq 3 3}}equal{{else}}not equal{{/eq}} -> equal
*/

helpers.eq = function(a, b, options) {
@@ -197,7 +199,7 @@ helpers.eq = function(a, b, options) {
* @return {String} Block, or inverse block if specified and falsey.
* @block
* @api public
* @example {{#gt 4 3}} greater than{{else}} not greater than{{/gt}} -> greater than
* @example {{#gt 4 3}} greater than{{else}} not greater than{{/gt}} -> ' greater than'
*/

helpers.gt = function(a, b, options) {
@@ -222,7 +224,7 @@ helpers.gt = function(a, b, options) {
* @return {String} Block, or inverse block if specified and falsey.
* @block
* @api public
* @example {{#gte 4 3}} greater than or equal{{else}} not greater than{{/gte}} -> greater than or equal
* @example {{#gte 4 3}} greater than or equal{{else}} not greater than{{/gte}} -> ' greater than or equal'
*/

helpers.gte = function(a, b, options) {
@@ -243,7 +245,7 @@ helpers.gte = function(a, b, options) {
* @return {String}
* @block
* @api public
* @example {{#has 'foobar' 'foo'}} has it{{else}} doesn't{{/has}} -> has it
* @example {{#has 'foobar' 'foo'}}has it{{else}}doesn't{{/has}} -> has it
*/

helpers.has = function(value, pattern, options) {
@@ -266,7 +268,10 @@ helpers.has = function(value, pattern, options) {
return util.value(has(this, value), this, options);
}

if ((Array.isArray(value) || util.isString(value)) && util.isString(pattern)) {
if (
(Array.isArray(value) || util.isString(value)) &&
util.isString(pattern)
) {
if (value.indexOf(pattern) > -1) {
return util.fn(true, this, options);
}
@@ -324,7 +329,7 @@ helpers.isTruthy = function(val, options) {
* @return {String} Block, or inverse block if specified and falsey.
* @block
* @api public
* @example {{#ifEven 2}} even {{else}} odd {{/ifEven}} -> even
* @example {{#ifEven 2}} even {{else}} odd {{/ifEven}} -> ' even '
*/

helpers.ifEven = function(num, options) {
@@ -333,7 +338,7 @@ helpers.ifEven = function(num, options) {

/**
* Conditionally renders a block if the remainder is zero when
* `a` operand is divided by `b`. If an inverse block is specified
* `b` operand is divided by `a`. If an inverse block is specified
* it will be rendered when the remainder is **not zero**.
*
* @param {Number}
@@ -342,7 +347,7 @@ helpers.ifEven = function(num, options) {
* @return {String} Block, or inverse block if specified and falsey.
* @block
* @api public
* @example {{#ifNth 10 2}} remainder {{else}} no remainder {{/ifNth}} -> remainder
* @example {{#ifNth 2 10}}remainder{{else}}no remainder{{/ifNth}} -> remainder
*/

helpers.ifNth = function(a, b, options) {
@@ -366,7 +371,7 @@ helpers.ifNth = function(a, b, options) {
* @return {String} Block, or inverse block if specified and falsey.
* @block
* @api public
* @example {{#ifOdd 3}} odd {{else}} even {{/ifOdd}} -> odd
* @example {{#ifOdd 3}}odd{{else}}even{{/ifOdd}} -> odd
*/

helpers.ifOdd = function(val, options) {
@@ -384,7 +389,7 @@ helpers.ifOdd = function(val, options) {
* @return {String}
* @block
* @api public
* @example {{#is 3 3}} is {{else}} is not {{/is}} -> is
* @example {{#is 3 3}} is {{else}} is not {{/is}} -> ' is '
*/

helpers.is = function(a, b, options) {
@@ -407,7 +412,7 @@ helpers.is = function(a, b, options) {
* @return {String}
* @block
* @api public
* @example {{#isnt 3 3}} isnt {{else}} is {{/isnt}} -> is
* @example {{#isnt 3 3}} isnt {{else}} is {{/isnt}} -> ' is '
*/

helpers.isnt = function(a, b, options) {
@@ -430,7 +435,7 @@ helpers.isnt = function(a, b, options) {
* @return {String} Block, or inverse block if specified and falsey.
* @block
* @api public
* @example {{#lt 2 3}} less than {{else}} more than or equal {{/lt}} -> less than
* @example {{#lt 2 3}} less than {{else}} more than or equal {{/lt}} -> ' less than '
*/

helpers.lt = function(a, b, options) {
@@ -455,7 +460,7 @@ helpers.lt = function(a, b, options) {
* @return {String} Block, or inverse block if specified and falsey.
* @block
* @api public
* @example {{#lte 2 3}} less than or equal {{else}} more than {{/lte}} -> less than or equal
* @example {{#lte 2 3}} less than or equal {{else}} more than {{/lte}} -> ' less than or equal '
*/

helpers.lte = function(a, b, options) {
@@ -477,7 +482,7 @@ helpers.lte = function(a, b, options) {
* @return {String} Block, or inverse block if specified and falsey.
* @block
* @api public
* @example {{#neither null null}} both falsey {{else}} both not falsey {{/neither}} -> both falsey
* @example {{#neither null null}}both falsey{{else}}both not falsey{{/neither}} -> both falsey
*/

helpers.neither = function(a, b, options) {
@@ -492,7 +497,7 @@ helpers.neither = function(a, b, options) {
* @return {String}
* @block
* @api public
* @example {{#not undefined }} falsey {{else}} not falsey {{/not}} -> falsey
* @example {{#not undefined }}falsey{{else}}not falsey{{/not}} -> falsey
*/

helpers.not = function(val, options) {
@@ -515,7 +520,7 @@ helpers.not = function(val, options) {
* @return {String} Block, or inverse block if specified and falsey.
* @block
* @api public
* @example {{#or 1 2 undefined }} at least one truthy {{else}} all falsey {{/or}} -> at least one truthy
* @example {{#or 1 2 undefined }} at least one truthy {{else}} all falsey {{/or}} -> ' at least one truthy '
*/

helpers.or = function(/* any, any, ..., options */) {
@@ -542,7 +547,7 @@ helpers.or = function(/* any, any, ..., options */) {
* @return {String} Inverse block by default, or block if falsey.
* @block
* @api public
* @example {{#unlessEq 2 1 }} not equal {{else}} equal {{/unlessEq}} -> not equal
* @example {{#unlessEq 2 1 }} not equal {{else}} equal {{/unlessEq}} -> ' not equal '
*/

helpers.unlessEq = function(a, b, options) {
@@ -563,7 +568,7 @@ helpers.unlessEq = function(a, b, options) {
* @return {String} Inverse block by default, or block if falsey.
* @block
* @api public
* @example {{#unlessGt 20 1 }} not greater than {{else}} greater than {{/unlessGt}} -> greater than
* @example {{#unlessGt 20 1 }} not greater than {{else}} greater than {{/unlessGt}} -> ' greater than '
*/

helpers.unlessGt = function(a, b, options) {
@@ -584,7 +589,7 @@ helpers.unlessGt = function(a, b, options) {
* @return {String} Block, or inverse block if specified and falsey.
* @block
* @api public
* @example {{#unlessLt 20 1 }} greater than or equal {{else}} less than {{/unlessLt}} -> greater than or equal
* @example {{#unlessLt 20 1 }}greater than or equal{{else}}less than{{/unlessLt}} -> greater than or equal
*/

helpers.unlessLt = function(a, b, options) {
@@ -605,7 +610,7 @@ helpers.unlessLt = function(a, b, options) {
* @return {String} Block, or inverse block if specified and falsey.
* @block
* @api public
* @example {{#unlessGteq 20 1 }} less than {{else}} greater than or equal to {{/unlessGteq}} -> greater than or equal to
* @example {{#unlessGteq 20 1 }} less than {{else}}greater than or equal to{{/unlessGteq}} -> greater than or equal to
*/

helpers.unlessGteq = function(a, b, options) {
@@ -626,7 +631,7 @@ helpers.unlessGteq = function(a, b, options) {
* @return {String} Block, or inverse block if specified and falsey.
* @block
* @api public
* @example {{#unlessLteq 20 1 }} greater than {{else}} less than or equal to {{/unlessLteq}} -> greater than
* @example {{#unlessLteq 20 1 }} greater than {{else}} less than or equal to {{/unlessLteq}} -> ' greater than '
*/

helpers.unlessLteq = function(a, b, options) {
4 changes: 2 additions & 2 deletions lib/number.js
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ var helpers = module.exports;
* @param {Number|String} `number`
* @return {String}
* @api public
* @example {{ bytes 1386 }} -> 1.4Kb
* @example {{ bytes 1386 1 }} -> 1.4 kB
*/

helpers.bytes = function(number, precision, options) {
@@ -129,7 +129,7 @@ helpers.toAbbr = function(number, precision) {
* @param {Number} `fractionDigits` Optional. An integer specifying the number of digits to use after the decimal point. Defaults to as many digits as necessary to specify the number.
* @return {Number}
* @api public
* @example {{ toExponential 10123 2 }} -> 101e+4
* @example {{ toExponential 10123 2 }} -> 1.01e+4
*/

helpers.toExponential = function(number, digits) {
40 changes: 19 additions & 21 deletions lib/string.js
Original file line number Diff line number Diff line change
@@ -128,7 +128,7 @@ helpers.center = function(str, spaces) {
* @param {String} `string` The string to chop.
* @return {String}
* @api public
* @example {{ chop ' ABC '}} -> 'ABC'
* @example {{ chop ' ABC '}} -> ABC
*/

helpers.chop = function(str) {
@@ -210,7 +210,7 @@ helpers.downcase = function() {
* @param {Number} `length` The desired length of the returned string.
* @return {String} The truncated string.
* @api public
* @example {{ellipsis 'foo bar baz', 7}} -> foo bar…
* @example {{ellipsis 'foo bar baz' 7}} -> foo bar…
*/

helpers.ellipsis = function(str, limit) {
@@ -392,7 +392,7 @@ helpers.prepend = function(str, prefix) {
* Render a block without processing mustache templates inside the block.
*
* ```handlebars
* {{{{#raw}}}}
* {{{{raw}}}}
* {{foo}}
* {{{{/raw}}}}
* <!-- results in: '{{foo}}' -->
@@ -402,7 +402,7 @@ helpers.prepend = function(str, prefix) {
* @return {String}
* @block
* @api public
* @example {{{{#raw}}}} {{foo}} {{{{/raw}}}} -> {{foo}}
* @example {{{{raw}}}}{{foo}}{{{{/raw}}}} -> \{{foo}}
*/

helpers.raw = function(options) {
@@ -431,7 +431,7 @@ helpers.raw = function(options) {
* @param {String} `substring`
* @return {String}
* @api public
* @example {{remove 'a b a b a b' 'a '}} -> b b b
* @example {{remove 'a b a b a b' 'a '}} -> b b b
*/

helpers.remove = function(str, ch) {
@@ -451,7 +451,7 @@ helpers.remove = function(str, ch) {
* @param {String} `substring`
* @return {String}
* @api public
* @example {{remove 'a b a b a b' 'a'}} -> b a b a b
* @example {{removeFirst 'a b a b a b' 'a'}} -> ' b a b a b'
*/

helpers.removeFirst = function(str, ch) {
@@ -472,7 +472,7 @@ helpers.removeFirst = function(str, ch) {
* @param {String} `b`
* @return {String}
* @api public
* @example {{replace 'a b a b a b' 'a' 'z'}} -> z b z b z b
* @example {{replace 'a b a b a b' 'a' 'z'}} -> z b z b z b
*/

helpers.replace = function(str, a, b) {
@@ -494,7 +494,7 @@ helpers.replace = function(str, a, b) {
* @param {String} `b`
* @return {String}
* @api public
* @example {{replace 'a b a b a b' 'a' 'z'}} -> z b a b a b
* @example {{replaceFirst 'a b a b a b' 'a' 'z'}} -> z b a b a b
*/

helpers.replaceFirst = function(str, a, b) {
@@ -517,10 +517,7 @@ helpers.replaceFirst = function(str, a, b) {
* @example {{reverse 'abcde'}} -> edcba
*/

helpers.reverse = function(str) {
if (typeof(str) !== 'string') return '';
return str.split('').reverse().join('');
};
helpers.reverse = require('./array').reverse;

/**
* Sentence case the given string
@@ -532,7 +529,7 @@ helpers.reverse = function(str) {
* @param {String} `str`
* @return {String}
* @api public
* @example {{sentence 'hello world. goodbye world.'}} -> Hello world. Goodbye world.
* @example {{sentence 'hello world. goodbye world.'}} -> Hello world. Goodbye world.
*/

helpers.sentence = function(str) {
@@ -572,7 +569,7 @@ helpers.snakecase = function(str) {
* @param {String} `string` The string to split.
* @return {String} `character` Default is an empty string.
* @api public
* @example {{split 'a,b,c'}} -> ['a', 'b', 'c']
* @example {{split 'a,b,c'}} -> ['a', 'b', 'c']
*/

helpers.split = function(str, ch) {
@@ -598,7 +595,7 @@ helpers.split = function(str, ch) {
* @return {String}
* @block
* @api public
* @example {{#startsWith 'Goodbye' 'Hello, world!'}} Yep {{else}} Nope {{/startsWith}} -> Nope
* @example {{#startsWith 'Goodbye' 'Hello, world!'}}Yep{{else}}Nope{{/startsWith}} -> Nope
*/

helpers.startsWith = function(prefix, str, options) {
@@ -623,7 +620,7 @@ helpers.startsWith = function(prefix, str, options) {
* @param {String} `str`
* @return {String}
* @api public
* @example {{#titleize 'this is title case' }} -> This Is Title Case
* @example {{titleize 'this is title case' }} -> This Is Title Case
*/

helpers.titleize = function(str) {
@@ -687,7 +684,7 @@ helpers.trimLeft = function(str) {
* @param {String} `string` The string to trim.
* @return {String}
* @api public
* @example {{trimRight ' ABC ' }} -> ' ABC '
* @example {{trimRight ' ABC ' }} -> ' ABC'
*/

helpers.trimRight = function(str) {
@@ -711,7 +708,7 @@ helpers.trimRight = function(str) {
* denote when the string has been truncated. Otherwise an ellipsis (`…`) will be used.
* @return {String} The truncated string.
* @api public
* @example {{truncate 'foo bar baz' 7 }} -> foo bar
* @example {{truncate 'foo bar baz' 7 }} -> foo bar
*/

helpers.truncate = function(str, limit, suffix) {
@@ -744,7 +741,7 @@ helpers.truncate = function(str, limit, suffix) {
* denote when the string has been truncated.
* @return {String} The truncated string.
* @api public
* @example {{truncateWords 'foo bar baz' 1 }} -> foo
* @example {{truncateWords 'foo bar baz' 1 }} -> foo
*/

helpers.truncateWords = function(str, count, suffix) {
@@ -755,10 +752,11 @@ helpers.truncateWords = function(str, count, suffix) {

var num = Number(count);
var arr = str.split(/[ \t]/);
if (num > arr.length) {
arr = arr.slice(0, num);
if (num >= arr.length) {
return str;
}

arr = arr.slice(0, num);
var val = arr.join(' ').trim();
return val + suffix;
}
10 changes: 5 additions & 5 deletions lib/url.js
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ var helpers = module.exports;
* @param {String} `str` The un-encoded string
* @return {String} The endcoded string
* @api public
* @example {{ encodeURI 'https://myurl?Hello There' }} -> https://myurl?Hello%20There
* @example {{ encodeURI 'https://myurl?Hello There' }} -> https%3A%2F%2Fmyurl%3FHello%20There
*/

helpers.encodeURI = function(str) {
@@ -32,7 +32,7 @@ helpers.encodeURI = function(str) {
* @param {String} `str`
* @return {String} Escaped string.
* @api public
* @example {{ escape 'https://myurl?Hello+There' }} -> https://myurl?Hello%20There
* @example {{ escape 'https://myurl?Hello+There' }} -> https%3A%2F%2Fmyurl%3FHello%2BThere
*/

helpers.escape = function(str) {
@@ -47,7 +47,7 @@ helpers.escape = function(str) {
* @param {String} `str`
* @return {String}
* @api public
* @example {{ decodeURI 'https://myurl?Hello%20There' }} -> https://myurl?=Hello There
* @example {{ decodeURI 'https://myurl?Hello%20There' }} -> https://myurl?Hello There
*/

helpers.decodeURI = function(str) {
@@ -90,7 +90,7 @@ helpers.urlParse = function(str) {
* @param {String} `url`
* @return {String} the url without the queryString
* @api public
* @example {{ stripQueryString 'https://myurl/api/test?foo=bar' }} -> 'https://myurl/api/test'
* @example {{ stripQuerystring 'https://myurl/api/test?foo=bar' }} -> 'https://myurl/api/test'
*/

helpers.stripQuerystring = function(str) {
@@ -111,7 +111,7 @@ helpers.stripQuerystring = function(str) {
* @param {String} `str`
* @return {String} the url with http protocol stripped
* @api public
* @example {{ stripProtocol 'https://myurl/api/test' }} -> 'myurl/api/test'
* @example {{ stripProtocol 'https://myurl/api/test' }} -> '//myurl/api/test'
*/

helpers.stripProtocol = function(str) {
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@budibase/handlebars-helpers",
"description": "More than 130 Handlebars helpers in ~20 categories. Helpers can be used with Assemble, Generate, Verb, Ghost, gulp-handlebars, grunt-handlebars, consolidate, or any node.js/Handlebars project.",
"version": "0.12.2",
"version": "0.13.0",
"homepage": "https://github.com/Budibase/handlebars-helpers",
"author": "Jon Schlinkert (https://github.com/jonschlinkert)",
"contributors": [
@@ -69,10 +69,9 @@
"node": ">=10.12.0"
},
"scripts": {
"lint": "./node_modules/.bin/eslint --ext js .",
"test": "./node_modules/.bin/rimraf .nyc_output && ./node_modules/.bin/nyc ./node_modules/.bin/mocha",
"update:readmemd": "verb",
"clean": "./node_modules/.bin/rimraf .nyc_output"
"lint": "eslint --ext js .",
"test": "mocha",
"update:readmemd": "verb"
},
"dependencies": {
"get-object": "^0.2.0",
@@ -99,8 +98,8 @@
"js-yaml": "^4.1.0",
"markdown-link": "^0.1.1",
"mocha": "^8.4.0",
"nyc": "^15.1.0",
"rimraf": "^3.0.2",
"sinon": "^17.0.1",
"template-helpers": "^1.0.1",
"templates": "^1.2.9",
"through2": "^4.0.2",
@@ -179,4 +178,4 @@
}
}
}
}
}
7 changes: 1 addition & 6 deletions test/array.js
Original file line number Diff line number Diff line change
@@ -39,12 +39,7 @@ describe('array', function() {
});
it('should return all of the items in an array before the given index', function() {
var fn = hbs.compile('{{before array 5}}');
assert.equal(fn(context), 'a,b,c');
});

it('should return all of the items in an array before the specified count', function() {
var fn = hbs.compile('{{before array 5}}');
assert.equal(fn(context), 'a,b,c');
assert.equal(fn(context), 'a,b,c,d');
});
});

132 changes: 132 additions & 0 deletions test/examples/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@

'use strict';

require('mocha');

const sinon = require('sinon');
sinon.stub(require('../../lib/uuid'), 'uuid').returns('f34ebc66-93bd-4f7c-b79b-92b5569138bc');

sinon.stub(require('../../lib/math'), 'random').returns(10);

var assert = require('assert');
var lib = require('../../lib/');

const fs = require('fs');
const doctrine = require('doctrine');
const path = require('path');

var handlebars = require('handlebars').create();
var helpers = require('../..');

function lookForward(lines, funcLines, idx) {
const funcLen = funcLines.length;
for (let i = idx, j = 0; i < idx + funcLen; ++i, j++) {
if (!lines[i].includes(funcLines[j])) {
return false;
}
}
return true;
}

function getCommentInfo(file, func) {
const lines = file.split('\n');
const funcLines = func.split('\n');
let comment = null;
for (let idx = 0; idx < lines.length; ++idx) {
// from here work back until we have the comment
if (lookForward(lines, funcLines, idx)) {
let fromIdx = idx;
let start = 0,
end = 0;
do {
if (lines[fromIdx].includes('*/')) {
end = fromIdx;
} else if (lines[fromIdx].includes('/*')) {
start = fromIdx;
}
if (start && end) {
break;
}
fromIdx--;
} while (fromIdx > 0);
comment = lines.slice(start, end + 1).join('\n');
}
}
if (comment == null) {
return { description: '' };
}
const docs = doctrine.parse(comment, { unwrap: true });
// some hacky fixes
docs.description = docs.description.replace(/\n/g, ' ');
docs.description = docs.description.replace(/[ ]{2,}/g, ' ');
docs.description = docs.description.replace(/is is/g, 'is');
const examples = docs.tags
.filter(el => el.title === 'example')
.map(el => el.description);
const blocks = docs.description.split('```');
if (examples.length > 0) {
docs.example = examples.join(' ');
}
// hacky example fix
if (docs.example && docs.example.includes('product')) {
docs.example = docs.example.replace('product', 'multiply');
}
docs.description = blocks[0].trim();
return docs;
}

function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

describe('examples', function() {
for (const key in lib) {
helpers[key]({ handlebars });

const group = lib[key];

const fileContent = fs.readFileSync(require.resolve(path.join('../../lib/', key)), 'utf8');

describe(key, function() {
for (const func in group) {
const { example } = getCommentInfo(fileContent, lib[key][func].toString());

example && it(func, function() {
let [hbs, expectedResult] = example.split('->').map(x => x.trim());

const context = {
double: i => i * 2,
isString: (x) => typeof(x) === 'string'
};

const arrays = hbs.match(/\[[^/\]]+\]/);
arrays && arrays.forEach((arrayString, i) => {
hbs = hbs.replace(new RegExp(escapeRegExp(arrayString)), `array${i}`);
context[`array${i}`] = JSON.parse(arrayString.replace(/\'/g, '"'));
});

if (expectedResult === undefined) {
// The function has no return value
return;
}

let result = handlebars.compile(hbs)(context);
// Trim 's
expectedResult = expectedResult.replace(/^\'|\'$/g, '');
try {
let parsedExpected;
if (
Array.isArray((parsedExpected = JSON.parse(expectedResult.replace(/\'/g, '"'))))
) {
expectedResult = parsedExpected.join(',');
}
} catch (e) {
// Nothing to parse
}
result = result.replace(/&amp;nbsp;/g, ' ');
assert.equal(result, expectedResult);
});
}
});
}
});
22 changes: 22 additions & 0 deletions test/string.js
Original file line number Diff line number Diff line change
@@ -359,6 +359,28 @@ describe('string', function() {
});
});

describe('truncateWords', function() {
it('should return then string truncated when the specified length is shorter than the word count', function() {
var fn = hbs.compile('{{truncateWords "foo bar baz" 2}}');
assert.equal(fn(), 'foo bar…');
});

it('should be able to truncate a single word', function() {
var fn = hbs.compile('{{truncateWords "foo bar baz" 1}}');
assert.equal(fn(), 'foo…');
});

it('should return the original string when the specified length matches the word count', function() {
var fn = hbs.compile('{{truncateWords "foo bar baz" 3}}');
assert.equal(fn(), 'foo bar baz');
});

it('should return the original string when the specified length is bigger than the word count', function() {
var fn = hbs.compile('{{truncateWords "foo bar baz" 4}}');
assert.equal(fn(), 'foo bar baz');
});
});

describe('uppercase', function() {
it('should return an empty string if undefined', function() {
var fn = hbs.compile('{{uppercase}}');
769 changes: 90 additions & 679 deletions yarn.lock

Large diffs are not rendered by default.