Skip to content

Commit a8a4d23

Browse files
first commit
0 parents  commit a8a4d23

File tree

9 files changed

+354
-0
lines changed

9 files changed

+354
-0
lines changed

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
extends: "@gianlucaguarini/eslint-config"
2+
env:
3+
mocha: true

.gitignore

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
11+
# Directory for instrumented libs generated by jscoverage/JSCover
12+
lib-cov
13+
14+
# Coverage directory used by tools like istanbul
15+
coverage
16+
17+
# nyc test coverage
18+
.nyc_output
19+
20+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
21+
.grunt
22+
23+
# node-waf configuration
24+
.lock-wscript
25+
26+
# Compiled binary addons (http://nodejs.org/api/addons.html)
27+
build/Release
28+
29+
# Dependency directories
30+
node_modules
31+
jspm_packages
32+
33+
# Optional npm cache directory
34+
.npm
35+
36+
# Optional REPL history
37+
.node_repl_history
38+
39+
# generated files
40+
index.js

.travis.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
language: node_js
2+
node_js:
3+
- "6.*"
4+
5+
branches:
6+
only:
7+
- master
8+
9+
before_install:
10+
11+
12+
- npm i @gianlucaguarini/[email protected]
13+
14+
15+
before_script:
16+
- npm run build
17+
18+
notifications:
19+
email: false
20+
21+
sudo: false

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Gianluca Guarini
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# bianco-images-loader
2+
3+
[![Build Status][travis-image]][travis-url]
4+
5+
[![NPM version][npm-version-image]][npm-url]
6+
[![NPM downloads][npm-downloads-image]][npm-url]
7+
[![MIT License][license-image]][license-url]
8+
9+
10+
11+
## Usage
12+
13+
14+
#### Load a single image
15+
```js
16+
import { loadImage } from 'bianco-images-loader'
17+
18+
loadImage('path/to/the/image.jpg')
19+
.then(img => {
20+
document.body.appendChild(img)
21+
})
22+
.catch(function(error) {
23+
// there was an error loading the image
24+
})
25+
26+
```
27+
28+
Or also DOM nodes:
29+
30+
```js
31+
import $ from 'bianco-query'
32+
import { loadImage } from 'bianco-images-loader'
33+
34+
loadImage($('img.cool'))
35+
.then(img => img.classList.add('loaded'))
36+
```
37+
38+
#### Load a multiple images
39+
40+
```js
41+
import { loadImages } from 'bianco-images-loader'
42+
43+
loadImages(['path/to/the/image1.jpg', 'path/to/the/image2.jpg'])
44+
.then(img => document.body.appendChild(img))
45+
.catch(error => {
46+
// there was an error loading one of images
47+
})
48+
49+
```
50+
51+
Or also...
52+
53+
```js
54+
import $ from 'bianco-query'
55+
import { loadImages } from 'bianco-images-loader'
56+
57+
loadImages($('img', '.main-content'))
58+
.then(img => img.classList.add('loaded'))
59+
.catch(error => {
60+
// there was an error loading one of images
61+
})
62+
63+
```
64+
65+
#### Load sequentially images using a generator
66+
67+
```js
68+
import { loadImagesGen } from 'bianco-images-loader'
69+
70+
const loader = loadImagesGen([
71+
'path/to/the/image1.jpg',
72+
'path/to/the/image2.jpg',
73+
'path/to/the/image3.jpg'
74+
])
75+
76+
// load the first image
77+
loader.next().value.then(img => {
78+
// do something with the image
79+
80+
// do something with the remaining images to load
81+
for (let promise of loader) {
82+
promise.then(i => {
83+
// do whathever you want here
84+
})
85+
}
86+
})
87+
```
88+
89+
## API
90+
91+
- `loadImage(String|Image)` load an image returning a promise
92+
- `loadImages(Array|NodeList)` load in parallel multiple images returning a promise
93+
- `loadImagesGen(Array|NodeList)` load multiple images sequentially returning a ES6 generator
94+
95+
[travis-image]:https://img.shields.io/travis/biancojs/images-loader.svg?style=flat-square
96+
[travis-url]:https://travis-ci.org/biancojs/images-loader
97+
98+
[license-image]:http://img.shields.io/badge/license-MIT-000000.svg?style=flat-square
99+
[license-url]:LICENSE.txt
100+
101+
[npm-version-image]:http://img.shields.io/npm/v/bianco-images-loader.svg?style=flat-square
102+
[npm-downloads-image]:http://img.shields.io/npm/dm/bianco-images-loader.svg?style=flat-square
103+
[npm-url]:https://npmjs.org/package/bianco-images-loader

index.next.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import domToArray from 'bianco.dom-to-array'
2+
3+
/**
4+
* Preload any image
5+
* @param { String|HTMLElement } img - Path to the image or image object
6+
* @returns { Promise } a promise that will be resolved when the image will be completely loaded
7+
*/
8+
export function loadImage(img) {
9+
const isUrl = typeof img === 'string'
10+
let i = isUrl ? document.createElement('img') : img
11+
12+
// this image was already loaded
13+
if (!isUrl && i.complete) return Promise.resolve(i)
14+
15+
// the image reference will set to null
16+
// to avoid memory leaks
17+
return new Promise((resolve, reject) => {
18+
i.onload = _ => resolve(i)
19+
i.onerror = i.onabort = reject
20+
if (isUrl) i.src = img
21+
})
22+
}
23+
24+
/**
25+
* Load in parallel a collection of images
26+
* @param { Array|NodeList } imgs - array of strings or <img> HTML elements
27+
* @returns { Promise } a promise that will be resolved when all the images will be loaded
28+
*/
29+
export function loadImages(imgs) {
30+
return Promise.all(domToArray(imgs).map(loadImage))
31+
}
32+
33+
/**
34+
* Load sequentially a collection of images
35+
* @param { Array|NodeList } imgs - array of strings or <img> HTML elements
36+
* @yields { Promise } a promise that will be resolved when the image will be completely loaded
37+
*/
38+
export function * loadImagesGen (imgs) {
39+
imgs = domToArray(imgs)
40+
for (let img of imgs) {
41+
yield loadImage(img)
42+
}
43+
}
44+
45+
export default {
46+
loadImage,
47+
loadImages,
48+
loadImagesGen
49+
}

package.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "bianco.images-loader",
3+
"version": "0.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"jsnext:main": "index.next.js",
7+
"scripts": {
8+
"prepublish": "npm run build && npm test",
9+
"lint": "eslint index.next.js test.js rollup.config.js",
10+
"build": "rollup -c",
11+
"test": "npm run lint && mocha test.js"
12+
},
13+
"files": [
14+
"index.js",
15+
"index.next.js"
16+
],
17+
"repository": {
18+
"type": "git",
19+
"url": "git+https://github.com/biancojs/images-loader.git"
20+
},
21+
"keywords": [
22+
"es6",
23+
"es2015",
24+
"images loader",
25+
"img",
26+
"loader",
27+
"generators"
28+
],
29+
"author": "Gianluca Guarini <[email protected]> (http://gianlucaguarini.com)",
30+
"license": "MIT",
31+
"bugs": {
32+
"url": "https://github.com/biancojs/images-loader/issues"
33+
},
34+
"homepage": "https://github.com/biancojs/images-loader#readme",
35+
"devDependencies": {
36+
"jsdom": "9.5.0",
37+
"jsdom-global": "2.1.0"
38+
},
39+
"dependencies": {
40+
"bianco.dom-to-array": "^0.0.4"
41+
}
42+
}

rollup.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default {
2+
entry: 'index.next.js',
3+
targets: [
4+
{
5+
dest: 'index.js',
6+
format: 'cjs'
7+
}
8+
]
9+
}

test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
require('jsdom-global')()
2+
const assert = require('assert')
3+
const jsdom = require('jsdom')
4+
const body = document.body
5+
const { loadImage, loadImages, loadImagesGen } = require('./')
6+
const pathGen = pathGenerator()
7+
8+
function * pathGenerator() {
9+
let i = 1
10+
while (i) {
11+
yield `data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7?${ i++ }`
12+
}
13+
}
14+
15+
describe('Bianco images-loader', function() {
16+
beforeEach(function () {
17+
var div = document.createElement('div')
18+
div.innerHTML = `
19+
<img src=''>
20+
`
21+
body.appendChild(div)
22+
})
23+
24+
it('It can load images in the DOM', function(done) {
25+
const img = document.querySelector('img')
26+
loadImage(img).then(function(i) {
27+
assert.equal(typeof i.src, 'string')
28+
done()
29+
})
30+
img.src = pathGen.next().value
31+
})
32+
33+
// this test does not work in jsdom somehow
34+
/* it('It can throw properly the errors', function(done) {
35+
const img = document.querySelector('img')
36+
loadImage(img).catch(function(e) {
37+
assert.equal(typeof e, Error)
38+
done()
39+
})
40+
img.src = pathGen.next().value
41+
})*/
42+
43+
it('It can load arrays of images urls', function(done) {
44+
loadImages([pathGen.next().value, pathGen.next().value]).then(function(imgs) {
45+
assert.equal(imgs.length, 2)
46+
done()
47+
})
48+
})
49+
50+
it('It can load images sequentially with a generator', function(done) {
51+
const gen = loadImagesGen([pathGen.next().value, pathGen.next().value])
52+
gen.next().value.then(function (i) {
53+
assert.equal(typeof i.src, 'string')
54+
done()
55+
})
56+
})
57+
58+
it('It can loop generator sequences', function(done) {
59+
const gen = loadImagesGen([pathGen.next().value, pathGen.next().value])
60+
const promises = []
61+
for (let p of gen) {
62+
promises.push(p)
63+
}
64+
Promise.all(promises).then(_ => done())
65+
})
66+
})

0 commit comments

Comments
 (0)