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

Commit 037f466

Browse files
authored
refactor: Removed dependency on lodash.keyBy (optimizely#416)
Summary: Replaced lodash values function with keyBy from js-sdk-utils and added missing functionality. This is part of an effort to remove lodash dependency to reduce bundle size. Test Plan: 1) Added Unit tests 2) All Unit tests and Full Stack SDK compatibility passed
1 parent c08435e commit 037f466

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

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

Lines changed: 8 additions & 2 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.
@@ -15,6 +15,7 @@
1515
*/
1616
var uuid = require('uuid');
1717
var _isFinite = require('lodash/isFinite');
18+
var keyBy = require('@optimizely/js-sdk-utils').keyBy;
1819
var MAX_NUMBER_LIMIT = Math.pow(2, 53);
1920

2021
module.exports = {
@@ -27,7 +28,12 @@ module.exports = {
2728
isFinite: function(number) {
2829
return _isFinite(number) && Math.abs(number) <= MAX_NUMBER_LIMIT;
2930
},
30-
keyBy: require('lodash/keyBy'),
31+
keyBy: function(arr, key) {
32+
if (!arr) return {};
33+
return keyBy(arr, function(item) {
34+
return item[key];
35+
});
36+
},
3137
forEach: require('lodash/forEach'),
3238
forOwn: require('lodash/forOwn'),
3339
uuid: function() {

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,35 @@ describe('lib/utils/fns', function() {
3737
assert.isTrue(fns.isFinite(-Math.pow(2, 53)));
3838
});
3939
});
40+
41+
describe('keyBy', function() {
42+
it('should return correct object when a key is provided', function() {
43+
var arr = [
44+
{ key1: 'row1', key2: 'key2row1' },
45+
{ key1: 'row2', key2: 'key2row2' },
46+
{ key1: 'row3', key2: 'key2row3' },
47+
{ key1: 'row4', key2: 'key2row4' },
48+
];
49+
50+
var obj = fns.keyBy(arr, 'key1');
51+
52+
assert.deepEqual(obj, {
53+
row1: { key1: 'row1', key2: 'key2row1' },
54+
row2: { key1: 'row2', key2: 'key2row2' },
55+
row3: { key1: 'row3', key2: 'key2row3' },
56+
row4: { key1: 'row4', key2: 'key2row4' }
57+
});
58+
});
59+
60+
it('should return empty object when first argument is null or undefined', function() {
61+
var obj = fns.keyBy(null, 'key1');
62+
assert.isEmpty(obj);
63+
64+
obj = fns.keyBy(undefined, 'key1');
65+
assert.isEmpty(obj);
66+
});
67+
});
68+
4069
describe('isNumber', function() {
4170
it('should return true in case of number', function() {
4271
assert.isTrue(fns.isNumber(3));

0 commit comments

Comments
 (0)