Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial attempt to refactor polyfills #809

Draft
wants to merge 2 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ out/
test/reports
test/test-bundle.js.map
test/stub/test-stub-bundle.js
**/dist
**/dist
test/integrations/requirejs/test-requirejs-bundle.js
21,187 changes: 18,815 additions & 2,372 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"build:webpack:cjs": "webpack --config test/integrations/cjs/webpack/webpack.config.js",
"build:rollup:module": "rollup --config test/integrations/module/rollup/rollup.config.js",
"build:webpack:module": "webpack --config test/integrations/module/webpack/webpack.config.js",
"build:rollup:requirejs": "rollup --config test/integrations/requirejs/rollup.requirejs.config.js",
"build:ts": "tsc -p .",
"test": "npm run build && npm run build:test-bundle && cross-env DEBUG=false karma start test/karma.config.js",
"test:debug": "cross-env DEBUG=true karma start test/karma.config.js",
Expand All @@ -46,7 +47,7 @@
"test:integrations:module": "npm run test:integrations:module:webpack && npm run test:integrations:module:rollup",
"test:integrations:module:webpack": "npm run build:webpack:module && npm run test:karma:webpack:module",
"test:integrations:module:rollup": "npm run build:rollup:module && npm run test:karma:rollup:module",
"test:requirejs": "npm run build && npm run test:requirejs:before && npm run test:requirejs:after",
"test:requirejs": "npm run build:iife && npm run build:rollup:requirejs && npm run test:requirejs:before && npm run test:requirejs:after",
"test:requirejs:before": "cross-env FILE_ORDER=before_mp karma start test/integrations/requirejs/karma.requirejs.config.js",
"test:requirejs:after": "cross-env FILE_ORDER=after_mp karma start test/integrations/requirejs/karma.requirejs.config.js",
"test:karma:webpack:cjs": "cross-env BUNDLER=webpack karma start test/integrations/cjs/karma.webpack.config.js",
Expand Down Expand Up @@ -105,7 +106,7 @@
"eslint": "^8.36.0",
"eslint-config-prettier": "9.0.0",
"eslint-plugin-prettier": "3.4.1",
"fetch-mock": "^7.5.1",
"fetch-mock": "^9.1.1",
"gts": "^5.0.1",
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.5.0",
Expand All @@ -125,7 +126,7 @@
"rollup": "^2.75.7",
"shelljs": "^0.8.4",
"should": "^7.1.0",
"sinon": "^11.0.0",
"sinon": "^16.1.3",
"ts-jest": "^29.1.0",
"tslib": "^2.1.0",
"typescript": "^5.0.2",
Expand Down
17 changes: 0 additions & 17 deletions src/mparticle-instance-manager.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,9 @@
import Polyfill from './polyfill';
import Types from './types';
import Constants from './constants';
import mParticleInstance from './mp-instance.js';
import _BatchValidator from './mockBatchCreator';
import MPSideloadedKit from './sideloadedKit';

if (!Array.prototype.forEach) {
Array.prototype.forEach = Polyfill.forEach;
}

if (!Array.prototype.map) {
Array.prototype.map = Polyfill.map;
}

if (!Array.prototype.filter) {
Array.prototype.filter = Polyfill.filter;
}

if (!Array.isArray) {
Array.prototype.isArray = Polyfill.isArray;
}

function mParticle() {
var self = this;
// Only leaving this here in case any clients are trying to access mParticle.Store, to prevent from throwing
Expand Down
106 changes: 0 additions & 106 deletions src/polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,111 +142,5 @@ var UTF8 = {
};

export default {
// forEach polyfill
// Production steps of ECMA-262, Edition 5, 15.4.4.18
// Reference: http://es5.github.io/#x15.4.4.18
forEach: function(callback, thisArg) {
var T, k;

if (this == null) {
throw new TypeError(' this is null or not defined');
}

var O = Object(this);
var len = O.length >>> 0;

if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function');
}

if (arguments.length > 1) {
T = thisArg;
}

k = 0;

while (k < len) {
var kValue;
if (k in O) {
kValue = O[k];
callback.call(T, kValue, k, O);
}
k++;
}
},

// map polyfill
// Production steps of ECMA-262, Edition 5, 15.4.4.19
// Reference: http://es5.github.io/#x15.4.4.19
map: function(callback, thisArg) {
var T, A, k;

if (this === null) {
throw new TypeError(' this is null or not defined');
}

var O = Object(this);
var len = O.length >>> 0;

if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function');
}

if (arguments.length > 1) {
T = thisArg;
}

A = new Array(len);

k = 0;

while (k < len) {
var kValue, mappedValue;
if (k in O) {
kValue = O[k];
mappedValue = callback.call(T, kValue, k, O);
A[k] = mappedValue;
}
k++;
}

return A;
},

// filter polyfill
// Prodcution steps of ECMA-262, Edition 5
// Reference: http://es5.github.io/#x15.4.4.20
filter: function(fun /*, thisArg*/) {
'use strict';

if (this === void 0 || this === null) {
throw new TypeError();
}

var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== 'function') {
throw new TypeError();
}

var res = [];
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++) {
if (i in t) {
var val = t[i];
if (fun.call(thisArg, val, i, t)) {
res.push(val);
}
}
}

return res;
},

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
isArray: function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
},

Base64: Base64,
};
8 changes: 2 additions & 6 deletions test/integrations/requirejs/karma.requirejs.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@ let files = [];
switch (FILE_ORDER) {
case 'before_mp':
files = [
'../../lib/mockhttprequest.js',
'../../../node_modules/fetch-mock/dist/es5/client-bundle.js',
'../../lib/require.2.3.6.min.js',
'../../../dist/mparticle.js',
'test-requirejs.js',
'test-requirejs-bundle.js',
];
break;
case 'after_mp':
files = [
'../../lib/mockhttprequest.js',
'../../../node_modules/fetch-mock/dist/es5/client-bundle.js',
'../../../dist/mparticle.js',
'../../lib/require.2.3.6.min.js',
'test-requirejs.js',
'test-requirejs-bundle.js',
];
break;
default:
Expand Down
17 changes: 17 additions & 0 deletions test/integrations/requirejs/rollup.requirejs.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';

const requirejs = {
input: 'test/integrations/requirejs/test-requirejs.js',
output: {
file: 'test/integrations/requirejs/test-requirejs-bundle.js',
format: 'iife',
name: 'mpRequireJs',
},
plugins: [
resolve(),
commonjs()
],
}

export default [requirejs];
33 changes: 16 additions & 17 deletions test/integrations/requirejs/test-requirejs.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import fetchMock from 'fetch-mock/esm/client';
import sinon from 'sinon';

describe('Require.JS Pages', function() {
it('loads mParticle properly', function() {
var server = new MockHttpServer();
server.start();
// TODO: https://mparticle-eng.atlassian.net/browse/SQDSDKS-5314
window.fetchMock.post('https://jssdks.mparticle.com/v3/JS/test_key/events', 200);
var mockServer = sinon.createFakeServer();
mockServer.respondImmediately = true;

mockServer.respondWith('https://identity.mparticle.com/v1/identify', [
200,
{},
JSON.stringify({ mpid: 'testMPID', is_logged_in: false }),
]);

fetchMock.post('https://jssdks.mparticle.com/v3/JS/test_key/events', 200);

window.mParticle = window.mParticle || {};
window.mParticle.config = {
workspaceToken: 'fooToken',
Expand All @@ -15,24 +25,13 @@ describe('Require.JS Pages', function() {
}
};

server.handle = function(request) {
request.setResponseHeader('Content-Type', 'application/json');
request.receive(
200,
JSON.stringify({
Store: {},
mpid: 'testMPID',
})
);
};

mParticle.init('test_key', window.mParticle.config);

window.fetchMock._calls = [];
fetchMock.resetHistory();

mParticle.logEvent('Test Event1');

const testEvent = window.fetchMock._calls[0];
const testEvent = fetchMock.calls()[0];
const testEventName = JSON.parse(testEvent[1].body).events[0].data.event_name;

testEventName.should.equal('Test Event1');
Expand Down
1 change: 0 additions & 1 deletion test/karma.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const { DEBUG } = process.env;
let files = [
'lib/mockhttprequest.js',
'lib/geomock.js',
'../node_modules/fetch-mock/dist/es5/client-bundle.js',
'../node_modules/sinon-browser-only/sinon.js',
'config.js',
'../dist/mparticle.js',
Expand Down
1 change: 0 additions & 1 deletion test/src/tests-apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { expect } from 'chai';
declare global {
interface Window {
mParticle: MParticleWebSDK;
fetchMock: any;
}
}

Expand Down
Loading
Loading