Skip to content

Commit

Permalink
#116 Add dist to test github instalation
Browse files Browse the repository at this point in the history
  • Loading branch information
junajan committed Feb 22, 2018
1 parent 145c5c8 commit 810d2eb
Show file tree
Hide file tree
Showing 10 changed files with 1,911 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.DS_Store
node_modules
*.log
dist
dist-test
tmp
config.js
Expand Down
92 changes: 92 additions & 0 deletions dist/common-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
var CommonUtils, _, debug,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

debug = require('debug')('sphere-product-import-common-utils');

_ = require('underscore');

_.mixin(require('underscore-mixins'));

CommonUtils = (function() {
function CommonUtils(logger) {
this.logger = logger;
this.uniqueObjectFilter = bind(this.uniqueObjectFilter, this);
debug("Enum Validator initialized.");
}

CommonUtils.prototype.uniqueObjectFilter = function(objCollection) {
var uniques;
uniques = [];
_.each(objCollection, (function(_this) {
return function(obj) {
if (!_this.isObjectPresentInArray(uniques, obj)) {
return uniques.push(obj);
}
};
})(this));
return uniques;
};

CommonUtils.prototype.isObjectPresentInArray = function(array, object) {
return _.find(array, function(element) {
return _.isEqual(element, object);
});
};


/**
* takes an array of sku chunks and returns an array of sku chunks
* where each chunk fits inside the query
*/

CommonUtils.prototype._separateSkusChunksIntoSmallerChunks = function(skus, queryLimit) {
var availableSkuBytes, chunks, fixBytes, getBytesOfChunk, whereQuery;
whereQuery = "masterVariant(sku in ()) or variants(sku in ())";
fixBytes = Buffer.byteLength(encodeURIComponent(whereQuery), 'utf-8');
availableSkuBytes = queryLimit - fixBytes;
getBytesOfChunk = function(chunk) {
var skuString;
skuString = encodeURIComponent("\"" + (chunk.join('","')) + "\"\"" + (chunk.join('","')) + "\"");
return Buffer.byteLength(skuString, 'utf-8');
};
chunks = _.reduce(skus, function(chunks, sku) {
var lastChunk;
lastChunk = _.clone(_.last(chunks));
lastChunk.push(sku);
if (getBytesOfChunk(lastChunk) < availableSkuBytes) {
chunks.pop();
chunks.push(lastChunk);
} else {
chunks.push([sku]);
}
return chunks;
}, [[]]);
return chunks;
};

CommonUtils.prototype.canBePublished = function(product, publishingStrategy) {
if (publishingStrategy === 'always') {
return true;
} else if (publishingStrategy === 'stagedAndPublishedOnly') {
if (product.hasStagedChanges === true && product.published === true) {
return true;
} else {
return false;
}
} else if (publishingStrategy === 'notStagedAndPublishedOnly') {
if (product.hasStagedChanges === false && product.published === true) {
return true;
} else {
return false;
}
} else {
this.logger.warn('unknown publishing strategy ' + publishingStrategy);
return false;
}
};

return CommonUtils;

})();

module.exports = CommonUtils;
82 changes: 82 additions & 0 deletions dist/ensure-default-attributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
var EnsureDefaultAttributes, Promise, _,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

_ = require('underscore');

_.mixin(require('underscore-mixins'));

Promise = require('bluebird');

EnsureDefaultAttributes = (function() {
function EnsureDefaultAttributes(logger, defaultAttributes1) {
this.logger = logger;
this.defaultAttributes = defaultAttributes1;
this._ensureInVariant = bind(this._ensureInVariant, this);
this.ensureDefaultAttributesInProduct = bind(this.ensureDefaultAttributesInProduct, this);
this.logger.debug('Ensuring default attributes');
}

EnsureDefaultAttributes.prototype.ensureDefaultAttributesInProduct = function(product, productFromServer) {
var masterVariant, updatedProduct, updatedVariants;
updatedProduct = _.deepClone(product);
if (productFromServer) {
masterVariant = productFromServer.masterVariant;
}
updatedProduct.masterVariant = this._ensureInVariant(product.masterVariant, masterVariant);
updatedVariants = _.map(product.variants, (function(_this) {
return function(variant) {
var serverVariant;
if (productFromServer) {
serverVariant = productFromServer.variants.filter(function(v) {
return v.sku === variant.sku;
})[0];
}
return _this._ensureInVariant(variant, serverVariant);
};
})(this));
updatedProduct.variants = updatedVariants;
return Promise.resolve(updatedProduct);
};

EnsureDefaultAttributes.prototype._ensureInVariant = function(variant, serverVariant) {
var defaultAttribute, defaultAttributes, extendedAttributes, i, len, serverAttributes;
defaultAttributes = _.deepClone(this.defaultAttributes);
if (!variant.attributes) {
return variant;
}
extendedAttributes = _.deepClone(variant.attributes);
if (serverVariant) {
serverAttributes = serverVariant.attributes;
}
for (i = 0, len = defaultAttributes.length; i < len; i++) {
defaultAttribute = defaultAttributes[i];
if (!this._isAttributeExisting(defaultAttribute, variant.attributes)) {
this._updateAttribute(serverAttributes, defaultAttribute, extendedAttributes);
}
}
variant.attributes = extendedAttributes;
return variant;
};

EnsureDefaultAttributes.prototype._updateAttribute = function(serverAttributes, defaultAttribute, extendedAttributes) {
var serverAttribute;
if (serverAttributes) {
serverAttribute = this._isAttributeExisting(defaultAttribute, serverAttributes);
if (serverAttribute) {
defaultAttribute.value = serverAttribute.value;
}
}
return extendedAttributes.push(defaultAttribute);
};

EnsureDefaultAttributes.prototype._isAttributeExisting = function(defaultAttribute, attributeList) {
return _.findWhere(attributeList, {
name: "" + defaultAttribute.name
});
};

return EnsureDefaultAttributes;

})();

module.exports = EnsureDefaultAttributes;
Loading

0 comments on commit 810d2eb

Please sign in to comment.