Skip to content

Commit d69e687

Browse files
authored
Add index.d.ts file (#7)
1 parent 4eaf4c4 commit d69e687

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

index.d.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
export type Candidate = {
2+
source: {
3+
value: string;
4+
startOffset: number;
5+
};
6+
width?: {
7+
value: number;
8+
};
9+
height?: {
10+
value: number;
11+
};
12+
density?: {
13+
value: number;
14+
};
15+
};
16+
/**
17+
* Parses the string value that appears in markup `<img srcset="here">`.
18+
*
19+
* @description A javascript parser for the [HTML5 srcset](http://www.w3.org/TR/html-srcset/) attribute, based on the [WHATWG reference algorithm](https://html.spec.whatwg.org/multipage/embedded-content.html#parse-a-srcset-attribute). It has an extensive test suite based on the [W3C srcset conformance checker](http://w3c-test.org/html/semantics/embedded-content/the-img-element/srcset/parse-a-srcset-attribute.html)
20+
*
21+
* @param {string} input - The string value to parse.
22+
* @returns {Array<Candidate>} An array of objects representing the image candidates.
23+
* @throws {Error} If the input string is empty or does not contain any image candidate strings.
24+
*
25+
* @example
26+
* ```ts
27+
* import parseSrcset from "@prettier/parse-srcset";
28+
*
29+
* parseSrcset('elva-fairy-320w.jpg, elva-fairy-480w.jpg 1.5x, elva-fairy-640w.jpg 2x');
30+
* // output:
31+
* [
32+
* { source: { value: 'elva-fairy-320w.jpg', startOffset: 0 } },
33+
* {
34+
* source: { value: 'elva-fairy-480w.jpg', startOffset: 21 },
35+
* density: { value: 1.5 }
36+
* },
37+
* {
38+
* source: { value: 'elva-fairy-640w.jpg', startOffset: 47 },
39+
* density: { value: 2 }
40+
* }
41+
* ]
42+
* ```
43+
*/
44+
declare function parseSrcset(input: string): Array<Candidate>;
45+
export default parseSrcset;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "A spec-conformant JavaScript parser for the HTML5 srcset attribute",
55
"exports": "./index.js",
66
"files": [
7-
"index.js"
7+
"index.js",
8+
"index.d.ts"
89
],
910
"type": "module",
1011
"directories": {

0 commit comments

Comments
 (0)