Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
implementing and testing
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
var format = require('util').format; | ||
|
||
function assertKey(key) { | ||
if (!key.name) throw new Error('key needs to have at least the [name] property set to a string'); | ||
} | ||
|
||
module.exports = function stringifyKey(key) { | ||
assertKey(key); | ||
|
||
return format( | ||
'%s%s%s%s%s' | ||
, (key.shift ? 'shift-' : '') | ||
, (key.meta ? 'meta-' : '') | ||
, (key.alt ? 'alt-' : '') | ||
, (key.ctrl ? 'ctrl-' : '') | ||
, key.name | ||
); | ||
}; | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use strict'; | ||
/*jshint asi: true */ | ||
|
||
var test = require('trap').test | ||
, stringify = require('..') | ||
|
||
test('given simple key without any modifier', function (t) { | ||
var key = { | ||
name: 'c', | ||
ctrl: false, | ||
meta: false, | ||
shift: false | ||
}; | ||
t.equal(stringify(key), 'c', 'returns just the name of the key') | ||
}) | ||
|
||
test('ctrl modifier', function (t) { | ||
var key = { | ||
name: 'c', | ||
ctrl: true, | ||
meta: false, | ||
shift: false | ||
}; | ||
t.equal(stringify(key), 'ctrl-c', 'returns ctrl-letter') | ||
}); | ||
|
||
test('shift modifier', function (t) { | ||
var key = { | ||
name: 'c', | ||
ctrl: false, | ||
meta: false, | ||
shift: true | ||
}; | ||
t.equal(stringify(key), 'shift-c', 'returns shift-letter') | ||
}); | ||
|
||
test('shift and ctrl modifier', function (t) { | ||
var key = { | ||
name: 'c', | ||
ctrl: true, | ||
meta: false, | ||
shift: true | ||
}; | ||
t.equal(stringify(key), 'shift-ctrl-c', 'returns shift-ctrl-letter') | ||
}); | ||
|
||
test('alt modifier', function (t) { | ||
var key = { | ||
name: 'c', | ||
ctrl: false, | ||
meta: false, | ||
alt: true | ||
}; | ||
t.equal(stringify(key), 'alt-c', 'returns alt-letter') | ||
}); | ||
|
||
test('shift, meta and ctrl modifier', function (t) { | ||
var key = { | ||
name: 'c', | ||
ctrl: true, | ||
meta: true, | ||
shift: true | ||
}; | ||
t.equal(stringify(key), 'shift-meta-ctrl-c', 'returns shift-meta-ctrl-letter') | ||
}); |