ππ¦ hbh-proxies
hbh-proxies is a lightweight, proxy-based utility toolkit for JavaScript and Node.js that enables fluent, chainable APIs for:
- β¨ Dynamic CSS generation (CSS-in-JS)
- π Proxy-driven function pipelines
- π§© Declarative, readable code without builders or config objects
This package heavily leverages JavaScript Proxy objects to create expressive, extensible APIs with minimal boilerplate.
β‘π Installation
1npm install hbh-proxies
π¦π Package Exports
1import { Builder, CSS } from "hbh-proxies";
π€ Export Map
| Export | Description |
|---|---|
Builder |
Generic proxy-based function pipeline builder |
CSS.Builder |
Chainable CSS generator |
CSS.Minify |
Standalone CSS minifier |
CSS.Prefixers.Props |
Set of CSS properties requiring vendor prefixes |
CSS.Prefixers.Prefixes |
Vendor prefix list |
π§ βοΈ ProxyBuilder (Generic Pipeline Builder)
The ProxyBuilder allows you to create chainable function pipelines dynamically.
β¨π Basic Usage
1import { Builder } from "hbh-proxies";3const api = new Builder()4 .use("upper", s => s.toUpperCase())5 .use("exclaim", s => s + "!")6 .use("repeat", (s, n) => s.repeat(n));8api.upper.exclaim.repeat("hello", 2);9// β "HELLO!HELLO!"
ππ§© How It Works
Each chained property corresponds to a function stored in an internal map. When the proxy is invoked, each function runs in order.
1api.upper.exclaim("test")2// upper β exclaim β result
π§©π Supported Invocation Styles
βΆοΈ Normal Call
1api.upper("hello")
π· Tagged Template
1api.upper`hello world`
π§ͺ Explicit Execution
1api.upper.exclaim.done("hello")2api.upper.exclaim.run("hello")3api.upper.exclaim.pipe("hello")
β±β‘ Async Pipelines
If your functions return promises:
1api.use("delay", async s => {2 await new Promise(r => setTimeout(r, 100));3 return s + " done";4});6await api.delay.asyncDone("task");
ππ Introspection
1api.upper.exclaim.steps2// β ["upper", "exclaim"]
β οΈπ« Error Handling
Accessing an undefined method throws:
1api.unknown("test");2// Error: Method "unknown" not found.
π¨π CSSProxyBuilder (CSS-in-JS)
CSS.Builder provides a declarative, chainable API for building CSS with:
- π§± Nested selectors
- π± Media queries
- π Keyframes
- 𧬠Vendor prefixing
- π§Ή Optional minification
β¨π― Basic Example
1import { CSS } from "hbh-proxies";3const css = new CSS.Builder()4 .button({5 padding: "10px",6 background: "blue",7 color: "white"8 })9 .button.hover({10 background: "darkblue"11 })12 .all;14console.log(css);
π¨ Output
1button { padding: 10px; background: blue; color: white; }2button:hover { background: darkblue; }
ππ Selector Chaining
| Chain | Result |
|---|---|
.div.span |
div span |
.button.hover |
button:hover |
.ul.li.a |
ul li a |
Pseudo-selectors (
:hover,:active, etc.) automatically attach without spaces.
π―π§© Applying Styles
You apply styles by calling the proxy:
1.div({2 margin: "0",3 padding: "0"4})
π±π Media Queries
Media queries are declared using keys starting with @media.
1.container({2 width: "100%",3 "@media (max-width: 600px)": {4 width: "90%"5 }6})
π¨ Output
1container { width: 100%; }2@media (max-width: 600px) {3 container { width: 90%; }4}
πβ¨ Keyframes
1const css = new CSS.Builder()2 .keyframes("fadeIn", {3 from: { opacity: 0 },4 to: { opacity: 1 }5 })6 .box({7 animation: "fadeIn 1s ease-in"8 })9 .all;
π¨ Output
1box { animation: fadeIn 1s ease-in; }3@keyframes fadeIn {4 from { opacity: 0; }5 to { opacity: 1; }6}
π§¬π§ Vendor Prefixing
Vendor prefixes are automatically applied to known properties:
1.box({2 transform: "scale(1.2)"3})
π¨ Output
1box {2 -webkit-transform: scale(1.2);3 -moz-transform: scale(1.2);4 -ms-transform: scale(1.2);5 -o-transform: scale(1.2);6 transform: scale(1.2);7}
π¦ Prefix Data
1CSS.Prefixers.Props // Set of prefixed properties2CSS.Prefixers.Prefixes // ['-webkit-', '-moz-', '-ms-', '-o-']
π§Ήβ‘ Minification
βΆοΈ Enable Minification
1const css = new CSS.Builder()2 .minify()3 .card({4 padding: "10px",5 border: "1px solid #000"6 })7 .all;
π¨ Output
1card{padding:10px;border:1px solid #000}
π π§ͺ Standalone Minifier
1import { CSS } from "hbh-proxies";3CSS.Minify(`4 /* comment */5 .box {6 padding: 10px;7 margin: 0;8 }9`);
π€π Retrieving CSS
| Property | Description |
|---|---|
.css |
Last generated CSS block |
.all |
Full accumulated CSS |
String(proxy) |
Same as .all |
π Proxies
The hbh-proxies package ships with utility libraries exposed via Proxies:
1import { Proxies } from "hbh-proxies";2const { HBHChalk, HBHString, DataConverter } = Proxies;
1οΈβ£ DataConverter
Purpose: Convert between common data types (strings, numbers, arrays, booleans, JSON).
Key Methods:
| Method | Description |
|---|---|
strToArray(str) |
Converts string to array of characters. |
strToArrayByComma(str) |
Splits a string by commas into an array. |
arrayToStr(arr) |
Joins an array into a string. |
arrayToStrWithSpace(arr) |
Joins an array into a string with spaces. |
strToNum(str) |
Converts string to Number. |
strToInt(str) |
Converts string to integer using parseInt. |
numToStr(num) |
Converts number to string. |
intToFloat(n) |
Converts integer to float using parseFloat. |
parseJSON(str) |
Parses JSON string, returns null on error. |
stringifyJSON(obj) |
Converts object to JSON string. |
boolToStr(b) |
Converts boolean to string. |
strToBool(str) |
Converts string to boolean (true, 1, yes, on β true). |
Usage Example:
1DataConverter.strToArray("hello");2// β ['h','e','l','l','o']4DataConverter.strToBool("Yes");5// β true7DataConverter.parseJSON('{"a":1}');8// β { a: 1 }
2οΈβ£ HBHChalk
Purpose: Terminal string styling (colors, backgrounds, styles, RGB/Hex).
Features:
| Method / Property | Description |
|---|---|
red, green, blue⦠|
Standard ANSI colors |
brightRed, brightGreen⦠|
Bright text colors |
bgRed, bgGreen⦠|
Background colors |
hex(colorHex) |
24-bit color by hex (text) |
bgHex(colorHex) |
24-bit color by hex (background) |
rgb(r,g,b) |
Text color using RGB |
bgRgb(r,g,b) |
Background RGB color |
color256(code) |
256-color text |
bgColor256(code) |
256-color background |
bold, italic, underline, dim, inverse, strikethrough |
Text styles |
error(str) |
Shortcut: red.bold(str) |
strip(str) |
Removes all ANSI codes |
preview(str) |
Prints styled string to console |
Usage Example:
1HBHChalk.red.bold("Error!");2HBHChalk.hex("#ff8800")("Warning");3HBHChalk.bgRgb(0,128,255)("Hello Background");4HBHChalk.strip("\x1b[31mRed\x1b[0m");5// β "Red"
3οΈβ£ HBHString
Purpose: Chainable string utilities for case conversion, trimming, formatting, encoding, and more.
Key Features:
| Category | Examples |
|---|---|
| Trimming / Whitespace | trim, ltrim, rtrim, collapse |
| Case Conversion | toUpper, toLower, capitalize, uncapitalize, title, swapCase, alternatingCase |
| Casing / Naming | toCamelCase, toPascalCase, toSnakeCase, toKebabCase, slugify |
| Reversing / Padding | reverse, padStart, padEnd, padBoth, mirror, center |
| Filtering / Stripping | removeVowels, removeConsonants, onlyAlpha, onlyNumeric, onlyAlphaNumeric, stripHTML, stripSymbols, stripNonAlpha, stripNonASCII, stripPunctuation |
| Encoding / Decoding | base64, unbase64, uriEncode, uriDecode |
| Quotes / Wrapping | quote, singleQuote, parens, brackets, braces, wrap, wrapWith, encloseDouble, encloseSingle |
| Number / Digit | extractDigits, removeDigits, hasNumbers, isNumeric, toNumber |
| Fun / Creative | shout, mockingSponge, emojiWrap, binary, hex |
| String Info / Checks | length, wordCount, lineCount, isUpper, isLower, isBlank, startsWithA, endsWithZ, isSlugSafe |
| Custom Suffix / Prefix | addPrefix, addSuffix, wrapSlug |
| Markdown / HTML | markdownBold, markdownItalic, htmlBold, htmlItalic |
Usage Example:
1HBHString.toUpper("hello"); 2// β "HELLO"4HBHString.toCamelCase("hello world"); 5// β "helloWorld"7HBHString.removeVowels("beautiful"); 8// β "btfl"10HBHString.slugify("HΓ©llΓΆ WΓΆrld!"); 11// β "hello-world"13HBHString.shout("hey"); 14// β "HEY!!!"16HBHString.binary("AB"); 17// β "1000001 1000010"
These libraries are fully chainable thanks to ProxyBuilder. Each method can be called directly, or combined in a pipeline:
1HBHString.toUpper.removeVowels("hello world");2// β "HLL WRLD"4DataConverter.strToArrayByComma.strToInt(["1","2","3"]);5// β [1, 2, 3]
π§ 𧬠Internal Design Notes
- All builders are immutable
- Each chain returns a new proxy
- CSS blocks are accumulated safely
- No DOM or browser dependencies
- Works in Node.js and browsers
π§ͺπ Compatibility
- Node.js β₯ 14
- Modern browsers with
Proxysupport - ESM only (
type: modulerecommended)
ππͺͺ License
ISC Β© HBH
β π Summary
| Feature | Supported |
|---|---|
| Proxy-based chaining | β |
| CSS-in-JS | β |
| Media queries | β |
| Keyframes | β |
| Vendor prefixes | β |
| Minification | β |
| Async pipelines | β |