From c373e4fa35758e1d3cd9cd4b0fc45b7db1c78bd9 Mon Sep 17 00:00:00 2001 From: Raju Ahmed Date: Fri, 13 Dec 2024 22:51:22 +0600 Subject: [PATCH 1/2] [FSSDK-11006] remove assign function and use spreading --- lib/core/audience_evaluator/index.ts | 5 ++-- lib/core/decision_service/index.ts | 2 +- lib/project_config/project_config.ts | 35 ++++++++++++++-------- lib/utils/fns/index.tests.js | 17 ----------- lib/utils/fns/index.ts | 25 ---------------- lib/utils/local_storage/tryLocalStorage.ts | 32 -------------------- 6 files changed, 26 insertions(+), 90 deletions(-) delete mode 100644 lib/utils/local_storage/tryLocalStorage.ts diff --git a/lib/core/audience_evaluator/index.ts b/lib/core/audience_evaluator/index.ts index 550694610..b39cacb8a 100644 --- a/lib/core/audience_evaluator/index.ts +++ b/lib/core/audience_evaluator/index.ts @@ -44,10 +44,11 @@ export class AudienceEvaluator { * @constructor */ constructor(UNSTABLE_conditionEvaluators: unknown) { - this.typeToEvaluatorMap = fns.assign({}, UNSTABLE_conditionEvaluators, { + this.typeToEvaluatorMap = { + ...UNSTABLE_conditionEvaluators as any, custom_attribute: customAttributeConditionEvaluator, third_party_dimension: odpSegmentsConditionEvaluator, - }); + }; } /** diff --git a/lib/core/decision_service/index.ts b/lib/core/decision_service/index.ts index 16718fe7e..5522e3905 100644 --- a/lib/core/decision_service/index.ts +++ b/lib/core/decision_service/index.ts @@ -312,7 +312,7 @@ export class DecisionService { const userProfile = this.getUserProfile(userId) || {} as UserProfile; const attributeExperimentBucketMap = attributes[CONTROL_ATTRIBUTES.STICKY_BUCKETING_KEY]; - return fns.assign({}, userProfile.experiment_bucket_map, attributeExperimentBucketMap); + return { ...userProfile.experiment_bucket_map, ...attributeExperimentBucketMap as any }; } /** diff --git a/lib/project_config/project_config.ts b/lib/project_config/project_config.ts index 2f9de78ff..6bb4446d1 100644 --- a/lib/project_config/project_config.ts +++ b/lib/project_config/project_config.ts @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { find, objectEntries, objectValues, sprintf, assign, keyBy } from '../utils/fns'; +import { find, objectEntries, objectValues, sprintf, keyBy } from '../utils/fns'; import { ERROR_MESSAGES, LOG_LEVEL, LOG_MESSAGES, FEATURE_VARIABLE_TYPES } from '../utils/enums'; import configValidator from '../utils/config_validator'; @@ -36,6 +36,7 @@ import { } from '../shared_types'; import { OdpConfig, OdpIntegrationConfig } from '../odp/odp_config'; import { Transformer } from '../utils/type'; +import exp from 'constants'; interface TryCreatingProjectConfigConfig { // TODO[OASIS-6649]: Don't use object type @@ -99,27 +100,27 @@ const MODULE_NAME = 'PROJECT_CONFIG'; // eslint-disable-next-line @typescript-eslint/no-explicit-any function createMutationSafeDatafileCopy(datafile: any): ProjectConfig { - const datafileCopy = assign({}, datafile); + const datafileCopy = { ...datafile }; datafileCopy.audiences = (datafile.audiences || []).map((audience: Audience) => { - return assign({}, audience); + return { ...audience }; }); datafileCopy.experiments = (datafile.experiments || []).map((experiment: Experiment) => { - return assign({}, experiment); + return { ...experiment }; }); datafileCopy.featureFlags = (datafile.featureFlags || []).map((featureFlag: FeatureFlag) => { - return assign({}, featureFlag); + return { ...featureFlag }; }); datafileCopy.groups = (datafile.groups || []).map((group: Group) => { - const groupCopy = assign({}, group); + const groupCopy = { ...group }; groupCopy.experiments = (group.experiments || []).map(experiment => { - return assign({}, experiment); + return { ...experiment }; }); return groupCopy; }); datafileCopy.rollouts = (datafile.rollouts || []).map((rollout: Rollout) => { - const rolloutCopy = assign({}, rollout); + const rolloutCopy = { ...rollout }; rolloutCopy.experiments = (rollout.experiments || []).map(experiment => { - return assign({}, experiment); + return { ...experiment }; }); return rolloutCopy; }); @@ -148,8 +149,11 @@ export const createProjectConfig = function(datafileObj?: JSON, datafileStr: str (projectConfig.audiences || []).forEach(audience => { audience.conditions = JSON.parse(audience.conditions as string); }); - projectConfig.audiencesById = keyBy(projectConfig.audiences, 'id'); - assign(projectConfig.audiencesById, keyBy(projectConfig.typedAudiences, 'id')); + + projectConfig.audiencesById = { + ...keyBy(projectConfig.audiences, 'id'), + ...keyBy(projectConfig.typedAudiences, 'id'), + } projectConfig.attributeKeyMap = keyBy(projectConfig.attributes, 'key'); projectConfig.eventKeyMap = keyBy(projectConfig.events, 'key'); @@ -159,7 +163,8 @@ export const createProjectConfig = function(datafileObj?: JSON, datafileStr: str Object.keys(projectConfig.groupIdMap || {}).forEach(Id => { experiments = projectConfig.groupIdMap[Id].experiments; (experiments || []).forEach(experiment => { - projectConfig.experiments.push(assign(experiment, { groupId: Id })); + experiment.groupId = Id; + projectConfig.experiments.push(experiment); }); }); @@ -226,7 +231,11 @@ export const createProjectConfig = function(datafileObj?: JSON, datafileStr: str experiment.variationKeyMap = keyBy(experiment.variations, 'key'); // Creates { : { key: , id: } } mapping for quick lookup - assign(projectConfig.variationIdMap, keyBy(experiment.variations, 'id')); + projectConfig.variationIdMap = { + ...projectConfig.variationIdMap, + ...keyBy(experiment.variations, 'id') + }; + objectValues(experiment.variationKeyMap || {}).forEach(variation => { if (variation.variables) { projectConfig.variationVariableUsageMap[variation.id] = keyBy(variation.variables, 'id'); diff --git a/lib/utils/fns/index.tests.js b/lib/utils/fns/index.tests.js index 0d07bdc16..f2be54fea 100644 --- a/lib/utils/fns/index.tests.js +++ b/lib/utils/fns/index.tests.js @@ -84,22 +84,5 @@ describe('lib/utils/fns', function() { assert.isFalse(fns.isNumber(null)); }); }); - - describe('assign', function() { - it('should return empty object when target is not provided', function() { - assert.deepEqual(fns.assign(), {}); - }); - - it('should copy correctly when Object.assign is available in environment', function() { - assert.deepEqual(fns.assign({ a: 'a'}, {b: 'b'}), { a: 'a', b: 'b' }); - }); - - it('should copy correctly when Object.assign is not available in environment', function() { - var originalAssign = Object.assign; - Object.assign = null; - assert.deepEqual(fns.assign({ a: 'a'}, {b: 'b'}, {c: 'c'}), { a: 'a', b: 'b', c: 'c' }); - Object.assign = originalAssign; - }); - }); }); }); diff --git a/lib/utils/fns/index.ts b/lib/utils/fns/index.ts index e53402a22..e7ea3d071 100644 --- a/lib/utils/fns/index.ts +++ b/lib/utils/fns/index.ts @@ -17,30 +17,6 @@ import { v4 } from 'uuid'; const MAX_SAFE_INTEGER_LIMIT = Math.pow(2, 53); -// eslint-disable-next-line -export function assign(target: any, ...sources: any[]): any { - if (!target) { - return {}; - } - if (typeof Object.assign === 'function') { - return Object.assign(target, ...sources); - } else { - const to = Object(target); - for (let index = 0; index < sources.length; index++) { - const nextSource = sources[index]; - if (nextSource !== null && nextSource !== undefined) { - for (const nextKey in nextSource) { - // Avoid bugs when hasOwnProperty is shadowed - if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) { - to[nextKey] = nextSource[nextKey]; - } - } - } - } - return to; - } -} - export function currentTimestamp(): number { return Math.round(new Date().getTime()); } @@ -165,7 +141,6 @@ export function checkArrayEquality(arrayA: string[], arrayB: string[]): boolean } export default { - assign, checkArrayEquality, currentTimestamp, isSafeInteger, diff --git a/lib/utils/local_storage/tryLocalStorage.ts b/lib/utils/local_storage/tryLocalStorage.ts deleted file mode 100644 index 252cbf8e7..000000000 --- a/lib/utils/local_storage/tryLocalStorage.ts +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Copyright 2023, Optimizely - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Checks to see if browser localStorage available. If so, runs and returns browserCallback. Otherwise, runs and returns nonBrowserCallback. - * @param {object} callbacks - * @param {[object.browserCallback]} callbacks.browserCallback - * @param {[object.nonBrowserCallback]} callbacks.nonBrowserCallback - * @returns - */ -export const tryWithLocalStorage = ({ - browserCallback, - nonBrowserCallback, -}: { - browserCallback: (localStorage?: Storage) => K; - nonBrowserCallback: () => K; -}): K => { - return typeof window !== 'undefined' ? browserCallback(window?.localStorage) : nonBrowserCallback(); -}; From cfa581190ced95e9c957efb606900a64752c106f Mon Sep 17 00:00:00 2001 From: Raju Ahmed Date: Fri, 13 Dec 2024 22:56:11 +0600 Subject: [PATCH 2/2] rem --- lib/project_config/project_config.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/project_config/project_config.ts b/lib/project_config/project_config.ts index 6bb4446d1..a9b618894 100644 --- a/lib/project_config/project_config.ts +++ b/lib/project_config/project_config.ts @@ -36,7 +36,6 @@ import { } from '../shared_types'; import { OdpConfig, OdpIntegrationConfig } from '../odp/odp_config'; import { Transformer } from '../utils/type'; -import exp from 'constants'; interface TryCreatingProjectConfigConfig { // TODO[OASIS-6649]: Don't use object type