New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@octetstream/object-to-form-data

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@octetstream/object-to-form-data - npm Package Compare versions

Comparing version 0.3.1 to 0.4.0

2

package.json
{
"name": "@octetstream/object-to-form-data",
"version": "0.3.1",
"version": "0.4.0",
"description": "Serialize given object/collection to a FormData.",

@@ -5,0 +5,0 @@ "repository": "https://github.com/octet-stream/object-to-form-data",

@@ -13,6 +13,11 @@ # object-to-form-data

`serialize(object[, root]) -> {FormData}`
`serialize(object[, options]) -> {FormData}`
* **{object}** object – Object to transform
* **{string}** root – Root key of a fieldname
* **{object | string | boolean}** options – Serialization options.
This argument might be an object with "root" and "strict" parameters.
Or you can pass one of them as the second argument:
+ **{boolean}** [strict = false] – if set to `true`, all `false` boolean
values will be omitted.
+ **{string}** [root = null] – Just a root key of all fieldnames

@@ -53,1 +58,7 @@ ## Usage

```
**Important!** If you're using this library for Node.js environments,
you also need the [formdata-node](https://github.com/octet-stream/form-data)
to serialize your objects/collections.
See documentation of this implementation to learn how to send queries
with that implementation.

@@ -1,7 +0,10 @@

type TIterable = any[] | {
[key : string]: any
type TIterable = any[] | {[key : string] : any}
type TOptions = boolean | string | {
root ?: string,
strict ?: boolean
}
declare function serialize(iterable : TIterable, root ?: string) : FormData
declare function serialize(iterable : TIterable, root ?: TOptions) : FormData
export default serialize

@@ -5,8 +5,20 @@ const FormData = require("./form-data")

const isArray = Array.isArray
const assign = Object.assign
const keys = Object.keys
const isFunction = value => typeof value === "function"
const isBoolean = value => typeof value === "boolean"
const isString = value => typeof value === "string"
const defaults = {
strict: false,
root: null
}
/**
* Transform given object/collection to form-data
*
* @param {object} object – An object to transform
* @param {object | array} object – An object to transform
* @param {string} [root = null] – Root key of a fieldname

@@ -16,3 +28,3 @@ *

*/
function serialize(iterable, root = null) {
function serialize(iterable, options = {}) {
if (!(isArray(iterable) || isPlainObject(iterable))) {

@@ -22,6 +34,23 @@ throw new TypeError("Expected object or array as the first argument.")

const method = typeof FormData.prototype.set === "function" ? "set" : "append"
if (isBoolean(options)) {
options = {strict: options}
} else if (isString(options) && options) {
options = {root: options}
}
const data = new FormData()
if (!isPlainObject(options)) {
throw new TypeError(
"The second argument can be an object, boolean or string value."
)
}
options = assign({}, defaults, options)
// Choose the serialization method for browsers which
// are support FormData API partially
// See: https://caniuse.com/#search=formdata
const method = isFunction(FormData.prototype.set) ? "set" : "append"
const fd = new FormData()
/**

@@ -37,9 +66,13 @@ * Set object fields to FormData instance

for (const key of keys(value)) {
const fieldname = prefix ? `${prefix}[${key}]` : key
const name = prefix ? `${prefix}[${key}]` : key
const field = value[key]
if (isArray(field) || isPlainObject(field)) {
set(fieldname, field)
set(name, field)
} else {
data[method](fieldname, field)
if ((options.strict && (isBoolean(field) && field === false))) {
continue
}
fd[method](name, field)
}

@@ -49,8 +82,11 @@ }

set(root, iterable)
set(options.root, iterable)
return data
return fd
}
const strict = (iterable, root) => serialize(iterable, {root, strict: true})
module.exports = serialize
module.exports.default = serialize
module.exports.strict = strict
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc