Skip to content

mattesja/concat-strings

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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-strings

From source

Clone this repository or download the files:

git clone https://github.com/mattesja/concat-strings.git
cd concat-strings
npm install

Usage

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.js

License

ISC

Author

mattesja

Repository

https://github.com/mattesja/concat-strings

Issues

Report issues at: https://github.com/mattesja/concat-strings/issues

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published