Skip to content

Commit

Permalink
fix(readme.md): incomplete usage
Browse files Browse the repository at this point in the history
elaborates on usage with styled-components as well as advanced usage for generating parsers and
parsing other types of case conventions
  • Loading branch information
Tbhesswebber committed Feb 17, 2019
1 parent 2ddf5e2 commit 3983d53
Showing 1 changed file with 52 additions and 5 deletions.
57 changes: 52 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
![codecov](https://img.shields.io/codecov/c/github/Tbhesswebber/style-object-to-css-string.svg?logo=codecov&style=popout-square)
![Commitizen friendly](<https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=popout-square&logo=travis&logoColor=rgba(0,0,0,0)>)

This project was inspired by some work with the [:nail_care:styled components](https://www.styled-components.com/) library. In creating a component that I will never use, I needed to provide the consumer with a way to pass down styles that I can then merge into various component definitions above my own css rules.
This project was inspired by some work with the [:nail_care:styled components](https://www.styled-components.com/) library. In creating a component that I will never use, I needed to provide the consumer with a way to pass down styles that I can then merge into various component definitions along with my own css rules.

Just in case, this package also exposes the ability to generate additional syntax parsers that can then be used with the primary export or as stand-alone string parsing functions.
Just in case, this package also exposes the ability to access or generate additional syntax parsers that can then be used with the primary export or as stand-alone string parsing functions.

## Usage

### Basic Usage

#### Without styled-components

```javascript
import styleToCss from 'style-obj-to-css-string';
// I now regret my choice in package name
Expand All @@ -26,7 +28,52 @@ const styles = {
const styleString = styleToCss(styles);
/**
* Generates all values as a single semicolon separated string
* "display: flex;flex-direction: column;align-items: center;justify-content: center;"/
* "display: flex;flex-direction: column;align-items: center;justify-content: center;"
*/
```

#### With :nail_care:styled-components

```javascript
import styled from 'styled-components';
import styleToCss from 'style-obj-to-css-string';

const StyledThingy = styled.p`
${/* Place properties that you will allow to be overwritten here - typically stylistic properties */}
color: meduimaquamarine;
font-size: 1rem;
background-color: thistle;
${props => props.styles && styleToCss(props.styles) /* we check for styles to avoid an invocation with undefined. may be fixed with future pull requests. */}
${/* Place properties that you will not allow to change here - typically structural properties */}
display: block;
margin: 1rem;
grid-column: 1 / -1;
`;
```

### Advanced Usage

This library can also be useful in parsing JSON from servers that might use other case-conventions. Simply bring in our list of `parsers` in your import or make your own with our `createParser` function!

**parsers** - An object containing multiple parse methods.

- parsers.snakeToKebab( str: string ): string - converts snake_case to kebab-case
- parsers.camelToKebab( str: string ): string - converts camelCase to kebab-case

**createParser** - A function that return a parsing function.

- createParser(matcher: string | RegExp, replacer: Function): Function - generates a parser function as seen in parsers. Note: replacer uses the same syntax as the second parameter for [String.prototype.replace](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter) (the first argument is the matched value)

```javascript
import styleToCss, { parsers, createParser } from 'style-to-css-string';
import axios from 'axios';

const kebabToCamel = createParser(/-[a-z]/, match => match.split('')[1].toUpperCase());

async function getStyleObj(path) {
const style = await axios.get(path);
const styleString = styleToCss(style, parsers.snakeToKebab);
}
```

## Contributing
Expand All @@ -51,6 +98,6 @@ $ npm run validate

I use [commitlint](https://github.com/conventional-changelog/commitlint) to lint git commits according to the [conventional changelog](https://github.com/conventional-changelog/commitlint/blob/master/%40commitlint/config-conventional/README.md). If you're as lazy as I am, feel free to use [commitizen cli](https://github.com/commitizen/cz-cli) either with your own installation or with `npm run cz` - if your commit still fails commitlint's rules, you can run `npm run cz -- --retry`.

#### :fire_engine:In Case of Emergency:fire_engine:
#### :fire:In Case of Emergency:fire:

Just in case something happens that forces you to run away from your computer screaming, just run `npm run fire`, which will create a new branch called "emergency", commit all of your current changes, and then push them to a new branch on your origin remote.
Just in case something happens that forces you to run away from your computer screaming, just run `npm run fire`, which will create a new branch called "emergency", commit all of your current changes, and then push them to a new branch on your origin remote. :fire_engine:

0 comments on commit 3983d53

Please sign in to comment.