Comparing version 2.0.1 to 3.0.0
@@ -5,3 +5,2 @@ declare namespace srcset { | ||
width?: number; | ||
height?: number; | ||
density?: number; | ||
@@ -34,3 +33,3 @@ } | ||
*/ | ||
parse(srcset: string): srcset.SrcSetDefinition[]; | ||
parse: (srcset: string) => srcset.SrcSetDefinition[]; | ||
@@ -66,5 +65,5 @@ /** | ||
*/ | ||
stringify(srcSetDefinitions: srcset.SrcSetDefinition[]): string; | ||
stringify: (srcSetDefinitions: srcset.SrcSetDefinition[]) => string; | ||
}; | ||
export = srcset; |
28
index.js
'use strict'; | ||
const integerRegex = /^\d+$/; | ||
const integerRegex = /^-?\d+$/; | ||
@@ -13,3 +13,3 @@ function deepUnique(array) { | ||
return deepUnique( | ||
string.split(',').map(part => { | ||
string.split(/,\s+/).map(part => { | ||
const result = {}; | ||
@@ -26,12 +26,18 @@ | ||
const value = element.slice(0, element.length - 1); | ||
const value = element.slice(0, -1); | ||
const postfix = element[element.length - 1]; | ||
const integerValue = parseInt(value, 10); | ||
const floatValue = parseFloat(value); | ||
const integerValue = Number.parseInt(value, 10); | ||
const floatValue = Number.parseFloat(value); | ||
if (postfix === 'w' && integerRegex.test(value)) { | ||
if (integerValue <= 0) { | ||
throw new Error('Width descriptor must be greater than zero'); | ||
} | ||
result.width = integerValue; | ||
} else if (postfix === 'h' && integerRegex.test(value)) { | ||
result.height = integerValue; | ||
} else if (postfix === 'x' && !Number.isNaN(floatValue)) { | ||
if (floatValue <= 0) { | ||
throw new Error('Pixel density descriptor must be greater than zero'); | ||
} | ||
result.density = floatValue; | ||
@@ -41,2 +47,6 @@ } else { | ||
} | ||
if (result.width && result.density) { | ||
throw new Error('Image candidate string cannot have both width descriptor and pixel density descriptor'); | ||
} | ||
}); | ||
@@ -62,6 +72,2 @@ | ||
if (element.height) { | ||
result.push(`${element.height}h`); | ||
} | ||
if (element.density) { | ||
@@ -68,0 +74,0 @@ result.push(`${element.density}x`); |
{ | ||
"name": "srcset", | ||
"version": "2.0.1", | ||
"description": "Parse and stringify the HTML <img> srcset attribute", | ||
"version": "3.0.0", | ||
"description": "Parse and stringify the HTML `<img>` srcset attribute", | ||
"license": "MIT", | ||
"repository": "sindresorhus/srcset", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=10" | ||
}, | ||
@@ -36,6 +37,6 @@ "scripts": { | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"tsd": "^0.7.2", | ||
"xo": "^0.24.0" | ||
"ava": "^2.4.0", | ||
"tsd": "^0.13.1", | ||
"xo": "^0.32.1" | ||
} | ||
} |
@@ -1,8 +0,7 @@ | ||
# srcset [![Build Status](https://travis-ci.org/sindresorhus/srcset.svg?branch=master)](https://travis-ci.org/sindresorhus/srcset) | ||
# srcset [![Build Status](https://travis-ci.com/sindresorhus/srcset.svg?branch=master)](https://travis-ci.com/github/sindresorhus/srcset) | ||
> Parse and stringify the HTML `<img>` [srcset](https://www.smashingmagazine.com/2013/08/webkit-implements-srcset-and-why-its-a-good-thing/) attribute. | ||
Useful if you're creating a polyfill, build-tool, etc. | ||
Can be useful if you're creating a build-tool. | ||
## Install | ||
@@ -14,3 +13,2 @@ | ||
## Usage | ||
@@ -59,3 +57,2 @@ | ||
## API | ||
@@ -65,9 +62,8 @@ | ||
Accepts a srcset string and returns an array of objects with the possible properties: `url` (always), `width`, `height`, `density`. | ||
Accepts a srcset string and returns an array of objects with the possible properties: `url` (always), `width`, `density`. | ||
### .stringify() | ||
Accepts an array of objects with the possible properties: `url` (required), `width`, `height`, `density` and returns a srcset string. | ||
Accepts an array of objects with the possible properties: `url` (required), `width`, `density` and returns a srcset string. | ||
--- | ||
@@ -74,0 +70,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6527
117
77