diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index c1870cf..3b8aa86 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -10,12 +10,12 @@ jobs:
fail-fast: false
matrix:
node-version:
+ - 16
- 14
- 12
- - 10
steps:
- uses: actions/checkout@v2
- - uses: actions/setup-node@v1
+ - uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
diff --git a/index.d.ts b/index.d.ts
index d760fa6..98c2236 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -1,3 +1,5 @@
+/* eslint-disable no-redeclare */
+
declare namespace srcset {
interface SrcSetDefinition {
url: string;
@@ -5,7 +7,7 @@ declare namespace srcset {
density?: number;
}
- interface SrcSetOptions {
+ interface Options {
/**
When strict mode is enabled, errors will be thrown on invalid input.
@@ -21,9 +23,9 @@ declare const srcset: {
/**
Parse the HTML `` [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`.
+ 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.
+ @param srcset - A “srcset” string.
@example
```
@@ -42,14 +44,14 @@ declare const srcset: {
// ]
```
*/
- parse: (srcset: string, options?: srcset.SrcSetOptions) => srcset.SrcSetDefinition[];
+ parse: (srcset: string, options?: srcset.Options) => srcset.SrcSetDefinition[];
/**
- Stringify `SrcSetDefinition`s. Accepts an array of `SrcSetDefinition` objects and returns a srcset string.
+ Stringify `SrcSetDefinition`s.
- @param SrcSetDefinitions - An array of `SrcSetDefinition` objects. Each object should have a `url` field and may have either `width` or `density`. When `options.strict` is set to false, a `height` field is also accepted, and multiple descriptors (`width`, `height`, and`density`) are accepted.
+ @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.
+ @returns A “srcset” string.
@example
```
@@ -70,7 +72,7 @@ declare const srcset: {
// banner-HD.jpg 2x, banner-phone.jpg 100w
```
*/
- stringify: (srcSetDefinitions: srcset.SrcSetDefinition[], options?: srcset.SrcSetOptions) => string;
+ stringify: (srcSetDefinitions: readonly srcset.SrcSetDefinition[], options?: srcset.Options) => string;
};
export = srcset;
diff --git a/index.js b/index.js
index 3d5b49a..2b0a40e 100644
--- a/index.js
+++ b/index.js
@@ -51,20 +51,32 @@ const validDescriptorCheck = (value, postfix, descriptor) => {
throw new TypeError(`${descriptor || value} is not a valid number`);
}
- if (postfix === 'w') {
- if (value <= 0) {
- throw new Error('Width descriptor must be greater than zero');
- } else if (!Number.isInteger(value)) {
- throw new TypeError('Width descriptor must be an integer');
+ switch (postfix) {
+ case 'w': {
+ if (value <= 0) {
+ throw new Error('Width descriptor must be greater than zero');
+ } else if (!Number.isInteger(value)) {
+ throw new TypeError('Width descriptor must be an integer');
+ }
+
+ break;
+ }
+
+ case 'x': {
+ if (value <= 0) {
+ throw new Error('Pixel density descriptor must be greater than zero');
+ }
+
+ break;
}
- } else if (postfix === 'x') {
- if (value <= 0) {
- throw new Error('Pixel density descriptor must be greater than zero');
+
+ case 'h': {
+ throw new Error('Height descriptor is no longer allowed');
+ }
+
+ default: {
+ throw new Error(`Invalid srcset descriptor: ${descriptor}`);
}
- } else if (postfix === 'h') {
- throw new Error('Height descriptor is no longer allowed');
- } else {
- throw new Error(`Invalid srcset descriptor: ${descriptor}`);
}
};
@@ -90,12 +102,23 @@ exports.parse = (string, {strict = false} = {}) => {
duplicateDescriptorCheck(allDescriptors, value, postfix);
}
- if (postfix === 'w') {
- result.width = value;
- } else if (postfix === 'h') {
- result.height = value;
- } else if (postfix === 'x') {
- result.density = value;
+ switch (postfix) {
+ case 'w': {
+ result.width = value;
+ break;
+ }
+
+ case 'h': {
+ result.height = value;
+ break;
+ }
+
+ case 'x': {
+ result.density = value;
+ break;
+ }
+
+ // No default
}
}
@@ -127,12 +150,25 @@ exports.stringify = (array, {strict = false} = {}) => {
for (const descriptorKey of descriptorKeys) {
const value = element[descriptorKey];
let postfix;
- if (descriptorKey === 'width') {
- postfix = 'w';
- } else if (descriptorKey === 'height') {
- postfix = 'h';
- } else if (descriptorKey === 'density') {
- postfix = 'x';
+ switch (descriptorKey) {
+ case 'width': {
+ postfix = 'w';
+
+ break;
+ }
+
+ case 'height': {
+ postfix = 'h';
+
+ break;
+ }
+
+ case 'density': {
+ postfix = 'x';
+
+ break;
+ }
+ // No default
}
const descriptor = `${value}${postfix}`;
diff --git a/index.test-d.ts b/index.test-d.ts
index 7ccd92d..c105ee4 100644
--- a/index.test-d.ts
+++ b/index.test-d.ts
@@ -1,12 +1,10 @@
import {expectType, expectError} from 'tsd';
-import srcset = require('.');
+import srcset = require('./index.js');
const parsed = srcset.parse('banner-HD.jpg 2x, banner-phone.jpg 100w');
expectType(parsed);
-parsed.push({url: 'banner-phone-HD.jpg'});
-parsed.push({url: 'banner-phone-HD.jpg', width: 100});
-parsed.push({url: 'banner-phone-HD.jpg', density: 2});
+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(srcset.stringify(parsed));
diff --git a/package.json b/package.json
index bf94be8..51a78ec 100644
--- a/package.json
+++ b/package.json
@@ -11,7 +11,7 @@
"url": "https://sindresorhus.com"
},
"engines": {
- "node": ">=10"
+ "node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
@@ -36,6 +36,6 @@
"devDependencies": {
"ava": "^2.4.0",
"tsd": "^0.13.1",
- "xo": "^0.32.1"
+ "xo": "^0.39.0"
}
}
diff --git a/readme.md b/readme.md
index 8608ac7..a419105 100644
--- a/readme.md
+++ b/readme.md
@@ -60,13 +60,13 @@ banner-HD.jpg 2x, banner-phone.jpg 100w, banner-super-HD.jpg 3x
Parse the HTML `` [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`, `height`, and `density`.
+Accepts a “srcset” string and returns an array of objects with the possible properties: `url` (always), `width`, `height`, and `density`.
#### string
Type: `string`
-A srcset string
+A “srcset” string.
#### options
@@ -74,21 +74,20 @@ Type: `object`
##### strict
-Type: `boolean`
-
+Type: `boolean`\
Default: `false`
-Enable or disable validation of the srcset string. When enabled, an invalid srcset string will cause an error to be thrown. When disabled, a best effort will be made to parse the string, potentially resulting in invalid or nonsensical output.
+When enabled, an invalid “srcset” string will cause an error to be thrown. When disabled, a best effort will be made to parse the string, potentially resulting in invalid or nonsensical output.
### .stringify(SrcSetDefinitions, options?)
-Stringify `SrcSetDefinition`s. Accepts an array of `SrcSetDefinition` objects and returns a srcset string.
+Stringify `SrcSetDefinition`s. Accepts an array of `SrcSetDefinition` objects and returns a “srcset” string.
#### srcsetDescriptors
Type: `array`
-An array of `SrcSetDefinition` objects. Each object should have a `url` field and may have `width`, `height` or `density` fields. When `options.strict` is set to true, only `width` or `density` is permitted.
+An array of `SrcSetDefinition` objects. Each object should have a `url` field and may have `width`, `height` or `density` fields. When the `strict` option is `true`, only `width` or `density` is accepted.
#### options
diff --git a/test.js b/test.js
index 5023c02..3fa0fb5 100644
--- a/test.js
+++ b/test.js
@@ -1,5 +1,5 @@
import test from 'ava';
-import srcset from '.';
+import srcset from './index.js';
test('.parse() should parse srcset', t => {
const fixture = ' banner-HD.jpeg 2x, banner-phone.jpeg 100w, http://site.com/image.jpg?foo=bar,lorem 3x ,banner.jpeg ';