From bd70fd2ada9cf3c27864f40de1952df9c62e47cd Mon Sep 17 00:00:00 2001 From: Carlos Carvalho Date: Mon, 5 Oct 2020 17:03:56 -0300 Subject: [PATCH 1/5] Added new algoritm --- Linear-Algebra/test/test.js | 2 +- String/CheckWordOccurrence.js | 25 +++++++++++++++++++++++ String/CheckWordOcurrence.test.js | 34 +++++++++++++++++++++++++++++++ package.json | 4 +++- 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 String/CheckWordOccurrence.js create mode 100644 String/CheckWordOcurrence.test.js diff --git a/Linear-Algebra/test/test.js b/Linear-Algebra/test/test.js index 5f1892db8..18cd00500 100644 --- a/Linear-Algebra/test/test.js +++ b/Linear-Algebra/test/test.js @@ -11,7 +11,7 @@ var assert = require('assert') var fs = require('fs') // file is included here -eval(fs.readFileSync('src/la_lib.js') + '') +eval(fs.readFileSync('../src/la_lib.js') + '') // Tests goes here // creating some vectors diff --git a/String/CheckWordOccurrence.js b/String/CheckWordOccurrence.js new file mode 100644 index 000000000..d17b2a0cf --- /dev/null +++ b/String/CheckWordOccurrence.js @@ -0,0 +1,25 @@ +/** + * Check and count occurrence of each word in a string + * Inputs a String eg. Madonna and Boolean + */ + +const checkWordOccurrence = (str, isCaseSensitive = false) => { + if (typeof str != 'string') { + throw new TypeError('The first param should be a string'); + } + if (typeof isCaseSensitive != 'boolean') { + throw new TypeError('The second param should be a boolean') + } + + let result = {} + if (str.length > 0) { + for (let i = 0; i < str.length; i++) { + const word = isCaseSensitive ? str[i] : str[i].toUpperCase() + if(/\s/.test(word)) continue; + result[word] = (!result[word]) ? 1 : result[word] + 1 + } + + } + return result; +} +export { checkWordOccurrence } \ No newline at end of file diff --git a/String/CheckWordOcurrence.test.js b/String/CheckWordOcurrence.test.js new file mode 100644 index 000000000..0e57ad655 --- /dev/null +++ b/String/CheckWordOcurrence.test.js @@ -0,0 +1,34 @@ +import {checkWordOccurrence} from './CheckWordOccurrence'; +describe('checkWordOccurrence', () => { + it('expects throw on insert wrong string', () => { + const value = 123; + expect(() => checkWordOccurrence(value)).toThrow(); + }); + it('expect throw on insert wrong param for case sensitive', () => { + const value = 'hello'; + expect(() => checkWordOccurrence(value, value)).toThrow(); + }); + it('check occurrence with case sensitive', () => { + const stringToTest = "A Mad World"; + const charsOccurrences = checkWordOccurrence(stringToTest, true); + const expectResult = {A: 1, M: 1, a: 1, d: 2, W: 1, l: 1, o: 1, r: 1}; + const occurrencesObjectKeys = Object.keys(charsOccurrences); + const expectObjectKeys = Object.keys(expectResult); + expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length); + expectObjectKeys.forEach(key => { + expect(expectResult[key]).toBe(charsOccurrences[key]); + }); + }); + it('check occurrence with case insensitive', () => { + const stringToTest = "A Mad World"; + const charsOccurrences = checkWordOccurrence(stringToTest, false); + const expectResult = {A: 2, D: 2, L: 1, M: 1, O: 1, R: 1, W: 1}; + const occurrencesObjectKeys = Object.keys(charsOccurrences); + const expectObjectKeys = Object.keys(expectResult); + expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length); + expectObjectKeys.forEach(key => { + expect(expectResult[key]).toBe(charsOccurrences[key]); + }); + + }); +}); \ No newline at end of file diff --git a/package.json b/package.json index eea349d1b..450a58349 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,9 @@ "node-fetch": "2.6.1" }, "standard": { - "env": [ "jest" ] + "env": [ + "jest" + ] }, "devDependencies": { "babel-jest": "^26.3.0", From 5ba5a603bb4fdde6294d4153c706124350769827 Mon Sep 17 00:00:00 2001 From: Carlos Carvalho Date: Mon, 5 Oct 2020 17:06:45 -0300 Subject: [PATCH 2/5] remove wrong edited file package.json --- package.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/package.json b/package.json index 450a58349..eea349d1b 100644 --- a/package.json +++ b/package.json @@ -16,9 +16,7 @@ "node-fetch": "2.6.1" }, "standard": { - "env": [ - "jest" - ] + "env": [ "jest" ] }, "devDependencies": { "babel-jest": "^26.3.0", From dd40c139d5e048a03b7f3a0b28bb1185bf02f913 Mon Sep 17 00:00:00 2001 From: Carlos Carvalho Date: Mon, 5 Oct 2020 23:44:20 -0300 Subject: [PATCH 3/5] remove wrong edited file --- Linear-Algebra/test/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Linear-Algebra/test/test.js b/Linear-Algebra/test/test.js index 18cd00500..5f1892db8 100644 --- a/Linear-Algebra/test/test.js +++ b/Linear-Algebra/test/test.js @@ -11,7 +11,7 @@ var assert = require('assert') var fs = require('fs') // file is included here -eval(fs.readFileSync('../src/la_lib.js') + '') +eval(fs.readFileSync('src/la_lib.js') + '') // Tests goes here // creating some vectors From fbd3442cf0c5cc69f259b99cea4397721fdf6d2e Mon Sep 17 00:00:00 2001 From: Carlos Carvalho Date: Mon, 5 Oct 2020 23:52:21 -0300 Subject: [PATCH 4/5] fix ident file --- String/CheckWordOccurrence.js | 30 ++++++++-------- String/CheckWordOcurrence.test.js | 58 +++++++++++++++---------------- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/String/CheckWordOccurrence.js b/String/CheckWordOccurrence.js index d17b2a0cf..f31df400b 100644 --- a/String/CheckWordOccurrence.js +++ b/String/CheckWordOccurrence.js @@ -4,22 +4,22 @@ */ const checkWordOccurrence = (str, isCaseSensitive = false) => { - if (typeof str != 'string') { - throw new TypeError('The first param should be a string'); - } - if (typeof isCaseSensitive != 'boolean') { - throw new TypeError('The second param should be a boolean') - } + if (typeof str != 'string') { + throw new TypeError('The first param should be a string'); + } + if (typeof isCaseSensitive != 'boolean') { + throw new TypeError('The second param should be a boolean') + } - let result = {} - if (str.length > 0) { - for (let i = 0; i < str.length; i++) { - const word = isCaseSensitive ? str[i] : str[i].toUpperCase() - if(/\s/.test(word)) continue; - result[word] = (!result[word]) ? 1 : result[word] + 1 - } + let result = {} + if (str.length > 0) { + for (let i = 0; i < str.length; i++) { + const word = isCaseSensitive ? str[i] : str[i].toUpperCase() + if (/\s/.test(word)) continue; + result[word] = (!result[word]) ? 1 : result[word] + 1 + } - } - return result; + } + return result; } export { checkWordOccurrence } \ No newline at end of file diff --git a/String/CheckWordOcurrence.test.js b/String/CheckWordOcurrence.test.js index 0e57ad655..cd5673885 100644 --- a/String/CheckWordOcurrence.test.js +++ b/String/CheckWordOcurrence.test.js @@ -1,34 +1,34 @@ -import {checkWordOccurrence} from './CheckWordOccurrence'; +import { checkWordOccurrence } from './CheckWordOccurrence'; describe('checkWordOccurrence', () => { - it('expects throw on insert wrong string', () => { - const value = 123; - expect(() => checkWordOccurrence(value)).toThrow(); + it('expects throw on insert wrong string', () => { + const value = 123; + expect(() => checkWordOccurrence(value)).toThrow(); + }); + it('expect throw on insert wrong param for case sensitive', () => { + const value = 'hello'; + expect(() => checkWordOccurrence(value, value)).toThrow(); + }); + it('check occurrence with case sensitive', () => { + const stringToTest = "A Mad World"; + const charsOccurrences = checkWordOccurrence(stringToTest, true); + const expectResult = { A: 1, M: 1, a: 1, d: 2, W: 1, l: 1, o: 1, r: 1 }; + const occurrencesObjectKeys = Object.keys(charsOccurrences); + const expectObjectKeys = Object.keys(expectResult); + expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length); + expectObjectKeys.forEach(key => { + expect(expectResult[key]).toBe(charsOccurrences[key]); }); - it('expect throw on insert wrong param for case sensitive', () => { - const value = 'hello'; - expect(() => checkWordOccurrence(value, value)).toThrow(); + }); + it('check occurrence with case insensitive', () => { + const stringToTest = "A Mad World"; + const charsOccurrences = checkWordOccurrence(stringToTest, false); + const expectResult = { A: 2, D: 2, L: 1, M: 1, O: 1, R: 1, W: 1 }; + const occurrencesObjectKeys = Object.keys(charsOccurrences); + const expectObjectKeys = Object.keys(expectResult); + expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length); + expectObjectKeys.forEach(key => { + expect(expectResult[key]).toBe(charsOccurrences[key]); }); - it('check occurrence with case sensitive', () => { - const stringToTest = "A Mad World"; - const charsOccurrences = checkWordOccurrence(stringToTest, true); - const expectResult = {A: 1, M: 1, a: 1, d: 2, W: 1, l: 1, o: 1, r: 1}; - const occurrencesObjectKeys = Object.keys(charsOccurrences); - const expectObjectKeys = Object.keys(expectResult); - expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length); - expectObjectKeys.forEach(key => { - expect(expectResult[key]).toBe(charsOccurrences[key]); - }); - }); - it('check occurrence with case insensitive', () => { - const stringToTest = "A Mad World"; - const charsOccurrences = checkWordOccurrence(stringToTest, false); - const expectResult = {A: 2, D: 2, L: 1, M: 1, O: 1, R: 1, W: 1}; - const occurrencesObjectKeys = Object.keys(charsOccurrences); - const expectObjectKeys = Object.keys(expectResult); - expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length); - expectObjectKeys.forEach(key => { - expect(expectResult[key]).toBe(charsOccurrences[key]); - }); - }); + }); }); \ No newline at end of file From 86ed0d882949c9ed0144dd53b71c85daf03b4a2c Mon Sep 17 00:00:00 2001 From: Carlos Carvalho Date: Tue, 6 Oct 2020 00:08:52 -0300 Subject: [PATCH 5/5] fix code style using standardjs --- String/CheckWordOccurrence.js | 38 +++++++++++----------- String/CheckWordOcurrence.test.js | 53 +++++++++++++++---------------- package-lock.json | 7 ++-- package.json | 4 ++- 4 files changed, 51 insertions(+), 51 deletions(-) diff --git a/String/CheckWordOccurrence.js b/String/CheckWordOccurrence.js index f31df400b..b86150c0b 100644 --- a/String/CheckWordOccurrence.js +++ b/String/CheckWordOccurrence.js @@ -1,25 +1,25 @@ /** - * Check and count occurrence of each word in a string - * Inputs a String eg. Madonna and Boolean - */ + * Check and count occurrence of each word in a string + * Inputs a String eg. Madonna and Boolean + **/ const checkWordOccurrence = (str, isCaseSensitive = false) => { - if (typeof str != 'string') { - throw new TypeError('The first param should be a string'); - } - if (typeof isCaseSensitive != 'boolean') { - throw new TypeError('The second param should be a boolean') - } + if (typeof str !== 'string') { + throw new TypeError('The first param should be a string') + } + if (typeof isCaseSensitive !== 'boolean') { + throw new TypeError('The second param should be a boolean') + } - let result = {} - if (str.length > 0) { - for (let i = 0; i < str.length; i++) { - const word = isCaseSensitive ? str[i] : str[i].toUpperCase() - if (/\s/.test(word)) continue; - result[word] = (!result[word]) ? 1 : result[word] + 1 - } + const result = {} + if (str.length > 0) { + for (let i = 0; i < str.length; i++) { + const word = isCaseSensitive ? str[i] : str[i].toUpperCase() + if (/\s/.test(word)) continue + result[word] = (!result[word]) ? 1 : result[word] + 1 + } + } - } - return result; + return result } -export { checkWordOccurrence } \ No newline at end of file +export { checkWordOccurrence } diff --git a/String/CheckWordOcurrence.test.js b/String/CheckWordOcurrence.test.js index cd5673885..2b1a88782 100644 --- a/String/CheckWordOcurrence.test.js +++ b/String/CheckWordOcurrence.test.js @@ -1,34 +1,33 @@ -import { checkWordOccurrence } from './CheckWordOccurrence'; +import { checkWordOccurrence } from './CheckWordOccurrence' describe('checkWordOccurrence', () => { it('expects throw on insert wrong string', () => { - const value = 123; - expect(() => checkWordOccurrence(value)).toThrow(); - }); + const value = 123 + expect(() => checkWordOccurrence(value)).toThrow() + }) it('expect throw on insert wrong param for case sensitive', () => { - const value = 'hello'; - expect(() => checkWordOccurrence(value, value)).toThrow(); - }); + const value = 'hello' + expect(() => checkWordOccurrence(value, value)).toThrow() + }) it('check occurrence with case sensitive', () => { - const stringToTest = "A Mad World"; - const charsOccurrences = checkWordOccurrence(stringToTest, true); - const expectResult = { A: 1, M: 1, a: 1, d: 2, W: 1, l: 1, o: 1, r: 1 }; - const occurrencesObjectKeys = Object.keys(charsOccurrences); - const expectObjectKeys = Object.keys(expectResult); - expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length); + const stringToTest = 'A Mad World' + const charsOccurrences = checkWordOccurrence(stringToTest, true) + const expectResult = { A: 1, M: 1, a: 1, d: 2, W: 1, l: 1, o: 1, r: 1 } + const occurrencesObjectKeys = Object.keys(charsOccurrences) + const expectObjectKeys = Object.keys(expectResult) + expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length) expectObjectKeys.forEach(key => { - expect(expectResult[key]).toBe(charsOccurrences[key]); - }); - }); + expect(expectResult[key]).toBe(charsOccurrences[key]) + }) + }) it('check occurrence with case insensitive', () => { - const stringToTest = "A Mad World"; - const charsOccurrences = checkWordOccurrence(stringToTest, false); - const expectResult = { A: 2, D: 2, L: 1, M: 1, O: 1, R: 1, W: 1 }; - const occurrencesObjectKeys = Object.keys(charsOccurrences); - const expectObjectKeys = Object.keys(expectResult); - expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length); + const stringToTest = 'A Mad World' + const charsOccurrences = checkWordOccurrence(stringToTest, false) + const expectResult = { A: 2, D: 2, L: 1, M: 1, O: 1, R: 1, W: 1 } + const occurrencesObjectKeys = Object.keys(charsOccurrences) + const expectObjectKeys = Object.keys(expectResult) + expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length) expectObjectKeys.forEach(key => { - expect(expectResult[key]).toBe(charsOccurrences[key]); - }); - - }); -}); \ No newline at end of file + expect(expectResult[key]).toBe(charsOccurrences[key]) + }) + }) +}) diff --git a/package-lock.json b/package-lock.json index 9bcf2855d..20ddcf663 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3734,12 +3734,11 @@ }, "dependencies": { "ansi-styles": { - "version": "4.2.1", - "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.2.1.tgz", - "integrity": "sha512-9VGjrMsG1vePxcSweQsN20KY/c4zN0h9fLjqAbwbPfahM3t+NL+M9HC8xeXG2I8pX5NoamTGNuomEUFI7fcUjA==", + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", "dev": true, "requires": { - "@types/color-name": "^1.1.1", "color-convert": "^2.0.1" } }, diff --git a/package.json b/package.json index eea349d1b..450a58349 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,9 @@ "node-fetch": "2.6.1" }, "standard": { - "env": [ "jest" ] + "env": [ + "jest" + ] }, "devDependencies": { "babel-jest": "^26.3.0",