Skip to content

Commit

Permalink
chore: optimize utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
quantizor committed Mar 16, 2023
1 parent 37f5b5f commit c50a8ce
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 23 deletions.
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
*.yml
dist
flow-typed
14 changes: 4 additions & 10 deletions packages/styled-components/src/sheet/GroupedTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const DefaultGroupedTag = class DefaultGroupedTag implements GroupedTag {
return index;
}

insertRules(group: number, rules: string | string[]) {
insertRules(group: number, rules: string[]) {
if (group >= this.groupSizes.length) {
const oldBuffer = this.groupSizes;
const oldSize = oldBuffer.length;
Expand All @@ -53,16 +53,10 @@ const DefaultGroupedTag = class DefaultGroupedTag implements GroupedTag {

let ruleIndex = this.indexOfGroup(group + 1);

if (Array.isArray(rules)) {
for (let i = 0, l = rules.length; i < l; i++) {
if (this.tag.insertRule(ruleIndex, rules[i])) {
this.groupSizes[group]++;
ruleIndex++;
}
}
} else {
if (this.tag.insertRule(ruleIndex, rules)) {
for (let i = 0, l = rules.length; i < l; i++) {
if (this.tag.insertRule(ruleIndex, rules[i])) {
this.groupSizes[group]++;
ruleIndex++;
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions packages/styled-components/src/sheet/Tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ export const CSSOMTag = class CSSOMTag implements Tag {
length: number;

constructor(target?: HTMLElement) {
const element = (this.element = makeStyleTag(target));
this.element = makeStyleTag(target);

// Avoid Edge bug where empty style elements don't create sheets
element.appendChild(document.createTextNode(''));
this.element.appendChild(document.createTextNode(''));

this.sheet = getSheet(element);
this.sheet = getSheet(this.element);
this.length = 0;
}

Expand All @@ -46,8 +46,9 @@ export const CSSOMTag = class CSSOMTag implements Tag {

getRule(index: number): string {
const rule = this.sheet.cssRules[index];

// Avoid IE11 quirk where cssText is inaccessible on some invalid rules
if (rule !== undefined && typeof rule.cssText === 'string') {
if (rule && rule.cssText) {
return rule.cssText;
} else {
return '';
Expand All @@ -62,8 +63,8 @@ export const TextTag = class TextTag implements Tag {
length: number;

constructor(target?: HTMLElement) {
const element = (this.element = makeStyleTag(target));
this.nodes = element.childNodes;
this.element = makeStyleTag(target);
this.nodes = this.element.childNodes;
this.length = 0;
}

Expand Down
9 changes: 3 additions & 6 deletions packages/styled-components/src/utils/isPlainObject.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
export default function isPlainObject(x: any): boolean {
export default function isPlainObject(x: any): x is Record<any, any> {
return (
x !== null &&
typeof x === 'object' &&
/* a check for empty prototype would be more typical, but that
doesn't play well with objects created in different vm contexts */
(!x.constructor || x.constructor.name === 'Object') &&
(x.toString ? x.toString() : Object.prototype.toString.call(x)) === '[object Object]' &&
x.constructor.name === Object.name &&
/* check for reasonable markers that the object isn't an element for react & preact/compat */
!('props' in x && (x.$$typeof || x.constructor === undefined))
!('props' in x && x.$$typeof)
);
}

0 comments on commit c50a8ce

Please sign in to comment.