b_short
Why b_short?
- 📦 Tiny: ~68KB minified + brotli (ESM), ~73KB (CJS) with all dependencies
- ⚡ Fast: Optimized TypeScript with smart caching
- 🎯 Complete: 35+ CSS shorthands including modern features
- 🔒 Type-Safe: Full TypeScript support
- ✅ Tested: 922 tests ensuring 100% accuracy
- 🎨 Flexible: CSS strings or JS objects (camelCase for React)
- 🔄 Bidirectional: Both expand and collapse APIs
Quick Start
npm install b_short
import { expand, collapse } from 'b_short';
expand('margin: 10px 20px');
collapse(`
margin-top: 10px;
margin-right: 20px;
margin-bottom: 10px;
margin-left: 20px;
`);
collapse({ 'margin-top': '10px', 'margin-right': '10px', 'margin-bottom': '10px', 'margin-left': '10px' });
expand('background: red url(img.png)', { format: 'js' });
API
expand(css, options?)
Expand CSS shorthand properties to longhand equivalents.
import * as b from 'b_short';
const result = b.expand('background: red', {
format: b.ExpandOptions.Format.CSS,
indent: b.ExpandOptions.Indent.TWO_SPACES,
separator: b.ExpandOptions.Separator.NEWLINE,
propertyGrouping: b.ExpandOptions.PropertyGrouping.BY_PROPERTY
});
Default Options
import { expand, DEFAULT_EXPAND_OPTIONS } from 'b_short';
const customOptions = {
...DEFAULT_EXPAND_OPTIONS,
indent: 2,
format: 'js'
};
collapse(properties, options?)
Collapse longhand properties to shorthand equivalents.
import { collapse } from 'b_short';
collapse({ 'overflow-x': 'hidden', 'overflow-y': 'auto' });
collapse('overflow-x: hidden;\noverflow-y: auto;', { indent: 2 });
Options:
indent (number): Indentation level for CSS string output (default: 0)
Result Format
interface ExpandResult {
ok: boolean;
result?: string | object;
issues: Array<Error | Warning>;
}
interface CollapseResult {
ok: boolean;
result: string | object;
issues: Array<Warning>;
}
Supported Shorthands (35+)
Box Model & Layout
margin • padding • border • border-width • border-style • border-color • border-top/right/bottom/left • border-radius • inset • overflow
Visual
background (multi-layer) • mask (multi-layer) • outline • text-decoration • text-emphasis
Layout Systems
flex • flex-flow • grid • grid-area • grid-column • grid-row • place-content • place-items • place-self • columns • column-rule
Animation & Motion
animation (multi-layer) • transition (multi-layer) • offset (motion path) • contain-intrinsic-size
Typography
font • list-style
Use Cases
CSS-in-JS Libraries - Perfect for styled-components, emotion, etc.
const styles = expand('margin: 1rem; padding: 0.5rem;', { format: 'js' });
Build Tools - PostCSS plugins, webpack loaders, vite plugins
const normalized = expand(rawCSS, { format: 'css' });
Static Analysis - Linting, optimization, documentation
const { result } = expand(css, { format: 'js' });
const properties = Object.keys(result);
React Inline Styles - Direct camelCase output
const { result } = expand('margin: 1rem', { format: 'js' });
return <div style={result}>Content</div>;
Advanced Features
Multiple Declarations
Both expand and collapse support processing multiple CSS declarations at once.
expand('margin: 10px; padding: 20px; border: 1px solid red');
collapse({
'margin-top': '10px',
'margin-right': '10px',
'margin-bottom': '10px',
'margin-left': '10px',
'padding-top': '20px',
'padding-right': '20px',
'padding-bottom': '20px',
'padding-left': '20px'
});
Multi-layer Support
expand('background: url(1.png), url(2.png) repeat-x');
Property Grouping
expand('border: 1px solid red; margin: 10px', {
propertyGrouping: 'by-property'
});
expand('border: 1px solid red; margin: 10px', {
propertyGrouping: 'by-side'
});
Error Handling
const result = expand('margin: invalid');
if (!result.ok) {
console.log(result.issues);
}
Performance
- Fast: Optimized for performance with LRU caching
- Small: 89KB unminified, ~68KB minified + brotli (ESM)
- Efficient: Handles 922 test cases in <2 seconds
TypeScript Support
Full type definitions included:
import type {
ExpandOptions,
ExpandResult,
Format,
PropertyGrouping
} from 'b_short';
Development
pnpm install
pnpm test
pnpm build
pnpm lint
Contributing
Contributions welcome! See CONTRIBUTING.md
License
MIT © alphabio
Acknowledgments