-
-
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 8, add TypeScript definition (#2)
- Loading branch information
1 parent
3f11b6a
commit aa4ad66
Showing
13 changed files
with
201 additions
and
157 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
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 +1 @@ | ||
* text=auto | ||
* text=auto eol=lf |
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 +1,2 @@ | ||
node_modules | ||
yarn.lock |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
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,6 +1,5 @@ | ||
sudo: false | ||
language: node_js | ||
node_js: | ||
- 'iojs' | ||
- '0.12' | ||
- '0.10' | ||
- '12' | ||
- '10' | ||
- '8' |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
declare namespace srcset { | ||
interface SrcSetDefinition { | ||
url: string; | ||
width?: number; | ||
height?: number; | ||
density?: number; | ||
} | ||
} | ||
|
||
declare const srcset: { | ||
/** | ||
Parse the HTML `<img>` [srcset](http://mobile.smashingmagazine.com/2013/08/21/webkit-implements-srcset-and-why-its-a-good-thing/) attribute. | ||
@param srcset - A srcset string. | ||
@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 } | ||
// ] | ||
``` | ||
*/ | ||
parse(srcset: string): srcset.SrcSetDefinition[]; | ||
|
||
/** | ||
Stringify `SrcSetDefinition`s. | ||
@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 }, | ||
{ url: 'banner-phone-HD.jpg', width: 100, density: 2 } | ||
]); | ||
console.log(stringified); | ||
// banner-HD.jpg 2x, banner-phone.jpg 100w, banner-phone-HD.jpg 100w 2x | ||
``` | ||
*/ | ||
stringify(srcSetDefinitions: srcset.SrcSetDefinition[]): string; | ||
}; | ||
|
||
export = srcset; |
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,63 +1,71 @@ | ||
'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); | ||
|
||
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 + '.'); | ||
} | ||
}); | ||
const value = element.substring(0, element.length - 1); | ||
const postfix = element[element.length - 1]; | ||
const integerValue = parseInt(value, 10); | ||
const floatValue = parseFloat(value); | ||
|
||
return ret; | ||
})); | ||
} | ||
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 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(', '); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {expectType, expectError} from 'tsd'; | ||
import srcset = require('.'); | ||
|
||
const parsed = srcset.parse('banner-HD.jpg 2x, banner-phone.jpg 100w'); | ||
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({})); | ||
|
||
expectType<string>(srcset.stringify(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 |
---|---|---|
@@ -1,21 +1,9 @@ | ||
The MIT License (MIT) | ||
MIT License | ||
|
||
Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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,41 +1,43 @@ | ||
{ | ||
"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": "[email protected]", | ||
"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": "1.0.0", | ||
"description": "Parse and stringify the HTML <img> srcset attribute", | ||
"license": "MIT", | ||
"repository": "sindresorhus/srcset", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "[email protected]", | ||
"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" | ||
} | ||
} |
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.