Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 1fea093

Browse files
committed
simplified implementation to only support keyBy based on string key
1 parent 747e234 commit 1fea093

File tree

2 files changed

+7
-29
lines changed

2 files changed

+7
-29
lines changed

packages/optimizely-sdk/lib/utils/fns/index.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2017, 2019, Optimizely
2+
* Copyright 2017, 2019-2020, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,14 +30,11 @@ module.exports = {
3030
isFinite: function(number) {
3131
return _isFinite(number) && Math.abs(number) <= MAX_NUMBER_LIMIT;
3232
},
33-
keyBy: function(arr, callback) {
33+
keyBy: function(arr, key) {
3434
if (!arr) return {};
35-
if (typeof callback === 'string' || callback instanceof String) {
36-
return keyBy(arr, function(item) {
37-
return item[callback];
38-
});
39-
}
40-
return keyBy(arr, callback);
35+
return keyBy(arr, function(item) {
36+
return item[key];
37+
});
4138
},
4239
forEach: require('lodash/forEach'),
4340
forOwn: require('lodash/forOwn'),

packages/optimizely-sdk/lib/utils/fns/index.tests.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,8 @@ describe('lib/utils/fns', function() {
3737
assert.isTrue(fns.isFinite(-Math.pow(2, 53)));
3838
});
3939
});
40+
4041
describe('keyBy', function() {
41-
it('should return correct object if callback is a function', function() {
42-
var arr = [
43-
{ key1: 'row1', key2: 'key2row1' },
44-
{ key1: 'row2', key2: 'key2row2' },
45-
{ key1: 'row3', key2: 'key2row3' },
46-
{ key1: 'row4', key2: 'key2row4' },
47-
];
48-
49-
var obj = fns.keyBy(arr, function(item) {
50-
return item['key1'];
51-
});
52-
53-
assert.deepEqual(obj, {
54-
row1: { key1: 'row1', key2: 'key2row1' },
55-
row2: { key1: 'row2', key2: 'key2row2' },
56-
row3: { key1: 'row3', key2: 'key2row3' },
57-
row4: { key1: 'row4', key2: 'key2row4' }
58-
});
59-
});
60-
6142
it('should return correct object if callback is a string key', function() {
6243
var arr = [
6344
{ key1: 'row1', key2: 'key2row1' },
@@ -75,7 +56,7 @@ describe('lib/utils/fns', function() {
7556
row4: { key1: 'row4', key2: 'key2row4' }
7657
});
7758
});
78-
59+
7960
it('should return empty object when first argument is null or undefined', function() {
8061
var obj = fns.keyBy(null, 'key1');
8162
assert.isEmpty(obj);

0 commit comments

Comments
 (0)