-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Require Node.js 12.20 and move to ESM
- Loading branch information
1 parent
9549e25
commit dac174c
Showing
6 changed files
with
115 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,70 @@ | ||
/* eslint-disable no-redeclare */ | ||
|
||
declare namespace srcset { | ||
interface SrcSetDefinition { | ||
url: string; | ||
width?: number; | ||
density?: number; | ||
} | ||
|
||
interface Options { | ||
/** | ||
When strict mode is enabled, errors will be thrown on invalid input. | ||
When disabled, a best effort will be made to handle invalid input and no errors will be thrown. The output may be invalid. | ||
@default false | ||
*/ | ||
strict?: boolean; | ||
} | ||
export interface SrcSetDefinition { | ||
readonly url: string; | ||
readonly width?: number; | ||
readonly density?: number; | ||
} | ||
|
||
declare const srcset: { | ||
export interface Options { | ||
/** | ||
Parse the HTML `<img>` [srcset](http://mobile.smashingmagazine.com/2013/08/21/webkit-implements-srcset-and-why-its-a-good-thing/) attribute. | ||
Accepts a “srcset” string and returns an array of objects with the possible properties: `url` (always), `width`, `density`, and `height`. | ||
When strict mode is enabled, errors will be thrown on invalid input. | ||
@param srcset - A “srcset” string. | ||
When disabled, a best effort will be made to handle invalid input and no errors will be thrown. The output may be invalid. | ||
@example | ||
``` | ||
import srcset = require('srcset'); | ||
console.log(srcset.parse('banner-HD.jpg 2x, banner-phone.jpg 100w')); | ||
// [ | ||
// { | ||
// url: 'banner-HD.jpg', | ||
// density: 2 | ||
// }, | ||
// { | ||
// url: 'banner-phone.jpg', | ||
// width: 100 | ||
// } | ||
// ] | ||
``` | ||
@default false | ||
*/ | ||
parse: (srcset: string, options?: srcset.Options) => srcset.SrcSetDefinition[]; | ||
|
||
/** | ||
Stringify `SrcSetDefinition`s. | ||
@param SrcSetDefinitions - Each object should have a `url` field and may have either `width` or `density`. When the `strict` option is `true`, only `width` or `density` is accepted. | ||
@returns A “srcset” string. | ||
@example | ||
``` | ||
import srcset = require('srcset'); | ||
const stringified = srcset.stringify([ | ||
{ | ||
url: 'banner-HD.jpg', | ||
density: 2 | ||
}, | ||
{ | ||
url: 'banner-phone.jpg', | ||
width: 100 | ||
} | ||
]); | ||
readonly strict?: boolean; | ||
} | ||
|
||
console.log(stringified); | ||
// banner-HD.jpg 2x, banner-phone.jpg 100w | ||
``` | ||
*/ | ||
stringify: (srcSetDefinitions: readonly srcset.SrcSetDefinition[], options?: srcset.Options) => string; | ||
}; | ||
/** | ||
Parse the HTML `<img>` [srcset](http://mobile.smashingmagazine.com/2013/08/21/webkit-implements-srcset-and-why-its-a-good-thing/) attribute. | ||
Accepts a “srcset” string and returns an array of objects with the possible properties: `url` (always), `width`, `density`, and `height`. | ||
@param srcset - A “srcset” string. | ||
@example | ||
``` | ||
import {parseSrcset} from 'srcset'; | ||
console.log(parseSrcset('banner-HD.jpg 2x, banner-phone.jpg 100w')); | ||
// [ | ||
// { | ||
// url: 'banner-HD.jpg', | ||
// density: 2 | ||
// }, | ||
// { | ||
// url: 'banner-phone.jpg', | ||
// width: 100 | ||
// } | ||
// ] | ||
``` | ||
*/ | ||
export function parseSrcset(srcset: string, options?: Options): SrcSetDefinition[]; | ||
|
||
/** | ||
Stringify `SrcSetDefinition`s. | ||
@param SrcSetDefinitions - Each object should have a `url` field and may have either `width` or `density`. When the `strict` option is `true`, only `width` or `density` is accepted. | ||
@returns A “srcset” string. | ||
@example | ||
``` | ||
import {stringifySrcset} from 'srcset'; | ||
const stringified = stringifySrcset([ | ||
{ | ||
url: 'banner-HD.jpg', | ||
density: 2 | ||
}, | ||
{ | ||
url: 'banner-phone.jpg', | ||
width: 100 | ||
} | ||
]); | ||
export = srcset; | ||
console.log(stringified); | ||
// banner-HD.jpg 2x, banner-phone.jpg 100w | ||
``` | ||
*/ | ||
export function stringifySrcset(srcSetDefinitions: readonly SrcSetDefinition[], options?: Options): string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import {expectType, expectError} from 'tsd'; | ||
import srcset = require('./index.js'); | ||
import {parseSrcset, stringifySrcset, SrcSetDefinition} from './index.js'; | ||
|
||
const parsed = srcset.parse('banner-HD.jpg 2x, banner-phone.jpg 100w'); | ||
expectType<srcset.SrcSetDefinition[]>(parsed); | ||
const parsed = parseSrcset('banner-HD.jpg 2x, banner-phone.jpg 100w'); | ||
expectType<SrcSetDefinition[]>(parsed); | ||
|
||
parsed.push({url: 'banner-phone-HD.jpg'}, {url: 'banner-phone-HD.jpg', width: 100}, {url: 'banner-phone-HD.jpg', density: 2}); | ||
expectError(parsed.push({})); | ||
|
||
expectType<string>(srcset.stringify(parsed)); | ||
expectType<string>(stringifySrcset(parsed)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,10 @@ | |
"email": "[email protected]", | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=12" | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava && tsd" | ||
|
@@ -34,8 +36,8 @@ | |
"element" | ||
], | ||
"devDependencies": { | ||
"ava": "^2.4.0", | ||
"tsd": "^0.13.1", | ||
"xo": "^0.39.0" | ||
"ava": "^3.15.0", | ||
"tsd": "^0.19.0", | ||
"xo": "^0.46.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.