jest-canvas-mock 2.1.0 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ # Version 2.1.1
4
+
5
+ - Feature: Support for ImageData instantiation using a source array(#45)
6
+
3
7
  # Version 2.1.0
4
8
 
5
9
  This minor version bump now has some major snapshot support but is backwards compatible.
@@ -33,7 +33,7 @@ function () {
33
33
  }
34
34
  }]);
35
35
 
36
- function ImageData(width, height) {
36
+ function ImageData(arr, w, h) {
37
37
  _classCallCheck(this, ImageData);
38
38
 
39
39
  _defineProperty(this, "_width", 0);
@@ -42,13 +42,41 @@ function () {
42
42
 
43
43
  _defineProperty(this, "_data", null);
44
44
 
45
- if (!Number.isFinite(height)) throw new TypeError('The source height is zero or not a number.');
46
- if (height === 0) throw new TypeError('The source height is zero or not a number.');
47
- if (!Number.isFinite(width)) throw new TypeError('The source width is zero or not a number.');
48
- if (width === 0) throw new TypeError('The source height is zero or not a number.');
49
- this._width = width;
50
- this._height = height;
51
- this._data = new Uint8ClampedArray(width * height * 4);
45
+ if (arguments.length === 2) {
46
+ if (arr instanceof Uint8ClampedArray) {
47
+ if (arr.length === 0) throw new RangeError('Source length must be a positive multiple of 4.');
48
+ if (arr.length % 4 !== 0) throw new RangeError('Source length must be a positive multiple of 4.');
49
+ if (!Number.isFinite(w)) throw new RangeError('The width is zero or not a number.');
50
+ if (w === 0) throw new RangeError('The width is zero or not a number.');
51
+ this._width = w;
52
+ this._height = arr.length / 4 / w;
53
+ this._data = arr;
54
+ } else {
55
+ var width = arr;
56
+ var height = w;
57
+ if (!Number.isFinite(height)) throw new RangeError('The height is zero or not a number.');
58
+ if (height === 0) throw new RangeError('The height is zero or not a number.');
59
+ if (!Number.isFinite(width)) throw new RangeError('The width is zero or not a number.');
60
+ if (width === 0) throw new RangeError('The width is zero or not a number.');
61
+ this._width = width;
62
+ this._height = height;
63
+ this._data = new Uint8ClampedArray(width * height * 4);
64
+ }
65
+ } else if (arguments.length === 3) {
66
+ if (!(arr instanceof Uint8ClampedArray)) throw new TypeError('First argument must be a Uint8ClampedArray when using 3 arguments.');
67
+ if (arr.length === 0) throw new RangeError('Source length must be a positive multiple of 4.');
68
+ if (arr.length % 4 !== 0) throw new RangeError('Source length must be a positive multiple of 4.');
69
+ if (!Number.isFinite(h)) throw new RangeError('The height is zero or not a number.');
70
+ if (h === 0) throw new RangeError('The height is zero or not a number.');
71
+ if (!Number.isFinite(w)) throw new RangeError('The width is zero or not a number.');
72
+ if (w === 0) throw new RangeError('The width is zero or not a number.');
73
+ if (arr.length !== w * h * 4) throw new RangeError('Source doesn\'n contain the exact number of pixels needed.');
74
+ this._width = w;
75
+ this._height = h;
76
+ this._data = arr;
77
+ } else {
78
+ throw new TypeError('Wrong number of arguments provided.');
79
+ }
52
80
  }
53
81
 
54
82
  return ImageData;
package/lib/index.js CHANGED
@@ -19,5 +19,5 @@ if (typeof window !== 'undefined') {
19
19
  global.window = (0, _window["default"])(window);
20
20
  }
21
21
 
22
- var ver = "2.1.0";
22
+ var ver = "2.1.1";
23
23
  exports.ver = ver;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jest-canvas-mock",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "description": "Mock a canvas in your jest tests.",
5
5
  "main": "lib/index.js",
6
6
  "types": "types/index.d.ts",