vinmonopolet-ts
Version:
Extracts information on products, categories and stores from Vinmonopolet
52 lines (51 loc) • 2.03 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
const class_validator_1 = require("class-validator");
const sizeMatcher = /cache\/(\d+)x(\d+)[/-]/;
class ProductImage {
constructor(img) {
this.format = img.format;
this.description = img.altText;
this.type = img.imageType;
this.url = img.url;
this.size = guessSizeFromUrl(img.url);
}
}
__decorate([
(0, class_validator_1.IsString)(),
(0, class_validator_1.IsNotEmpty)()
], ProductImage.prototype, "format", void 0);
__decorate([
(0, class_validator_1.IsString)(),
(0, class_validator_1.IsOptional)()
], ProductImage.prototype, "description", void 0);
__decorate([
(0, class_validator_1.IsString)(),
(0, class_validator_1.IsNotEmpty)()
], ProductImage.prototype, "type", void 0);
__decorate([
(0, class_validator_1.IsString)(),
(0, class_validator_1.IsNotEmpty)()
], ProductImage.prototype, "url", void 0);
__decorate([
(0, class_validator_1.IsOptional)()
], ProductImage.prototype, "size", void 0);
ProductImage.prototype.toString = function () {
return this.url;
};
function guessSizeFromUrl(url) {
const [, width, height] = (url && url.match(sizeMatcher)) || [];
return width && height
? {
maxWidth: Number(width),
maxHeight: Number(height),
}
: null;
}
exports.default = ProductImage;