Skip to content

Commit 4b5a7b9

Browse files
committed
add File#toJSON() tests and docs
1 parent 1dd3767 commit 4b5a7b9

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

Readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ here for compatibility with the [W3C File API Draft](http://dev.w3.org/2006/weba
182182

183183
If hash calculation was set, you can read the hex digest out of this var.
184184

185+
#### Formidable.File#toJSON()
186+
187+
This method returns a JSON-representation of the file, allowing you to
188+
`JSON.stringify()` the file which is useful for logging and responding
189+
to requests.
185190

186191
### Events
187192

lib/file.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,12 @@ File.prototype.toJSON = function() {
3838
path: this.path,
3939
name: this.name,
4040
type: this.type,
41-
hash: this.hash,
4241
mtime: this.lastModifiedDate,
4342
length: this.length,
4443
filename: this.filename,
4544
mime: this.mime
46-
}
47-
}
45+
};
46+
};
4847

4948
File.prototype.write = function(buffer, cb) {
5049
var self = this;

test/unit/test-file.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var common = require('../common');
2+
var test = require('utest');
3+
var assert = common.assert;
4+
var File = common.require('file');
5+
6+
var file;
7+
var now = new Date;
8+
test('IncomingForm', {
9+
before: function() {
10+
file = new File({
11+
size: 1024,
12+
path: '/tmp/cat.png',
13+
name: 'cat.png',
14+
type: 'image/png',
15+
lastModifiedDate: now,
16+
filename: 'cat.png',
17+
mime: 'image/png'
18+
})
19+
},
20+
21+
'#toJSON()': function() {
22+
var obj = file.toJSON();
23+
var len = Object.keys(obj).length;
24+
assert.equal(1024, obj.size);
25+
assert.equal('/tmp/cat.png', obj.path);
26+
assert.equal('cat.png', obj.name);
27+
assert.equal('image/png', obj.type);
28+
assert.equal('image/png', obj.mime);
29+
assert.equal('cat.png', obj.filename);
30+
assert.equal(now, obj.mtime);
31+
assert.equal(len, 8);
32+
}
33+
});

0 commit comments

Comments
 (0)