Skip to content

Commit 28a6522

Browse files
author
Philipp Alferov
committed
Validate passed args
1 parent 84413e9 commit 28a6522

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

src/angular-file-saver.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,24 @@ angular
1616

1717
function SaveAs() {
1818

19-
function isBlobInstance (data) {
20-
return data instanceof Blob;
19+
function handleErrors (msg) {
20+
throw new Error(msg);
21+
}
22+
23+
function isArray (obj) {
24+
return Object.prototype.toString.call(obj) === '[object Array]';
25+
}
26+
27+
function isObject (obj) {
28+
return obj !== null && typeof obj === 'object';
29+
}
30+
31+
function isString (obj) {
32+
return typeof obj === 'string' || obj instanceof String;
33+
}
34+
35+
function isBlobInstance (obj) {
36+
return obj instanceof Blob;
2137
}
2238

2339
function save(blob, filename) {
@@ -42,14 +58,25 @@ angular
4258

4359
download: function (data, filename, options) {
4460
var blob;
45-
data = data instanceof Array ? data : [data];
61+
62+
if (!isArray(data) || !isBlobInstance(data)) {
63+
handleErrors('Data argument should be represented as an array or Blob instance');
64+
}
65+
66+
if (!isString(filename)) {
67+
handleErrors('Filename argument should be a string');
68+
}
69+
70+
if (!isObject(options)) {
71+
handleErrors('Options argument should be an object');
72+
}
4673

4774
if (isBlobInstance(data)) {
48-
save(data, filename);
75+
return save(data, filename);
4976
}
5077

5178
blob = new Blob(data, options);
52-
save(blob, filename);
79+
return save(blob, filename);
5380
}
5481
};
5582
}

0 commit comments

Comments
 (0)