mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
fix code style using standardjs
This commit is contained in:
@ -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 }
|
||||
export { checkWordOccurrence }
|
||||
|
@ -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]);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
expect(expectResult[key]).toBe(charsOccurrences[key])
|
||||
})
|
||||
})
|
||||
})
|
||||
|
7
package-lock.json
generated
7
package-lock.json
generated
@ -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"
|
||||
}
|
||||
},
|
||||
|
@ -16,7 +16,9 @@
|
||||
"node-fetch": "2.6.1"
|
||||
},
|
||||
"standard": {
|
||||
"env": [ "jest" ]
|
||||
"env": [
|
||||
"jest"
|
||||
]
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-jest": "^26.3.0",
|
||||
|
Reference in New Issue
Block a user