@ecomplus/utils
Version:
JS utility functions to E-Com Plus (not only) related apps
59 lines (58 loc) • 2.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/web.dom-collections.for-each.js");
require("core-js/modules/es.symbol.js");
require("core-js/modules/es.symbol.description.js");
require("core-js/modules/es.symbol.iterator.js");
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
/**
* @method
* @memberof ecomUtils
* @name imgSizes
* @description Splits image size string and returns object with 'width' and 'height'.
* @param {Object.<string, *>|string} img - Image object body or size string
* @returns {Object.<string, *>}
*
* @example
* // Using a img sizes string as parameter
* const size = '200x50'
* ecomUtils.imgSizes(size)
* // => {width: 200, height: 50}
* // Using a img sizes object as parameter
* const sizeObj = {size: '1000x1000'}
* ecomUtils.imgSizes(sizeObj)
* // => {width: 1000, height: 1000}
*/
var imgSizes = function imgSizes(img) {
// defaul sizes object with zeros
var sizes = {
width: 0,
height: 0
};
var sizeString;
if (_typeof(img) === 'object' && img !== null) {
sizeString = img.size;
} else {
// expect to receive img as size string
sizeString = img;
}
if (typeof sizeString === 'string') {
sizeString.split('x').forEach(function (value, index) {
// parse width and height to numbers
value = parseFloat(value);
if (!isNaN(value)) {
sizes[index === 0 ? 'width' : 'height'] = value;
}
});
}
// returns object with width and height
return sizes;
};
var _default = exports.default = imgSizes;