jonas_bird-palindrome 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +43 -0
- package/package.json +24 -0
- package/readme.md +17 -0
- package/test/test.js +37 -0
package/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// -*- compile-command: "node palindrome.js" -*-
|
|
2
|
+
module.exports = Phrase;
|
|
3
|
+
|
|
4
|
+
// controversial approach
|
|
5
|
+
String.prototype.reverse = function () {
|
|
6
|
+
return Array.from(this).reverse().join("");
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
// Defines a Phrase object.
|
|
10
|
+
function Phrase(content) {
|
|
11
|
+
this.content = content;
|
|
12
|
+
// Returns content processed for palindrome testing
|
|
13
|
+
this.processedContent = function processedContent() {
|
|
14
|
+
return this.letters().toLowerCase();
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// Returns the letters in the content
|
|
18
|
+
// example: new Phrase("Hello, world!").letters() === "Helloworld"
|
|
19
|
+
this.letters = function letters() {
|
|
20
|
+
const lettersRegEx = /[a-z]/gi;
|
|
21
|
+
return (this.content.match(lettersRegEx) || []).join("");
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// Returns true if the phrase is a palindrome, false otherwise
|
|
25
|
+
this.palindrome = function palindrome() {
|
|
26
|
+
return this.processedContent() === this.processedContent().reverse();
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
this.louder = function () {
|
|
30
|
+
return this.content.toUpperCase();
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function TranslatedPhrase(content, translation) {
|
|
35
|
+
this.content = content;
|
|
36
|
+
this.translation = translation;
|
|
37
|
+
// Example of overriding a method
|
|
38
|
+
// Returns translation processed for palindrome testing.
|
|
39
|
+
this.processedContent = function processedContent() {
|
|
40
|
+
return this.processor(this.translation);
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
TranslatedPhrase.prototype = new Phrase();
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "jonas_bird-palindrome",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Palindrome detector",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "mocha"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/jonas-bird/jonas-bird-palindrome.git"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"learn-enough",
|
|
15
|
+
"javascript",
|
|
16
|
+
"palindrome"
|
|
17
|
+
],
|
|
18
|
+
"author": "",
|
|
19
|
+
"license": "ISC",
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/jonas_bird/jonas-bird-palindrome/issues"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://github.com/jonas_bird/jonas-bird-palindrome#readme"
|
|
24
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Phrase object (with palindrome detector)
|
|
2
|
+
|
|
3
|
+
This is a sample NPM module created in [*Learn Enough JavaScript to Be Dangerous*](https://www.learnenough.com/javascript-tutorial) by Michael Hartl.
|
|
4
|
+
|
|
5
|
+
The module can be used as follows:
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
$ npm install --global jonas-bird-palindrome
|
|
9
|
+
$ vim test.js
|
|
10
|
+
let Phrase = require("jonas-bird-palindrome");
|
|
11
|
+
let napoleonsLament = new Phrase("Able was I, ere I saw Elba.");
|
|
12
|
+
console.log(napoleonsLament.palindrome());
|
|
13
|
+
$ node test.js
|
|
14
|
+
true
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
|
package/test/test.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
let assert = require("assert");
|
|
2
|
+
let Phrase = require("../index.js");
|
|
3
|
+
|
|
4
|
+
describe("Phrase", function () {
|
|
5
|
+
describe("#palindrome", function () {
|
|
6
|
+
it("should return false for non-palindrome", function () {
|
|
7
|
+
let nonPalindrome = new Phrase("apple");
|
|
8
|
+
assert(!nonPalindrome.palindrome());
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it("should return true for a plain palindrome", function () {
|
|
12
|
+
let plainPalindrome = new Phrase("racecar");
|
|
13
|
+
assert(plainPalindrome.palindrome());
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("should return true for a mixed-case palindrome", function () {
|
|
17
|
+
let mixedCasePalindrome = new Phrase("RaCecAr");
|
|
18
|
+
assert(mixedCasePalindrome.palindrome());
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("should return true for a palindrome with punctuation", function () {
|
|
22
|
+
let punctuatedPalindrome = new Phrase("Madam, I'm Adam.");
|
|
23
|
+
assert(punctuatedPalindrome.palindrome());
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe("#letters", function () {
|
|
28
|
+
it("should return only letters", function () {
|
|
29
|
+
let punctuatedPalindrome = new Phrase("Madam, I'm Adam.");
|
|
30
|
+
assert.strictEqual(punctuatedPalindrome.letters(), "MadamImAdam");
|
|
31
|
+
});
|
|
32
|
+
it("should return the empty string on no match", function () {
|
|
33
|
+
let noLetters = new Phrase("1234.56");
|
|
34
|
+
assert.strictEqual(noLetters.letters(), "");
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
});
|