Skip to content

Commit 9aa4b5f

Browse files
committed
Use JavaScript Standard Style.
1 parent 2825835 commit 9aa4b5f

File tree

5 files changed

+438
-459
lines changed

5 files changed

+438
-459
lines changed

.jshintignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

js/canvas-to-blob.js

Lines changed: 93 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -14,96 +14,98 @@
1414

1515
/*global window, atob, Blob, ArrayBuffer, Uint8Array, define, module */
1616

17-
(function (window) {
18-
'use strict';
19-
var CanvasPrototype = window.HTMLCanvasElement &&
20-
window.HTMLCanvasElement.prototype,
21-
hasBlobConstructor = window.Blob && (function () {
22-
try {
23-
return Boolean(new Blob());
24-
} catch (e) {
25-
return false;
26-
}
27-
}()),
28-
hasArrayBufferViewSupport = hasBlobConstructor && window.Uint8Array &&
29-
(function () {
30-
try {
31-
return new Blob([new Uint8Array(100)]).size === 100;
32-
} catch (e) {
33-
return false;
34-
}
35-
}()),
36-
BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder ||
37-
window.MozBlobBuilder || window.MSBlobBuilder,
38-
dataURIPattern = /^data:((.*?)(;charset=.*?)?)(;base64)?,/,
39-
dataURLtoBlob = (hasBlobConstructor || BlobBuilder) && window.atob &&
40-
window.ArrayBuffer && window.Uint8Array && function (dataURI) {
41-
var matches,
42-
mediaType,
43-
isBase64,
44-
dataString,
45-
byteString,
46-
arrayBuffer,
47-
intArray,
48-
i,
49-
bb;
50-
// Parse the dataURI components as per RFC 2397
51-
matches = dataURI.match(dataURIPattern);
52-
if (!matches) {
53-
throw new Error('invalid data URI');
54-
}
55-
// Default to text/plain;charset=US-ASCII
56-
mediaType = matches[2] ?
57-
matches[1] :
58-
'text/plain' + (matches[3] || ';charset=US-ASCII');
59-
isBase64 = !!matches[4];
60-
dataString = dataURI.slice(matches[0].length);
61-
if (isBase64) {
62-
// Convert base64 to raw binary data held in a string:
63-
byteString = atob(dataString);
64-
} else {
65-
// Convert base64/URLEncoded data component to raw binary:
66-
byteString = decodeURIComponent(dataString);
67-
}
68-
// Write the bytes of the string to an ArrayBuffer:
69-
arrayBuffer = new ArrayBuffer(byteString.length);
70-
intArray = new Uint8Array(arrayBuffer);
71-
for (i = 0; i < byteString.length; i += 1) {
72-
intArray[i] = byteString.charCodeAt(i);
73-
}
74-
// Write the ArrayBuffer (or ArrayBufferView) to a blob:
75-
if (hasBlobConstructor) {
76-
return new Blob(
77-
[hasArrayBufferViewSupport ? intArray : arrayBuffer],
78-
{type: mediaType}
79-
);
80-
}
81-
bb = new BlobBuilder();
82-
bb.append(arrayBuffer);
83-
return bb.getBlob(mediaType);
84-
};
85-
if (window.HTMLCanvasElement && !CanvasPrototype.toBlob) {
86-
if (CanvasPrototype.mozGetAsFile) {
87-
CanvasPrototype.toBlob = function (callback, type, quality) {
88-
if (quality && CanvasPrototype.toDataURL && dataURLtoBlob) {
89-
callback(dataURLtoBlob(this.toDataURL(type, quality)));
90-
} else {
91-
callback(this.mozGetAsFile('blob', type));
92-
}
93-
};
94-
} else if (CanvasPrototype.toDataURL && dataURLtoBlob) {
95-
CanvasPrototype.toBlob = function (callback, type, quality) {
96-
callback(dataURLtoBlob(this.toDataURL(type, quality)));
97-
};
98-
}
17+
;(function (window) {
18+
'use strict'
19+
20+
var CanvasPrototype = window.HTMLCanvasElement &&
21+
window.HTMLCanvasElement.prototype
22+
var hasBlobConstructor = window.Blob && (function () {
23+
try {
24+
return Boolean(new Blob())
25+
} catch (e) {
26+
return false
9927
}
100-
if (typeof define === 'function' && define.amd) {
101-
define(function () {
102-
return dataURLtoBlob;
103-
});
104-
} else if (typeof module === 'object' && module.exports) {
105-
module.exports = dataURLtoBlob;
106-
} else {
107-
window.dataURLtoBlob = dataURLtoBlob;
28+
}())
29+
var hasArrayBufferViewSupport = hasBlobConstructor && window.Uint8Array &&
30+
(function () {
31+
try {
32+
return new Blob([new Uint8Array(100)]).size === 100
33+
} catch (e) {
34+
return false
35+
}
36+
}())
37+
var BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder ||
38+
window.MozBlobBuilder || window.MSBlobBuilder
39+
var dataURIPattern = /^data:((.*?)(;charset=.*?)?)(;base64)?,/
40+
var dataURLtoBlob = (hasBlobConstructor || BlobBuilder) && window.atob &&
41+
window.ArrayBuffer && window.Uint8Array &&
42+
function (dataURI) {
43+
var matches,
44+
mediaType,
45+
isBase64,
46+
dataString,
47+
byteString,
48+
arrayBuffer,
49+
intArray,
50+
i,
51+
bb
52+
// Parse the dataURI components as per RFC 2397
53+
matches = dataURI.match(dataURIPattern)
54+
if (!matches) {
55+
throw new Error('invalid data URI')
56+
}
57+
// Default to text/plain;charset=US-ASCII
58+
mediaType = matches[2]
59+
? matches[1]
60+
: 'text/plain' + (matches[3] || ';charset=US-ASCII')
61+
isBase64 = !!matches[4]
62+
dataString = dataURI.slice(matches[0].length)
63+
if (isBase64) {
64+
// Convert base64 to raw binary data held in a string:
65+
byteString = atob(dataString)
66+
} else {
67+
// Convert base64/URLEncoded data component to raw binary:
68+
byteString = decodeURIComponent(dataString)
69+
}
70+
// Write the bytes of the string to an ArrayBuffer:
71+
arrayBuffer = new ArrayBuffer(byteString.length)
72+
intArray = new Uint8Array(arrayBuffer)
73+
for (i = 0; i < byteString.length; i += 1) {
74+
intArray[i] = byteString.charCodeAt(i)
75+
}
76+
// Write the ArrayBuffer (or ArrayBufferView) to a blob:
77+
if (hasBlobConstructor) {
78+
return new Blob(
79+
[hasArrayBufferViewSupport ? intArray : arrayBuffer],
80+
{type: mediaType}
81+
)
82+
}
83+
bb = new BlobBuilder()
84+
bb.append(arrayBuffer)
85+
return bb.getBlob(mediaType)
86+
}
87+
if (window.HTMLCanvasElement && !CanvasPrototype.toBlob) {
88+
if (CanvasPrototype.mozGetAsFile) {
89+
CanvasPrototype.toBlob = function (callback, type, quality) {
90+
if (quality && CanvasPrototype.toDataURL && dataURLtoBlob) {
91+
callback(dataURLtoBlob(this.toDataURL(type, quality)))
92+
} else {
93+
callback(this.mozGetAsFile('blob', type))
94+
}
95+
}
96+
} else if (CanvasPrototype.toDataURL && dataURLtoBlob) {
97+
CanvasPrototype.toBlob = function (callback, type, quality) {
98+
callback(dataURLtoBlob(this.toDataURL(type, quality)))
99+
}
108100
}
109-
}(window));
101+
}
102+
if (typeof define === 'function' && define.amd) {
103+
define(function () {
104+
return dataURLtoBlob
105+
})
106+
} else if (typeof module === 'object' && module.exports) {
107+
module.exports = dataURLtoBlob
108+
} else {
109+
window.dataURLtoBlob = dataURLtoBlob
110+
}
111+
}(window))

package.json

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,15 @@
2222
"license": "MIT",
2323
"main": "./js/canvas-to-blob.js",
2424
"devDependencies": {
25-
"jshint": "2.8.0",
2625
"mocha-phantomjs": "4.0.1",
26+
"standard": "6.0.7",
2727
"uglify-js": "2.6.1"
2828
},
2929
"scripts": {
30-
"test": "jshint js test && mocha-phantomjs test/index.html",
30+
"test": "standard js/*.js test/*.js && mocha-phantomjs test/index.html",
3131
"build": "cd js && uglifyjs canvas-to-blob.js -c -m -o canvas-to-blob.min.js --source-map canvas-to-blob.min.js.map",
3232
"preversion": "npm test",
3333
"version": "npm run build && git add -A js",
3434
"postversion": "git push --tags origin master master:gh-pages && npm publish"
35-
},
36-
"jshintConfig": {
37-
"bitwise": true,
38-
"curly": true,
39-
"eqeqeq": true,
40-
"forin": true,
41-
"freeze": true,
42-
"immed": true,
43-
"latedef": true,
44-
"newcap": true,
45-
"noarg": true,
46-
"noempty": true,
47-
"nonbsp": true,
48-
"nonew": true,
49-
"quotmark": true,
50-
"undef": true,
51-
"unused": true,
52-
"strict": true
5335
}
5436
}

test/test.js

Lines changed: 73 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -11,85 +11,82 @@
1111

1212
/*global window, describe, it, Blob */
1313

14-
(function (expect) {
15-
'use strict';
14+
;(function (expect) {
15+
'use strict'
1616

17-
// 80x60px GIF image (color black, base64 data):
18-
var b64Data = 'R0lGODdhUAA8AIABAAAAAP///ywAAAAAUAA8AAACS4SPqcvtD6' +
19-
'OctNqLs968+w+G4kiW5omm6sq27gvH8kzX9o3n+s73/g8MCofE' +
20-
'ovGITCqXzKbzCY1Kp9Sq9YrNarfcrvcLDovH5PKsAAA7',
21-
imageUrl = 'data:image/gif;base64,' + b64Data,
22-
blob = window.dataURLtoBlob && window.dataURLtoBlob(imageUrl);
17+
// 80x60px GIF image (color black, base64 data):
18+
var b64Data = 'R0lGODdhUAA8AIABAAAAAP///ywAAAAAUAA8AAACS4SPqcvtD6' +
19+
'OctNqLs968+w+G4kiW5omm6sq27gvH8kzX9o3n+s73/g8MCofE' +
20+
'ovGITCqXzKbzCY1Kp9Sq9YrNarfcrvcLDovH5PKsAAA7'
21+
var imageUrl = 'data:image/gif;base64,' + b64Data
22+
var blob = window.dataURLtoBlob && window.dataURLtoBlob(imageUrl)
2323

24-
describe('canvas.toBlob', function () {
24+
describe('canvas.toBlob', function () {
25+
it('Converts a canvas element to a blob and passes it to the callback function', function (done) {
26+
window.loadImage(blob, function (canvas) {
27+
canvas.toBlob(
28+
function (newBlob) {
29+
done()
30+
expect(newBlob).to.be.a(Blob)
31+
}
32+
)
33+
}, {canvas: true})
34+
})
2535

26-
it('Converts a canvas element to a blob and passes it to the callback function', function (done) {
27-
window.loadImage(blob, function (canvas) {
28-
canvas.toBlob(
29-
function (newBlob) {
30-
done();
31-
expect(newBlob).to.be.a(Blob);
32-
}
33-
);
34-
}, {canvas: true});
35-
});
36+
it('Converts a canvas element to a PNG blob', function (done) {
37+
window.loadImage(blob, function (canvas) {
38+
canvas.toBlob(
39+
function (newBlob) {
40+
done()
41+
expect(newBlob.type).to.be('image/png')
42+
},
43+
'image/png'
44+
)
45+
}, {canvas: true})
46+
})
3647

37-
it('Converts a canvas element to a PNG blob', function (done) {
38-
window.loadImage(blob, function (canvas) {
39-
canvas.toBlob(
40-
function (newBlob) {
41-
done();
42-
expect(newBlob.type).to.be('image/png');
43-
},
44-
'image/png'
45-
);
46-
}, {canvas: true});
47-
});
48+
it('Converts a canvas element to a JPG blob', function (done) {
49+
window.loadImage(blob, function (canvas) {
50+
canvas.toBlob(
51+
function (newBlob) {
52+
done()
53+
expect(newBlob.type).to.be('image/jpeg')
54+
},
55+
'image/jpeg'
56+
)
57+
}, {canvas: true})
58+
})
4859

49-
it('Converts a canvas element to a JPG blob', function (done) {
50-
window.loadImage(blob, function (canvas) {
51-
canvas.toBlob(
52-
function (newBlob) {
53-
done();
54-
expect(newBlob.type).to.be('image/jpeg');
55-
},
56-
'image/jpeg'
57-
);
58-
}, {canvas: true});
59-
});
60+
it('Keeps the aspect ratio of the canvas image', function (done) {
61+
window.loadImage(blob, function (canvas) {
62+
canvas.toBlob(
63+
function (newBlob) {
64+
window.loadImage(newBlob, function (img) {
65+
done()
66+
expect(img.width).to.be(canvas.width)
67+
expect(img.height).to.be(canvas.height)
68+
})
69+
}
70+
)
71+
}, {canvas: true})
72+
})
6073

61-
it('Keeps the aspect ratio of the canvas image', function (done) {
62-
window.loadImage(blob, function (canvas) {
63-
canvas.toBlob(
64-
function (newBlob) {
65-
window.loadImage(newBlob, function (img) {
66-
done();
67-
expect(img.width).to.be(canvas.width);
68-
expect(img.height).to.be(canvas.height);
69-
});
70-
}
71-
);
72-
}, {canvas: true});
73-
});
74-
75-
it('Keeps the image data of the canvas image', function (done) {
76-
window.loadImage(blob, function (canvas) {
77-
canvas.toBlob(
78-
function (newBlob) {
79-
window.loadImage(newBlob, function (newCanvas) {
80-
var canvasData = canvas.getContext('2d')
81-
.getImageData(0, 0, canvas.width, canvas.height),
82-
newCanvasData = newCanvas.getContext('2d')
83-
.getImageData(0, 0, newCanvas.width, newCanvas.height);
84-
done();
85-
expect(canvasData.width).to.be(newCanvasData.width);
86-
expect(canvasData.height).to.be(newCanvasData.height);
87-
}, {canvas: true});
88-
}
89-
);
90-
}, {canvas: true});
91-
});
92-
93-
});
94-
95-
}(this.expect));
74+
it('Keeps the image data of the canvas image', function (done) {
75+
window.loadImage(blob, function (canvas) {
76+
canvas.toBlob(
77+
function (newBlob) {
78+
window.loadImage(newBlob, function (newCanvas) {
79+
var canvasData = canvas.getContext('2d')
80+
.getImageData(0, 0, canvas.width, canvas.height)
81+
var newCanvasData = newCanvas.getContext('2d')
82+
.getImageData(0, 0, newCanvas.width, newCanvas.height)
83+
done()
84+
expect(canvasData.width).to.be(newCanvasData.width)
85+
expect(canvasData.height).to.be(newCanvasData.height)
86+
}, {canvas: true})
87+
}
88+
)
89+
}, {canvas: true})
90+
})
91+
})
92+
}(this.expect))

0 commit comments

Comments
 (0)