Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/perf_improvements' into v3_produ…
Browse files Browse the repository at this point in the history
…ction_staging
  • Loading branch information
saikumarrs committed Feb 11, 2022
2 parents 39d802c + 9b414bf commit 62f0983
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 15 deletions.
21 changes: 21 additions & 0 deletions __tests__/camelcase.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import camelcase from "../utils/camelcase";

test("convert string with space to camelcase format", () => {
expect(camelcase("camel case")).toBe("camelCase");
});

test("convert string with underscore to camelcase format", () => {
expect(camelcase("camel_case")).toBe("camelCase");
});

test("convert string with first letter capital to camelcase format", () => {
expect(camelcase("Camelcase")).toBe("camelcase");
});

test("convert uppercase string to camelcase format", () => {
expect(camelcase("CAMELCASE")).toBe("camelcase");
});

test("convert string with first letter capital for each word to camelcase format", () => {
expect(camelcase("Camel Case")).toBe("camelCase");
});
51 changes: 45 additions & 6 deletions analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
DEST_SDK_BASE_URL,
CDN_INT_DIR,
INTG_SUFFIX,
POLYFILL_URL,
} from "./utils/constants";
import RudderElementBuilder from "./utils/RudderElementBuilder";
import Storage from "./utils/storage";
Expand Down Expand Up @@ -922,14 +923,13 @@ class Analytics {
}

/**
* Call control pane to get client configs
*
* Load after polyfills are loaded
* @param {*} writeKey
* @memberof Analytics
* @param {*} serverUrl
* @param {*} options
* @returns
*/
load(writeKey, serverUrl, options) {
// logger.debug("inside load ")
if (this.loaded) return;
loadAfterPolyfill(writeKey, serverUrl, options) {
if (options && options.cookieConsentManager)
this.cookieConsentOptions = cloneDeep(options.cookieConsentManager);
if (!this.isValidWriteKey(writeKey) || !this.isValidServerUrl(serverUrl)) {
Expand Down Expand Up @@ -1051,6 +1051,45 @@ class Analytics {
}
}

/**
* Call control pane to get client configs
*
* @param {*} writeKey
* @memberof Analytics
*/
load(writeKey, serverUrl, options) {
// logger.debug("inside load ");
if (this.loaded) return;

// check if the below features are available in the browser or not
// If not present dynamically load from the polyfill cdn
if (
!String.prototype.endsWith ||
!String.prototype.startsWith ||
!String.prototype.includes ||
!Array.prototype.find ||
!Array.prototype.includes ||
!Promise ||
!Object.entries
) {
ScriptLoader("polyfill", POLYFILL_URL);
const self = this;
const interval = setInterval(function () {
// check if the polyfill is loaded
if (window.hasOwnProperty("polyfill")) {
clearInterval(interval);
self.loadAfterPolyfill(writeKey, serverUrl, options);
}
}, 100);

setTimeout(() => {
clearInterval(interval);
}, MAX_WAIT_FOR_INTEGRATION_LOAD);
} else {
this.loadAfterPolyfill(writeKey, serverUrl, options);
}
}

ready(callback) {
if (!this.loaded) return;
if (typeof callback === "function") {
Expand Down
2 changes: 1 addition & 1 deletion integrations/Fullstory/browser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable class-methods-use-this */
/* eslint-disable no-undef */
import camelcase from "camelcase";
import camelcase from "../../utils/camelcase";
import logger from "../../utils/logUtil";
import { NAME } from "./constants";

Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"@segment/top-domain": "^3.0.1",
"auto-changelog": "^2.3.0",
"babel-eslint": "^10.1.0",
"camelcase": "^6.2.0",
"component-each": "^0.2.6",
"component-emitter": "github:component/emitter",
"component-querystring": "^2.0.1",
Expand All @@ -68,7 +67,6 @@
"obj-case": "^0.2.1",
"on-body": "0.0.1",
"rudder-component-cookie": "0.0.1",
"set-value": "^4.1.0",
"universal-analytics": "^0.4.23"
},
"devDependencies": {
Expand Down
51 changes: 51 additions & 0 deletions utils/camelcase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// util function to convert the input to string type
function convertToString(input) {
if (input) {
if (typeof input === "string") {
return input;
}

return String(input);
}
return "";
}

// convert string to words
function toWords(input) {
input = convertToString(input);

var regex =
/[A-Z\xC0-\xD6\xD8-\xDE]?[a-z\xDF-\xF6\xF8-\xFF]+|[A-Z\xC0-\xD6\xD8-\xDE]+(?![a-z\xDF-\xF6\xF8-\xFF])|\d+/g;

return input.match(regex);
}

// convert the input array to camel case
function toCamelCase(inputArray) {
let result = "";

for (let i = 0, len = inputArray.length; i < len; i++) {
const currentStr = inputArray[i];

let tempStr = currentStr.toLowerCase();

if (i !== 0) {
// convert first letter to upper case (the word is in lowercase)
tempStr = tempStr.substr(0, 1).toUpperCase() + tempStr.substr(1);
}

result += tempStr;
}

return result;
}

// this function call all other functions

function camelcase(input) {
const words = toWords(input);

return toCamelCase(words);
}

export default camelcase;
4 changes: 4 additions & 0 deletions utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ const DEST_SDK_BASE_URL = `https://cdn.rudderlabs.com/v1.1/${CDN_INT_DIR}`;
const MAX_WAIT_FOR_INTEGRATION_LOAD = 10000;
const INTEGRATION_LOAD_CHECK_INTERVAL = 1000;
const INTG_SUFFIX = "_RS";
const POLYFILL_URL =
"https://polyfill.io/v3/polyfill.min.js?features=Array.prototype.find%2CArray.prototype.includes%2CPromise%2CString.prototype.endsWith%2CString.prototype.includes%2CString.prototype.startsWith%2CObject.entries";

export {
ReservedPropertyKeywords,
ECommerceParamNames,
Expand All @@ -83,6 +86,7 @@ export {
MAX_WAIT_FOR_INTEGRATION_LOAD,
INTEGRATION_LOAD_CHECK_INTERVAL,
INTG_SUFFIX,
POLYFILL_URL,
};
/* module.exports = {
MessageType: MessageType,
Expand Down
16 changes: 10 additions & 6 deletions utils/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// import * as XMLHttpRequestNode from "Xmlhttprequest";
import { parse } from "component-url";
import get from "get-value";
import set from "set-value";
import logger from "./logUtil";
import { commonNames } from "./integration_cname";
import { clientToServerNames } from "./client_server_name";
Expand Down Expand Up @@ -532,7 +531,13 @@ function extractCustomFields(message, destination, keys, exclusionFields) {
});
objKeys.map((k) => {
if (!(typeof messageContext[k] === "undefined")) {
set(destination, k, get(messageContext, k));
if (destination) {
destination[k] = get(messageContext, k);
} else {
destination = {
k: get(messageContext, k),
};
}
}
});
}
Expand Down Expand Up @@ -582,11 +587,10 @@ function getDefinedTraits(message) {
get(traitsValue, "firstName") &&
get(traitsValue, "lastName")
) {
set(
traitsValue.name = `${get(traitsValue, "firstName")} ${get(
traitsValue,
"name",
`${get(traitsValue, "firstName")} ${get(traitsValue, "lastName")}`
);
"lastName"
)}`;
}
return traitsValue;
}
Expand Down

0 comments on commit 62f0983

Please sign in to comment.