Skip to content

Instantly share code, notes, and snippets.

@jakeNiemiec jakeNiemiec/bad.js Secret
Created Nov 7, 2018

Embed
What would you like to do?
getUniqueAttrs(products) {
const globalAttrs = [];
const globalAttrsList = [];
const globalUniqueAttrsList = [];
const productAttrs = [];
const allAttrs = [];
const allProducts = [];
// console.warn('getUniqueAttrs');
// console.warn(products);
const modelAttrs = this.modelAttrsInit();
for (const product of products) {
// EACH PRODUCT
productAttrs[product.sku] = {}; // New object for every product
if (!allProducts.includes(product.sku)) {
allProducts.push(product.sku);
}
for (const attr in product.attrs) {
if (product.attrs.hasOwnProperty(attr)) {
if (product.attrs[attr]['display']) {
// build all attrs
if (!allAttrs.includes(attr)) {
allAttrs.push(attr);
}
// EACH ATTR
productAttrs[product.sku][attr] = {
'title': product.attrs[attr].key,
'body': product.attrs[attr].value,
'group': product.attrs[attr].group
};
//TODO: EXTRACT ME
if (modelAttrs[attr]) {
const pAttr = product.attrs[attr];
// console.warn('w',modelAttrs[attr]);
if (!modelAttrs[attr].map(p => p.raw[0]).includes(pAttr.raw[0])) {
pAttr['index'] = attr;
modelAttrs[attr].push(pAttr);
}
}
}
}
}
}
for (const attr of allAttrs) {
// of all values, get the *current* attr of the first product
if (productAttrs && allProducts && attr) {
// TODO: REFACTOR me
if (this.attrsAreEqual(productAttrs, allProducts, attr)) {
// all attrs are equal, this is a global attr
globalAttrs.push({
title: productAttrs[allProducts[0]][attr].title,
body: productAttrs[allProducts[0]][attr].body,
group: productAttrs[allProducts[0]][attr].group
});
globalAttrsList.push(attr);
}
else {
// The attr may not be on the first object
for (let i = 0; i < allProducts.length; i++) {
if (productAttrs[allProducts[i]][attr]) {
// Push the unique attr, we do not need the sku as long as we have the name
globalUniqueAttrsList.push(attr);
break;
}
}
}
}
}
this.globalAttrs = globalAttrs;
this.globalAttrsList = globalAttrsList;
this.globalUniqueAttrsList = globalUniqueAttrsList;
this._allAttrs = allAttrs;
this.allProducts = allProducts;
this.productAttrs = productAttrs;
// console.debug('ua', productAttrs);
// console.debug('aa', allAttrs);
// console.debug('ap', allProducts);
// console.debug('unique attrs', this.globalUniqueAttrsList);
// console.debug('global attrs', this.globalAttrsList);
// console.debug('a',this.attrs);
this.loadSingleUniqueAttrs();
// console.debug('globalUniqueAttrs',this.globalUniqueAttrs);
// console.debug('globalAttrs',this.globalAttrs);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.