An object of CSS properties that don't accept values with units
92
Quality
Pending
Does it follow best practices?
Impact
92%
0.92xAverage score across 7 eval scenarios
Build a utility that normalizes CSS style objects by automatically appending 'px' units to numeric values where appropriate, while leaving unitless properties as-is.
You need to implement a function that accepts a style object (with string keys and string or numeric values) and returns a normalized style object where:
width, height, margin, padding) should have 'px' appendedflex, opacity, zIndex) should remain as numbers0 should be treated as unitless regardless of property{ flex: 1, width: 200 }, it returns { flex: 1, width: '200px' } @test{ opacity: 0.5, margin: 10, zIndex: 999 }, it returns { opacity: 0.5, margin: '10px', zIndex: 999 } @test{ lineHeight: 1.5, fontSize: 16 }, it returns { lineHeight: 1.5, fontSize: '16px' } @test{ width: 0, height: 0 }, it returns { width: 0, height: 0 } @test@generates
/**
* Normalizes a CSS style object by adding 'px' units to numeric values where appropriate.
*
* @param {Object} styles - An object with CSS property names as keys and string or numeric values
* @returns {Object} A normalized style object with units added where needed
*/
function normalizeStyles(styles) {
// IMPLEMENTATION HERE
}
module.exports = { normalizeStyles };Provides information about which CSS properties accept unitless numeric values.
Install with Tessl CLI
npx tessl i tessl/npm-emotion--unitless