IcssToTs
Convert ICSS module exports to strongly typed TypeScript objects
☄️ Bug reports / feature requests »
Table Of Contents
⬆
👋 About The Project
This project aims to provide a simple way of working with strongly typed objects that were exported from ICSS modules.
Given an ICSS module like this:
:export {
foo: red;
bar-baz: 255, 255, 255;
}
A TypeScript object conforming to this interface will be returned when using the string- and the RGB-parser respectively:
interface MyIcssVariables {
readonly foo: string;
readonly bar: {
readonly baz: {
readonly red: number;
readonly green: number;
readonly blue: number;
},
};
}
⬆
🚀 Installation
Bun
bun add icss-to-ts
Yarn
yarn add icss-to-ts
PNPM
pnpm add icss-to-ts
NPM
npm i icss-to-ts
⬆
👀 Usage
import icssToTs from 'icss-to-ts';
import { icssToTs } from 'icss-to-ts';
:export {
foo: red;
bar-baz: 255, 255, 255;
}
:export {
foo: red;
bar: {
baz: 255, 255, 255;
};
}
import { icssToTs, parsers } from 'icss-to-ts';
import icssExports from './path/to/icss-file.module.css';
const css = icssToTs(icssExports, {
foo: parsers.asString,
bar: {
baz: parsers.asRgb,
},
});
console.log(css);
⬆
💻 API
🏠 Built-in Parsers
asString
Strips surrounding single or double quotes from the value if present.
asString('foo');
asString('\'foo\'');
asString('"foo"');
Note: All of the below parsers internally call to asString
before further processing.
asNumber
Ensures that the given value is a finite number.
asNumber('0');
asNumber('"1"');
asNumber('\'1.21\'');
asNumber('1px');
asNumber('Infinity');
asNumber('NaN');
asBoolean
Ensures that the given value is a representation of a boolean.
asBoolean('0');
asBoolean('"1"');
asBoolean('false');
asBoolean('true');
asBoolean('');
asBoolean('2');
asBoolean('foo');
asUnit
Extracts numbers from values with a specific unit.
Some unit parsers for the most common use cases are packaged with this library.
Note: Uses asNumber
internally.
asPx('10px');
asRem('10rem');
asEm('10em');
asPercent('10%');
asVh('10vh');
asVw('10vw');
asDvh('10dvh');
asDvw('10dvw');
asPx('10pix');
asRem('10');
asUnit('vmax')('50vmax');
asUnit('vmin')('50vmin');
asUnit('vmin')('50vmax');
asTuple
Parses a one-dimensional array from a space- and/or comma-separated list.
asTuple('0 1 2');
asTuple('0, 1, 2');
asTuple('0, 1 2');
asTuple('"0, 1, 2"');
asTuple('foo bar baz');
asHex
Ensures that the given value is a valid hex color.
Two separate parsers are available: One for a string and one for a number representation.
asHexString('#E1E1E1');
asHexNumber('#E1E1E1');
asHexNumber('E1E1E1');
asHexNumber('#E1E1E1E1');
asHexNumber('#FFFFFG');
asHexa
Ensures that the given value is a valid hexa color.
Two separate parsers are available: One for a string and one for a number representation.
asHexaString('#E1E1E1E1');
asHexaNumber('#E1E1E1E1');
asHexaNumber('E1E1E1E1');
asHexaNumber('#E1E1E1E1E1');
asHexaNumber('#FFFFFFFG');
asRgb
Parses an object confirming to the Rgb interface.
Note: Uses asNumber
and asTuple
internally.
asRgb('0 127 255');
asRgb('0, 127, 255');
asRgb('0, 127 255');
asRgb('0 127');
asRgb('-1 256 42');
asRgb('red green blue');
asRgba
Parses an object confirming to the Rgba interface.
Note: Uses asNumber
and asTuple
internally.
asRgba('0 127 255 1');
asRgba('0, 127, 255 1');
asRgba('0, 127 255 1');
asRgba('0 127 255 2');
asRgba('0 127 255 -1');
asRgba('red green blue alpha');
🌲 Custom Parsers
If none of the built-in parsers satisfy your needs you can simply provide your own parser inside the schema.
Simple example of a parser for a tuple of numbers:
import { icssToTs, asNumber, asTuple } from 'icss-to-ts';
import icssExports from './path/to/icss-file.module.css';
const css = icssToTs(icssExports, {
foo: (value: string): number[] => asTuple(value).map(asNumber),
});
console.log(css);
⬆
❤️ Contributing
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".
Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch =>
git checkout -b feature/my-new-feature
- Commit your Changes =>
git commit -m 'feat(my-new-feature): add some awesome new feature'
- Push to the Branch =>
git push origin feature/my-new-feature
- Open a Pull Request
⬆
⭐ License
Distributed under the MIT License. See LICENSE for more information.
⬆
🌐 Acknowledgments
⬆