Skip to content

Commit 78a6f84

Browse files
Add optional override feature in invert object utility helper
In utilities, `invertObject` can now take an optional 2nd parameter which is an override function. The function takes the parameters key and value and the override is only valid if it returns with an array of 2 items. Otherwise, the default swapping behavior will occur as usual. The motivation for adding this feature is because React's SVGDOMPropertyConfig has properrty keys with values of 0. As a result, the override will come in handy when dealing with those properties. Add utilities tests to verify the new feature in the helper.
1 parent 376d229 commit 78a6f84

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

lib/utilities.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,32 @@ function camelCase(string) {
2929
/**
3030
* Swap key with value in an object.
3131
*
32-
* @param {Object} obj - The object.
33-
* @return {Object} - The inverted object.
32+
* @param {Object} obj - The object.
33+
* @param {Function} [override] - The override method.
34+
* @return {Object} - The inverted object.
3435
*/
35-
function invertObject(obj) {
36+
function invertObject(obj, override) {
3637
if (typeof obj !== 'object' || !obj) { // null is an object
3738
throw new Error('`invert`: First argument must be an object.');
3839
}
3940

40-
var result = {};
4141
var key;
4242
var value;
43+
var isOverridePresent = typeof override === 'function';
44+
var overrides = {};
45+
var result = {};
4346

4447
for (key in obj) {
4548
value = obj[key];
49+
50+
if (isOverridePresent) {
51+
overrides = override(key, value);
52+
if (overrides && overrides.length === 2) {
53+
result[overrides[0]] = overrides[1];
54+
continue;
55+
}
56+
}
57+
4658
if (typeof value === 'string') {
4759
result[value] = key;
4860
}

test/utilities.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ describe('utilties', function() {
3636
describe('`invertObject` helper', function() {
3737
var invertObject = utilities.invertObject;
3838

39+
it('throws an error if the first argument is invalid', function() {
40+
[undefined, null, 'foo', 1337].forEach(function(parameter) {
41+
assert.throws(function() { invertObject(parameter); });
42+
});
43+
})
44+
3945
it('swaps key with value for object', function() {
4046
assert.deepEqual(
4147
invertObject({ foo: 'bar', baz: 'qux' }),
@@ -59,11 +65,29 @@ describe('utilties', function() {
5965
);
6066
});
6167

62-
it('throws an error if the first argument is invalid', function() {
63-
[undefined, null, 'foo', 1337].forEach(function(parameter) {
64-
assert.throws(function() { invertObject(parameter); });
65-
});
66-
})
68+
it('uses override if valid', function() {
69+
assert.deepEqual(
70+
invertObject({ foo: 'bar', baz: 'qux' }, function(key, value) {
71+
if (key === 'foo') {
72+
return ['key', 'value'];
73+
}
74+
}),
75+
{ key: 'value', qux: 'baz' }
76+
);
77+
});
78+
79+
it('does not use override if invalid', function() {
80+
assert.deepEqual(
81+
invertObject({ foo: 'bar', baz: 'qux' }, function(key, value) {
82+
if (key === 'foo') {
83+
return ['key'];
84+
} else if (key === 'baz') {
85+
return { key: 'value' };
86+
}
87+
}),
88+
{ bar: 'foo', qux: 'baz' }
89+
);
90+
});
6791
});
6892

6993
});

0 commit comments

Comments
 (0)