Skip to content

Commit

Permalink
got rid of lodash for the meta-builder
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeeCoder committed Mar 30, 2018
1 parent afe2758 commit ea4e409
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/container-query-meta-builder/.size-limit
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[
{
path: "dist/bundle.cjs.js",
limit: "6.4KB"
limit: "1.7KB"
},
{
path: "dist/bundle.esm.js",
limit: "6.4KB"
limit: "1.7KB"
}
]
2 changes: 1 addition & 1 deletion packages/container-query-meta-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"build:cjs": "BABEL_ENV=production rollup -c rollup/rollup.cjs.js",
"build": "npm-run-all build:*",
"size": "size-limit",
"why-size": "size-limit --why",
"prepublish": "yarn build"
},
"devDependencies": {
Expand All @@ -34,7 +35,6 @@
"size-limit": "^0.15.1"
},
"dependencies": {
"lodash": "^4.17.4",
"object-assign": "^4.1.1"
},
"keywords": [
Expand Down
45 changes: 38 additions & 7 deletions packages/container-query-meta-builder/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,38 @@ export const QUERIES = "f";
// export const SELECTOR = "selector";
// export const QUERIES = "queries";

const isEmpty = obj => Object.keys(obj).length === 0;

const sameCondition = (cond1, cond2) => {
if (!Array.isArray(cond1) || !Array.isArray(cond2)) {
return cond1 === cond2;
}

if (cond1.length !== cond2.length) {
return false;
}

cond1 = cond1[0];
cond2 = cond2[0];

if (cond1.length !== cond2.length) {
return false;
}

const outerArrLength = cond1.length;
for (let i = 0; i < outerArrLength; i++) {
const innerArrLength = cond1[i].length;

for (let j = 0; j < innerArrLength; j++) {
if (cond1[i][j] !== cond2[i][j]) {
return false;
}
}
}

return true;
};

const getElementData = (queries, conditions = null, selector = null) => {
const newElementData = {};

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

if (
(!query[CONDITIONS] && !conditions) ||
_.isEqual(query[CONDITIONS], conditions)
sameCondition(query[CONDITIONS], conditions)
) {
const elementsLength = query[ELEMENTS].length;
for (let j = 0; j < elementsLength; j++) {
Expand Down Expand Up @@ -75,7 +107,7 @@ export default class MetaBuilder {
}

flush() {
if (_.isEmpty(this.current.styles) && _.isEmpty(this.current.values)) {
if (isEmpty(this.current.styles) && isEmpty(this.current.values)) {
// nothing to flush
return;
}
Expand All @@ -87,15 +119,15 @@ export default class MetaBuilder {
);

// Merge new styles to the stored element data
if (!_.isEmpty(this.current.styles)) {
if (!isEmpty(this.current.styles)) {
if (!storedElementData[STYLES]) {
storedElementData[STYLES] = {};
}

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

if (!_.isEmpty(this.current.values)) {
if (!isEmpty(this.current.values)) {
if (!storedElementData[VALUES]) {
storedElementData[VALUES] = {};
}
Expand All @@ -113,12 +145,11 @@ export default class MetaBuilder {
const style = [];

if (typeof node === "string") {
const matches = node.match(/([^:]+):(.+)/);
if (!matches) {
const parts = node.match(/ *([^:]+): *(.+)/);
if (!parts) {
throw new Error(`Invalid CSS declaration format: "${node}"`);
}

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

0 comments on commit ea4e409

Please sign in to comment.