Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
implementing and testing
  • Loading branch information
thlorenz committed Jan 21, 2013
1 parent f76b886 commit 30a350d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
19 changes: 19 additions & 0 deletions stringify-key.js
@@ -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
);
};

65 changes: 65 additions & 0 deletions test/stringify-key.js
@@ -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')
});

0 comments on commit 30a350d

Please sign in to comment.