A library for string manipulation.
Local installation to any node project:
$ npm install @shah-a/string-lib
Require the package in your script and call any of the available functions.
Examples:
const s = require('@shah-a/string-lib');
console.log(s.makeHashtag('Amazing bongo drums for sale'));
// Output: ['#Amazing', '#Bongo', '#Drums']
console.log(s.shift('Shift me right by 3', 3));
// Output: 'y 3Shift me right b'
console.log(s.shift('Shift me left by 3', -3));
// Output: 'ft me left by 3Shi'
That's it! 😃
Reference list of available functions (sorted by the assignment's corresponding challenge number):
snakeCase(s: string)
snakeCaseNoSpecial(s: string)
customSeparator(s: string, separator?: string)
customSeparatorNoSpecial(s: string, separator?: string)
Uppercases the first character in a string.
s.capFirst('salaam');
// returns 'Salaam'
Lowercases the first character in a string.
s.lowerFirst('Salaam');
// returns 'salaam'
Uppercases the first letter of each word in a string.
s.capWords('salaam world');
// returns 'Salaam World'
Uppercases the first letter of each word in a string except for the following words:
'the', 'in', 'a', 'an', 'and', 'but', 'for', 'at', 'by', 'from'
The first word is always uppercased, even if it's on the exception list.
s.capTitle('the most foo in bar');
// returns 'The Most Foo in Bar'
Uppercases all characters in a string.
Note: This function is an alias for the built-in String.prototype.toUpperCase
function.
s.allCaps('salaam world');
// returns 'SALAAM WORLD'
Removes all spaces from the beginning and end of a string along with any extra spaces in the middle.
If more than one space appears in the middle of a string, it is replaced by a single space.
s.removeExtraSpaces(' Salaam world! ');
// returns 'Salaam world!'
Removes extra spaces and replaces spaces with a hyphen "-". Also makes all characters lowercase.
s.kebobCase(' Kebob Case! ');
// returns 'kebob-case!'
Removes extra spaces and replaces spaces with a hyphen "-". Also makes all characters lowercase and removes special characters.
s.kebobCaseNoSpecial(' Kebob Case! ');
// returns 'kebob-case'
Removes extra spaces and replaces spaces with an underscore "_". Also makes all characters lowercase.
s.snakeCase(' Snake Case! ');
// returns 'snake_case!'
Removes extra spaces and replaces spaces with an underscore "_". Also makes all characters lowercase and removes special characters.
s.snakeCaseNoSpecial(' Snake Case! ');
// returns 'snake_case'
Removes extra spaces and replaces spaces with a custom separator. Also makes all characters lowercase.
s.customSeparator(' Custom Case! ', '🐒');
// returns 'custom🐒case!'
If no separator is provided, the default separator is ' '
(one space).
s.customSeparator(' Custom Case! ');
// returns 'custom case!'
Removes extra spaces and replaces spaces with a custom separator. Also makes all characters lowercase and removes special characters.
s.customSeparatorNoSpecial(' Custom Case! ', '🐒');
// returns 'custom🐒case'
If no separator is provided, the default separator is ' '
(one space).
s.customSeparatorNoSpecial(' Custom Case! ');
// returns 'custom case'
Lowercases the first character of the first word. Then uppercases the first character of all other words. Also removes all spaces.
s.camelCase('Camel Case!');
// returns 'camelCase!'
Lowercases the first character of the first word. Then uppercases the first character of all other words. Also removes all spaces and removes special characters.
s.camelCaseNoSpecial('Camel Case!');
// returns 'camelCase'
Shifts the characters of a string to the right or left. If the step
parameter is positive, characters will be shifted to the right. If the step
parameter is negative, the characters will be shifted to the left.
s.shift('Shift me right by 3', 3);
// returns 'y 3Shift me right b'
s.shift('Shift me left by 3', -3);
// returns 'ft me left by 3Shi'
If no step
is provided, the default step
is 1
.
s.shift('Shift me right by 1');
// returns '1Shift me right by '
Converts a string to a list of hashtags. A hashtag begins with '#' and has no spaces. Also uppercases the hashtagged words.
If the string has more than three words, picks the three longest and returns hashtags from those.
s.makeHashtag('Salaam world');
// returns ['#Salaam', '#World']
s.makeHashtag('Amazing bongo drums for sale');
// returns ['#Amazing', '#Bongo', '#Drums']
Returns true if a string is empty or contains only whitespace. Whitespace includes: spaces, line returns, and tabs. These characters can be represented with: '\n' (new line), '\r' (carriage return), and '\t' (tab).
s.isEmpty('Abc def');
// returns false
s.isEmpty(' \n \n\r\t');
// returns true
s.isEmpty(`
`);
// returns true