Skip to content

Commit 5df2adc

Browse files
committed
Added the legacy code in order to get passed FSC
1 parent 7a6fda2 commit 5df2adc

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Copyright 2017, 2019-2020, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
var uuid = require('uuid');
17+
var MAX_SAFE_INTEGER_LIMIT = Math.pow(2, 53);
18+
var keyBy = require('@optimizely/js-sdk-utils').keyBy;
19+
module.exports = {
20+
assign: function(target) {
21+
if (!target) {
22+
return {};
23+
}
24+
if (typeof Object.assign === 'function') {
25+
return Object.assign.apply(Object, arguments);
26+
} else {
27+
var to = Object(target);
28+
for (var index = 1; index < arguments.length; index++) {
29+
var nextSource = arguments[index];
30+
if (nextSource !== null && nextSource !== undefined) {
31+
for (var nextKey in nextSource) {
32+
// Avoid bugs when hasOwnProperty is shadowed
33+
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
34+
to[nextKey] = nextSource[nextKey];
35+
}
36+
}
37+
}
38+
}
39+
return to;
40+
}
41+
},
42+
currentTimestamp: function() {
43+
return Math.round(new Date().getTime());
44+
},
45+
isSafeInteger: function(number) {
46+
return typeof number == 'number' && Math.abs(number) <= MAX_SAFE_INTEGER_LIMIT;
47+
},
48+
keyBy: function(arr, key) {
49+
if (!arr) return {};
50+
return keyBy(arr, function(item) {
51+
return item[key];
52+
});
53+
},
54+
uuid: function() {
55+
return uuid.v4();
56+
},
57+
isNumber: function(value) {
58+
return typeof value === 'number';
59+
},
60+
};
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Copyright 2019, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
var chai = require('chai');
18+
var assert = chai.assert;
19+
var fns = require('./');
20+
21+
describe('lib/utils/fns', function() {
22+
describe('APIs', function() {
23+
describe('isFinite', function() {
24+
it('should return false for invalid numbers', function() {
25+
assert.isFalse(fns.isSafeInteger(Infinity));
26+
assert.isFalse(fns.isSafeInteger(-Infinity));
27+
assert.isFalse(fns.isSafeInteger(NaN));
28+
assert.isFalse(fns.isSafeInteger(undefined));
29+
assert.isFalse(fns.isSafeInteger('3'));
30+
assert.isFalse(fns.isSafeInteger(Math.pow(2, 53) + 2));
31+
assert.isFalse(fns.isSafeInteger(-Math.pow(2, 53) - 2));
32+
});
33+
34+
it('should return true for valid numbers', function() {
35+
assert.isTrue(fns.isSafeInteger(0));
36+
assert.isTrue(fns.isSafeInteger(10));
37+
assert.isTrue(fns.isSafeInteger(10.5));
38+
assert.isTrue(fns.isSafeInteger(Math.pow(2, 53)));
39+
assert.isTrue(fns.isSafeInteger(-Math.pow(2, 53)));
40+
});
41+
});
42+
43+
describe('keyBy', function() {
44+
it('should return correct object when a key is provided', function() {
45+
var arr = [
46+
{ key1: 'row1', key2: 'key2row1' },
47+
{ key1: 'row2', key2: 'key2row2' },
48+
{ key1: 'row3', key2: 'key2row3' },
49+
{ key1: 'row4', key2: 'key2row4' },
50+
];
51+
52+
var obj = fns.keyBy(arr, 'key1');
53+
54+
assert.deepEqual(obj, {
55+
row1: { key1: 'row1', key2: 'key2row1' },
56+
row2: { key1: 'row2', key2: 'key2row2' },
57+
row3: { key1: 'row3', key2: 'key2row3' },
58+
row4: { key1: 'row4', key2: 'key2row4' },
59+
});
60+
});
61+
62+
it('should return empty object when first argument is null or undefined', function() {
63+
var obj = fns.keyBy(null, 'key1');
64+
assert.isEmpty(obj);
65+
66+
obj = fns.keyBy(undefined, 'key1');
67+
assert.isEmpty(obj);
68+
});
69+
});
70+
71+
describe('isNumber', function() {
72+
it('should return true in case of number', function() {
73+
assert.isTrue(fns.isNumber(3));
74+
});
75+
it('should return true in case of value from Number object ', function() {
76+
assert.isTrue(fns.isNumber(Number.MIN_VALUE));
77+
});
78+
it('should return true in case of Infinity ', function() {
79+
assert.isTrue(fns.isNumber(Infinity));
80+
});
81+
it('should return false in case of string', function() {
82+
assert.isFalse(fns.isNumber('3'));
83+
});
84+
it('should return false in case of null', function() {
85+
assert.isFalse(fns.isNumber(null));
86+
});
87+
});
88+
});
89+
});

0 commit comments

Comments
 (0)