Skip to content

Commit

Permalink
Merge branch 'master' of github.com:bitbar/finka-js
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
#	dist/finka.min.js
#	docs/String.html
#	test/String.js
  • Loading branch information
monikacil committed Oct 9, 2018
2 parents 072da9c + 780534a commit bb710d5
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 45 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
],
"quotes": [
"error",
"single"
"single",
{ "avoidEscape": true }
],
"semi": [
"error",
Expand Down
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#### 1.2.3 (2018-10-08)
#### 1.2.3 (2018-10-07)

* `String`

* fixed `getSimilarity` static method
* fixed `toCamelCase` method
* added `toSnakeCase` method
* added test to this method

* `Date`

* fixed `daysFromNow` test


#### 1.2.2 (2018-09-21)

Expand Down
12 changes: 9 additions & 3 deletions dist/finka.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@
*/
String.getSimilarity = function(a, b) {
var l = Math.max(a.length, b.length);
return (l - String.editDistance(a, b) / 2) / l;
return (l - String.editDistance(a, b)) / l;
};

/**
Expand Down Expand Up @@ -491,6 +491,9 @@
// clean kebab and snake case
value = value.replace(/[-_]/g, ' ');

// clean special characters
value = value.replace(/[^a-z0-9 ]/gi, '');

// clean pascal case
value = value.lowerFirstLetter();

Expand All @@ -499,6 +502,9 @@
value = value.replace(/[A-Z][a-z]/g, function(m) { return ' ' + m.toLowerCase(); });
value = value.replace(/([a-z0-9])([A-Z])/g, function(m, m1, m2) { return m1 + ' ' + m2; });

// minimize white spaces
value = value.trim().replace(/\s{2,}/g, ' ');

return value;
};

Expand All @@ -512,9 +518,9 @@

// normalize
value = value.noCase();

// replace
value = value.replace(/ [a-z]/gi, function(m) { return m[1].toUpperCase(); });
value = value.replace(/ [a-z0-9]/gi, function(m) { return m[1].toUpperCase(); });

return value;
};
Expand Down
12 changes: 9 additions & 3 deletions docs/String.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ <h1><a href="index.html" class="link">Finka.js</a></h1>
*/
String.getSimilarity = function(a, b) {
var l = Math.max(a.length, b.length);
return (l - String.editDistance(a, b) / 2) / l;
return (l - String.editDistance(a, b)) / l;
};

/**
Expand Down Expand Up @@ -200,6 +200,9 @@ <h1><a href="index.html" class="link">Finka.js</a></h1>
// clean kebab and snake case
value = value.replace(/[-_]/g, ' ');

// clean special characters
value = value.replace(/[^a-z0-9 ]/gi, '');

// clean pascal case
value = value.lowerFirstLetter();

Expand All @@ -208,6 +211,9 @@ <h1><a href="index.html" class="link">Finka.js</a></h1>
value = value.replace(/[A-Z][a-z]/g, function(m) { return ' ' + m.toLowerCase(); });
value = value.replace(/([a-z0-9])([A-Z])/g, function(m, m1, m2) { return m1 + ' ' + m2; });

// minimize white spaces
value = value.trim().replace(/\s{2,}/g, ' ');

return value;
};

Expand All @@ -221,9 +227,9 @@ <h1><a href="index.html" class="link">Finka.js</a></h1>

// normalize
value = value.noCase();

// replace
value = value.replace(/ [a-z]/gi, function(m) { return m[1].toUpperCase(); });
value = value.replace(/ [a-z0-9]/gi, function(m) { return m[1].toUpperCase(); });

return value;
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"build": "npx rollup -c",
"test": "npx mocha --file test/_init.js --recursive test",
"coveralls": "npx istanbul cover _mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | npx coveralls && rm -rf ./coverage",
"coveralls": "npx istanbul cover _mocha --report lcovonly -- --file test/_init.js --recursive test && cat ./coverage/lcov.info | npx coveralls && rm -rf ./coverage",
"docs": "npx jsdoc -c jsdoc.json",
"lint": "npx eslint ."
},
Expand Down
12 changes: 9 additions & 3 deletions src/String.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ String.editDistance = function(a, b) {
*/
String.getSimilarity = function(a, b) {
var l = Math.max(a.length, b.length);
return (l - String.editDistance(a, b) / 2) / l;
return (l - String.editDistance(a, b)) / l;
};

/**
Expand Down Expand Up @@ -97,6 +97,9 @@ String.prototype.noCase = function() {
// clean kebab and snake case
value = value.replace(/[-_]/g, ' ');

// clean special characters
value = value.replace(/[^a-z0-9 ]/gi, '');

// clean pascal case
value = value.lowerFirstLetter();

Expand All @@ -105,6 +108,9 @@ String.prototype.noCase = function() {
value = value.replace(/[A-Z][a-z]/g, function(m) { return ' ' + m.toLowerCase(); });
value = value.replace(/([a-z0-9])([A-Z])/g, function(m, m1, m2) { return m1 + ' ' + m2; });

// minimize white spaces
value = value.trim().replace(/\s{2,}/g, ' ');

return value;
};

Expand All @@ -118,9 +124,9 @@ String.prototype.toCamelCase = function() {

// normalize
value = value.noCase();

// replace
value = value.replace(/ [a-z]/gi, function(m) { return m[1].toUpperCase(); });
value = value.replace(/ [a-z0-9]/gi, function(m) { return m[1].toUpperCase(); });

return value;
};
Expand Down
4 changes: 3 additions & 1 deletion test/Date.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ describe('Date', function () {

describe('#daysFromNow', function () {
it('Returns proper timestamp', function() {
expect(Date.daysFromNow(1)).to.be.equal(Date.now() + day);
var expected = Date.now() + day;
var test = Date.daysFromNow(1);
expect(test).to.be.within(expected, expected + 1);
});
});

Expand Down
135 changes: 103 additions & 32 deletions test/String.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,7 @@ const expect = chai.expect;

describe('String', function () {

describe('.noCase', function () {
it('Supports camelCase', function() {
var test = 'mySuperTest111HeHe111HTML';
expect(test.noCase()).to.be.equal('my super test 111 he he 111 HTML');
});

it('Supports PascalCase', function() {
var test = 'MySuperTest111HeHe111HTML';
expect(test.noCase()).to.be.equal('my super test 111 he he 111 HTML');
});

it('Supports kebab-case', function() {
var test = 'my-super-test-111-he-he-111-HTML';
expect(test.noCase()).to.be.equal('my super test 111 he he 111 HTML');
});

it('Supports snake_case', function() {
var test = 'my_super_test_111_he_111_HTML';
expect(test.noCase()).to.be.equal('my super test 111 he 111 HTML');
});

it('Supports CAPITALIZED_SNAKE_CASE', function() {
var test = 'MY_SUPER_TEST_111_HE';
expect(test.noCase()).to.be.equal('my super test 111 he');
});
});

describe('.editDistance', function () {

describe('#editDistance', function () {
it('Is associative', function() {
var word1 = 'kitte';
var word2 = 'kitten';
Expand All @@ -57,15 +29,46 @@ describe('String', function () {
expect(String.editDistance(word1, word2)).to.be.equal(3);
});

it('Takes into account camelCase', function() {
it('Is case sensitive', function() {
var word1 = 'Kitten';
var word2 = 'kitten';
expect(String.editDistance(word1, word2)).to.be.equal(1);
});
});

describe('.capitaliseFirstLetter', function () {
describe('#getSimilarity', function() {
it('Is associative', function() {
var word1 = 'chleb';
var word2 = 'chlep';
expect(String.getSimilarity(word1, word2)).to.be.equal(0.8);
expect(String.getSimilarity(word2, word1)).to.be.equal(0.8);
});

it('Returns 100% (1.0) similarity when no difference between strings', function() {
var word = 'chleb';
expect(String.getSimilarity(word, word)).to.be.equal(1);
});

it('Returns proper similarity', function() {
var word1 = 'chleb';
var word2 = 'chlebeczek';
expect(String.getSimilarity(word1, word2)).to.be.equal(0.5);
});

it('Returns proper similarity (2)', function() {
var word1 = 'chleb';
var word2 = 'belhc';
expect(String.getSimilarity(word1, word2)).to.be.equal(0.2);
});

it('Is case sensitive', function() {
var word1 = 'Chleb';
var word2 = 'chleb';
expect(String.getSimilarity(word1, word2)).to.be.equal(0.8);
});
});

describe('.capitaliseFirstLetter', function () {
it('Return string with only first letter capitalized', function() {
var word = 'que Tal?';
var result = 'Que Tal?';
Expand All @@ -85,8 +88,75 @@ describe('String', function () {
});
});

describe('.toSnakeCase', function () {
describe('.lowerFirstLetter', function () {
it('Return string with only first letter lowered', function() {
var word = 'Que Tal?';
var result = 'que Tal?';
expect(word.lowerFirstLetter()).to.be.equal(result);
});

it('Return string with very first letter lowered', function() {
var word = '?Works!';
var result = '?works!';
expect(word.lowerFirstLetter()).to.be.equal(result);
});
});

describe('.noCase', function () {
it('Supports camelCase', function() {
var test = 'mySuperTest111HeHe111HTML';
expect(test.noCase()).to.be.equal('my super test 111 he he 111 HTML');
});

it('Supports PascalCase', function() {
var test = 'MySuperTest111HeHe111HTML';
expect(test.noCase()).to.be.equal('my super test 111 he he 111 HTML');
});

it('Supports kebab-case', function() {
var test = 'my-super-test-111-he-he-111-HTML';
expect(test.noCase()).to.be.equal('my super test 111 he he 111 HTML');
});

it('Supports snake_case', function() {
var test = 'my_super_test_111_he_111_HTML';
expect(test.noCase()).to.be.equal('my super test 111 he 111 HTML');
});

it('Supports CAPITALIZED_SNAKE_CASE', function() {
var test = 'MY_SUPER_TEST_111_HE';
expect(test.noCase()).to.be.equal('my super test 111 he');
});
});

describe('.toCamelCase', function () {
it('Should make no change at all', function() {
var test = 'hello';
expect(test.toCamelCase()).to.be.equal(test);
});

it('Supports normal sentence', function() {
var test = "Hello, I'm from District 9 and I'm looking for a job!";
expect(test.toCamelCase()).to.be.equal('helloImFromDistrict9AndImLookingForAJob');
});

it('Supports kebab-case', function() {
var test = 'my-super-test-111-he-he-111-HTML';
expect(test.toCamelCase()).to.be.equal('mySuperTest111HeHe111HTML');
});

it('Supports snake_case', function() {
var test = 'my_super_test_111_he_111_HTML';
expect(test.toCamelCase()).to.be.equal('mySuperTest111He111HTML');
});

it('Supports CAPITALIZED_SNAKE_CASE', function() {
var test = 'MY_SUPER_TEST_111_HE';
expect(test.toCamelCase()).to.be.equal('mySuperTest111He');
});
});

describe('.toSnakeCase', function () {
it('CamelCase to snake_case', function() {
var test = 'itShouldWorks';
expect(test.toSnakeCase()).to.be.equal('it_should_works');
Expand Down Expand Up @@ -125,4 +195,5 @@ describe('String', function () {
expect(test.toSnakeCase(true)).to.be.equal('IT_SHOULD_WORKS');
});
});

});

0 comments on commit bb710d5

Please sign in to comment.