summaryrefslogtreecommitdiff
path: root/node_modules/@jet/environment/models/artwork.js
blob: 324138d538b18315558354972bd2a17a7c07bbde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeArtworkURLTemplate = void 0;
const validation = require("../json/validation");
const optional_1 = require("../types/optional");
const urls_1 = require("../util/urls");
/**
 * Regex to parse artwork URL template string.
 */
const URL_TEMPLATE_PARSER = new RegExp("^({w}|[0-9]+(?:.[0-9]*)?)x({h}|[0-9]+(?:.[0-9]*)?)({c}|[a-z]{2}).({f}|[a-z]+)$");
/**
 * Create an instance of artwork URL template from string.
 * @param fromString - String to create artwork URL template from.
 * @returns A new artwork URL template or `null` if string
 * does not represent a valid artwork URL template.
 */
function makeArtworkURLTemplate(fromString) {
    // A valid URL that ends with '{w}x{h}{c}.{f}'
    // with any of placeholders possibly resolved to an actual value.
    const url = new urls_1.URL(fromString);
    if (url.pathname === undefined) {
        validation.context("makeArtworkURLTemplate", () => {
            validation.unexpectedType("ignoredValue", "A valid URL string", fromString);
        });
        return null;
    }
    // Expecting 5 matches: whole string + width, height, crop code and format.
    const lastPathComponent = fromString.substring(fromString.lastIndexOf("/") + 1);
    const matches = URL_TEMPLATE_PARSER.exec(lastPathComponent);
    if ((0, optional_1.isNothing)(matches) || matches.length !== 5) {
        validation.context("makeArtworkURLTemplate", () => {
            validation.unexpectedType("ignoredValue", "A valid artwork URL template ending with {w}x{h}{c}.{f} format", lastPathComponent);
        });
        return null;
    }
    return fromString;
}
exports.makeArtworkURLTemplate = makeArtworkURLTemplate;
//# sourceMappingURL=artwork.js.map