Methods
String

String Methods

import { string } from 'ajuda'

compare

Description

Compares two strings to check if they are the same (length, characters in order).

string.compare(string: string, _string: string): boolean

Parameters

{string} string The first string to be compared.

{string} _string The second string to be compared.

Returns

{Boolean} true if the strings are equal, false otherwise.

Example

let alpha = 'Hello World.'
let bravo = 'Hello World.'
let charlie = 'Hello Earth.'
let delta = 'Hello Planet.'
 
string.compare(alpha, bravo) // true
string.compare(alpha, charlie) // false
string.compare(bravo, delta) // false

isAnagram

Description

Compares two strings to check if they are anagrammatic, that is, if both strings contain a different arrangement of the same characters.

string.isAnagram(string: string, _string: string): boolean

Parameters

{string} string The first string to be compared.

{string} _string The second string to be compared.

Returns

{Boolean} true if the strings are anagrammatic, false otherwise.

Example

Compares two strings to check if they are anagrammatic, that is, if both strings contain a different arrangement of the same characters.

let root = 'root', toor = 'toor', boot = 'boot'
 
string.isAnagram(root, toor) // true
string.isAnagram(root, boot) // false

reverse

Description

Reverses a given string of characters.

string.reverse(string: string): string

Parameters

{string} string A string that needs reversing.

Returns

{string} A reversed string.

Example

string.reverse('dlrow olleh')
// 'hello world'

slugify

Description

Converts a string of text into a URL-friendly slug. Sanitizes for slug-safe characters, i.e. letters, numbers, dashes.

string.slugify(string: string): string

Parameters

{string} string A string of words in any case.

Returns

{string} A URL slug.

Example

let example = 'Alice and Bob discover mushrooms!'
 
string.slugify(example)
// alice-and-bob-discover-mushrooms

wordCount

Description

Returns the number of words in a string. Does not count punctuation.

string.wordCount(string: string): number

Parameters

{string} string A string of words in any case.

Returns

{number} The number of words.

Example

let example = 'JavaScript, TypeScript, and WebAssembly at the edge, worldwide.'
 
string.wordCount(example)
// 8