Comparing version 1.0.1 to 1.0.2
166
index.js
function capitalize(string) { | ||
// return string[0].toUpperCase() + string.slice(1) | ||
return string.charAt(0).toUpperCase() + string.slice(1); | ||
// const wordsArray = string.split('') | ||
// wordsArray[0] = wordsArray[0].toUpperCase() | ||
// return wordsArray.join('') | ||
return string.charAt(0).toUpperCase() + string.slice(1); | ||
} | ||
function allCaps(string) { | ||
const convertedString = string.toUpperCase() | ||
return convertedString | ||
const convertedString = string.toUpperCase(); | ||
return convertedString; | ||
} | ||
function capitalizeWords(string) { | ||
const wordsArray = string.split(' ') | ||
const upperWords = wordsArray.map(word => { | ||
return capitalize(word) | ||
}) | ||
// console.log(upperWords) | ||
// for (let i = 0; i < wordsArray.length; i++) { | ||
// const word = wordsArray[i] | ||
// wordsArray[i] = word.charAt(0).toUpperCase() + word.slice(1) | ||
// } | ||
return upperWords.join(' ') | ||
const wordsArray = string.split(' '); | ||
const upperWords = wordsArray.map((word) => capitalize(word)); | ||
return upperWords.join(' '); | ||
} | ||
function capitalizeHeadline(string) { | ||
const wordsToNotInclude = ['the', 'in', 'a', 'an', 'and', 'but', 'for', 'at', 'by', 'from']; | ||
const wordsArray = string.split(' '); | ||
const wordsToNotInclude = ['the', 'in', 'a', 'an', 'and', 'but', 'for', 'at', 'by', 'from']; | ||
const wordsArray = string.split(' '); | ||
for (let i = 0; i < wordsArray.length; i++) { | ||
const word = wordsArray[i]; | ||
for (let i = 0; i < wordsArray.length; i += 1) { | ||
const word = wordsArray[i]; | ||
if (i === 0 || !wordsToNotInclude.includes(word.toLowerCase())) { | ||
wordsArray[i] = word.charAt(0).toUpperCase() + word.slice(1); | ||
} | ||
if (i === 0 || !wordsToNotInclude.includes(word.toLowerCase())) { | ||
wordsArray[i] = word.charAt(0).toUpperCase() + word.slice(1); | ||
} | ||
} | ||
return wordsArray.join(' '); | ||
return wordsArray.join(' '); | ||
} | ||
function removeExtraSpaces(string) { | ||
const str = string.trim() | ||
const wordsArray = str.split(' ') | ||
const filteredWords = wordsArray.filter(word => word !== '') | ||
return filteredWords.join(' ') | ||
const str = string.trim(); | ||
const wordsArray = str.split(' '); | ||
const filteredWords = wordsArray.filter((word) => word !== ''); | ||
return filteredWords.join(' '); | ||
} | ||
function kebobCase(string) { | ||
const cleanedString = removeExtraSpaces(string); | ||
const validCharacters = /^[a-zA-Z0-9\s_-]+$/; | ||
const lowerString = cleanedString.toLowerCase() | ||
const charArray = lowerString.split('') | ||
// using regular expression in validCharacters to filter out unwanted characters. | ||
const kebobString = charArray | ||
.filter(char => validCharacters.test(char)) | ||
.join('') | ||
.split(/\s+/) | ||
.join('-') | ||
return kebobString; | ||
const cleanedString = removeExtraSpaces(string); | ||
const validCharacters = /^[a-zA-Z0-9\s_-]+$/; | ||
const lowerString = cleanedString.toLowerCase(); | ||
const charArray = lowerString.split(''); | ||
// using regular expression in validCharacters to filter out unwanted characters. | ||
const kebobString = charArray | ||
.filter((char) => validCharacters.test(char)) | ||
.join('') | ||
.split(/\s+/) | ||
.join('-'); | ||
return kebobString; | ||
} | ||
function snakeCase(string) { | ||
const kebobString = kebobCase(string); | ||
const snakeString = kebobString | ||
.split('-') | ||
.join('_') | ||
return snakeString; | ||
const kebobString = kebobCase(string); | ||
const snakeString = kebobString | ||
.split('-') | ||
.join('_'); | ||
return snakeString; | ||
} | ||
function camelCase(string) { | ||
const wordsArray = string.split(' ') | ||
const camelWords = wordsArray.map((word, i) => { | ||
if (i === 0) { | ||
return word.toLowerCase() | ||
} else { | ||
return capitalize(word) | ||
} | ||
}) | ||
return camelWords.join('') | ||
const wordsArray = string.split(' '); | ||
const camelWords = wordsArray.map((word, i) => { | ||
if (i === 0) { | ||
return word.toLowerCase(); | ||
} | ||
return capitalize(word); | ||
}); | ||
return camelWords.join(''); | ||
} | ||
function shift(string) { | ||
if (string.length <= 1) { | ||
return string | ||
} | ||
if (string.length <= 1) { | ||
return string; | ||
} | ||
const firstChar = string.charAt(0) | ||
const slicedString = string.slice(1) | ||
const firstChar = string.charAt(0); | ||
const slicedString = string.slice(1); | ||
const shiftedString = slicedString + firstChar | ||
const shiftedString = slicedString + firstChar; | ||
return shiftedString | ||
return shiftedString; | ||
} | ||
function makeHashTag(string) { | ||
const wordsArray = string.split(' ') | ||
// https://stackoverflow.com/questions/10630766/how-to-sort-an-array-based-on-the-length-of-each-element | ||
wordsArray.sort((a, b) => b.length - a.length) | ||
const hashTags = wordsArray.slice(0, 3).map(word => `#${word.toLowerCase()}`); | ||
const wordsArray = string.split(' '); | ||
// https://stackoverflow.com/questions/10630766/how-to-sort-an-array-based-on-the-length-of-each-element | ||
wordsArray.sort((a, b) => b.length - a.length); | ||
const hashTags = wordsArray.slice(0, 3).map((word) => `#${word.toLowerCase()}`); | ||
return hashTags | ||
return hashTags; | ||
} | ||
// White space includes: spaces, line returns, and tabs. These characters can be represented with: \n (new line) \r (carrige return), \t (tab) | ||
// White space includes: spaces, line returns, and tabs. | ||
// These characters can be represented with: \n (new line) \r (carrige return), \t (tab) | ||
function isEmpty(string) { | ||
const trimmedStr = string.trim(); | ||
const trimmedStr = string.trim(); | ||
for (const char of trimmedStr) { | ||
// Check if the character is a whitespace character | ||
if (char !== '\n' && char !== '\r' && char !== '\t') { | ||
return false | ||
} | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (const char of trimmedStr) { | ||
// Check if the character is a whitespace character | ||
if (char !== '\n' && char !== '\r' && char !== '\t') { | ||
return false; | ||
} | ||
} | ||
return true | ||
return true; | ||
} | ||
module.exports.capitalize = capitalize | ||
module.exports.allCaps = allCaps | ||
module.exports.capitalizeWords = capitalizeWords | ||
module.exports.capitalizeHeadline = capitalizeHeadline | ||
module.exports.removeExtraSpaces = removeExtraSpaces | ||
module.exports.kebobCase = kebobCase | ||
module.exports.snakeCase = snakeCase | ||
module.exports.camelCase = camelCase | ||
module.exports.shift = shift | ||
module.exports.makeHashTag = makeHashTag | ||
module.exports.isEmpty = isEmpty | ||
module.exports.capitalize = capitalize; | ||
module.exports.allCaps = allCaps; | ||
module.exports.capitalizeWords = capitalizeWords; | ||
module.exports.capitalizeHeadline = capitalizeHeadline; | ||
module.exports.removeExtraSpaces = removeExtraSpaces; | ||
module.exports.kebobCase = kebobCase; | ||
module.exports.snakeCase = snakeCase; | ||
module.exports.camelCase = camelCase; | ||
module.exports.shift = shift; | ||
module.exports.makeHashTag = makeHashTag; | ||
module.exports.isEmpty = isEmpty; |
{ | ||
"name": "strinify", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "The strinify npm package provides string manipulation functions designed to enhance JavaScript's built-in string capabilities. It provides a set of intuitive functions that allow you to easily transform and manipulate strings in various ways.", | ||
@@ -40,4 +40,7 @@ "main": "index.js", | ||
"devDependencies": { | ||
"eslint": "^8.47.0", | ||
"eslint-config-airbnb-base": "^15.0.0", | ||
"eslint-plugin-import": "^2.28.1", | ||
"jest": "^29.6.3" | ||
} | ||
} |
@@ -0,1 +1,7 @@ | ||
[](https://www.npmjs.com/package/string-in-js) | ||
[](https://www.npmjs.com/package/string-in-js) | ||
[NPM Package](https://www.npmjs.com/package/strinify) | ||
# strinify npm package | ||
@@ -2,0 +8,0 @@ |
90
test.js
const { | ||
capitalize, | ||
allCaps, | ||
capitalizeWords, | ||
capitalizeHeadline, | ||
removeExtraSpaces, | ||
kebobCase, | ||
snakeCase, | ||
camelCase, | ||
shift, | ||
makeHashTag, | ||
isEmpty | ||
} = require('strinify') | ||
capitalize, | ||
allCaps, | ||
capitalizeWords, | ||
capitalizeHeadline, | ||
removeExtraSpaces, | ||
kebobCase, | ||
snakeCase, | ||
camelCase, | ||
shift, | ||
makeHashTag, | ||
isEmpty, | ||
} = require('strinify'); | ||
test('capitalize returns the first character of a given string in uppercase', () => { | ||
expect(capitalize("hello world!")).toBe("Hello world!") | ||
expect(capitalize("hello World test!")).toBe("Hello World test!") | ||
}) | ||
expect(capitalize('hello world!')).toBe('Hello world!'); | ||
expect(capitalize('hello World test!')).toBe('Hello World test!'); | ||
}); | ||
test('allCaps makes all characters in the string uppercase', () => { | ||
expect(allCaps('hello world')).toBe('HELLO WORLD') | ||
expect(allCaps('test')).toBe('TEST') | ||
}) | ||
expect(allCaps('hello world')).toBe('HELLO WORLD'); | ||
expect(allCaps('test')).toBe('TEST'); | ||
}); | ||
test('capitalizeWords should capitalize the first character of each word', () => { | ||
expect(capitalizeWords('hello world')).toBe('Hello World') | ||
expect(capitalizeWords('this is a test')).toBe('This Is A Test') | ||
}) | ||
expect(capitalizeWords('hello world')).toBe('Hello World'); | ||
expect(capitalizeWords('this is a test')).toBe('This Is A Test'); | ||
}); | ||
test('capitalizeHeadline should capitalize except certain words', () => { | ||
expect(capitalizeHeadline('for welcome to the world but')).toBe('For Welcome To the World but') | ||
expect(capitalizeHeadline('in this but')).toBe('In This but') | ||
}) | ||
expect(capitalizeHeadline('for welcome to the world but')).toBe('For Welcome To the World but'); | ||
expect(capitalizeHeadline('in this but')).toBe('In This but'); | ||
}); | ||
test('removeExtraSpaces should remove extra spaces', () => { | ||
expect(removeExtraSpaces('hello world')).toBe('hello world') | ||
expect(removeExtraSpaces(' remove extra spaces ')).toBe('remove extra spaces') | ||
}) | ||
expect(removeExtraSpaces('hello world')).toBe('hello world'); | ||
expect(removeExtraSpaces(' remove extra spaces ')).toBe('remove extra spaces'); | ||
}); | ||
test('kebobCase should convert to kebob case', () => { | ||
expect(kebobCase('Hello World')).toBe('hello-world') | ||
expect(kebobCase('Some String, Here')).toBe('some-string-here') | ||
}) | ||
expect(kebobCase('Hello World')).toBe('hello-world'); | ||
expect(kebobCase('Some String, Here')).toBe('some-string-here'); | ||
}); | ||
test('snakeCase should convert to snake case', () => { | ||
expect(snakeCase('Hello World')).toBe('hello_world') | ||
expect(snakeCase('Some String, Here')).toBe('some_string_here') | ||
}) | ||
expect(snakeCase('Hello World')).toBe('hello_world'); | ||
expect(snakeCase('Some String, Here')).toBe('some_string_here'); | ||
}); | ||
test('camelCase should convert to camel case', () => { | ||
expect(camelCase('Hello World')).toBe('helloWorld') | ||
expect(camelCase('Some String, Here')).toBe('someString,Here') | ||
}) | ||
expect(camelCase('Hello World')).toBe('helloWorld'); | ||
expect(camelCase('Some String, Here')).toBe('someString,Here'); | ||
}); | ||
test('shift should shift the first character to the end', () => { | ||
expect(shift('Hello World')).toBe('ello WorldH') | ||
expect(shift('Test')).toBe('estT') | ||
}) | ||
expect(shift('Hello World')).toBe('ello WorldH'); | ||
expect(shift('Test')).toBe('estT'); | ||
}); | ||
test('makeHashTag should convert to hash tags', () => { | ||
expect(makeHashTag('Amazing bongo drums for sale')).toEqual(['#amazing', '#bongo', '#drums']) | ||
expect(makeHashTag('This is another test')).toEqual(['#another','#this', '#test' ]) | ||
}) | ||
expect(makeHashTag('Amazing bongo drums for sale')).toEqual(['#amazing', '#bongo', '#drums']); | ||
expect(makeHashTag('This is another test')).toEqual(['#another', '#this', '#test']); | ||
}); | ||
test('isEmpty should check if a string is empty or contains only whitespace', () => { | ||
expect(isEmpty('Hello World')).toBe(false) | ||
expect(isEmpty(' ')).toBe(true) | ||
}) | ||
expect(isEmpty('Hello World')).toBe(false); | ||
expect(isEmpty(' ')).toBe(true); | ||
}); |
13038
5
170
223
4