Last active
April 21, 2017 10:00
-
-
Save szepeshazi/b4827c679ba11ed8897e8fc4bbeabd40 to your computer and use it in GitHub Desktop.
StringSet implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const StringSetBase = require("./string-set-base"); | |
class StringSet extends StringSetBase { | |
constructor() { | |
super(); | |
this.stringSet = new Set(); | |
} | |
/** | |
* Adds a new element to the set | |
* @param {String} element | |
*/ | |
add(element) { | |
if (typeof element !== 'string') { | |
throw new TypeError("StringSet.add(element) only accepts a single input string"); | |
} | |
this.stringSet.add(element); | |
} | |
/** | |
* Removes and element from the set | |
* @param {String} element | |
*/ | |
remove(element) { | |
if (typeof element !== 'string') { | |
throw new TypeError("StringSet.remove(element) only accepts a single input string"); | |
} | |
this.stringSet.delete(element); | |
} | |
/** | |
* Decides if element is part of the set | |
* @param {String} element | |
* @return {Boolean} | |
*/ | |
contains(element) { | |
if (typeof element !== 'string') { | |
throw new TypeError("StringSet.contains(element) only accepts a single input string"); | |
} | |
return this.stringSet.has(element); | |
} | |
} | |
module.exports = StringSet; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const StringSet = require('../skawa/string-set/string-set'); | |
describe('StringSet', () => { | |
let mySet; | |
beforeEach(function () { | |
mySet = new StringSet(); | |
}); | |
it('should not contain any given element by default', function () { | |
expect(mySet.contains('abrakadabra')).toBeFalsy("Should not contain 'abrakadabra' by default"); | |
}); | |
it('should contain a given element after add()', function () { | |
expect(mySet.contains('abrakadabra')).toBeFalsy("Should not contain 'abrakadabra' by default"); | |
mySet.add('abrakadabra'); | |
expect(mySet.contains('abrakadabra')).toBeTruthy("Should contain 'abrakadabra' after calling add()"); | |
}); | |
it('should add and remove elements only once', function () { | |
expect(mySet.contains('abrakadabra')).toBeFalsy("Should not contain 'abrakadabra' by default"); | |
mySet.add('abrakadabra'); | |
mySet.add('abrakadabra'); | |
expect(mySet.contains('abrakadabra')).toBeTruthy("Should contain 'abrakadabra' after calling add()"); | |
mySet.remove('abrakadabra'); | |
expect(mySet.contains('abrakadabra')).toBeFalsy("Should not contain 'abrakadabra' after calling remove()"); | |
}); | |
it('should not accept non-string values', function () { | |
try { | |
mySet.add(42); | |
fail('should throw an exception when adding non-string values'); | |
} catch (exception) { | |
expect(exception.message).toEqual('StringSet.add(element) only accepts a single input string'); | |
} | |
}); | |
it('should not check for non-string values', function () { | |
try { | |
mySet.contains(false); | |
fail('should throw an exception when checking for non-string values') | |
} catch (exception) { | |
expect(exception.message).toEqual('StringSet.contains(element) only accepts a single input string'); | |
} | |
}); | |
it('should not try to remove non-string values', function () { | |
try { | |
mySet.remove(undefined); | |
fail('should throw an exception when trying to remove non-string values'); | |
} catch (exception) { | |
expect(exception.message).toEqual('StringSet.remove(element) only accepts a single input string'); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment