Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ConstantineYurevich committed Aug 6, 2017
1 parent fd64edc commit 60084fc
Show file tree
Hide file tree
Showing 37 changed files with 328 additions and 272 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Expand Up @@ -7,8 +7,11 @@
"class-methods-use-this": 0,
"no-underscore-dangle": 0,
"import/first": 0,
"import/prefer-default-export": 0,
"no-multi-assign": 0,
"prefer-rest-params": 0
"prefer-rest-params": 0,
"prefer-spread": 0,
"no-useless-escape": 0
},
"env": {
"browser": true
Expand Down
13 changes: 9 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Expand Up @@ -46,7 +46,7 @@
"grunt": "^1.0.1",
"grunt-aws-s3": "^0.14.5",
"grunt-contrib-clean": "^1.0.0",
"grunt-contrib-compress": "^1.3.0",
"grunt-contrib-compress": "^1.3.0n",
"grunt-contrib-uglify": "^2.0.0",
"grunt-eslint": "^19.0.0",
"karma": "^1.3.0",
Expand All @@ -62,12 +62,13 @@
"uglifyify": "^3.0.1"
},
"dependencies": {
"core-js": "^2.4.0",
"async": "2.1.1",
"component-emitter": "1.1.3",
"core-js": "^2.4.0",
"crypto-js": "^3.1.9-1",
"driveback-utils": "git://github.com/driveback/utils.git",
"js-cookie": "^2.1.0",
"lockr": "^0.8.4",
"whatwg-fetch": "^2.0.3",
"driveback-utils": "git://github.com/driveback/utils.git"
"whatwg-fetch": "^2.0.3"
}
}
2 changes: 1 addition & 1 deletion src/CookieStorage.js
@@ -1,5 +1,5 @@
import cookie from 'js-cookie';
import topDomain from 'driveback-utils/topDomain.js';
import topDomain from 'driveback-utils/topDomain';

class CookieStorage {
constructor(options = {}) {
Expand Down
76 changes: 50 additions & 26 deletions src/DDHelper.js
Expand Up @@ -28,69 +28,93 @@ class DDHelper {
return clone(digitalData.product);
}
// search in listings
for (const listingKey of ['listing', 'recommendation', 'wishlist']) {
let result;

['listing', 'recommendation', 'wishlist'].some((listingKey) => {
let listings = digitalData[listingKey];
if (listings) {
if (!Array.isArray(listings)) {
listings = [listings];
}
for (const listing of listings) {
listings.some((listing) => {
if (listing.items && listing.items.length) {
for (const listingItem of listing.items) {
listing.items.some((listingItem) => {
if (matchProduct(id, skuCode, listingItem)) {
const product = clone(listingItem);
return product;
result = clone(listingItem);
return true;
}
}
return false;
});
if (result) return true;
}
}
return false;
});
}
}
if (result) return true;
return false;
});

if (result) return result;

// search in cart
if (digitalData.cart && digitalData.cart.lineItems && digitalData.cart.lineItems.length) {
for (const lineItem of digitalData.cart.lineItems) {
digitalData.cart.lineItems.some((lineItem) => {
if (matchProduct(id, skuCode, lineItem.product)) {
return clone(lineItem.product);
result = clone(lineItem.product);
return true;
}
}
return false;
});
}

return result;
}

static getListItem(id, digitalData, listId) {
// search in listings
const listingItem = {};
for (const listingKey of ['listing', 'recommendation', 'wishlist']) {
let result;

['listing', 'recommendation', 'wishlist'].some((listingKey) => {
let listings = digitalData[listingKey];
if (listings) {
if (!Array.isArray(listings)) {
listings = [listings];
}
for (const listing of listings) {
listings.some((listing) => {
if (listing.items && listing.items.length && (!listId || listId === listing.listId)) {
for (let i = 0, length = listing.items.length; i < length; i++) {
for (let i = 0, length = listing.items.length; i < length; i += 1) {
if (matchProductById(id, listing.items[i])) {
const product = clone(listing.items[i]);
listingItem.product = product;
listingItem.position = (i + 1);
listingItem.listId = listId || listing.listId;
listingItem.listName = listing.listName;
return listingItem;
result = {};
result.product = product;
result.position = (i + 1);
result.listId = listId || listing.listId;
result.listName = listing.listName;
return true;
}
}
}
}
return false;
});
if (result) return true;
}
}
return false;
});

return result;
}

static getCampaign(id, digitalData) {
let result;
if (digitalData.campaigns && digitalData.campaigns.length) {
for (const campaign of digitalData.campaigns) {
digitalData.campaigns.some((campaign) => {
if (campaign.id && String(campaign.id) === String(id)) {
return clone(campaign);
result = clone(campaign);
return true;
}
}
return false;
});
}
return result;
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/DDStorage.js
Expand Up @@ -17,7 +17,7 @@ class DDStorage {
persistedKeys.push(key);
this.updatePersistedKeys(persistedKeys);
}
return this.storage.set(key, value, exp);
this.storage.set(key, value, exp);
}
}

Expand Down Expand Up @@ -62,9 +62,9 @@ class DDStorage {

clear() {
const persistedKeys = this.getPersistedKeys();
for (const key of persistedKeys) {
persistedKeys.forEach((key) => {
this.storage.remove(key);
}
});
this.storage.remove(keyPersistedKeys);
this.storage.remove(keyLastEventTimestamp);
}
Expand Down

0 comments on commit 60084fc

Please sign in to comment.