formdata-polyfill
Advanced tools
Comparing version 1.0.2 to 1.0.3
156
FormData.js
const map = new WeakMap | ||
const wm = o => map.get(o) | ||
const NativeFormData = self.FormData | ||
class FormData { | ||
function normilizeValue(value) { | ||
filename = value[1] | ||
value = value[0] | ||
if (value instanceof Blob) | ||
value = new File([value], filename, { | ||
type: value.type, | ||
lastModified: value.lastModified | ||
}) | ||
return value | ||
} | ||
function stringify(name) { | ||
if (!arguments.length) | ||
throw new TypeError('1 argument required, but only 0 present.') | ||
return [name + ''] | ||
} | ||
function normilizeArgs(name, value, filename) { | ||
if (arguments.length < 2) | ||
throw new TypeError(`2 arguments required, but only ${arguments.length} present.`) | ||
return value instanceof Blob | ||
? [name + '', value, filename !== undefined | ||
? filename + '' | ||
: value[Symbol.toStringTag] === 'File' | ||
? value.name | ||
: 'Blob'] | ||
: [name + '', value + ''] | ||
} | ||
/** | ||
* @implements {Iterable} | ||
*/ | ||
class FormDataPolyfill { | ||
/** | ||
* FormData class | ||
* | ||
* @param HTMLFormElement form | ||
* @param {HTMLElement=} form | ||
*/ | ||
@@ -15,3 +50,3 @@ constructor(form) { | ||
if (!form) | ||
if (!form) | ||
return this | ||
@@ -33,3 +68,2 @@ | ||
} | ||
} | ||
@@ -41,10 +75,9 @@ | ||
* | ||
* @param String name field name | ||
* @param Mixed value string / blob / file | ||
* @param String filename filename to use with blob | ||
* @return Void | ||
* @param {String} name field name | ||
* @param {String|Blob|File} value string / blob / file | ||
* @param {String=} filename filename to use with blob | ||
* @return {Undefined} | ||
*/ | ||
append(name, value, filename) { | ||
let map = wm(this) | ||
name += '' | ||
@@ -61,7 +94,7 @@ if (!map[name]) | ||
* | ||
* @param String name Field name | ||
* @return Void | ||
* @param {String} name Field name | ||
* @return {Undefined} | ||
*/ | ||
delete(name) { | ||
delete wm(this)[name += ''] | ||
delete wm(this)[name] | ||
} | ||
@@ -73,31 +106,18 @@ | ||
* | ||
* @return Iterator | ||
* @return {Iterator} | ||
*/ | ||
*entries() { | ||
let map = wm(this) | ||
let opts = {} | ||
for (let name in map) | ||
for (let [value, filename] of map[name]) { | ||
if (value instanceof File) { | ||
filename = filename || value.name | ||
opts.type = value.type | ||
opts.lastModified = value.lastModified | ||
} | ||
if (value instanceof Blob) { | ||
value = new File([value], filename === undefined ? 'blob' : filename + '', opts) | ||
} | ||
yield [name, value] | ||
} | ||
for (let value of map[name]) | ||
yield [name, normilizeValue(value)] | ||
} | ||
/** | ||
* Iterate over all fields | ||
* | ||
* @param Function callback Executed for each item with parameters (value, name, thisArg) | ||
* @param Boolean thisArg `this` context for callback function | ||
* @return Void | ||
* @param {Function} callback Executed for each item with parameters (value, name, thisArg) | ||
* @param {Object=} thisArg `this` context for callback function | ||
* @return {Undefined} | ||
*/ | ||
@@ -113,10 +133,8 @@ forEach(callback, thisArg) { | ||
* | ||
* @param String name Field name | ||
* @return Mixed value Fields value | ||
* @param {String} name Field name | ||
* @return {String|File} value Fields value | ||
*/ | ||
get(name) { | ||
let map = wm(this) | ||
name += '' | ||
return map[name] ? map[name][0] : null | ||
return map[name] ? normilizeValue(map[name][0]) : null | ||
} | ||
@@ -128,7 +146,7 @@ | ||
* | ||
* @param String name Fields name | ||
* @return Array [name, value] | ||
* @param {String} name Fields name | ||
* @return {Array} [value, value] | ||
*/ | ||
getAll(name) { | ||
return (wm(this)[name += ''] || []).concat() | ||
return (wm(this)[name] || []).map(normilizeValue) | ||
} | ||
@@ -140,14 +158,14 @@ | ||
* | ||
* @param String name Field name | ||
* @return Boolean | ||
* @param {String} name Field name | ||
* @return {boolean} | ||
*/ | ||
has(name) { | ||
return (name+'') in wm(this) | ||
return name in wm(this) | ||
} | ||
/** | ||
* Iterate over all fields name | ||
* | ||
* @return Iterator | ||
* @return {Iterator} | ||
*/ | ||
@@ -163,9 +181,9 @@ *keys() { | ||
* | ||
* @param String name Filed name | ||
* @param String value Field value | ||
* @param String filename Filename (optional) | ||
* @return Void | ||
* @param {String} name Filed name | ||
* @param {String} value Field value | ||
* @param {String=} filename Filename (optional) | ||
* @return {Undefined} | ||
*/ | ||
set(name, value, filename) { | ||
wm(this)[name + ''] = [[value, filename]] | ||
wm(this)[name] = [[value, filename]] | ||
} | ||
@@ -177,3 +195,3 @@ | ||
* | ||
* @return Iterator | ||
* @return {Iterator} | ||
*/ | ||
@@ -189,3 +207,3 @@ *values() { | ||
* | ||
* @return Object ReadableStream | ||
* @return {ReadableStream} | ||
*/ | ||
@@ -200,10 +218,11 @@ stream() { | ||
/** | ||
* Return a native (perhaps degraded) FormData with only a `append` method | ||
* Can throw if it's not supported | ||
* | ||
* @return {[type]} [description] | ||
* | ||
* @return {FormData} | ||
*/ | ||
_asNative() { | ||
let fd = new NativeFormData | ||
let fd = new FormData | ||
@@ -219,6 +238,7 @@ for (let [name, value] of this) | ||
* [_blob description] | ||
* @return {[type]} [description] | ||
* | ||
* @return {Blob} [description] | ||
*/ | ||
_blob() { | ||
var boundary = "----FormDataPolyfill" + Math.random(); | ||
var boundary = '----FormDataPolyfill' + Math.random() | ||
var chunks = [] | ||
@@ -229,3 +249,3 @@ | ||
if (value instanceof File) { | ||
if (value[Symbol.toStringTag] === 'File') { | ||
chunks.push( | ||
@@ -254,3 +274,3 @@ `Content-Disposition: form-data; name="${name}"; filename="${value.name}"\r\n`, | ||
* | ||
* @return Iterator | ||
* @return {Iterator} | ||
*/ | ||
@@ -266,3 +286,3 @@ [Symbol.iterator]() { | ||
* | ||
* @return String [Object FormData] | ||
* @return {String} FormData | ||
*/ | ||
@@ -274,2 +294,16 @@ get [Symbol.toStringTag]() { | ||
module.exports = FormData | ||
for (let [method, overide] of [ | ||
['append', normilizeArgs], | ||
['delete', stringify], | ||
['get', stringify], | ||
['getAll', stringify], | ||
['has', stringify], | ||
['set', normilizeArgs] | ||
]) { | ||
let orig = FormDataPolyfill.prototype[method] | ||
FormDataPolyfill.prototype[method] = function() { | ||
return orig.apply(this, overide(...arguments)) | ||
} | ||
} | ||
module.exports = FormDataPolyfill |
{ | ||
"name": "formdata-polyfill", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "HTML5 `FormData` polyfill for Browsers.", | ||
@@ -5,0 +5,0 @@ "main": "FormData.js", |
@@ -17,9 +17,4 @@ # FormData | ||
fd = new FormData(form) | ||
blob = fd._blob() | ||
xhr.send(fd._blob()) | ||
// Do this... | ||
// important to set correct mimetype | ||
xhr.setRequestHeader('Content-Type', blob.type) // multipart/form-data; boundary=xxx | ||
xhr.send(blob) | ||
xhr.send(fd) // This don't work... Needs to be a native FormData | ||
@@ -26,0 +21,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10148
240
43