ponyfills.js 1.13 KB
Newer Older
Chok's avatar
Chok committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module.exports = function init(global) {
  var interface = { FormData: FormData };

  // expose all constructor functions for testing purposes
  if (init.debug) {
    interface.Iterator = Iterator;
  }
  
  function FormData() {
    this.__items = [];
  }

  FormData.prototype.append = function(name, value, filename) {
    if (global.File && value instanceof global.File) {
      // nothing to do
    } else if (global.Blob && value instanceof global.Blob) {
      // mimic File instance by adding missing properties
      value.lastModifiedDate = new Date();
      value.name = filename !== undefined ? filename : 'blob';
    } else {
      value = String(value);
    }

    this.__items.push([ name, value ]);
  };

  FormData.prototype.entries = function() {
    return new Iterator(this.__items);
  };

  function Iterator(items) {
    this.__items = items;
    this.__position = -1;
  }

  Iterator.prototype.next = function() {
    this.__position += 1;

    if (this.__position < this.__items.length) {
      return { done: false, value: this.__items[this.__position] };
    }

    return { done: true, value: undefined };
  }

  return interface;
};