concat-strings
v1.0.3
Published
A simple Node.js module for concatenating strings with various options and error handling
Readme
concat-strings
A simple Node.js module that provides functions to concatenate strings with various options.
Description
This module concatenates two or more strings and provides additional functionality like concatenating with custom separators. It includes proper error handling to ensure all inputs are valid strings.
Installation
From npm
npm install concat-stringsFrom source
Clone this repository or download the files:
git clone https://github.com/mattesja/concat-strings.git
cd concat-strings
npm installUsage
Basic String Concatenation
const { concatStrings } = require('concat-strings');
// Concatenate multiple strings
const result = concatStrings('Hello', ' ', 'World', '!');
console.log(result); // Output: "Hello World!"
// Concatenate two strings
const greeting = concatStrings('Good', ' morning');
console.log(greeting); // Output: "Good morning"String Concatenation with Separator
const { concatStringsWithSeparator } = require('concat-strings');
// Concatenate with a custom separator
const fruits = concatStringsWithSeparator(' - ', 'Apple', 'Banana', 'Orange');
console.log(fruits); // Output: "Apple - Banana - Orange"
// Concatenate with comma separator
const names = concatStringsWithSeparator(', ', 'John', 'Jane', 'Bob');
console.log(names); // Output: "John, Jane, Bob"Using Both Functions
const { concatStrings, concatStringsWithSeparator } = require('concat-strings');
const firstName = 'John';
const lastName = 'Doe';
// Basic concatenation
const fullName = concatStrings(firstName, ' ', lastName);
console.log(fullName); // Output: "John Doe"
// With separator
const formattedName = concatStringsWithSeparator(' | ', firstName, lastName);
console.log(formattedName); // Output: "John | Doe"API Reference
concatStrings(...strings)
Concatenates multiple strings together without any separator.
Parameters:
...strings(string): Variable number of string arguments to concatenate
Returns:
string: The concatenated result
Throws:
TypeError: If any argument is not a string
concatStringsWithSeparator(separator, ...strings)
Concatenates multiple strings with a specified separator between them.
Parameters:
separator(string): The separator to insert between strings...strings(string): Variable number of string arguments to concatenate
Returns:
string: The concatenated result with separators
Throws:
TypeError: If the separator or any string argument is not a string
Error Handling
Both functions include robust error handling:
try {
concatStrings('Hello', 123); // This will throw an error
} catch (error) {
console.error(error.message); // "Argument at index 1 is not a string. Expected string, got number"
}
try {
concatStringsWithSeparator(null, 'Hello', 'World'); // This will throw an error
} catch (error) {
console.error(error.message); // "Separator must be a string. Expected string, got object"
}Testing
You can test the functions by uncommenting the example usage lines in index.js and running:
node index.jsLicense
ISC
Author
mattesja
Repository
https://github.com/mattesja/concat-strings
Issues
Report issues at: https://github.com/mattesja/concat-strings/issues
