UNPKG

vinmonopolet-ts

Version:

Extracts information on products, categories and stores from Vinmonopolet

55 lines (54 loc) 1.82 kB
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; }; import { IsNotEmpty, IsOptional, IsString } from "class-validator"; const sizeMatcher = /cache\/(\d+)x(\d+)[/-]/; class ProductImage { format; description; type; url; size; constructor(img) { this.format = img.format; this.description = img.altText; this.type = img.imageType; this.url = img.url; this.size = guessSizeFromUrl(img.url); } } __decorate([ IsString(), IsNotEmpty() ], ProductImage.prototype, "format", void 0); __decorate([ IsString(), IsOptional() ], ProductImage.prototype, "description", void 0); __decorate([ IsString(), IsNotEmpty() ], ProductImage.prototype, "type", void 0); __decorate([ IsString(), IsNotEmpty() ], ProductImage.prototype, "url", void 0); __decorate([ 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; } export default ProductImage;