Skip to content

Commit

Permalink
feat: strf-9673 Remove lodash from paper-handlebars deps (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
jairo-bc authored Feb 1, 2023
1 parent 64688bd commit 1df2eef
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 136 deletions.
1 change: 1 addition & 0 deletions helpers/3p/utils/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ module.exports = {
getObject: getObject[0].factory(),
forOwn: require('./lib/forOwn'),
merge: require('./lib/mixinDeep'),
deepMatches: require('./lib/deepMatches'),
};
57 changes: 57 additions & 0 deletions helpers/3p/utils/lib/deepMatches.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Recursively compare objects
*/
const typeOf = require("./kindOf");

function deepMatches(val, value) {
if (typeOf(val) === "object") {
if (Array.isArray(val) && Array.isArray(value)) {
return matchArray(val, value);
} else {
return matchObject(val, value);
}
} else {
return isMatch(val, value);
}
}

function containsMatch(array, value) {
var len = array.length;
var i = -1;

while (++i < len) {
if (deepMatches(array[i], value)) {
return true;
}
}
return false;
}

function matchArray(arr, value) {
var len = value.length;
var i = -1;

while (++i < len) {
if (!containsMatch(arr, value[i])) {
return false;
}
}
return true;
}

function matchObject(obj, value) {
for (var key in value) {
if (value.hasOwnProperty(key)) {
if (deepMatches(obj[key], value[key]) === false) {
return false;
}
}
}
return true;
}

function isMatch(target, val) {
return target === val;
}

module.exports = deepMatches;
55 changes: 1 addition & 54 deletions helpers/3p/utils/lib/makeIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* COPY of https://raw.githubusercontent.com/jonschlinkert/make-iterator/0.3.0/index.js
*/
const typeOf = require('./kindOf');
const deepMatches = require('./deepMatches');

module.exports = function makeIterator(target, thisArg) {
switch (typeOf(target)) {
Expand Down Expand Up @@ -31,60 +32,6 @@ module.exports = function makeIterator(target, thisArg) {
}
};

function containsMatch(array, value) {
var len = array.length;
var i = -1;

while (++i < len) {
if (deepMatches(array[i], value)) {
return true;
}
}
return false;
}

function matchArray(arr, value) {
var len = value.length;
var i = -1;

while (++i < len) {
if (!containsMatch(arr, value[i])) {
return false;
}
}
return true;
}

function matchObject(obj, value) {
for (var key in value) {
if (value.hasOwnProperty(key)) {
if (deepMatches(obj[key], value[key]) === false) {
return false;
}
}
}
return true;
}

/**
* Recursively compare objects
*/

function deepMatches(val, value) {
if (typeOf(val) === 'object') {
if (Array.isArray(val) && Array.isArray(value)) {
return matchArray(val, value);
} else {
return matchObject(val, value);
}
} else {
return isMatch(val, value);
}
}

function isMatch(target, val) {
return target === val;
}

function prop(name) {
return function(obj) {
Expand Down
3 changes: 1 addition & 2 deletions helpers/all.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const _ = require('lodash');
const utils = require('./3p/utils');

/**
Expand All @@ -16,7 +15,7 @@ const factory = () => {
const opts = args.pop();

// Check if all the args are valid / truthy
const result = _.every(args, function (arg) {
const result = args.every( function (arg) {
if (utils.isArray(arg)) {
return !!arg.length;
}
Expand Down
10 changes: 6 additions & 4 deletions helpers/any.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const _ = require('lodash');
const utils = require('./3p/utils');
const deepMatches = require('./3p/utils/lib/deepMatches');

/**
* Yield block if any object within a collection matches supplied predicate
Expand All @@ -18,12 +18,14 @@ const factory = () => {
const predicate = opts.hash;

if (!utils.isEmpty(predicate)) {
// With options hash, we check the contents of first argument
any = _.some(args[0], predicate);
if (utils.isArray(args[0])) {
// With options hash, we check the contents of first argument
any = args[0].some((v) => deepMatches(v, predicate));
}
} else {
// DEPRECATED: Moved to #or helper
// Without options hash, we check all the arguments
any = _.some(args, function (arg) {
any = args.some(function (arg) {
if (utils.isArray(arg)) {
return !!arg.length;
}
Expand Down
6 changes: 4 additions & 2 deletions helpers/contains.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const _ = require('lodash');

const utils = require('./3p/utils');

/**
* Is any value included in a collection or a string?
Expand All @@ -12,7 +13,8 @@ const _ = require('lodash');
const factory = () => {
return function(container, value) {
const options = arguments[arguments.length - 1];
const contained = _.includes(container, value);
const preparedContainer = utils.isObject(container) ? Object.values(container) : container;
const contained = preparedContainer.includes(value);

// Yield block if true
if (contained) {
Expand Down
16 changes: 13 additions & 3 deletions helpers/deprecated/pick.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
'use strict';

const _ = require('lodash');

const factory = () => {
/**
* @deprecate
*/
return function() {
return _.pick.apply(null, arguments);
return function(...args) {
const target = args.shift();
const toReturn = {};
const paths = args[0];
if (paths) {
paths.forEach((key) => {
if (target.hasOwnProperty(key)) {
toReturn[key] = target[key];
}
})
}

return toReturn;
};
};

Expand Down
46 changes: 25 additions & 21 deletions helpers/lib/fonts.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const _ = require('lodash');
const URL = require('url')
const utils = require('../3p/utils');

const {resourceHintAllowedTypes, addResourceHint, defaultResourceHintState} = require('../lib/resourceHints');

Expand All @@ -24,30 +24,32 @@ const fontProviders = {
var collection = [],
familyHash = {};

_.each(fonts, function fontsIterator(font) {
Object.keys(fonts).forEach(function fontsIterator(key) {
const font = fonts[key];
var split = font.split('_'),
familyKey = split[1], // Eg: Open+Sans
weights = split[2]; // Eg: 400,700italic

if (_.isEmpty(familyKey)) {
if (utils.isEmpty(familyKey)) {
return;
}

if (_.isUndefined(weights)) {
if (utils.isUndefined(weights)) {
weights = '';
}

if (!_.isArray(familyHash[familyKey])) {
if (!utils.isArray(familyHash[familyKey])) {
familyHash[familyKey] = [];
}

weights = weights.split(',');

familyHash[familyKey].push(weights);
familyHash[familyKey] = _.uniq(_.flatten(familyHash[familyKey]));
familyHash[familyKey] = [...new Set(familyHash[familyKey].flat())]
});

_.each(familyHash, function fontHashIterator(weights, family) {
Object.keys(familyHash).forEach(function fontHashIterator(family) {
const weights = familyHash[family];
collection.push(family + ':' + weights.join(','));
});

Expand Down Expand Up @@ -111,14 +113,14 @@ const fontProviders = {
module.exports = function (format, themeSettings, handlebars, options) {

const collectedFonts = {};
_.each(themeSettings, function (value, key) {
Object.keys(themeSettings).forEach(function (key) {
//check that -font is on end of string but not start of string
const fontKeySuffix = '-font';
if (!key.endsWith(fontKeySuffix)) {
return;
}

const splits = value.split('_');
const splits = themeSettings[key].split('_');
const provider = splits[0];

if (typeof fontProviders[provider] === 'undefined') {
Expand All @@ -129,34 +131,36 @@ module.exports = function (format, themeSettings, handlebars, options) {
collectedFonts[provider] = [];
}

collectedFonts[provider].push(value);
collectedFonts[provider].push(themeSettings[key]);
});

// Parse font strings based on provider
const parsedFonts = _.mapValues(collectedFonts, function (value, key) {
return fontProviders[key].parser(value);
const parsedFonts = {};
Object.keys(collectedFonts).forEach(function (key) {
parsedFonts[key] = fontProviders[key].parser(collectedFonts[key]);
});

// Format output based on requested format
switch (format) {
case 'linkElements':

const formattedFonts = _.mapValues(parsedFonts, function (value, key) {
fontProviders[key].generateResourceHints(options.globals, value, options.fontDisplay);
return fontProviders[key].buildLink(value, options.fontDisplay);
const formattedFonts = {};
Object.keys(parsedFonts).forEach(function (key) {
fontProviders[key].generateResourceHints(options.globals, parsedFonts[key], options.fontDisplay);
formattedFonts[key] = fontProviders[key].buildLink(parsedFonts[key], options.fontDisplay);
});
return new handlebars.SafeString(_.values(formattedFonts).join(''));
return new handlebars.SafeString(Object.values(formattedFonts).join(''));

case 'webFontLoaderConfig':
// Build configs
const configs = _.mapValues(parsedFonts, function (value, key) {
return fontProviders[key].buildFontLoaderConfig(value);
const configs = {};
Object.keys(parsedFonts).forEach(function (key) {
configs[key] = fontProviders[key].buildFontLoaderConfig(parsedFonts[key]);
});

// Merge them
const fontLoaderConfig = {};
_.each(configs, function (config) {
return Object.assign(fontLoaderConfig, config);
Object.values(configs).forEach((config) => {
Object.assign(fontLoaderConfig, config);
});
return fontLoaderConfig;

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

const _ = require('lodash');
const utils = require('./3p/utils');

/**
Expand All @@ -16,7 +15,7 @@ const factory = () => {
const opts = args.pop();

// Evaluate all args in args array to see if any are truthy
const any = _.some(args, function (arg) {
const any = args.some(function (arg) {
if (utils.isArray(arg)) {
return !!arg.length;
}
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');

const factory = () => {
return function(collection, path) {
return _.map(collection, item => item.hasOwnProperty(path) ? item[path] : undefined);
return collection.map(item => item.hasOwnProperty(path) ? item[path] : undefined);
};
};

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"date.js": "^0.3.3",
"handlebars": "3.0.8",
"he": "^1.2.0",
"lodash": "^4.17.21",
"moment": "^2.29.4",
"remarkable": "^2.0.1",
"stringz": "^0.1.1"
Expand Down
Loading

0 comments on commit 1df2eef

Please sign in to comment.