Skip to content

Commit 9337233

Browse files
author
Philipp Alferov
committed
Change API
1 parent 2bae3f6 commit 9337233

File tree

2 files changed

+19
-39
lines changed

2 files changed

+19
-39
lines changed

src/angular-file-saver.service.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,24 @@ module.exports = function FileSaver(Blob, SaveAs, FileSaverUtils) {
1919
/**
2020
* saveAs - Immediately starts saving a file, returns undefined.
2121
*
22-
* @param {object} config Set of options such as data, filename
23-
* and Blob constructor options. Options - optional parameter if data
24-
* is represented by blob instance
22+
* @param {object} config Set of options such as filename and Blob options.
2523
* @return {undefined}
2624
*/
2725

2826
saveAs: function(config) {
29-
config = config || {};
27+
config = angular.extend({}, config);
3028
var data = config.data;
3129
var filename = config.filename;
32-
var options = config.options;
3330

34-
if (!FileSaverUtils.isArray(data) && !isBlobInstance(data)) {
35-
FileSaverUtils.handleErrors('Data argument should be represented as an array or Blob instance');
31+
if (!isBlobInstance(data)) {
32+
FileSaverUtils.handleErrors('Data argument should be a blob instance');
3633
}
3734

3835
if (!FileSaverUtils.isString(filename)) {
3936
FileSaverUtils.handleErrors('Filename argument should be a string');
4037
}
4138

42-
if (isBlobInstance(data)) {
43-
return save(data, filename);
44-
}
45-
46-
var blob = new Blob(data, options);
47-
return save(blob, filename);
39+
return save(data, filename);
4840
}
4941
};
5042
};

test/angular-file-saver-bundle.spec.js

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,38 @@
22

33
describe('angular-file-saver', function() {
44

5-
var FileSaver, config;
5+
var FileSaver, Blob;
66

77
beforeEach(function() {
88

99
angular.mock.module('ngFileSaver');
1010

11-
angular.mock.inject(function(_FileSaver_) {
11+
angular.mock.inject(function(_FileSaver_, _Blob_) {
1212
FileSaver = _FileSaver_;
13+
Blob = _Blob_;
1314
});
1415
});
1516

1617
describe('passed arguments are correct', function() {
1718

18-
beforeEach(function() {
19-
config = {
20-
filename: 'xfiles.txt',
21-
options: {
22-
type: 'text/plain;charset=utf-8'
23-
}
19+
it('should throw an error if provided data is an array', function() {
20+
var config = {
21+
data: ['text'],
22+
filename: 'xfiles.txt'
2423
};
25-
});
26-
27-
it('should save a file if provided data is an array', function() {
28-
config.data = ['text'];
2924

30-
expect(function() {
31-
FileSaver.saveAs(config);
32-
}).not.toThrow();
25+
expect(function() { FileSaver.saveAs(config); }).toThrow();
3326
});
3427

3528
it('should save a file if provided data is a blob', function() {
36-
config.data = new Blob(['text'], config.options);
29+
var data = new Blob(['text'], { type: 'text/plain;charset=utf-8' });
3730

38-
expect(function() {
39-
FileSaver.saveAs(config);
40-
}).not.toThrow();
41-
});
42-
43-
it('should save a file if options object is not passed', function() {
44-
config.data = ['text'];
31+
var config = {
32+
data: data,
33+
filename: 'xfiles.txt'
34+
};
4535

46-
expect(function() {
47-
FileSaver.saveAs(config);
48-
}).not.toThrow();
36+
expect(function() { FileSaver.saveAs(config); }).not.toThrow();
4937
});
5038

5139
});

0 commit comments

Comments
 (0)