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 type-safe CSS property validation system that can distinguish between properties that accept unitless numeric values and those that require units.
Implement a TypeScript module that provides the following functionality:
Create a type guard function isUnitlessProperty that:
Create a function formatStyleValue that:
Create a function validateStyleObject that:
any typesProvides a lookup object for CSS properties that don't accept values with units.
// File: validator.test.ts
import { isUnitlessProperty } from './validator';
// Test unitless properties
console.log(isUnitlessProperty('flex')); // Should print: true
console.log(isUnitlessProperty('opacity')); // Should print: true
console.log(isUnitlessProperty('zIndex')); // Should print: true
// Test properties that need units
console.log(isUnitlessProperty('width')); // Should print: false
console.log(isUnitlessProperty('height')); // Should print: false
console.log(isUnitlessProperty('padding')); // Should print: false// File: formatter.test.ts
import { formatStyleValue } from './validator';
// Unitless properties should not get units
console.log(formatStyleValue('flex', 1)); // Should print: 1 (or "1")
console.log(formatStyleValue('opacity', 0.5)); // Should print: 0.5 (or "0.5")
console.log(formatStyleValue('zIndex', 999)); // Should print: 999 (or "999")
// Properties with units should get 'px' appended
console.log(formatStyleValue('width', 100)); // Should print: "100px"
console.log(formatStyleValue('height', 200)); // Should print: "200px"
// Zero should not get units
console.log(formatStyleValue('width', 0)); // Should print: 0 (or "0")// File: object-validator.test.ts
import { validateStyleObject } from './validator';
const styles = {
flex: 1,
opacity: 0.8,
width: 200,
zIndex: 10,
margin: 20
};
const result = validateStyleObject(styles);
console.log(JSON.stringify(result, null, 2));
// Should print:
// {
// "flex": true,
// "opacity": true,
// "width": false,
// "zIndex": true,
// "margin": false
// }validator.ts: Main implementation file with all three functionsvalidator.test.ts: Test file demonstrating the type guardformatter.test.ts: Test file demonstrating value formattingobject-validator.test.ts: Test file demonstrating style object validationInstall with Tessl CLI
npx tessl i tessl/npm-emotion--unitless