Skip to content

Commit

Permalink
chore: update badges
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Feb 18, 2021
1 parent 08c6d8f commit b84395e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
39 changes: 22 additions & 17 deletions babel-plugin-optimize-obj-str/readme.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# babel-plugin-optimize-obj-str
# babel-plugin-optimize-obj-str [![CI](https://github.com/lukeed/obj-str/workflows/CI/badge.svg)](https://github.com/lukeed/obj-str/actions) [![codecov](https://badgen.net/codecov/c/github/lukeed/obj-str)](https://codecov.io/gh/lukeed/obj-str)

> [Babel](https://babeljs.io/) plugin to optimize [`obj-str`](../) calls by replacing them with an equivalent unrolled expression.
Even though the `obj-str` function is negligible in size over-the-wire, the motivation for this plugin is that transformed expressions [execute almost twice as fast as equivalent calls.](#performance)

```js
import objstr from 'obj-str';

objstr({
'my-classname': true,
'another-one': maybe,
Expand All @@ -27,6 +28,7 @@ npm install --save-dev babel-plugin-optimize-obj-str
**Via babel.config.js** ([recommended](https://babeljs.io/docs/en/configuration)):

```js
// babel.config.js
module.exports = {
plugins: ['optimize-obj-str'],
};
Expand All @@ -41,25 +43,22 @@ babel --plugins optimize-obj-str script.js
## Options

```js
// babel.config.js
module.exports = {
plugins: [
[
'optimize-obj-str',
{
strict: false,
},
],
['optimize-obj-str', {
strict: false,
}],
],
};
```

### `strict`
### options.strict

| type | default |
| ------------- | ------- |
| **`boolean`** | `false` |
Type: `boolean` <br>
Default: `false`

**Enable to fail when encountering calls that cannot be optimized.**
Allow Babel to throw errors when encountering `obj-str` calls that cannot be optimized.

We can only optimize function calls whose single argument is an **object literal** containing only **keyed properties**.

Expand Down Expand Up @@ -95,6 +94,7 @@ Instead, when setting the option **`{ strict: true }`** the plugin errors out to
> 2 | objstr(cannotOptimizeIdentifierArg);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</code></pre></blockquote>

## Caveats

### Performance
Expand All @@ -105,10 +105,13 @@ You should not expect this transform should to reduce bundle size. Depending on

### Leading Space

Direct results from `objstr()` always omit a leading space. This is not the case when y using this transform:
Direct results from `objstr()` always omit a leading space. This is not the case when using this transform:

```js
objstr({ a: false, foo: true });
objstr({
a: false,
foo: true
});

// transformed:
(' foo');
Expand All @@ -121,15 +124,17 @@ You must ensure that your expression consumers ignore this leading space. A [cla
Object literals may contain duplicate property names, in which case [the lastly defined value is preserved.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Duplicate_property_names)

```js
objstr({ dupe: one, dupe: two });
objstr({
dupe: one,
dupe: two
});

// Transformed:
'' + (two ? 'dupe' : '');
```

The example above is transformed properly since the duplicate property names are literal and constant. The plugin does its best to override duplicates by comparing property name expressions, but it's unable to compare equal computed results whose expressions vary.
The example above is transformed properly since the duplicate property names are literal and constant. The plugin does its best to override duplicates by comparing property name expressions, but it's unable to compare equal computed results whose expressions vary:

<!-- prettier-ignore -->
```js
objstr({
good: one,
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# obj-str [![Build Status](https://travis-ci.org/lukeed/obj-str.svg?branch=master)](https://travis-ci.org/lukeed/obj-str)
# obj-str [![CI](https://github.com/lukeed/obj-str/workflows/CI/badge.svg)](https://github.com/lukeed/obj-str/actions) [![codecov](https://badgen.net/codecov/c/github/lukeed/obj-str)](https://codecov.io/gh/lukeed/obj-str)

> A tiny (96B) library for serializing Object values to Strings.
Expand Down Expand Up @@ -31,7 +31,7 @@ import React from 'react';
import objstr from 'obj-str';

const TodoItem = ({ text, isDone, disabled }) => (
<li className={ objstr({ item:true, completed:isDone, disabled }) }>
<li className={ objstr({ item:true, completed:isDone, disabled }) }>
<input type="checkbox" disabled={ disabled } checked={ isDone } />
<label>{ text }</label>
</li>
Expand Down

0 comments on commit b84395e

Please sign in to comment.