expf-sigma-node.js 3.0.5 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/public/sigma.js +55 -16
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expf-sigma-node.js",
3
- "version": "3.0.5",
3
+ "version": "3.2.0",
4
4
  "description": "expf-sigma-node.js lets you manage features flags and remote config across web, server side applications. Deliver true Continuous Integration. Get builds out faster. Control who has access to new features.",
5
5
  "main": "public/sigma.js",
6
6
  "keywords": [
package/public/sigma.js CHANGED
@@ -32,6 +32,7 @@ var import_node_fetch = __toESM(require("node-fetch"));
32
32
 
33
33
  // src/modules/sigmaUserData.js
34
34
  var SigmaUserData = class {
35
+ platform = "all";
35
36
  constructor() {
36
37
  this.user = {
37
38
  userId: null,
@@ -941,15 +942,15 @@ function generateRandomId() {
941
942
  }
942
943
 
943
944
  // src/helpers/getUserGroup.js
944
- function getUserGroup(salt, exposureRate, controlBucketPerc, forcedExp) {
945
+ function getUserGroup(salt, exposureRate, controlBucket, forcedExp) {
945
946
  let userInGroup = "a";
946
947
  let currentDiapason = 0;
947
948
  if (Math.abs(salt) / 100 <= parseFloat(exposureRate) * 100 || forcedExp) {
948
949
  let bucket = Math.abs(salt) % 100;
949
- for (let i = 0; i < controlBucketPerc.length; i++) {
950
- currentDiapason += controlBucketPerc[i].weight * 100;
950
+ for (let i = 0; i < controlBucket.length; i++) {
951
+ currentDiapason += controlBucket[i].weight * 100;
951
952
  if (bucket <= currentDiapason) {
952
- return userInGroup = controlBucketPerc[i].name;
953
+ return userInGroup = controlBucket[i].name;
953
954
  }
954
955
  }
955
956
  return userInGroup;
@@ -957,6 +958,40 @@ function getUserGroup(salt, exposureRate, controlBucketPerc, forcedExp) {
957
958
  return false;
958
959
  }
959
960
 
961
+ // src/helpers/filterByPlatform.js
962
+ function filterByPlatform(config, platformName) {
963
+ if (!Object.keys(config).length)
964
+ return;
965
+ if (!platformName)
966
+ return config;
967
+ let filteredConfig = { ...config };
968
+ const { feature_flags, experiments } = filteredConfig;
969
+ filteredConfig.feature_flags = generateFilteredList(feature_flags, platformName);
970
+ filteredConfig.experiments = generateFilteredList(experiments, platformName);
971
+ return filteredConfig;
972
+ }
973
+ function generateFilteredList(array, name) {
974
+ if (!array.length)
975
+ return [];
976
+ let listName = null;
977
+ if (Array.isArray(name)) {
978
+ listName = [...name];
979
+ } else if (typeof name === "string") {
980
+ listName = [name];
981
+ }
982
+ if (listName.find((n) => n === "all"))
983
+ return array;
984
+ array = array.filter((item) => {
985
+ for (let index in listName) {
986
+ if (!item.platform)
987
+ return item;
988
+ if (item.platform === listName[index])
989
+ return item;
990
+ }
991
+ });
992
+ return array;
993
+ }
994
+
960
995
  // src/sigma.js
961
996
  if (typeof import_node_fetch.default.default !== "undefined")
962
997
  import_node_fetch.default.default;
@@ -1118,6 +1153,7 @@ var Sigma = class {
1118
1153
  if (typeof data !== "object") {
1119
1154
  throw new Error(`typeof config.json is not an object`);
1120
1155
  }
1156
+ data = filterByPlatform(data, this.userData.platform || this.sigmaUserData.platform);
1121
1157
  hash = await SHA256.hex(JSON.stringify(data));
1122
1158
  if (!localStorageHash || localStorageHash !== hash) {
1123
1159
  this.cache.set(sigmaHash, hash);
@@ -1313,7 +1349,7 @@ var Sigma = class {
1313
1349
  }
1314
1350
  }
1315
1351
  }
1316
- return false;
1352
+ return null;
1317
1353
  }
1318
1354
  getSplitById(experiment) {
1319
1355
  if (!experiment)
@@ -1386,7 +1422,8 @@ var Sigma = class {
1386
1422
  let experiment = null;
1387
1423
  let layer = null;
1388
1424
  let bounds = [];
1389
- let userInGroupExperiment = null;
1425
+ let groupName = null;
1426
+ let groupIndex = null;
1390
1427
  let forcedUser = false;
1391
1428
  let splitById = null;
1392
1429
  for (let i = 0; i < sigmaDataLs.experiments.length; i++) {
@@ -1397,15 +1434,15 @@ var Sigma = class {
1397
1434
  if (sigmaDataLs.experiments[i]["name"] === experimentName && !experiment) {
1398
1435
  splitById = this.getSplitById(sigmaDataLs.experiments[i]);
1399
1436
  forcedUser = checkForcedList(sigmaDataLs.experiments[i].forced_user_list, this.sigmaUserData.user.userId);
1400
- userInGroupExperiment = this.findUserInForcedGroup(sigmaDataLs.experiments[i]);
1401
- experiment = this.experimentDefinition(sigmaDataLs.experiments[i], userInGroupExperiment);
1437
+ groupName = this.findUserInForcedGroup(sigmaDataLs.experiments[i]);
1438
+ experiment = this.experimentDefinition(sigmaDataLs.experiments[i], groupName);
1402
1439
  break;
1403
1440
  }
1404
1441
  }
1405
1442
  const localStorageGroupName = `sigma_group_${experimentName}`;
1406
1443
  if (experiment) {
1407
- if (!userInGroupExperiment) {
1408
- userInGroupExperiment = this.calcUserInGroup(
1444
+ if (!groupName) {
1445
+ groupName = this.calcUserInGroup(
1409
1446
  experiment.salt,
1410
1447
  experiment.allocation,
1411
1448
  experiment.groups,
@@ -1415,13 +1452,14 @@ var Sigma = class {
1415
1452
  splitById
1416
1453
  );
1417
1454
  }
1418
- this.cache.set(localStorageGroupName, userInGroupExperiment);
1455
+ this.cache.set(localStorageGroupName, groupName);
1456
+ groupIndex = experiment.groups.findIndex((i) => i.name == groupName);
1419
1457
  }
1420
1458
  const getParamValue = (paramName) => {
1421
1459
  if (!paramName || !experiment) {
1422
1460
  return null;
1423
1461
  }
1424
- const groupName = this.cache.get(localStorageGroupName);
1462
+ const groupName2 = this.cache.get(localStorageGroupName);
1425
1463
  let params = null;
1426
1464
  for (let i = 0; i < experiment.params.length; i++) {
1427
1465
  if (experiment.params[i]["name"] === paramName) {
@@ -1434,7 +1472,7 @@ var Sigma = class {
1434
1472
  }
1435
1473
  let paramValue = null;
1436
1474
  for (let item in params.values) {
1437
- if (params.values[item].group === groupName) {
1475
+ if (params.values[item].group === groupName2) {
1438
1476
  paramValue = doTypeConversion(params.type, params.values[item].value);
1439
1477
  }
1440
1478
  }
@@ -1444,7 +1482,7 @@ var Sigma = class {
1444
1482
  if (!flagName || !experiment) {
1445
1483
  return null;
1446
1484
  }
1447
- const groupName = this.cache.get(localStorageGroupName);
1485
+ const groupName2 = this.cache.get(localStorageGroupName);
1448
1486
  let flagExperiment = null;
1449
1487
  for (let i = 0; i < experiment.feature_flags.length; i++) {
1450
1488
  if (experiment.feature_flags[i]["name"] === flagName) {
@@ -1456,7 +1494,7 @@ var Sigma = class {
1456
1494
  return null;
1457
1495
  let flag = null;
1458
1496
  for (let i = 0; i < flagExperiment.groups.length; i++) {
1459
- if (flagExperiment.groups[i]["group"] === groupName) {
1497
+ if (flagExperiment.groups[i]["group"] === groupName2) {
1460
1498
  flag = flagExperiment.groups[i];
1461
1499
  break;
1462
1500
  }
@@ -1468,7 +1506,8 @@ var Sigma = class {
1468
1506
  };
1469
1507
  return {
1470
1508
  getParamValue,
1471
- getFeatureValue
1509
+ getFeatureValue,
1510
+ groupIndex
1472
1511
  };
1473
1512
  }
1474
1513
  async getAllUserExperiments() {