join-words
v1.0.4
Published
a lightweight and easy-to-use JavaScript utility for formatting an array of words into a human-readable string.
Downloads
10
Readme
join-words
join-words is a lightweight and easy-to-use JavaScript utility for formatting an array of words into a human-readable string. It handles edge cases like empty arrays, single words, and proper punctuation for lists of two or more words.
Features
- Empty Array Handling: Returns an empty string for an empty array.
- Single Word Support: Returns the word itself when the array contains only one word.
- Two Words: Joins two words with "and".
- Three or More Words: Joins all but the last word with commas, and appends "and" before the last word.
- Lightweight: No dependencies, simple and efficient.
Installation
Install the library via npm:
npm install join-wordsOr using yarn:
yarn add join-wordsUsage
Importing the Library
You can import the library into your project using CommonJS:
const joinWords = require('join-words');Examples
const joinWords = require('join-words');
console.log(joinWords([])); // ""
console.log(joinWords(["apple"])); // "apple"
console.log(joinWords(["apple", "banana"])); // "apple and banana"
console.log(joinWords(["apple", "banana", "cherry"])); // "apple, banana, and cherry"
console.log(joinWords(["apple", "banana", "cherry", "date"])); // "apple, banana, cherry, and date"Edge Cases
- Empty Array: An empty array will return an empty string.
joinWords([]); // ""- Single Word: An array with one word will return the word itself.
joinWords(["apple"]); // "apple"- Two Words: Two words are joined with "and".
joinWords(["apple", "banana"]); // "apple and banana"- Three or More Words: Words are joined with commas, and "and" is appended before the last word.
joinWords(["apple", "banana", "cherry"]); // "apple, banana, and cherry"
joinWords(["apple", "banana", "cherry", "date"]); // "apple, banana, cherry, and date"API
joinWords(words)
Parameters:
words(Array of Strings): The array of words to format.
Returns:
String: A formatted string of the words.
Behavior:
- Returns an empty string if the array is empty.
- Returns the single word if there is only one word in the array.
- Joins two words with "and".
- Joins three or more words with commas and appends "and" before the last word.
Why Use join-words?
- Simplifies formatting of word lists in natural language.
- Reduces boilerplate code for creating readable lists.
- Handles edge cases seamlessly.
- Lightweight and dependency-free.
Contributing
Contributions are welcome! If you'd like to improve or add features to this library, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Write clear and concise code with comments where necessary.
- Add tests for your changes (if applicable).
- Submit a pull request.
Development
Clone the repository:
git clone https://github.com/OroTeam/join-words.git cd join-wordsInstall dependencies:
npm installRun tests:
npm test
License
This project is licensed under the MIT License. You are free to use, modify, and distribute this library as long as you include the license in your project.
Support
If you encounter any issues or have questions, feel free to open an issue on the GitHub repository or contact the maintainer.
Acknowledgments
Special thanks to the open-source community for inspiring the creation of this library. Your feedback and contributions make this project better!
Enjoy using join-words! 🎉
