Skip to content
This repository

add File#toJSON() #184

Merged
merged 2 commits into from over 1 year ago

3 participants

TJ Holowaychuk Sven Lito Felix Geisendörfer
TJ Holowaychuk

void of ._writeStream

TJ Holowaychuk add File#toJSON()
void of ._writeStream
1dd3767
Sven Lito
Collaborator

cool. would you be able to add a quick test for this before we merge this?

TJ Holowaychuk

yup, dont have time ATM but I can after work

Sven Lito
Collaborator

@visionmedia i suppose you haven't had time to look at this yet?

Felix Geisendörfer felixge commented on the diff October 31, 2012
lib/file.js
@@ -32,6 +32,20 @@ File.prototype.open = function() {
32 32
   this._writeStream = new WriteStream(this.path);
33 33
 };
34 34
 
  35
+File.prototype.toJSON = function() {
  36
+  return {
  37
+    size: this.size,
  38
+    path: this.path,
  39
+    name: this.name,
  40
+    type: this.type,
  41
+    hash: this.hash,
  42
+    mtime: this.lastModifiedDate,
  43
+    length: this.length,
  44
+    filename: this.filename,
  45
+    mime: this.mime
  46
+  }
1
Felix Geisendörfer Owner
felixge added a note October 31, 2012

nitpick: missing semicolon ; )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Felix Geisendörfer
Owner

I'm ok with merging this as is. But in order for it to become official API it needs to have:

  • one test
  • mention in the docs
TJ Holowaychuk

done! let me know if you want other changes

Sven Lito
Collaborator

i think this is sufficient enough. thanks!

Sven Lito svnlto closed this November 01, 2012
Sven Lito svnlto reopened this November 01, 2012
Sven Lito svnlto merged commit 799c7a4 into from November 01, 2012
Sven Lito svnlto closed this November 01, 2012
Felix Geisendörfer
Owner

Sweet, thanks @visionmedia !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Oct 17, 2012
TJ Holowaychuk add File#toJSON()
void of ._writeStream
1dd3767
Oct 31, 2012
TJ Holowaychuk add File#toJSON() tests and docs 4b5a7b9
This page is out of date. Refresh to see the latest.
5  Readme.md
Source Rendered
@@ -182,6 +182,11 @@ here for compatibility with the [W3C File API Draft](http://dev.w3.org/2006/weba
182 182
 
183 183
 If hash calculation was set, you can read the hex digest out of this var.
184 184
 
  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.
185 190
 
186 191
 ### Events
187 192
 
13  lib/file.js
@@ -32,6 +32,19 @@ File.prototype.open = function() {
32 32
   this._writeStream = new WriteStream(this.path);
33 33
 };
34 34
 
  35
+File.prototype.toJSON = function() {
  36
+  return {
  37
+    size: this.size,
  38
+    path: this.path,
  39
+    name: this.name,
  40
+    type: this.type,
  41
+    mtime: this.lastModifiedDate,
  42
+    length: this.length,
  43
+    filename: this.filename,
  44
+    mime: this.mime
  45
+  };
  46
+};
  47
+
35 48
 File.prototype.write = function(buffer, cb) {
36 49
   var self = this;
37 50
   this._writeStream.write(buffer, function() {
33  test/unit/test-file.js
... ...
@@ -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
+});
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.