A simple Node.js module that provides functions to concatenate strings with various options.
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.
npm install concat-stringsClone this repository or download the files:
git clone https://github.com/mattesja/concat-strings.git
cd concat-strings
npm installconst { 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"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"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"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
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
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"
}You can test the functions by uncommenting the example usage lines in index.js and running:
node index.jsISC
mattesja
https://github.com/mattesja/concat-strings
Report issues at: https://github.com/mattesja/concat-strings/issues