image-promise
Advanced tools
Comparing version 5.0.0-0 to 5.0.0-1
@@ -1,6 +0,8 @@ | ||
/*! npm.im/image-promise 5.0.0-0 */ | ||
/*! npm.im/image-promise 5.0.0-1 */ | ||
'use strict'; | ||
function load(image) { | ||
if (typeof image === 'string') { | ||
if (!image) { | ||
return Promise.reject(); | ||
} else if (typeof image === 'string') { | ||
// If image is a string, "convert" it to an <img> | ||
@@ -14,9 +16,18 @@ var src = image; | ||
return Promise.all([].map.call(image, load)); | ||
} else if (image.tagName !== 'IMG') { | ||
// If it's not an <img> tag, reject | ||
return Promise.reject(); | ||
} | ||
var promise = new Promise(function (resolve, reject) { | ||
function fullfill(e) { | ||
image.removeEventListener('load', fullfill); | ||
image.removeEventListener('error', fullfill); | ||
if (e.type === 'load') { | ||
if (image.naturalWidth) { // Truthy if loaded successfully | ||
resolve(image); | ||
} else if (image.complete) { // True if failed, at this point | ||
reject(image); | ||
} else { | ||
image.addEventListener('load', fullfill); | ||
image.addEventListener('error', fullfill); | ||
} | ||
function fullfill() { | ||
if (image.naturalWidth) { | ||
resolve(image); | ||
@@ -26,9 +37,5 @@ } else { | ||
} | ||
image.removeEventListener('load', fullfill); | ||
image.removeEventListener('error', fullfill); | ||
} | ||
if (image.complete) { | ||
resolve(image); | ||
} else { | ||
image.addEventListener('load', fullfill); | ||
image.addEventListener('error', fullfill); | ||
} | ||
}); | ||
@@ -35,0 +42,0 @@ promise.image = image; |
@@ -1,4 +0,6 @@ | ||
/*! npm.im/image-promise 5.0.0-0 */ | ||
/*! npm.im/image-promise 5.0.0-1 */ | ||
function load(image) { | ||
if (typeof image === 'string') { | ||
if (!image) { | ||
return Promise.reject(); | ||
} else if (typeof image === 'string') { | ||
// If image is a string, "convert" it to an <img> | ||
@@ -12,9 +14,18 @@ var src = image; | ||
return Promise.all([].map.call(image, load)); | ||
} else if (image.tagName !== 'IMG') { | ||
// If it's not an <img> tag, reject | ||
return Promise.reject(); | ||
} | ||
var promise = new Promise(function (resolve, reject) { | ||
function fullfill(e) { | ||
image.removeEventListener('load', fullfill); | ||
image.removeEventListener('error', fullfill); | ||
if (e.type === 'load') { | ||
if (image.naturalWidth) { // Truthy if loaded successfully | ||
resolve(image); | ||
} else if (image.complete) { // True if failed, at this point | ||
reject(image); | ||
} else { | ||
image.addEventListener('load', fullfill); | ||
image.addEventListener('error', fullfill); | ||
} | ||
function fullfill() { | ||
if (image.naturalWidth) { | ||
resolve(image); | ||
@@ -24,9 +35,5 @@ } else { | ||
} | ||
image.removeEventListener('load', fullfill); | ||
image.removeEventListener('error', fullfill); | ||
} | ||
if (image.complete) { | ||
resolve(image); | ||
} else { | ||
image.addEventListener('load', fullfill); | ||
image.addEventListener('error', fullfill); | ||
} | ||
}); | ||
@@ -33,0 +40,0 @@ promise.image = image; |
{ | ||
"name": "image-promise", | ||
"version": "5.0.0-0", | ||
"version": "5.0.0-1", | ||
"description": "Load an image and return a promise in the browser, in 0.3KB", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -1,40 +0,61 @@ | ||
# image-promise | ||
# image-promise [![gzipped size][badge-gzip]](#no-link) [![Travis build status][badge-travis]][link-travis] [![npm version][badge-version]][link-npm] [![npm downloads][badge-downloads]][link-npm] | ||
[badge-gzip]: https://badges.herokuapp.com/size/github/bfred-it/image-promise/master/dist/image-promise.min.js?gzip=true&label=gzipped%20size | ||
[badge-travis]: https://api.travis-ci.org/bfred-it/image-promise.svg | ||
[badge-version]: https://img.shields.io/npm/v/image-promise.svg | ||
[badge-downloads]: https://img.shields.io/npm/dt/image-promise.svg | ||
[link-travis]: https://travis-ci.org/bfred-it/image-promise | ||
[link-npm]: https://www.npmjs.com/package/image-promise | ||
> Load an image and return a promise in the browser, in 0.3KB, no dependencies | ||
[![gzipped size](https://badges.herokuapp.com/size/github/bfred-it/image-promise/master/dist/image-promise.min.js?gzip=true&label=gzipped%20size)](#readme) | ||
[![Travis build status](https://api.travis-ci.org/bfred-it/image-promise.svg?branch=master)](https://travis-ci.org/bfred-it/image-promise) | ||
[![npm version](https://img.shields.io/npm/v/image-promise.svg)](https://www.npmjs.com/package/image-promise) | ||
It can be used in two ways: | ||
- given a URL, generate an `<img>` and wait for it to load: | ||
```js | ||
loadImage('img.jpg').then(/*it's loaded!*/) | ||
``` | ||
- given an `<img>`, wait for it to load: | ||
```js | ||
const img = document.querySelector('img.my-image'); | ||
loadImage(img).then(/*it's loaded!*/) | ||
``` | ||
## Install | ||
Pick your favorite: | ||
```html | ||
<script src="dist/image-promise.min.js"></script> | ||
``` | ||
```sh | ||
npm install --save image-promise | ||
``` | ||
```js | ||
import loadImage from 'image-promise'; | ||
var loadImage = require('image-promise'); | ||
``` | ||
If you don't use node/babel, include this: | ||
```html | ||
<script src="dist/image-promise.min.js"></script> | ||
```js | ||
import loadImage from 'image-promise'; | ||
``` | ||
It uses the ES2015 `window.Promise`, so if you need to support [older browsers](http://caniuse.com/#feat=promises) (IE<=11) you need a polyfill. | ||
## Usage | ||
```js | ||
loadImage( '/cat.jpg' ); | ||
loadImage( '/cat.jpg' ); // one string | ||
loadImage( document.querySelector('img') ); // one element | ||
// Returns a Promise that resolves with an image (`<img>`) | ||
loadImage( ['/cat.jpg', '/dog.png'] ); | ||
loadImage( ['/cat.jpg', '/dog.png'] ); // Array of strings | ||
loadImage( document.querySelectorAll('img') ); // any Array-like list of elements | ||
// Returns a Promise that resolves with **an array of images.** | ||
loadImage( document.querySelector('img') ); // one element | ||
loadImage( document.querySelectorAll('img') ); // any Array-like list of elements | ||
// The promises resolve when the provided <img>s are loaded | ||
``` | ||
The promises resolve when the provided `<img>`s are loaded. | ||
## Examples | ||
@@ -57,3 +78,3 @@ | ||
```js | ||
loadImage(['/cat.jpg', '/dog.png']) // array of URLs | ||
loadImage(['/cat.jpg', '/dog.png']) | ||
.then(function (allImgs) { | ||
@@ -71,3 +92,3 @@ console.log(allImgs.length, 'images loaded!', allImgs); | ||
None! But you need to polyfill `window.Promise` in IE<=11 | ||
None! But you need to polyfill `window.Promise` in IE11 and lower. | ||
@@ -74,0 +95,0 @@ ## License |
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
6172
81
96