paulos-palindrome
Version:
__An NPM module sample.__ _Usage:_ ```javascript npm install paulos-palindrome let Phrase = require('paulos-palindrome') let string = new Phrase("Did I do, O God, did I as I said I’d do? Good! I did.") console.log(string.palindrome()) ```
32 lines (27 loc) • 894 B
JavaScript
module.exports = Phrase
// Adds 'reverse' to all strings
String.prototype.reverse = function(){
return Array.from(this).reverse().join('')
}
// Defines a Phrase object
function Phrase(content){
this.content = content;
// Returns content processed for palindrome testing
this.processedContent = function processedContent(){
return this.letters().toLowerCase()
}
// Returns the letters in the content. For example:
// new Phrase("Hello, world!").letters() === 'Helloworld'
this.letters = function letters() {
const lettersRegEx = /[a-z]/gi;
return (this.content.match(lettersRegEx) || []).join('')
}
// Returns true if the phrase is a palindrome, false otherwise
this.palindrome = function palindrome(){
if (this.processedContent()){
return this.processedContent() == this.processedContent().reverse()
} else {
return false
}
}
}