๐ช Provides ways of testing whether an array of chars is present inside a given String. โ
โจ Since v.2.0.0
chars-in-string
is a hybrid module that supports both CommonJS (legacy) and ES modules, thanks to Modern Module.
An enum
is available publicly, used for setting the position of the search, see more below. ๐
enum Position = {
Any,
Start,
End
}
It allows you define where the matching should occur, at the beggining of the String
, at the end or anywhere (default).
function charsInString(characters: string[], string: String, position = Position.Any, caseSensitive = true): boolean
characters: string[]
- the characters to search for, expects a single character per entry, if multiple are found it will take the first one,
string: String
- the String which needs to be checked,
[position: Position=Position.Any]
- controls where the matching should occur, at the beggining of the String
, at the end or anywhere (default),
[caseSensitive: boolean=true]
- controls whether the search is case-sensitive, defalts to true.
function stringsInString(strings, string, position = Position.Any, caseSensitive = true
strings[]: string[]
- the strings to search for,
string: String
- the String which needs to be checked,
[position: Position=Position.Any]
- controls where the matching should occur, at the beggining of the String
, at the end or anywhere (default),
[caseSensitive: boolean=true]
- controls whether the search is case-sensitive, defalts to true.
Install it by:
npm i "@igor.dvlpr/chars-in-string"
and then use it like:
const { charsInString, stringsInString, Position } = require('@igor.dvlpr/chars-in-string')
console.log(charsInString([], '')) // prints false
console.log(charsInString([], 'test')) // prints false
console.log(charsInString([':', ','], '')) // prints false
console.log(charsInString([':', ','], 'hello:world')) // prints true
console.log(charsInString([':', ',', '^'], 'helloworld^')) // prints true
console.log(charsInString([':', ',', '^'], 'helloworld')) // prints false
console.log(stringsInString(['abc', 'owom', 'wqp', 'world', 'hel'], 'helloworld', Position.Start)) // prints true