@unikue/ts-lang-utils 1.2.8 → 1.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ export declare function extractExtension(imageSource?: string | null, allowedExtensions?: Array<string | null | undefined>, fallback?: string): string;
@@ -0,0 +1,62 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/util/ImageUtils/extractExtension.ts
20
+ var extractExtension_exports = {};
21
+ __export(extractExtension_exports, {
22
+ extractExtension: () => extractExtension
23
+ });
24
+ module.exports = __toCommonJS(extractExtension_exports);
25
+ var import_UriUtils = require("../UriUtils");
26
+ var import_UriUtils2 = require("../UriUtils");
27
+ var DEFAULT_EXTENSIONS = [".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp", ".heic", ".heif", ".svg"];
28
+ function extractExtension(imageSource, allowedExtensions = DEFAULT_EXTENSIONS, fallback = ".jpg") {
29
+ if (!imageSource) {
30
+ return fallback;
31
+ }
32
+ let pathOnly = imageSource;
33
+ if ((0, import_UriUtils.isHttp)(imageSource) || (0, import_UriUtils2.isHttps)(imageSource)) {
34
+ const qIndex = pathOnly.indexOf("?");
35
+ const hIndex = pathOnly.indexOf("#");
36
+ let cutoff = pathOnly.length;
37
+ if (qIndex !== -1) {
38
+ cutoff = Math.min(cutoff, qIndex);
39
+ }
40
+ if (hIndex !== -1) {
41
+ cutoff = Math.min(cutoff, hIndex);
42
+ }
43
+ pathOnly = pathOnly.substring(0, cutoff);
44
+ }
45
+ const lastDot = pathOnly.lastIndexOf(".");
46
+ if (lastDot === -1) {
47
+ return fallback;
48
+ }
49
+ const ext = pathOnly.substring(lastDot).toLowerCase();
50
+ if (ext === ".") {
51
+ return fallback;
52
+ }
53
+ if (allowedExtensions && allowedExtensions.length) {
54
+ const lowerExtensions = allowedExtensions.map((e) => e == null ? void 0 : e.toLowerCase());
55
+ return lowerExtensions.includes(ext) ? ext : fallback;
56
+ }
57
+ return ext;
58
+ }
59
+ // Annotate the CommonJS export names for ESM import in node:
60
+ 0 && (module.exports = {
61
+ extractExtension
62
+ });
@@ -1,5 +1,6 @@
1
1
  export { detectDimension } from './detectDimension';
2
2
  export { detectSource } from './detectSource';
3
+ export { extractExtension } from './extractExtension';
3
4
  export { isImage } from './isImage';
4
5
  export { isGifImage } from './isGifImage';
5
6
  export { isJpgImage } from './isJpgImage';
@@ -21,6 +21,7 @@ var ImageUtils_exports = {};
21
21
  __export(ImageUtils_exports, {
22
22
  detectDimension: () => import_detectDimension.detectDimension,
23
23
  detectSource: () => import_detectSource.detectSource,
24
+ extractExtension: () => import_extractExtension.extractExtension,
24
25
  isGifImage: () => import_isGifImage.isGifImage,
25
26
  isImage: () => import_isImage.isImage,
26
27
  isJpgImage: () => import_isJpgImage.isJpgImage,
@@ -29,6 +30,7 @@ __export(ImageUtils_exports, {
29
30
  module.exports = __toCommonJS(ImageUtils_exports);
30
31
  var import_detectDimension = require("./detectDimension");
31
32
  var import_detectSource = require("./detectSource");
33
+ var import_extractExtension = require("./extractExtension");
32
34
  var import_isImage = require("./isImage");
33
35
  var import_isGifImage = require("./isGifImage");
34
36
  var import_isJpgImage = require("./isJpgImage");
@@ -37,6 +39,7 @@ var import_isPngImage = require("./isPngImage");
37
39
  0 && (module.exports = {
38
40
  detectDimension,
39
41
  detectSource,
42
+ extractExtension,
40
43
  isGifImage,
41
44
  isImage,
42
45
  isJpgImage,
@@ -0,0 +1 @@
1
+ export declare function extractExtension(imageSource?: string | null, allowedExtensions?: Array<string | null | undefined>, fallback?: string): string;
@@ -0,0 +1,38 @@
1
+ import { isHttp } from "../UriUtils";
2
+ import { isHttps } from "../UriUtils";
3
+ var DEFAULT_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.bmp', '.heic', '.heif', '.svg'];
4
+ export function extractExtension(imageSource) {
5
+ var allowedExtensions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_EXTENSIONS;
6
+ var fallback = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '.jpg';
7
+ if (!imageSource) {
8
+ return fallback;
9
+ }
10
+ var pathOnly = imageSource;
11
+ if (isHttp(imageSource) || isHttps(imageSource)) {
12
+ var qIndex = pathOnly.indexOf('?');
13
+ var hIndex = pathOnly.indexOf('#');
14
+ var cutoff = pathOnly.length;
15
+ if (qIndex !== -1) {
16
+ cutoff = Math.min(cutoff, qIndex);
17
+ }
18
+ if (hIndex !== -1) {
19
+ cutoff = Math.min(cutoff, hIndex);
20
+ }
21
+ pathOnly = pathOnly.substring(0, cutoff);
22
+ }
23
+ var lastDot = pathOnly.lastIndexOf('.');
24
+ if (lastDot === -1) {
25
+ return fallback;
26
+ }
27
+ var ext = pathOnly.substring(lastDot).toLowerCase();
28
+ if (ext === '.') {
29
+ return fallback;
30
+ }
31
+ if (allowedExtensions && allowedExtensions.length) {
32
+ var lowerExtensions = allowedExtensions.map(function (e) {
33
+ return e === null || e === void 0 ? void 0 : e.toLowerCase();
34
+ });
35
+ return lowerExtensions.includes(ext) ? ext : fallback;
36
+ }
37
+ return ext;
38
+ }
@@ -1,5 +1,6 @@
1
1
  export { detectDimension } from './detectDimension';
2
2
  export { detectSource } from './detectSource';
3
+ export { extractExtension } from './extractExtension';
3
4
  export { isImage } from './isImage';
4
5
  export { isGifImage } from './isGifImage';
5
6
  export { isJpgImage } from './isJpgImage';
@@ -1,5 +1,6 @@
1
1
  export { detectDimension } from "./detectDimension";
2
2
  export { detectSource } from "./detectSource";
3
+ export { extractExtension } from "./extractExtension";
3
4
  export { isImage } from "./isImage";
4
5
  export { isGifImage } from "./isGifImage";
5
6
  export { isJpgImage } from "./isJpgImage";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unikue/ts-lang-utils",
3
- "version": "1.2.8",
3
+ "version": "1.2.9",
4
4
  "title": "TsLangUtils",
5
5
  "description": "Common lang utilities for typescript",
6
6
  "homepage": "https://unikueltd.github.io/ts-lang-utils",
@@ -68,14 +68,15 @@
68
68
  "@unikue/typedoc-theme-dumi": "^1.1.3",
69
69
  "del-cli": "^7.0.0",
70
70
  "eslint": "^10.6.0",
71
- "eslint-plugin-jest": "^29.15.3",
72
- "father": "^4.6.23",
71
+ "eslint-plugin-jest": "^29.15.4",
72
+ "father": "^4.6.25",
73
73
  "gh-pages": "^6.3.0",
74
74
  "globals": "^17.7.0",
75
+ "jest-environment-jsdom": "^30.4.1",
75
76
  "ts-jest": "^29.4.11",
76
77
  "typedoc": "~0.28.19",
77
78
  "typescript": "^6.0.3",
78
- "typescript-eslint": "^8.62.0"
79
+ "typescript-eslint": "^8.62.1"
79
80
  },
80
81
  "peerDependencies": {
81
82
  "typescript": ">=4.0.0"