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

STRF-8652 eliminate lodash from paper 2.x #212

Open
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
35 changes: 9 additions & 26 deletions helpers/all.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var _ = require('lodash');
const isObject = require('../lib/utils/isObject');

/**
* Yield block only if all arguments are valid
Expand All @@ -9,39 +8,23 @@ var _ = require('lodash');
* {{#all items theme_settings.optionA theme_settings.optionB}} ... {{/all}}
*/
function helper(paper) {
paper.handlebars.registerHelper('all', function () {

var args = [], opts, result;

// Translate arguments to array safely
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}

// Take the last argument (content) out of testing array
opts = args.pop();
paper.handlebars.registerHelper('all', function (...args) {
const opts = args.pop();
let result;

// Check if all the arguments are valid / truthy
result = _.all(args, function (arg) {
if (_.isArray(arg)) {
result = args.every(arg => {
if (Array.isArray(arg)) {
return !!arg.length;
}
// If an empty object is passed, arg is false
else if (_.isEmpty(arg) && _.isObject(arg)) {
if (isObject(arg) && !Object.keys(arg).length) {
return false;
}
// Everything else
else {
return !!arg;
}
return !!arg;
});

// If everything was valid, then "all" condition satisfied
if (result) {
return opts.fn(this);
} else {
return opts.inverse(this);
}
return result ? opts.fn(this) : opts.inverse(this);
});
}

Expand Down
49 changes: 15 additions & 34 deletions helpers/any.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var _ = require('lodash');
const isObject = require('../lib/utils/isObject');
const isMatch = require("../lib/utils/isMatch");

/**
* Yield block if any object within a collection matches supplied predicate
Expand All @@ -9,48 +9,29 @@ var _ = require('lodash');
* {{#any items selected=true}} ... {{/any}}
*/
function helper(paper) {
paper.handlebars.registerHelper('any', function () {

var args = [],
opts,
predicate,
any;

// Translate arguments to array safely
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}

// Take the last argument (content) out of testing array
opts = args.pop();
predicate = opts.hash;

if (!_.isEmpty(predicate)) {
// With options hash, we check the contents of first argument
any = _.any(args[0], predicate);
paper.handlebars.registerHelper('any', function (...args) {
const opts = args.pop();
let any;

// With options hash, we check the contents of first argument
if (opts.hash && Object.keys(opts.hash).length) {
// This works fine for both arrays and objects
any = isObject(args[0]) && Object.values(args[0]).some(item => isMatch(item, opts.hash));
} else {
// DEPRECATED: Moved to #or helper
// Without options hash, we check all the arguments
any = _.any(args, function (arg) {
if (_.isArray(arg)) {
any = args.some(arg => {
if (Array.isArray(arg)) {
return !!arg.length;
}
// If an empty object is passed, arg is false
else if (_.isEmpty(arg) && _.isObject(arg)) {
if (isObject(arg) && !Object.keys(arg).length) {
return false;
}
// Everything else
else {
return !!arg;
}
return !!arg;
});
}

if (any) {
return opts.fn(this);
}

return opts.inverse(this);
return any ? opts.fn(this) : opts.inverse(this);
});
}

Expand Down
15 changes: 5 additions & 10 deletions helpers/contains.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var _ = require('lodash');
const includes = require('../lib/utils/includes');

/**
* Is any value included in a collection or a string?
Expand All @@ -10,17 +10,12 @@ var _ = require('lodash');
* {{#contains font_path "Roboto"}} ... {{/contains}}
*/
function helper(paper) {
paper.handlebars.registerHelper('contains', function () {
var args = Array.prototype.slice.call(arguments, 0, -1),
options = _.last(arguments),
contained = _.contains.apply(_, args);
paper.handlebars.registerHelper('contains', function (...args) {
const options = args.pop();
const contained = includes(...args);
MaxGenash marked this conversation as resolved.
Show resolved Hide resolved

// Yield block if true
if (contained) {
return options.fn(this);
} else {
return options.inverse(this);
}
return contained ? options.fn(this) : options.inverse(this);
});
}

Expand Down
6 changes: 3 additions & 3 deletions helpers/deprecated.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

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

function helper(paper) {
paper.handlebars.registerHelper('pick', function () {
return _.pick.apply(null, arguments);
paper.handlebars.registerHelper('pick', function (...args) {
return pickBy(...args);
});

/**
Expand Down
19 changes: 8 additions & 11 deletions helpers/for.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
'use strict';

var _ = require('lodash');
const isObject = require('../lib/utils/isObject');

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

function isOptions(obj) {
return _.isObject(obj) && obj.fn;
return obj && obj.fn;
}

if (isOptions(to)) {
context = {};
to = from;
from = 1;

} else if (isOptions(context)) {
if (_.isObject(to)) {
context = to;
to = from;
from = 1;
}
} else if (isOptions(context) && isObject(to)) {
context = to;
to = from;
from = 1;
}

if (to < from) {
Expand All @@ -36,7 +33,7 @@ function helper(paper) {
to = from + maxIterations - 1;
}

for (var i = from; i <= to; i++) {
for (let i = from; i <= to; i++) {
context.$index = i;
output += options.fn(context);
}
Expand Down
17 changes: 10 additions & 7 deletions helpers/getImage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var _ = require('lodash');
const SafeString = require('handlebars').SafeString;
const common = require('./../lib/common');

Expand All @@ -9,18 +8,22 @@ function helper(paper) {
var sizeRegex = /^(\d+?)x(\d+?)$/g;
var settings = paper.themeSettings || {};
var presets = settings._images;
var isImageDataValid = image &&
typeof image.data === 'string' &&
common.isValidURL(image.data) &&
image.data.includes('{:size}')
var size;
var width;
var height;

if (!_.isPlainObject(image) || !_.isString(image.data)
|| !common.isValidURL(image.data) || image.data.indexOf('{:size}') === -1) {
if (!isImageDataValid) {
// return empty string if not a valid image object
defaultImageUrl = defaultImageUrl ? defaultImageUrl : '';
return _.isString(image) ? image : defaultImageUrl;
return image && typeof image === 'string'
? image
: (defaultImageUrl || '');
}

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

return new SafeString(image.data.replace('{:size}', size));
});
};
}

module.exports = helper;
7 changes: 2 additions & 5 deletions helpers/limit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

var _ = require('lodash');

/**
* Limit an array to the second argument
*
Expand All @@ -10,11 +8,10 @@ var _ = require('lodash');
*/
function helper(paper) {
paper.handlebars.registerHelper('limit', function (data, limit) {

if (_.isString(data)) {
if (typeof data === 'string') {
return data.substring(0, limit);
}
if (!_.isArray(data)) {
if (!Array.isArray(data)) {
return [];
}

Expand Down
13 changes: 6 additions & 7 deletions helpers/money.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
'use strict';

var _ = require('lodash');

/**
* Format numbers
*
* @param integer n: length of decimal
* @param mixed s: thousands delimiter
* @param mixed c: decimal delimiter
* @param {number} value
MaxGenash marked this conversation as resolved.
Show resolved Hide resolved
* @param {number} n - length of decimal
* @param {string} s - thousands delimiter
* @param {string} c - decimal delimiter
*/
function numberFormat(value, n, s, c) {
var re = '\\d(?=(\\d{3})+' + (n > 0 ? '\\D' : '$') + ')',
Expand All @@ -18,9 +17,9 @@ function numberFormat(value, n, s, c) {

function helper(paper) {
paper.handlebars.registerHelper('money', function (value) {
var money = paper.settings.money;
const money = paper.settings.money;

if (!_.isNumber(value)) {
if (typeof value !== 'number') {
return '';
}

Expand Down
36 changes: 9 additions & 27 deletions helpers/or.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var _ = require('lodash');
const isObject = require('../lib/utils/isObject');

/**
* Yield block if any object within a collection matches supplied predicate
Expand All @@ -9,39 +8,22 @@ var _ = require('lodash');
* {{#or 1 0 0 0 0 0}} ... {{/or}}
*/
function helper(paper) {
paper.handlebars.registerHelper('or', function () {
var args = [],
opts,
any;

// Translate arguments to array safely
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}

// Take the last argument (content) out of testing array
opts = args.pop();
paper.handlebars.registerHelper('or', function (...args) {
const opts = args.pop();
let any;

// Without options hash, we check all the arguments
any = _.any(args, function (arg) {
if (_.isArray(arg)) {
any = args.some(arg => {
if (Array.isArray(arg)) {
return !!arg.length;
}
// If an empty object is passed, arg is false
else if (_.isEmpty(arg) && _.isObject(arg)) {
if (isObject(arg) && !Object.keys(arg).length) {
return false;
}
// Everything else
else {
return !!arg;
}
return !!arg;
});

if (any) {
return opts.fn(this);
}

return opts.inverse(this);
return any ? opts.fn(this) : opts.inverse(this);
});
}

Expand Down
4 changes: 1 addition & 3 deletions helpers/pluck.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

var _ = require('lodash');

function helper(paper) {
paper.handlebars.registerHelper('pluck', function (collection, path) {
return _.pluck(collection, path);
return collection.map(item => item[path])
});
}

Expand Down
Loading