Skip to content

Commit ea4e409

Browse files
committed
got rid of lodash for the meta-builder
1 parent afe2758 commit ea4e409

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[
22
{
33
path: "dist/bundle.cjs.js",
4-
limit: "6.4KB"
4+
limit: "1.7KB"
55
},
66
{
77
path: "dist/bundle.esm.js",
8-
limit: "6.4KB"
8+
limit: "1.7KB"
99
}
1010
]

packages/container-query-meta-builder/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"build:cjs": "BABEL_ENV=production rollup -c rollup/rollup.cjs.js",
1717
"build": "npm-run-all build:*",
1818
"size": "size-limit",
19+
"why-size": "size-limit --why",
1920
"prepublish": "yarn build"
2021
},
2122
"devDependencies": {
@@ -34,7 +35,6 @@
3435
"size-limit": "^0.15.1"
3536
},
3637
"dependencies": {
37-
"lodash": "^4.17.4",
3838
"object-assign": "^4.1.1"
3939
},
4040
"keywords": [

packages/container-query-meta-builder/src/index.js

+38-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,38 @@ export const QUERIES = "f";
1717
// export const SELECTOR = "selector";
1818
// export const QUERIES = "queries";
1919

20+
const isEmpty = obj => Object.keys(obj).length === 0;
21+
22+
const sameCondition = (cond1, cond2) => {
23+
if (!Array.isArray(cond1) || !Array.isArray(cond2)) {
24+
return cond1 === cond2;
25+
}
26+
27+
if (cond1.length !== cond2.length) {
28+
return false;
29+
}
30+
31+
cond1 = cond1[0];
32+
cond2 = cond2[0];
33+
34+
if (cond1.length !== cond2.length) {
35+
return false;
36+
}
37+
38+
const outerArrLength = cond1.length;
39+
for (let i = 0; i < outerArrLength; i++) {
40+
const innerArrLength = cond1[i].length;
41+
42+
for (let j = 0; j < innerArrLength; j++) {
43+
if (cond1[i][j] !== cond2[i][j]) {
44+
return false;
45+
}
46+
}
47+
}
48+
49+
return true;
50+
};
51+
2052
const getElementData = (queries, conditions = null, selector = null) => {
2153
const newElementData = {};
2254

@@ -30,7 +62,7 @@ const getElementData = (queries, conditions = null, selector = null) => {
3062

3163
if (
3264
(!query[CONDITIONS] && !conditions) ||
33-
_.isEqual(query[CONDITIONS], conditions)
65+
sameCondition(query[CONDITIONS], conditions)
3466
) {
3567
const elementsLength = query[ELEMENTS].length;
3668
for (let j = 0; j < elementsLength; j++) {
@@ -75,7 +107,7 @@ export default class MetaBuilder {
75107
}
76108

77109
flush() {
78-
if (_.isEmpty(this.current.styles) && _.isEmpty(this.current.values)) {
110+
if (isEmpty(this.current.styles) && isEmpty(this.current.values)) {
79111
// nothing to flush
80112
return;
81113
}
@@ -87,15 +119,15 @@ export default class MetaBuilder {
87119
);
88120

89121
// Merge new styles to the stored element data
90-
if (!_.isEmpty(this.current.styles)) {
122+
if (!isEmpty(this.current.styles)) {
91123
if (!storedElementData[STYLES]) {
92124
storedElementData[STYLES] = {};
93125
}
94126

95127
objectAssign(storedElementData[STYLES], this.current.styles);
96128
}
97129

98-
if (!_.isEmpty(this.current.values)) {
130+
if (!isEmpty(this.current.values)) {
99131
if (!storedElementData[VALUES]) {
100132
storedElementData[VALUES] = {};
101133
}
@@ -113,12 +145,11 @@ export default class MetaBuilder {
113145
const style = [];
114146

115147
if (typeof node === "string") {
116-
const matches = node.match(/([^:]+):(.+)/);
117-
if (!matches) {
148+
const parts = node.match(/ *([^:]+): *(.+)/);
149+
if (!parts) {
118150
throw new Error(`Invalid CSS declaration format: "${node}"`);
119151
}
120152

121-
const parts = matches.map(part => _.trim(part));
122153
style.push(parts[1], parts[2]);
123154
} else if (
124155
typeof node === "object" &&

0 commit comments

Comments
 (0)