Joinable

Join strings easily by removing the repetitive falsy
checks. Construct strings like form validation, CSS classes, URLs and more.
Usage
npm install joinable
import joinable from "joinable";
joinable("potato", undefined, "rice");
About
What is Joinable: A library to join strings together without the need to check if a value is a falsy like undefined
.
Why use Joinable: Keep your code base clean by removing the repetitive falsy
checks and improve the readability.
More information about Joinable
Handle falsy false, 0, "", undefined, null, NaN
API
joinable
is the default export and an alias of joinStrings
.
joinable(...joinables [, options]) : string
import joinable from 'joinable';
joinable('potato', undefined, 'rice', null, 'carrot');
joinable('potato', 'rice', 'carrot', {separator: ','});
Join strings based on another value like a boolean.
import joinable from 'joinable';
joinable('potato', [true, 'spinach']);
joinable('potato', [false, 'spinach']);
joinable('potato', [null, 'spinach']);
Have a default value if a falsy passed.
import joinable from 'joinable';
joinable('potato', [true, 'spinach', 'beetroot']);
joinable('potato', [false, 'spinach', 'beetroot']);
joinable('potato', [null, 'spinach', 'beetroot']);
Joining classNames in ReactJS
Problem
Example of typical logic string concatenation in ReactJS component with if statements. General issues: verbose, unnecessary repetitive complexity and mutation:
import React from "react";
const MyComponent = props => {
let myClass = "panel ";
if (props.hide) myClass += "invisible ";
if (props.hasBoarder) myClass += "sparkleBoarder ";
if (props.className) myClass += props.className;
return <div className={myClass}>{props.children}</div>;
};
While this works fine you will probably need to repeat that similar flow for a lot of components and some will have additional complexity round it.
Solution
Same component as above but lets keep it clean with joinable
.
import React from "react";
import joinable from "joinable";
const MyComponent = props => {
const myClass = joinable(
"potato",
props.className,
[props.hide, "invisible"],
[props.hasBoarder, "sparkleBoarder"]
);
return <div className={myClass}>{props.children}</div>;
};
prefixStrings
prefixStrings(prefix, ...joinables [, options]) : string
import { prefixStrings } from 'joinable';
prefixStrings('pre-', undefined, 'rice', null, 'carrot');
prefixStrings(falsy, undefined, 'rice', null, 'carrot');
prefixStrings('pre-', undefined, 'rice', null, 'carrot', {separator: ','});
joinExp
Note: no ifArrays can be used in joinables
joinExp(regexp, ...joinables [, options]) : string
import { joinExp } from 'joinable';
joinExp(/m+/, 'cucumber');
joinExp(/(m|n)+/, 'cucumber', false, 'sandwich');
joinExp(/r+/, 'cucumber');
joinExp('', 'cucumber');
joinIf
joinIf(ifArray)
import { joinIf } from 'joinable';
joinIf([true, 'spinach']);
joinIf([1==2, 'spinach']);
joinIf([1==1, 'spinach', 'broccoli']);
joinIf([1==2, 'spinach', 'broccoli']);
joinIf('lettuce');
Useful to make joining easier to read combine both joinable
and joinIf
.
import joinable, { joinIf } from 'joinable';
joinable('potato', joinIf([true, 'spinach'])) # => 'potato spinach'
joinObject
joinObject({object} [, separator, separator]) : string
import { joinObject } from 'joinable';
joinObject({ chicken: 'burger', spare: 'ribs' })
joinObject({ chicken: 'burger', spare: 'ribs' }, ',')
joinObject({ chicken: 'burger', spare: 'ribs' }, ';', ',')
joinObject({ salad: null, chicken: 'burger', spare: 'ribs' }, ';', ',')
Contribute:
Install: npm i
npm test
- unit tests and eslint
npm run benchmark
- run performance tests
This project follows Semantic Versioning