Skip to content

Commit

Permalink
Use Symbols for some constants
Browse files Browse the repository at this point in the history
  • Loading branch information
ryu1kn committed Jun 3, 2017
1 parent 88a338b commit 1d1f79b
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 27 deletions.
4 changes: 3 additions & 1 deletion lib/commands/toggle-case-sensitivity.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

const PatternAction = require('../const').PatternAction;

class ToggleCaseSensitivityCommand {

constructor(params) {
Expand All @@ -16,7 +18,7 @@ class ToggleCaseSensitivityCommand {

const visibleEditors = this._vsWindow.visibleTextEditors;
const decorationOperator = this._decorationOperatorFactory.create(visibleEditors);
decorationOperator.updateDecoration(decorationId, 'toggle-case-sensitivity');
decorationOperator.updateDecoration(decorationId, PatternAction.TOGGLE_CASE_SENSITIVITY);
})
.catch(e => this._handleError(e));
}
Expand Down
4 changes: 3 additions & 1 deletion lib/commands/toggle-whole-match.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

const PatternAction = require('../const').PatternAction;

class ToggleWholeMatchCommand {

constructor(params) {
Expand All @@ -16,7 +18,7 @@ class ToggleWholeMatchCommand {

const visibleEditors = this._vsWindow.visibleTextEditors;
const decorationOperator = this._decorationOperatorFactory.create(visibleEditors);
decorationOperator.updateDecoration(decorationId, 'toggle-whole-match');
decorationOperator.updateDecoration(decorationId, PatternAction.TOGGLE_WHOLE_MATCH);
})
.catch(e => this._handleError(e));
}
Expand Down
11 changes: 8 additions & 3 deletions lib/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ module.exports = {
EXTENSION_NAME: 'TextMarker',

Event: {
EXTENSION_READY: 'TEXT_MARKER_READY',
TOGGLED_CASE_SENSITIVITY: 'TOGGLED_CASE_SENSITIVITY',
MATCHING_MODE_INITIALISED: 'MATCHING_MODE_INITIALISED'
EXTENSION_READY: Symbol('text-marker-ready'),
TOGGLED_CASE_SENSITIVITY: Symbol('case-sensitivity-toggled'),
MATCHING_MODE_INITIALISED: Symbol('matching-mode-initialised')
},

PatternAction: {
TOGGLE_CASE_SENSITIVITY: Symbol('toggle-case-sensitivity'),
TOGGLE_WHOLE_MATCH: Symbol('toggle-whole-match')
}

};
8 changes: 4 additions & 4 deletions lib/matching-mode-registry.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

const Const = require('./const');
const Event = require('./const').Event;

class MatchingModeRegistry {

Expand All @@ -10,14 +10,14 @@ class MatchingModeRegistry {
}

_broadcastReady() {
this._eventBus.once(Const.Event.EXTENSION_READY, () => {
this._eventBus.emit(Const.Event.MATCHING_MODE_INITIALISED, this.mode);
this._eventBus.once(Event.EXTENSION_READY, () => {
this._eventBus.emit(Event.MATCHING_MODE_INITIALISED, this.mode);
});
}

toggleCaseSensitivity() {
this._ignoreCase = !this._ignoreCase;
this._eventBus.emit(Const.Event.TOGGLED_CASE_SENSITIVITY, this.mode);
this._eventBus.emit(Event.TOGGLED_CASE_SENSITIVITY, this.mode);
}

get mode() {
Expand Down
6 changes: 4 additions & 2 deletions lib/pattern-converter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@

const PatternAction = require('./const').PatternAction;

class PatternConverter {

convert(pattern, convertAction) {
switch (convertAction) {
case 'toggle-case-sensitivity':
case PatternAction.TOGGLE_CASE_SENSITIVITY:
return pattern.toggleCaseSensitivity();
case 'toggle-whole-match':
case PatternAction.TOGGLE_WHOLE_MATCH:
return pattern.toggleWholeMatch();
default:
throw new Error(`Unknown action ${convertAction}`);
Expand Down
5 changes: 3 additions & 2 deletions lib/toggle-case-sensitivity-mode-button.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

const Const = require('./const');
const Event = Const.Event;

class ToggleCaseSensitivityModeButton {

Expand All @@ -10,8 +11,8 @@ class ToggleCaseSensitivityModeButton {
}

_registerListeners() {
this._eventBus.on(Const.Event.MATCHING_MODE_INITIALISED, this._initialiseButton.bind(this));
this._eventBus.on(Const.Event.TOGGLED_CASE_SENSITIVITY, this._updateButton.bind(this));
this._eventBus.on(Event.MATCHING_MODE_INITIALISED, this._initialiseButton.bind(this));
this._eventBus.on(Event.TOGGLED_CASE_SENSITIVITY, this._updateButton.bind(this));
}

_initialiseButton(params) {
Expand Down
3 changes: 2 additions & 1 deletion test/unit/commands/toggle-case-sensitivity.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

const PatternAction = require('../../../lib/const').PatternAction;
const ToggleCaseSensitivityCommand = require('../../../lib/commands/toggle-case-sensitivity');

suite('ToggleCaseSensitivityCommand', () => {
Expand All @@ -12,7 +13,7 @@ suite('ToggleCaseSensitivityCommand', () => {

return command.execute().then(() => {
expect(decorationOperatorFactory.create).to.have.been.calledWith(['EDITOR_1', 'EDITOR_2']);
expect(decorationOperator.updateDecoration).to.have.been.calledWith('DECORATION_ID', 'toggle-case-sensitivity');
expect(decorationOperator.updateDecoration).to.have.been.calledWith('DECORATION_ID', PatternAction.TOGGLE_CASE_SENSITIVITY);
expect(highlightPatternPicker.pick).to.have.been.calledWith('Select a pattern to toggle case sensitivity');
});
});
Expand Down
3 changes: 2 additions & 1 deletion test/unit/commands/toggle-whole-match.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

const PatternAction = require('../../../lib/const').PatternAction;
const ToggleWholeMatchCommand = require('../../../lib/commands/toggle-whole-match');

suite('ToggleWholeMatchCommand', () => {
Expand All @@ -12,7 +13,7 @@ suite('ToggleWholeMatchCommand', () => {

return command.execute().then(() => {
expect(decorationOperatorFactory.create).to.have.been.calledWith(['EDITOR_1', 'EDITOR_2']);
expect(decorationOperator.updateDecoration).to.have.been.calledWith('DECORATION_ID', 'toggle-whole-match');
expect(decorationOperator.updateDecoration).to.have.been.calledWith('DECORATION_ID', PatternAction.TOGGLE_WHOLE_MATCH);
expect(highlightPatternPicker.pick).to.have.been.calledWith('Select a pattern to toggle partial/whole match');
});
});
Expand Down
3 changes: 2 additions & 1 deletion test/unit/decoration-operator.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

const DecorationOperator = require('../../lib/decoration-operator');
const PatternAction = require('../../lib/const').PatternAction;
const PatternConverter = require('../../lib/pattern-converter');

suite('DecorationOperator', () => {
Expand Down Expand Up @@ -132,7 +133,7 @@ suite('DecorationOperator', () => {
textDecorator,
patternConverter: new PatternConverter()
});
operator.updateDecoration('DECORATION_ID', 'toggle-case-sensitivity');
operator.updateDecoration('DECORATION_ID', PatternAction.TOGGLE_CASE_SENSITIVITY);

expect(decorationRegistry.updatePattern).to.have.been.calledWith('DECORATION_ID', 'NEW_PATTERN');
expect(textDecorator.undecorate).to.have.been.calledWith(editors, ['DECORATION_TYPE']);
Expand Down
8 changes: 4 additions & 4 deletions test/unit/matching-mode-registry.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

const EventEmitter = require('events');
const Const = require('../../lib/const');
const Event = require('../../lib/const').Event;
const MatchingModeRegistry = require('../../lib/matching-mode-registry');

suite('MatchingModeRegistry', () => {
Expand All @@ -22,7 +22,7 @@ suite('MatchingModeRegistry', () => {
const eventBus = new EventEmitter();
const registry = new MatchingModeRegistry({eventBus});

eventBus.on(Const.Event.TOGGLED_CASE_SENSITIVITY, mode => {
eventBus.on(Event.TOGGLED_CASE_SENSITIVITY, mode => {
expect(mode).to.have.property('ignoreCase');
done();
});
Expand All @@ -33,11 +33,11 @@ suite('MatchingModeRegistry', () => {
const eventBus = new EventEmitter();
new MatchingModeRegistry({eventBus}); // eslint-disable-line no-new

eventBus.on(Const.Event.MATCHING_MODE_INITIALISED, mode => {
eventBus.on(Event.MATCHING_MODE_INITIALISED, mode => {
expect(mode).to.have.property('ignoreCase');
done();
});
eventBus.emit(Const.Event.EXTENSION_READY);
eventBus.emit(Event.EXTENSION_READY);
});

});
5 changes: 3 additions & 2 deletions test/unit/pattern-converter.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

const PatternAction = require('../../lib/const').PatternAction;
const PatternConverter = require('../../lib/pattern-converter');
const StringPattern = require('../../lib/patterns/string');

Expand All @@ -9,7 +10,7 @@ suite('PatternConverter', () => {
const pattern = new StringPattern({phrase: 'text'});
expect(pattern.ignoreCase).to.be.false;

const pattern2 = converter.convert(pattern, 'toggle-case-sensitivity');
const pattern2 = converter.convert(pattern, PatternAction.TOGGLE_CASE_SENSITIVITY);
expect(pattern2.ignoreCase).to.be.true;
});

Expand All @@ -18,7 +19,7 @@ suite('PatternConverter', () => {
const pattern = new StringPattern({phrase: 'text'});
expect(pattern.wholeMatch).to.be.false;

const pattern2 = converter.convert(pattern, 'toggle-whole-match');
const pattern2 = converter.convert(pattern, PatternAction.TOGGLE_WHOLE_MATCH);
expect(pattern2.wholeMatch).to.be.true;
});

Expand Down
10 changes: 5 additions & 5 deletions test/unit/toggle-case-sensitivity-mode-button.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

const EventEmitter = require('events');
const Const = require('../../lib/const');
const Event = require('../../lib/const').Event;
const ToggleCaseSensitivityModeButton = require('../../lib/toggle-case-sensitivity-mode-button');

suite('ToggleCaseSensitivityModeButton', () => {
Expand All @@ -10,7 +10,7 @@ suite('ToggleCaseSensitivityModeButton', () => {
const statusBarItem = {show: sinon.spy()};
new ToggleCaseSensitivityModeButton({eventBus, statusBarItem}); // eslint-disable-line no-new

eventBus.on(Const.Event.MATCHING_MODE_INITIALISED, () => {
eventBus.on(Event.MATCHING_MODE_INITIALISED, () => {
expect(statusBarItem).to.contain({
command: 'textmarker.toggleModeForCaseSensitivity',
text: '[Aa]',
Expand All @@ -19,23 +19,23 @@ suite('ToggleCaseSensitivityModeButton', () => {
expect(statusBarItem.show).to.have.been.called;
done();
});
eventBus.emit(Const.Event.MATCHING_MODE_INITIALISED, {ignoreCase: false});
eventBus.emit(Event.MATCHING_MODE_INITIALISED, {ignoreCase: false});
});

test('it updates button appearance on receving case sensitivity mode change', done => {
const eventBus = new EventEmitter();
const statusBarItem = {show: sinon.spy()};
new ToggleCaseSensitivityModeButton({eventBus, statusBarItem}); // eslint-disable-line no-new

eventBus.on(Const.Event.TOGGLED_CASE_SENSITIVITY, () => {
eventBus.on(Event.TOGGLED_CASE_SENSITIVITY, () => {
expect(statusBarItem).to.contain({
text: 'Aa',
tooltip: 'TextMarker: Case Insensitive Mode'
});
expect(statusBarItem.show).to.not.have.been.called;
done();
});
eventBus.emit(Const.Event.TOGGLED_CASE_SENSITIVITY, {ignoreCase: true});
eventBus.emit(Event.TOGGLED_CASE_SENSITIVITY, {ignoreCase: true});
});

});

0 comments on commit 1d1f79b

Please sign in to comment.