Comparing version 1.0.0 to 2.0.0
104
index.js
'use strict'; | ||
var numberIsNan = require('number-is-nan'); | ||
var arrayUniq = require('array-uniq'); | ||
var reInt = /^\d+$/; | ||
const arrayUniq = require('array-uniq'); | ||
function deepUnique(arr) { | ||
return arr.sort().filter(function (el, i) { | ||
return JSON.stringify(el) !== JSON.stringify(arr[i - 1]); | ||
const integerRegex = /^\d+$/; | ||
function deepUnique(array) { | ||
return array.sort().filter((element, index) => { | ||
return JSON.stringify(element) !== JSON.stringify(array[index - 1]); | ||
}); | ||
} | ||
exports.parse = function (str) { | ||
return deepUnique(str.split(',').map(function (el) { | ||
var ret = {}; | ||
exports.parse = string => { | ||
return deepUnique( | ||
string.split(',').map(part => { | ||
const result = {}; | ||
el.trim().split(/\s+/).forEach(function (el, i) { | ||
if (i === 0) { | ||
return ret.url = el; | ||
} | ||
part | ||
.trim() | ||
.split(/\s+/) | ||
.forEach((element, index) => { | ||
if (index === 0) { | ||
result.url = element; | ||
return; | ||
} | ||
var value = el.substring(0, el.length - 1); | ||
var postfix = el[el.length - 1]; | ||
var intVal = parseInt(value, 10); | ||
var floatVal = parseFloat(value); | ||
const value = element.slice(0, element.length - 1); | ||
const postfix = element[element.length - 1]; | ||
const integerValue = parseInt(value, 10); | ||
const floatValue = parseFloat(value); | ||
if (postfix === 'w' && reInt.test(value)) { | ||
ret.width = intVal; | ||
} else if (postfix === 'h' && reInt.test(value)) { | ||
ret.height = intVal; | ||
} else if (postfix === 'x' && !numberIsNan(floatVal)) { | ||
ret.density = floatVal; | ||
} else { | ||
throw new Error('Invalid srcset descriptor: ' + el + '.'); | ||
} | ||
}); | ||
if (postfix === 'w' && integerRegex.test(value)) { | ||
result.width = integerValue; | ||
} else if (postfix === 'h' && integerRegex.test(value)) { | ||
result.height = integerValue; | ||
} else if (postfix === 'x' && !Number.isNaN(floatValue)) { | ||
result.density = floatValue; | ||
} else { | ||
throw new Error(`Invalid srcset descriptor: ${element}`); | ||
} | ||
}); | ||
return ret; | ||
})); | ||
} | ||
return result; | ||
}) | ||
); | ||
}; | ||
exports.stringify = function (arr) { | ||
return arrayUniq(arr.map(function (el) { | ||
if (!el.url) { | ||
throw new Error('URL is required.'); | ||
} | ||
exports.stringify = array => { | ||
return arrayUniq( | ||
array.map(element => { | ||
if (!element.url) { | ||
throw new Error('URL is required'); | ||
} | ||
var ret = [el.url]; | ||
const result = [element.url]; | ||
if (el.width) { | ||
ret.push(el.width + 'w'); | ||
} | ||
if (element.width) { | ||
result.push(`${element.width}w`); | ||
} | ||
if (el.height) { | ||
ret.push(el.height + 'h'); | ||
} | ||
if (element.height) { | ||
result.push(`${element.height}h`); | ||
} | ||
if (el.density) { | ||
ret.push(el.density + 'x'); | ||
} | ||
if (element.density) { | ||
result.push(`${element.density}x`); | ||
} | ||
return ret.join(' '); | ||
})).join(', '); | ||
} | ||
return result.join(' '); | ||
}) | ||
).join(', '); | ||
}; |
{ | ||
"name": "srcset", | ||
"version": "1.0.0", | ||
"description": "Parse and stringify the HTML <img> srcset attribute", | ||
"keywords": [ | ||
"html", | ||
"attribute", | ||
"image", | ||
"img", | ||
"src", | ||
"parse", | ||
"stringify", | ||
"srcset", | ||
"responsive", | ||
"picture", | ||
"element" | ||
], | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"repository": "sindresorhus/srcset", | ||
"scripts": { | ||
"test": "mocha" | ||
}, | ||
"dependencies": { | ||
"array-uniq": "^1.0.2", | ||
"number-is-nan": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*" | ||
}, | ||
"engines": { | ||
"node": ">=0.10.0" | ||
}, | ||
"license": "MIT", | ||
"files": [ | ||
"index.js" | ||
] | ||
"name": "srcset", | ||
"version": "2.0.0", | ||
"description": "Parse and stringify the HTML <img> srcset attribute", | ||
"license": "MIT", | ||
"repository": "sindresorhus/srcset", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava && tsd" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"keywords": [ | ||
"html", | ||
"attribute", | ||
"image", | ||
"img", | ||
"src", | ||
"parse", | ||
"stringify", | ||
"srcset", | ||
"responsive", | ||
"picture", | ||
"element" | ||
], | ||
"dependencies": { | ||
"array-uniq": "^2.1.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"tsd": "^0.7.2", | ||
"xo": "^0.24.0" | ||
} | ||
} |
# srcset [![Build Status](https://travis-ci.org/sindresorhus/srcset.svg?branch=master)](https://travis-ci.org/sindresorhus/srcset) | ||
> Parse and stringify the HTML `<img>` [srcset](http://mobile.smashingmagazine.com/2013/08/21/webkit-implements-srcset-and-why-its-a-good-thing/) attribute. | ||
> Parse and stringify the HTML `<img>` [srcset](https://www.smashingmagazine.com/2013/08/webkit-implements-srcset-and-why-its-a-good-thing/) attribute. | ||
@@ -11,3 +11,3 @@ Useful if you're creating a polyfill, build-tool, etc. | ||
``` | ||
$ npm install --save srcset | ||
$ npm install srcset | ||
``` | ||
@@ -29,16 +29,26 @@ | ||
```js | ||
var srcset = require('srcset'); | ||
const srcset = require('srcset'); | ||
var parsed = srcset.parse('banner-HD.jpg 2x, banner-phone.jpg 100w'); | ||
const parsed = srcset.parse('banner-HD.jpg 2x, banner-phone.jpg 100w'); | ||
console.log(parsed); | ||
/* | ||
[ | ||
{ url: 'banner-HD.jpg', density: 2 }, | ||
{ url: 'banner-phone.jpg', width: 100 } | ||
{ | ||
url: 'banner-HD.jpg', | ||
density: 2 | ||
}, | ||
{ | ||
url: 'banner-phone.jpg', | ||
width: 100 | ||
} | ||
] | ||
*/ | ||
parsed.push({ url: 'banner-phone-HD.jpg', width: 100, density: 2 }); | ||
parsed.push({ | ||
url: 'banner-phone-HD.jpg', | ||
width: 100, | ||
density: 2 | ||
}); | ||
var stringified = srcset.stringify(parsed); | ||
const stringified = srcset.stringify(parsed); | ||
console.log(stringified); | ||
@@ -64,2 +74,2 @@ /* | ||
MIT © [Sindre Sorhus](http://sindresorhus.com) | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
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
5982
1
5
115
73
3
+ Addedarray-uniq@2.1.0(transitive)
- Removednumber-is-nan@^1.0.0
- Removedarray-uniq@1.0.3(transitive)
- Removednumber-is-nan@1.0.1(transitive)
Updatedarray-uniq@^2.1.0