Skip to content

Commit 1df2eef

Browse files
authored
feat: strf-9673 Remove lodash from paper-handlebars deps (#237)
1 parent 64688bd commit 1df2eef

13 files changed

Lines changed: 127 additions & 136 deletions

File tree

helpers/3p/utils/base.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ module.exports = {
1818
getObject: getObject[0].factory(),
1919
forOwn: require('./lib/forOwn'),
2020
merge: require('./lib/mixinDeep'),
21+
deepMatches: require('./lib/deepMatches'),
2122
};
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Recursively compare objects
3+
*/
4+
const typeOf = require("./kindOf");
5+
6+
function deepMatches(val, value) {
7+
if (typeOf(val) === "object") {
8+
if (Array.isArray(val) && Array.isArray(value)) {
9+
return matchArray(val, value);
10+
} else {
11+
return matchObject(val, value);
12+
}
13+
} else {
14+
return isMatch(val, value);
15+
}
16+
}
17+
18+
function containsMatch(array, value) {
19+
var len = array.length;
20+
var i = -1;
21+
22+
while (++i < len) {
23+
if (deepMatches(array[i], value)) {
24+
return true;
25+
}
26+
}
27+
return false;
28+
}
29+
30+
function matchArray(arr, value) {
31+
var len = value.length;
32+
var i = -1;
33+
34+
while (++i < len) {
35+
if (!containsMatch(arr, value[i])) {
36+
return false;
37+
}
38+
}
39+
return true;
40+
}
41+
42+
function matchObject(obj, value) {
43+
for (var key in value) {
44+
if (value.hasOwnProperty(key)) {
45+
if (deepMatches(obj[key], value[key]) === false) {
46+
return false;
47+
}
48+
}
49+
}
50+
return true;
51+
}
52+
53+
function isMatch(target, val) {
54+
return target === val;
55+
}
56+
57+
module.exports = deepMatches;

helpers/3p/utils/lib/makeIterator.js

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* COPY of https://raw.githubusercontent.com/jonschlinkert/make-iterator/0.3.0/index.js
33
*/
44
const typeOf = require('./kindOf');
5+
const deepMatches = require('./deepMatches');
56

67
module.exports = function makeIterator(target, thisArg) {
78
switch (typeOf(target)) {
@@ -31,60 +32,6 @@ module.exports = function makeIterator(target, thisArg) {
3132
}
3233
};
3334

34-
function containsMatch(array, value) {
35-
var len = array.length;
36-
var i = -1;
37-
38-
while (++i < len) {
39-
if (deepMatches(array[i], value)) {
40-
return true;
41-
}
42-
}
43-
return false;
44-
}
45-
46-
function matchArray(arr, value) {
47-
var len = value.length;
48-
var i = -1;
49-
50-
while (++i < len) {
51-
if (!containsMatch(arr, value[i])) {
52-
return false;
53-
}
54-
}
55-
return true;
56-
}
57-
58-
function matchObject(obj, value) {
59-
for (var key in value) {
60-
if (value.hasOwnProperty(key)) {
61-
if (deepMatches(obj[key], value[key]) === false) {
62-
return false;
63-
}
64-
}
65-
}
66-
return true;
67-
}
68-
69-
/**
70-
* Recursively compare objects
71-
*/
72-
73-
function deepMatches(val, value) {
74-
if (typeOf(val) === 'object') {
75-
if (Array.isArray(val) && Array.isArray(value)) {
76-
return matchArray(val, value);
77-
} else {
78-
return matchObject(val, value);
79-
}
80-
} else {
81-
return isMatch(val, value);
82-
}
83-
}
84-
85-
function isMatch(target, val) {
86-
return target === val;
87-
}
8835

8936
function prop(name) {
9037
return function(obj) {

helpers/all.js

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

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

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

1817
// Check if all the args are valid / truthy
19-
const result = _.every(args, function (arg) {
18+
const result = args.every( function (arg) {
2019
if (utils.isArray(arg)) {
2120
return !!arg.length;
2221
}

helpers/any.js

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

3-
const _ = require('lodash');
43
const utils = require('./3p/utils');
4+
const deepMatches = require('./3p/utils/lib/deepMatches');
55

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

2020
if (!utils.isEmpty(predicate)) {
21-
// With options hash, we check the contents of first argument
22-
any = _.some(args[0], predicate);
21+
if (utils.isArray(args[0])) {
22+
// With options hash, we check the contents of first argument
23+
any = args[0].some((v) => deepMatches(v, predicate));
24+
}
2325
} else {
2426
// DEPRECATED: Moved to #or helper
2527
// Without options hash, we check all the arguments
26-
any = _.some(args, function (arg) {
28+
any = args.some(function (arg) {
2729
if (utils.isArray(arg)) {
2830
return !!arg.length;
2931
}

helpers/contains.js

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

3-
const _ = require('lodash');
3+
4+
const utils = require('./3p/utils');
45

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

1719
// Yield block if true
1820
if (contained) {

helpers/deprecated/pick.js

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

3-
const _ = require('lodash');
43

54
const factory = () => {
65
/**
76
* @deprecate
87
*/
9-
return function() {
10-
return _.pick.apply(null, arguments);
8+
return function(...args) {
9+
const target = args.shift();
10+
const toReturn = {};
11+
const paths = args[0];
12+
if (paths) {
13+
paths.forEach((key) => {
14+
if (target.hasOwnProperty(key)) {
15+
toReturn[key] = target[key];
16+
}
17+
})
18+
}
19+
20+
return toReturn;
1121
};
1222
};
1323

helpers/lib/fonts.js

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

3-
const _ = require('lodash');
43
const URL = require('url')
4+
const utils = require('../3p/utils');
55

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

@@ -24,30 +24,32 @@ const fontProviders = {
2424
var collection = [],
2525
familyHash = {};
2626

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

32-
if (_.isEmpty(familyKey)) {
33+
if (utils.isEmpty(familyKey)) {
3334
return;
3435
}
3536

36-
if (_.isUndefined(weights)) {
37+
if (utils.isUndefined(weights)) {
3738
weights = '';
3839
}
3940

40-
if (!_.isArray(familyHash[familyKey])) {
41+
if (!utils.isArray(familyHash[familyKey])) {
4142
familyHash[familyKey] = [];
4243
}
4344

4445
weights = weights.split(',');
4546

4647
familyHash[familyKey].push(weights);
47-
familyHash[familyKey] = _.uniq(_.flatten(familyHash[familyKey]));
48+
familyHash[familyKey] = [...new Set(familyHash[familyKey].flat())]
4849
});
4950

50-
_.each(familyHash, function fontHashIterator(weights, family) {
51+
Object.keys(familyHash).forEach(function fontHashIterator(family) {
52+
const weights = familyHash[family];
5153
collection.push(family + ':' + weights.join(','));
5254
});
5355

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

113115
const collectedFonts = {};
114-
_.each(themeSettings, function (value, key) {
116+
Object.keys(themeSettings).forEach(function (key) {
115117
//check that -font is on end of string but not start of string
116118
const fontKeySuffix = '-font';
117119
if (!key.endsWith(fontKeySuffix)) {
118120
return;
119121
}
120122

121-
const splits = value.split('_');
123+
const splits = themeSettings[key].split('_');
122124
const provider = splits[0];
123125

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

132-
collectedFonts[provider].push(value);
134+
collectedFonts[provider].push(themeSettings[key]);
133135
});
134136

135137
// Parse font strings based on provider
136-
const parsedFonts = _.mapValues(collectedFonts, function (value, key) {
137-
return fontProviders[key].parser(value);
138+
const parsedFonts = {};
139+
Object.keys(collectedFonts).forEach(function (key) {
140+
parsedFonts[key] = fontProviders[key].parser(collectedFonts[key]);
138141
});
139142

140143
// Format output based on requested format
141144
switch (format) {
142145
case 'linkElements':
143-
144-
const formattedFonts = _.mapValues(parsedFonts, function (value, key) {
145-
fontProviders[key].generateResourceHints(options.globals, value, options.fontDisplay);
146-
return fontProviders[key].buildLink(value, options.fontDisplay);
146+
const formattedFonts = {};
147+
Object.keys(parsedFonts).forEach(function (key) {
148+
fontProviders[key].generateResourceHints(options.globals, parsedFonts[key], options.fontDisplay);
149+
formattedFonts[key] = fontProviders[key].buildLink(parsedFonts[key], options.fontDisplay);
147150
});
148-
return new handlebars.SafeString(_.values(formattedFonts).join(''));
151+
return new handlebars.SafeString(Object.values(formattedFonts).join(''));
149152

150153
case 'webFontLoaderConfig':
151154
// Build configs
152-
const configs = _.mapValues(parsedFonts, function (value, key) {
153-
return fontProviders[key].buildFontLoaderConfig(value);
155+
const configs = {};
156+
Object.keys(parsedFonts).forEach(function (key) {
157+
configs[key] = fontProviders[key].buildFontLoaderConfig(parsedFonts[key]);
154158
});
155159

156160
// Merge them
157161
const fontLoaderConfig = {};
158-
_.each(configs, function (config) {
159-
return Object.assign(fontLoaderConfig, config);
162+
Object.values(configs).forEach((config) => {
163+
Object.assign(fontLoaderConfig, config);
160164
});
161165
return fontLoaderConfig;
162166

helpers/or.js

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

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

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

1817
// Evaluate all args in args array to see if any are truthy
19-
const any = _.some(args, function (arg) {
18+
const any = args.some(function (arg) {
2019
if (utils.isArray(arg)) {
2120
return !!arg.length;
2221
}

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
const factory = () => {
64
return function(collection, path) {
7-
return _.map(collection, item => item.hasOwnProperty(path) ? item[path] : undefined);
5+
return collection.map(item => item.hasOwnProperty(path) ? item[path] : undefined);
86
};
97
};
108

0 commit comments

Comments
 (0)