1.2.0 • Published 2 years ago

@shah-a/string-lib v1.2.0

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

FEW 2.1 Assignment 1: String Library

npm (scoped) npm

Description

A library for string manipulation.

Installation

Local installation to any node project:

$ npm install @shah-a/string-lib

Usage

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! 😃

API

Reference list of available functions (sorted by the assignment's corresponding challenge number):

Functions

Challenge 1

Challenge 2

Challenge 3

Challenge 4

Challenge 5

Challenge 6

Challenge 7

Challenge 8

Challenge 9

Challenge 10

Reference

capFirst(s: string)

Uppercases the first character in a string.

s.capFirst('salaam');
// returns 'Salaam'

lowerFirst(s: string)

Lowercases the first character in a string.

s.lowerFirst('Salaam');
// returns 'salaam'

capWords(s: string)

Uppercases the first letter of each word in a string.

s.capWords('salaam world');
// returns 'Salaam World'

capTitle(s: string)

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'

allCaps(s: string)

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'

removeExtraSpaces(s: string)

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!'

kebobCase(s: string)

Removes extra spaces and replaces spaces with a hyphen "-". Also makes all characters lowercase.

s.kebobCase(' Kebob Case! ');
// returns 'kebob-case!'

kebobCaseNoSpecial(s: string)

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'

snakeCase(s: string)

Removes extra spaces and replaces spaces with an underscore "_". Also makes all characters lowercase.

s.snakeCase(' Snake Case! ');
// returns 'snake_case!'

snakeCaseNoSpecial(s: string)

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'

customSeparator(s: string, separator?: string)

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!'

customSeparatorNoSpecial(s: string, separator?: string)

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'

camelCase(s: string)

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!'

camelCaseNoSpecial(s: string)

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'

shift(s: string, step?: number)

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 '

makeHashtag(s: string)

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']

isEmpty(s: string)

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
1.2.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago