Skip to content

Commit

Permalink
Match the latest spec (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
willianveiga authored Jul 12, 2020
1 parent 5a0de0c commit 898b749
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
1 change: 0 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ declare namespace srcset {
interface SrcSetDefinition {
url: string;
width?: number;
height?: number;
density?: number;
}
}
Expand Down
22 changes: 14 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const integerRegex = /^\d+$/;
const integerRegex = /^-?\d+$/;

function deepUnique(array) {
return array.sort().filter((element, index) => {
Expand All @@ -10,7 +10,7 @@ function deepUnique(array) {

exports.parse = string => {
return deepUnique(
string.split(',').map(part => {
string.split(/,\s+/).map(part => {
const result = {};

part
Expand All @@ -28,14 +28,24 @@ exports.parse = string => {
const floatValue = 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;
} else {
throw new Error(`Invalid srcset descriptor: ${element}`);
}

if (result.width && result.density) {
throw new Error('Image candidate string cannot have both width descriptor and pixel density descriptor');
}
});

return result;
Expand All @@ -56,10 +66,6 @@ exports.stringify = array => {
result.push(`${element.width}w`);
}

if (element.height) {
result.push(`${element.height}h`);
}

if (element.density) {
result.push(`${element.density}x`);
}
Expand Down
1 change: 0 additions & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ expectType<srcset.SrcSetDefinition[]>(parsed);

parsed.push({url: 'banner-phone-HD.jpg'});
parsed.push({url: 'banner-phone-HD.jpg', width: 100});
parsed.push({url: 'banner-phone-HD.jpg', height: 100});
parsed.push({url: 'banner-phone-HD.jpg', density: 2});
expectError(parsed.push({}));

Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ banner-HD.jpg 2x, banner-phone.jpg 100w, banner-phone-HD.jpg 100w 2x

### .parse()

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.


---
Expand Down
15 changes: 10 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,30 @@ import test from 'ava';
import srcset from '.';

test('.parse() should parse srcset', t => {
const fixture = ' banner-HD.jpeg 2x, banner-HD.jpeg 2x, banner-HD.jpeg 2x, banner-phone.jpeg 100w, banner-phone-HD.jpeg 100w 2x ';
const fixture = ' banner-HD.jpeg 2x, banner-HD.jpeg 2x, banner-HD.jpeg 2x, banner-phone.jpeg 100w, http://site.com/image.jpg?foo=bar,lorem 1x ';

t.deepEqual(srcset.parse(fixture), [
{url: 'banner-HD.jpeg', density: 2},
{url: 'banner-phone.jpeg', width: 100},
{url: 'banner-phone-HD.jpeg', width: 100, density: 2}
{url: 'http://site.com/image.jpg?foo=bar,lorem', density: 1}
]);
});

test('.parse() should not parse srcset', t => {
t.throws(() => srcset.parse('banner-phone-HD.jpeg 100w 2x'));
t.throws(() => srcset.parse('banner-phone-HD.jpeg -100w'));
t.throws(() => srcset.parse('banner-phone-HD.jpeg -2x'));
});

test('.stringify() should stringify srcset', t => {
const fixture = [
{url: 'banner-HD.jpeg', density: 2},
{url: 'banner-HD.jpeg', density: 2},
{url: 'banner-phone.jpeg', width: 100},
{url: 'banner-phone-HD.jpeg', width: 100, density: 2}
{url: 'banner-phone.jpeg', width: 100}
];

t.deepEqual(
srcset.stringify(fixture),
'banner-HD.jpeg 2x, banner-phone.jpeg 100w, banner-phone-HD.jpeg 100w 2x'
'banner-HD.jpeg 2x, banner-phone.jpeg 100w'
);
});

0 comments on commit 898b749

Please sign in to comment.