@necto/image 1.0.1

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.
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";var t,e=Object.defineProperty,i=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,o=Object.prototype.hasOwnProperty,n={};((t,i)=>{for(var r in i)e(t,r,{get:i[r],enumerable:!0})})(n,{getImageSizes:()=>d,getImageStyle:()=>u,inferDimensions:()=>h,parseDimension:()=>s,transformProps:()=>g}),module.exports=(t=n,((t,n,a,c)=>{if(n&&"object"==typeof n||"function"==typeof n)for(let u of r(n))o.call(t,u)||u===a||e(t,u,{get:()=>n[u],enumerable:!(c=i(n,u))||c.enumerable});return t})(e({},"__esModule",{value:!0}),t));var a=require("@necto/dom"),c=require("@necto/url");function u({width:t,height:e,aspectRatio:i,layout:r,objectFit:o,background:n}){const u={};return o&&(u["object-fit"]=o),(0,c.isUrl)(n)?(u["background-image"]=`url(${n})`,u["background-size"]="cover",u["background-repeat"]="no-repeat"):n&&(u.background=n),"fixed"===r?(void 0!==t&&(u.width=(0,a.toPx)(t)),void 0!==e&&(u.height=(0,a.toPx)(e))):"constrained"===r?(u.width="100%",void 0!==t&&(u["max-width"]=(0,a.toPx)(t)),void 0!==e&&(u["max-height"]=(0,a.toPx)(e)),i&&(u["aspect-ratio"]=String(i))):"fullWidth"===r&&(u.width="100%",void 0!==e&&(u.height=(0,a.toPx)(e)),i&&(u["aspect-ratio"]=String(i))),Object.keys(u).length>0?u:void 0}function d(t,e){if(t&&e)switch(e){case"constrained":return`(min-width: ${t}px) ${t}px, 100vw`;case"fixed":return`${t}px`;case"fullWidth":return"100vw";default:return}}function s(t){if(void 0===t)return;if("number"==typeof t)return t;const e=Number.parseInt(t,10);return Number.isNaN(e)?void 0:e}function h(t){let e=s(t.width),i=s(t.height);return t.aspectRatio&&(e&&!i?i=Math.round(e/t.aspectRatio):i&&!e&&(e=Math.round(i*t.aspectRatio))),{width:e,height:i}}function g(t){const{src:e,width:i,height:r,aspectRatio:o,layout:n,priority:a=!1,background:c,objectFit:d,unstyled:s=!1}=t,h={src:e,loading:a?"eager":"lazy",decoding:a?"sync":"async",fetchpriority:a?"high":void 0};if(!s){const t=u({width:i,height:r,aspectRatio:o,layout:n,objectFit:d,background:c});t&&(h.style=t)}return"fixed"===n&&(h.width=i,h.height=r),h}
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Copyright (c) Corinvo, LLC. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ /** CSS properties object */
9
+ type CSSProperties = Record<string, string>;
10
+ /** Options for generating image styles */
11
+ interface StyleOptions {
12
+ width?: number | string;
13
+ height?: number | string;
14
+ aspectRatio?: number;
15
+ layout?: ImageLayout;
16
+ objectFit?: 'cover' | 'contain' | 'fill' | 'none' | 'scale-down';
17
+ background?: string;
18
+ }
19
+ type ImageStyleProps = StyleOptions;
20
+
21
+ /**
22
+ * Copyright (c) Corinvo, LLC. and affiliates.
23
+ *
24
+ * This source code is licensed under the MIT license found in the
25
+ * LICENSE file in the root directory of this source tree.
26
+ */
27
+
28
+ /** HTML image element attributes (output of transform) */
29
+ interface ImageAttributes {
30
+ src?: string;
31
+ srcset?: string;
32
+ sizes?: string;
33
+ width?: number | string;
34
+ height?: number | string;
35
+ alt?: string;
36
+ loading?: 'lazy' | 'eager';
37
+ decoding?: 'sync' | 'async' | 'auto';
38
+ fetchpriority?: 'high' | 'low' | 'auto';
39
+ style?: CSSProperties;
40
+ }
41
+ /** Image layout mode */
42
+ type ImageLayout = 'fixed' | 'constrained' | 'fullWidth';
43
+ /** CSS object-fit mode */
44
+ type ObjectFit = 'cover' | 'contain' | 'fill' | 'none' | 'scale-down';
45
+ /** Options for the transform function */
46
+ interface TransformOptions {
47
+ src: string;
48
+ width?: number | string;
49
+ height?: number | string;
50
+ aspectRatio?: number;
51
+ layout?: ImageLayout;
52
+ priority?: boolean;
53
+ background?: string;
54
+ objectFit?: ObjectFit;
55
+ unstyled?: boolean;
56
+ }
57
+
58
+ /**
59
+ * Copyright (c) Corinvo, LLC. and affiliates.
60
+ *
61
+ * This source code is licensed under the MIT license found in the
62
+ * LICENSE file in the root directory of this source tree.
63
+ */
64
+
65
+ /** Generates CSS styles for an image based on layout */
66
+ declare function getImageStyle({ width, height, aspectRatio, layout, objectFit, background }: StyleOptions): CSSProperties | undefined;
67
+ /** Generates the HTML sizes attribute for responsive images */
68
+ declare function getImageSizes(width?: number, layout?: ImageLayout): string | undefined;
69
+
70
+ /**
71
+ * Copyright (c) Corinvo, LLC. and affiliates.
72
+ *
73
+ * This source code is licensed under the MIT license found in the
74
+ * LICENSE file in the root directory of this source tree.
75
+ */
76
+ /** Parses a dimension value to a number */
77
+ declare function parseDimension(value: number | string | undefined): number | undefined;
78
+ /** Infers width/height from props and aspect ratio */
79
+ declare function inferDimensions(props: {
80
+ width?: number | string;
81
+ height?: number | string;
82
+ aspectRatio?: number;
83
+ }): {
84
+ width?: number;
85
+ height?: number;
86
+ };
87
+
88
+ /**
89
+ * Copyright (c) Corinvo, LLC. and affiliates.
90
+ *
91
+ * This source code is licensed under the MIT license found in the
92
+ * LICENSE file in the root directory of this source tree.
93
+ */
94
+
95
+ /** Transforms image props into img element attributes */
96
+ declare function transformProps(props: TransformOptions): ImageAttributes;
97
+
98
+ export { type CSSProperties, type ImageAttributes, type ImageLayout, type ImageStyleProps, type ObjectFit, type StyleOptions, type TransformOptions, getImageSizes, getImageStyle, inferDimensions, parseDimension, transformProps };
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Copyright (c) Corinvo, LLC. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ /** CSS properties object */
9
+ type CSSProperties = Record<string, string>;
10
+ /** Options for generating image styles */
11
+ interface StyleOptions {
12
+ width?: number | string;
13
+ height?: number | string;
14
+ aspectRatio?: number;
15
+ layout?: ImageLayout;
16
+ objectFit?: 'cover' | 'contain' | 'fill' | 'none' | 'scale-down';
17
+ background?: string;
18
+ }
19
+ type ImageStyleProps = StyleOptions;
20
+
21
+ /**
22
+ * Copyright (c) Corinvo, LLC. and affiliates.
23
+ *
24
+ * This source code is licensed under the MIT license found in the
25
+ * LICENSE file in the root directory of this source tree.
26
+ */
27
+
28
+ /** HTML image element attributes (output of transform) */
29
+ interface ImageAttributes {
30
+ src?: string;
31
+ srcset?: string;
32
+ sizes?: string;
33
+ width?: number | string;
34
+ height?: number | string;
35
+ alt?: string;
36
+ loading?: 'lazy' | 'eager';
37
+ decoding?: 'sync' | 'async' | 'auto';
38
+ fetchpriority?: 'high' | 'low' | 'auto';
39
+ style?: CSSProperties;
40
+ }
41
+ /** Image layout mode */
42
+ type ImageLayout = 'fixed' | 'constrained' | 'fullWidth';
43
+ /** CSS object-fit mode */
44
+ type ObjectFit = 'cover' | 'contain' | 'fill' | 'none' | 'scale-down';
45
+ /** Options for the transform function */
46
+ interface TransformOptions {
47
+ src: string;
48
+ width?: number | string;
49
+ height?: number | string;
50
+ aspectRatio?: number;
51
+ layout?: ImageLayout;
52
+ priority?: boolean;
53
+ background?: string;
54
+ objectFit?: ObjectFit;
55
+ unstyled?: boolean;
56
+ }
57
+
58
+ /**
59
+ * Copyright (c) Corinvo, LLC. and affiliates.
60
+ *
61
+ * This source code is licensed under the MIT license found in the
62
+ * LICENSE file in the root directory of this source tree.
63
+ */
64
+
65
+ /** Generates CSS styles for an image based on layout */
66
+ declare function getImageStyle({ width, height, aspectRatio, layout, objectFit, background }: StyleOptions): CSSProperties | undefined;
67
+ /** Generates the HTML sizes attribute for responsive images */
68
+ declare function getImageSizes(width?: number, layout?: ImageLayout): string | undefined;
69
+
70
+ /**
71
+ * Copyright (c) Corinvo, LLC. and affiliates.
72
+ *
73
+ * This source code is licensed under the MIT license found in the
74
+ * LICENSE file in the root directory of this source tree.
75
+ */
76
+ /** Parses a dimension value to a number */
77
+ declare function parseDimension(value: number | string | undefined): number | undefined;
78
+ /** Infers width/height from props and aspect ratio */
79
+ declare function inferDimensions(props: {
80
+ width?: number | string;
81
+ height?: number | string;
82
+ aspectRatio?: number;
83
+ }): {
84
+ width?: number;
85
+ height?: number;
86
+ };
87
+
88
+ /**
89
+ * Copyright (c) Corinvo, LLC. and affiliates.
90
+ *
91
+ * This source code is licensed under the MIT license found in the
92
+ * LICENSE file in the root directory of this source tree.
93
+ */
94
+
95
+ /** Transforms image props into img element attributes */
96
+ declare function transformProps(props: TransformOptions): ImageAttributes;
97
+
98
+ export { type CSSProperties, type ImageAttributes, type ImageLayout, type ImageStyleProps, type ObjectFit, type StyleOptions, type TransformOptions, getImageSizes, getImageStyle, inferDimensions, parseDimension, transformProps };
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ import{toPx as t}from"@necto/dom";import{isUrl as i}from"@necto/url";function e({width:e,height:o,aspectRatio:r,layout:n,objectFit:a,background:c}){const d={};return a&&(d["object-fit"]=a),i(c)?(d["background-image"]=`url(${c})`,d["background-size"]="cover",d["background-repeat"]="no-repeat"):c&&(d.background=c),"fixed"===n?(void 0!==e&&(d.width=t(e)),void 0!==o&&(d.height=t(o))):"constrained"===n?(d.width="100%",void 0!==e&&(d["max-width"]=t(e)),void 0!==o&&(d["max-height"]=t(o)),r&&(d["aspect-ratio"]=String(r))):"fullWidth"===n&&(d.width="100%",void 0!==o&&(d.height=t(o)),r&&(d["aspect-ratio"]=String(r))),Object.keys(d).length>0?d:void 0}function o(t,i){if(t&&i)switch(i){case"constrained":return`(min-width: ${t}px) ${t}px, 100vw`;case"fixed":return`${t}px`;case"fullWidth":return"100vw";default:return}}function r(t){if(void 0===t)return;if("number"==typeof t)return t;const i=Number.parseInt(t,10);return Number.isNaN(i)?void 0:i}function n(t){let i=r(t.width),e=r(t.height);return t.aspectRatio&&(i&&!e?e=Math.round(i/t.aspectRatio):e&&!i&&(i=Math.round(e*t.aspectRatio))),{width:i,height:e}}function a(t){const{src:i,width:o,height:r,aspectRatio:n,layout:a,priority:c=!1,background:d,objectFit:h,unstyled:u=!1}=t,s={src:i,loading:c?"eager":"lazy",decoding:c?"sync":"async",fetchpriority:c?"high":void 0};if(!u){const t=e({width:o,height:r,aspectRatio:n,layout:a,objectFit:h,background:d});t&&(s.style=t)}return"fixed"===a&&(s.width=o,s.height=r),s}export{o as getImageSizes,e as getImageStyle,n as inferDimensions,r as parseDimension,a as transformProps};
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@necto/image",
3
+ "version": "1.0.1",
4
+ "description": "Framework-agnostic image transformation utilities for CDN providers",
5
+ "scripts": {
6
+ "build": "tsup --minify terser"
7
+ },
8
+ "author": "Corinvo OSS Team",
9
+ "license": "MIT",
10
+ "dependencies": {
11
+ "@necto/dom": "workspace:*",
12
+ "@necto/url": "workspace:*"
13
+ },
14
+ "devDependencies": {
15
+ "@types/node": "^22.14.1",
16
+ "tsup": "^8.4.0"
17
+ },
18
+ "files": [
19
+ "dist",
20
+ "README.md"
21
+ ],
22
+ "type": "module",
23
+ "types": "./dist/index.d.ts",
24
+ "main": "./dist/index.cjs",
25
+ "module": "./dist/index.mjs",
26
+ "exports": {
27
+ ".": {
28
+ "types": "./dist/index.d.ts",
29
+ "import": "./dist/index.mjs",
30
+ "require": "./dist/index.cjs"
31
+ }
32
+ }
33
+ }