Skip to content

Commit 2dc1b70

Browse files
author
Bart Veneman
committed
reduce bundle size (5.25kb -> 5.23) by not repeating DivisionByZero logic
1 parent fafb7dc commit 2dc1b70

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/index.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ import { hasVendorPrefix } from './vendor-prefix.js'
1616
import { isCustom, isHack, isProperty } from './properties/property-utils.js'
1717
import { OccurrenceCounter } from './occurrence-counter.js'
1818

19+
function ratio(part, total) {
20+
if (total === 0) return 0
21+
return part / total
22+
}
23+
1924
/**
2025
* Analyze CSS
2126
* @param {string} css
@@ -478,7 +483,7 @@ const analyze = (css) => {
478483
embeddedContent: assign(embeddedContent, {
479484
size: {
480485
total: embedSize,
481-
ratio: css.length === 0 ? 0 : embedSize / css.length,
486+
ratio: ratio(embedSize, css.length),
482487
},
483488
}),
484489
},
@@ -507,7 +512,7 @@ const analyze = (css) => {
507512
keyframes.count(), {
508513
prefixed: assign(
509514
prefixedKeyframes.count(), {
510-
ratio: keyframes.size() === 0 ? 0 : prefixedKeyframes.size() / keyframes.size()
515+
ratio: ratio(prefixedKeyframes.size(), keyframes.size())
511516
}),
512517
}),
513518
container: containers.count(),
@@ -517,7 +522,7 @@ const analyze = (css) => {
517522
total: totalRules,
518523
empty: {
519524
total: emptyRules,
520-
ratio: totalRules === 0 ? 0 : emptyRules / totalRules
525+
ratio: ratio(emptyRules, totalRules)
521526
},
522527
sizes: assign(
523528
ruleSizes.aggregate(),
@@ -550,7 +555,7 @@ const analyze = (css) => {
550555
selectors: {
551556
total: totalSelectors,
552557
totalUnique: totalUniqueSelectors,
553-
uniquenessRatio: totalSelectors === 0 ? 0 : totalUniqueSelectors / totalSelectors,
558+
uniquenessRatio: ratio(totalUniqueSelectors, totalSelectors),
554559
specificity: {
555560
min: minSpecificity === undefined ? [0, 0, 0] : minSpecificity,
556561
max: maxSpecificity === undefined ? [0, 0, 0] : maxSpecificity,
@@ -570,45 +575,45 @@ const analyze = (css) => {
570575
}),
571576
id: assign(
572577
ids.count(), {
573-
ratio: totalSelectors === 0 ? 0 : ids.size() / totalSelectors,
578+
ratio: ratio(ids.size(), totalSelectors),
574579
}),
575580
accessibility: assign(
576581
a11y.count(), {
577-
ratio: totalSelectors === 0 ? 0 : a11y.size() / totalSelectors,
582+
ratio: ratio(a11y.size(), totalSelectors),
578583
}),
579584
keyframes: keyframeSelectors.count(),
580585
},
581586
declarations: {
582587
total: totalDeclarations,
583588
totalUnique: totalUniqueDeclarations,
584-
uniquenessRatio: totalDeclarations === 0 ? 0 : totalUniqueDeclarations / totalDeclarations,
589+
uniquenessRatio: ratio(totalUniqueDeclarations, totalDeclarations),
585590
// @TODO: deprecated, remove in next major version
586591
unique: {
587592
total: totalUniqueDeclarations,
588-
ratio: totalDeclarations === 0 ? 0 : totalUniqueDeclarations / totalDeclarations,
593+
ratio: ratio(totalUniqueDeclarations, totalDeclarations),
589594
},
590595
importants: {
591596
total: importantDeclarations,
592-
ratio: totalDeclarations === 0 ? 0 : importantDeclarations / totalDeclarations,
597+
ratio: ratio(importantDeclarations, totalDeclarations),
593598
inKeyframes: {
594599
total: importantsInKeyframes,
595-
ratio: importantDeclarations === 0 ? 0 : importantsInKeyframes / importantDeclarations,
600+
ratio: ratio(importantsInKeyframes, importantDeclarations),
596601
},
597602
},
598603
},
599604
properties: assign(
600605
properties.count(), {
601606
prefixed: assign(
602607
propertyVendorPrefixes.count(), {
603-
ratio: properties.size() === 0 ? 0 : propertyVendorPrefixes.size() / properties.size(),
608+
ratio: ratio(propertyVendorPrefixes.size(), properties.size()),
604609
}),
605610
custom: assign(
606611
customProperties.count(), {
607-
ratio: properties.size() === 0 ? 0 : customProperties.size() / properties.size(),
612+
ratio: ratio(customProperties.size(), properties.size()),
608613
}),
609614
browserhacks: assign(
610615
propertyHacks.count(), {
611-
ratio: properties.size() === 0 ? 0 : propertyHacks.size() / properties.size(),
616+
ratio: ratio(propertyHacks.size(), properties.size()),
612617
}),
613618
complexity: propertyComplexities.aggregate(),
614619
}),

0 commit comments

Comments
 (0)