Skip to content

Commit 376d229

Browse files
Update invert object utility helper to swap only if value is string
- The helper will no longer throw an error if the object isn't flat - Update utilities tests and remove trivial test cases
1 parent 5dae6e4 commit 376d229

File tree

2 files changed

+7
-20
lines changed

2 files changed

+7
-20
lines changed

lib/utilities.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,9 @@ function invertObject(obj) {
4343

4444
for (key in obj) {
4545
value = obj[key];
46-
if (typeof value === 'object') {
47-
throw new Error('`invert`: Object must be flat.');
46+
if (typeof value === 'string') {
47+
result[value] = key;
4848
}
49-
result[value] = key;
5049
}
5150

5251
return result;

test/utilities.js

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,41 +41,29 @@ describe('utilties', function() {
4141
invertObject({ foo: 'bar', baz: 'qux' }),
4242
{ bar: 'foo', qux: 'baz' }
4343
);
44+
});
4445

45-
// check for unusual cases
46+
it('swaps only if value is string', function() {
4647
assert.deepEqual(
4748
invertObject({
4849
$: 'dollar',
4950
_: 'underscore',
5051
num: 1,
51-
u: undefined
52+
u: undefined,
53+
n: null
5254
}),
5355
{
5456
dollar: '$',
55-
underscore: '_',
56-
'1': 'num',
57-
'undefined': 'u'
57+
underscore: '_'
5858
}
5959
);
6060
});
6161

62-
it('swaps key with value for array', function() {
63-
assert.deepEqual(
64-
invertObject(['zero', 'one']),
65-
{ 'zero': '0', 'one': '1' }
66-
);
67-
});
68-
6962
it('throws an error if the first argument is invalid', function() {
7063
[undefined, null, 'foo', 1337].forEach(function(parameter) {
7164
assert.throws(function() { invertObject(parameter); });
7265
});
7366
})
74-
75-
it('throws an error if object is not flat', function() {
76-
assert.throws(function() { invertObject({ nested: {} }); });
77-
assert.throws(function() { invertObject({ obj: null }); });
78-
})
7967
});
8068

8169
});

0 commit comments

Comments
 (0)