Conditional spread operator for human. A readable way to spread an array or an object conditionally.
At Potloc we like it when our code is easy to read, write and self-explanatory. One thing we often do is to use the spread operator, but sometimes based on certain condition we do not want to execute a specif spread operation. There are multiple ways to achive this, but none are "easy to read, write and self-explanatory". Here are some examples:
The example below is easy to read and write, but might not be as self-explanatory because of the usaged Object.assign. Also where is the the famous spread operator that we has developpers love?
function buildConfig(host: string, isSecure: boolean) {
const config = { host, ssl: false, protocol: "http" };
if (isSecure) {
Object.assign(config, { ssl: true, protocol: "https" });
}
return config;
}
The example below is not easy to read, write or even self-explanatory. It looks likes someones is trying to be a bit too smart, but at least it uses our beloved spread operator!
const buildConfig = (host: string, isSecure: boolean) => ({
host,
ssl: false,
protocol: "http",
...(isSecure ? { ssl: true, protocol: "https" } : {}),
});
Since we are developpers, we've decided to write a small piece of code to make the conditionnal spread easy to read, write and self-explanatory. Boom π₯ spready was born! Here is the same example using it.
import spready from "@potloc/spready";
const buildConfig = (host: string, isSecure: boolean) => ({
host,
ssl: false,
protocol: "http",
...spready({ ssl: true, protocol: "https" }).if(isSecure),
});
If you are not yet convinced go to the usage section for more examples.
Install @potloc/spready
using the package manager of your chose, import it and start using it!
npm install @potloc/spready
pnpm add @potloc/spready
yarn add @potloc/spready
It returns the object passed in parameter if the condition is thruty.
import spready from "@potloc/spready";
// { foo: "bar", baz: "qux" }
const a = {
foo: "bar",
...spready({ baz: "qux" }).if(true),
};
// { foo: "bar" }
const b = {
foo: "bar",
...spready({ baz: "qux" }).if(false),
};
It returns the object passed in parameter if the condition is falsy.
import spready from "@potloc/spready";
// { foo: "bar" }
const a = {
foo: "bar",
...spready({ baz: "qux" }).unless(true),
};
// { foo: "bar", baz: "qux" }
const b = {
foo: "bar",
...spready({ baz: "qux" }).unless(false),
};
It can also conditionally invoke code if we pass a callback in parameter.
import spready from "@potloc/spready";
// it will also log "Hello World!"
// { foo: "bar", baz: "qux" }
const a = {
foo: "bar",
...spready(() => {
console.log("Hello World!");
return { baz: "qux" };
}).if(true),
};
// it won't log "Hello World!"
// { foo: "bar" }
const b = {
foo: "bar",
...spready(() => {
console.log("Hello World!");
return { baz: "qux" };
}).if(false),
};
It can increase the readability of your JSX code.
import spready from "@potloc/spready";
const SumitButton = (props) => {
const { isSubmitting } = props;
return (
<button
type="submit"
disabled={isSubmitting}
{...spready({ className: "is-loading" }).if(isSubmitting)}
>
Submit
</button>
);
};
Potloc is dedicated to building a welcoming, diverse, safe community. We expect everyone participating in the Potloc community to abide by our Code of Conduct. Please read it. Please follow it. In the Potloc community, we work hard to build each other up and create amazing things together.
Pull requests are welcome. If you'd like to contribute to @potloc/spready, that's awesome, and we β€οΈ you. Check out our guide to contributing for more information.
You can have a look at the discussions secion to see if someone else has faced the same issue. Do not hesitate to open a new discussion using the Q&A category and we'll try our best to reply to you as soon as possible.
Licensed under the MIT license.