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