Skip to content

Commit 77b2082

Browse files
author
MaxGenash
committed
feat: (strf-8652) eliminate lodash from paper 2.x
1 parent 9bb8822 commit 77b2082

29 files changed

+671
-314
lines changed

helpers/all.js

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
2-
3-
var _ = require('lodash');
2+
const isObject = require('../lib/utils/isObject');
43

54
/**
65
* Yield block only if all arguments are valid
@@ -9,39 +8,23 @@ var _ = require('lodash');
98
* {{#all items theme_settings.optionA theme_settings.optionB}} ... {{/all}}
109
*/
1110
function helper(paper) {
12-
paper.handlebars.registerHelper('all', function () {
13-
14-
var args = [], opts, result;
15-
16-
// Translate arguments to array safely
17-
for (var i = 0; i < arguments.length; i++) {
18-
args.push(arguments[i]);
19-
}
20-
21-
// Take the last argument (content) out of testing array
22-
opts = args.pop();
11+
paper.handlebars.registerHelper('all', function (...args) {
12+
const opts = args.pop();
13+
let result;
2314

2415
// Check if all the arguments are valid / truthy
25-
result = _.all(args, function (arg) {
26-
if (_.isArray(arg)) {
16+
result = args.every(arg => {
17+
if (Array.isArray(arg)) {
2718
return !!arg.length;
2819
}
29-
// If an empty object is passed, arg is false
30-
else if (_.isEmpty(arg) && _.isObject(arg)) {
20+
if (isObject(arg) && !Object.keys(arg).length) {
3121
return false;
3222
}
33-
// Everything else
34-
else {
35-
return !!arg;
36-
}
23+
return !!arg;
3724
});
3825

3926
// If everything was valid, then "all" condition satisfied
40-
if (result) {
41-
return opts.fn(this);
42-
} else {
43-
return opts.inverse(this);
44-
}
27+
return result ? opts.fn(this) : opts.inverse(this);
4528
});
4629
}
4730

helpers/any.js

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
2-
3-
var _ = require('lodash');
2+
const isObject = require('../lib/utils/isObject');
3+
const isMatch = require("../lib/utils/isMatch");
44

55
/**
66
* Yield block if any object within a collection matches supplied predicate
@@ -9,48 +9,29 @@ var _ = require('lodash');
99
* {{#any items selected=true}} ... {{/any}}
1010
*/
1111
function helper(paper) {
12-
paper.handlebars.registerHelper('any', function () {
13-
14-
var args = [],
15-
opts,
16-
predicate,
17-
any;
18-
19-
// Translate arguments to array safely
20-
for (var i = 0; i < arguments.length; i++) {
21-
args.push(arguments[i]);
22-
}
23-
24-
// Take the last argument (content) out of testing array
25-
opts = args.pop();
26-
predicate = opts.hash;
27-
28-
if (!_.isEmpty(predicate)) {
29-
// With options hash, we check the contents of first argument
30-
any = _.any(args[0], predicate);
12+
paper.handlebars.registerHelper('any', function (...args) {
13+
const opts = args.pop();
14+
let any;
15+
16+
// With options hash, we check the contents of first argument
17+
if (opts.hash && Object.keys(opts.hash).length) {
18+
// This works fine for both arrays and objects
19+
any = isObject(args[0]) && Object.values(args[0]).some(item => isMatch(item, opts.hash));
3120
} else {
3221
// DEPRECATED: Moved to #or helper
3322
// Without options hash, we check all the arguments
34-
any = _.any(args, function (arg) {
35-
if (_.isArray(arg)) {
23+
any = args.some(arg => {
24+
if (Array.isArray(arg)) {
3625
return !!arg.length;
3726
}
38-
// If an empty object is passed, arg is false
39-
else if (_.isEmpty(arg) && _.isObject(arg)) {
27+
if (isObject(arg) && !Object.keys(arg).length) {
4028
return false;
4129
}
42-
// Everything else
43-
else {
44-
return !!arg;
45-
}
30+
return !!arg;
4631
});
4732
}
4833

49-
if (any) {
50-
return opts.fn(this);
51-
}
52-
53-
return opts.inverse(this);
34+
return any ? opts.fn(this) : opts.inverse(this);
5435
});
5536
}
5637

helpers/contains.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
var _ = require('lodash');
3+
const includes = require('../lib/utils/includes');
44

55
/**
66
* Is any value included in a collection or a string?
@@ -10,17 +10,12 @@ var _ = require('lodash');
1010
* {{#contains font_path "Roboto"}} ... {{/contains}}
1111
*/
1212
function helper(paper) {
13-
paper.handlebars.registerHelper('contains', function () {
14-
var args = Array.prototype.slice.call(arguments, 0, -1),
15-
options = _.last(arguments),
16-
contained = _.contains.apply(_, args);
13+
paper.handlebars.registerHelper('contains', function (...args) {
14+
const options = args.pop();
15+
const contained = includes(...args);
1716

1817
// Yield block if true
19-
if (contained) {
20-
return options.fn(this);
21-
} else {
22-
return options.inverse(this);
23-
}
18+
return contained ? options.fn(this) : options.inverse(this);
2419
});
2520
}
2621

helpers/deprecated.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict';
22

3-
var _ = require('lodash');
3+
const pickBy = require("../lib/utils/pickBy");
44

55
function helper(paper) {
6-
paper.handlebars.registerHelper('pick', function () {
7-
return _.pick.apply(null, arguments);
6+
paper.handlebars.registerHelper('pick', function (...args) {
7+
return pickBy(...args);
88
});
99

1010
/**

helpers/for.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
'use strict';
22

3-
var _ = require('lodash');
3+
const isObject = require('../lib/utils/isObject');
44

55
function helper(paper) {
66
paper.handlebars.registerHelper('for', function (from, to, context) {
77
const options = arguments[arguments.length - 1];
88
const maxIterations = 100;
9-
var output = '';
9+
let output = '';
1010

1111
function isOptions(obj) {
12-
return _.isObject(obj) && obj.fn;
12+
return obj && obj.fn;
1313
}
1414

1515
if (isOptions(to)) {
1616
context = {};
1717
to = from;
1818
from = 1;
19-
20-
} else if (isOptions(context)) {
21-
if (_.isObject(to)) {
22-
context = to;
23-
to = from;
24-
from = 1;
25-
}
19+
} else if (isOptions(context) && isObject(to)) {
20+
context = to;
21+
to = from;
22+
from = 1;
2623
}
2724

2825
if (to < from) {
@@ -36,7 +33,7 @@ function helper(paper) {
3633
to = from + maxIterations - 1;
3734
}
3835

39-
for (var i = from; i <= to; i++) {
36+
for (let i = from; i <= to; i++) {
4037
context.$index = i;
4138
output += options.fn(context);
4239
}

helpers/getImage.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
var _ = require('lodash');
43
const SafeString = require('handlebars').SafeString;
54
const common = require('./../lib/common');
65

@@ -9,18 +8,22 @@ function helper(paper) {
98
var sizeRegex = /^(\d+?)x(\d+?)$/g;
109
var settings = paper.themeSettings || {};
1110
var presets = settings._images;
11+
var isImageDataValid = image &&
12+
typeof image.data === 'string' &&
13+
common.isValidURL(image.data) &&
14+
image.data.includes('{:size}')
1215
var size;
1316
var width;
1417
var height;
1518

16-
if (!_.isPlainObject(image) || !_.isString(image.data)
17-
|| !common.isValidURL(image.data) || image.data.indexOf('{:size}') === -1) {
19+
if (!isImageDataValid) {
1820
// return empty string if not a valid image object
19-
defaultImageUrl = defaultImageUrl ? defaultImageUrl : '';
20-
return _.isString(image) ? image : defaultImageUrl;
21+
return image && typeof image === 'string'
22+
? image
23+
: (defaultImageUrl || '');
2124
}
2225

23-
if (_.isPlainObject(presets) && _.isPlainObject(presets[presetName])) {
26+
if (presets && presets[presetName]) {
2427
// If preset is one of the given presets in _images
2528
width = parseInt(presets[presetName].width, 10) || 5120;
2629
height = parseInt(presets[presetName].height, 10) || 5120;
@@ -42,6 +45,6 @@ function helper(paper) {
4245

4346
return new SafeString(image.data.replace('{:size}', size));
4447
});
45-
};
48+
}
4649

4750
module.exports = helper;

helpers/limit.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict';
22

3-
var _ = require('lodash');
4-
53
/**
64
* Limit an array to the second argument
75
*
@@ -10,11 +8,10 @@ var _ = require('lodash');
108
*/
119
function helper(paper) {
1210
paper.handlebars.registerHelper('limit', function (data, limit) {
13-
14-
if (_.isString(data)) {
11+
if (typeof data === 'string') {
1512
return data.substring(0, limit);
1613
}
17-
if (!_.isArray(data)) {
14+
if (!Array.isArray(data)) {
1815
return [];
1916
}
2017

helpers/money.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
'use strict';
22

3-
var _ = require('lodash');
4-
53
/**
64
* Format numbers
75
*
8-
* @param integer n: length of decimal
9-
* @param mixed s: thousands delimiter
10-
* @param mixed c: decimal delimiter
6+
* @param {number} value
7+
* @param {number} n - length of decimal
8+
* @param {string} s - thousands delimiter
9+
* @param {string} c - decimal delimiter
1110
*/
1211
function numberFormat(value, n, s, c) {
1312
var re = '\\d(?=(\\d{3})+' + (n > 0 ? '\\D' : '$') + ')',
@@ -18,9 +17,9 @@ function numberFormat(value, n, s, c) {
1817

1918
function helper(paper) {
2019
paper.handlebars.registerHelper('money', function (value) {
21-
var money = paper.settings.money;
20+
const money = paper.settings.money;
2221

23-
if (!_.isNumber(value)) {
22+
if (typeof value !== 'number') {
2423
return '';
2524
}
2625

helpers/or.js

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
2-
3-
var _ = require('lodash');
2+
const isObject = require('../lib/utils/isObject');
43

54
/**
65
* Yield block if any object within a collection matches supplied predicate
@@ -9,39 +8,22 @@ var _ = require('lodash');
98
* {{#or 1 0 0 0 0 0}} ... {{/or}}
109
*/
1110
function helper(paper) {
12-
paper.handlebars.registerHelper('or', function () {
13-
var args = [],
14-
opts,
15-
any;
16-
17-
// Translate arguments to array safely
18-
for (var i = 0; i < arguments.length; i++) {
19-
args.push(arguments[i]);
20-
}
21-
22-
// Take the last argument (content) out of testing array
23-
opts = args.pop();
11+
paper.handlebars.registerHelper('or', function (...args) {
12+
const opts = args.pop();
13+
let any;
2414

2515
// Without options hash, we check all the arguments
26-
any = _.any(args, function (arg) {
27-
if (_.isArray(arg)) {
16+
any = args.some(arg => {
17+
if (Array.isArray(arg)) {
2818
return !!arg.length;
2919
}
30-
// If an empty object is passed, arg is false
31-
else if (_.isEmpty(arg) && _.isObject(arg)) {
20+
if (isObject(arg) && !Object.keys(arg).length) {
3221
return false;
3322
}
34-
// Everything else
35-
else {
36-
return !!arg;
37-
}
23+
return !!arg;
3824
});
3925

40-
if (any) {
41-
return opts.fn(this);
42-
}
43-
44-
return opts.inverse(this);
26+
return any ? opts.fn(this) : opts.inverse(this);
4527
});
4628
}
4729

helpers/pluck.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
'use strict';
22

3-
var _ = require('lodash');
4-
53
function helper(paper) {
64
paper.handlebars.registerHelper('pluck', function (collection, path) {
7-
return _.pluck(collection, path);
5+
return collection.map(item => item[path])
86
});
97
}
108

0 commit comments

Comments
 (0)