npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

multipart-object

v1.2.0

Published

convert multipart data to nested data

Downloads

186

Readme

Multipart-object

CI build

library to convert a classic object to a nested object, for send nested data in multipart http request. A parser for nodejs is provided.

Installation

# with yarn
yarn add multipart-object
# npm
npm install multipart-object
# cnd for nestedMultiPart function
 <script src="https://unpkg.com/multipart-object/dist/nestedMultiPart.js" defer></script>

Convert data to nestedMultiPart

How is work

//es module
import nestedMultiPart from "multipart-object"
// with cdn
const toObject = windows.nestedMultiPart.toObject
const toFormData = windows.nestedMultiPart.toFormData


const data = {
    key: "value",
    array: [
        "value1",
        true,
        "value3"
        42,
        {
            anotherkey: "value",
            anotherkey2: "value2"
        }
    ],
    what: {
        nice: "library"
    }
}

const nestedData = nestedMultiPart.toObject(data)
// output
{
  "key": 'value',
  'array[0]': 'value1',
  'array[1]': true,
  'array[2]': 'value3',
  'array[3]': 42,
  'array[4][anotherkey]': 'value',
  'array[4][anotherkey2]': 'value2',
  'what[nice]': 'library'
}
// you have to nestedMultiPartForm is return FormData with nested formated
const nestedData = nestedMultiPart.toFormData(data)

Options

const options = {
	/*
		Separators:
		with bracket:  article[0][title][authors][0]: "jhon doe"
		with dot:      article.0.title.authors.0: "jhon doe"
		with mixed:      article[0]title.authors[0]: "jhon doe"
		with mixedDot:      article[0].title.authors[0]: "jhon doe"
	*/
	separator: 'bracket' or 'dot' or 'mixed' or 'mixedDot', // default is bracket
}

nestedMultiPart(data, options)

Parser for multipart nested

If your project are mande in python, you can use this lib nested_multipart_parser

A parser made for nodeJs

For this working perfectly you need to follow this rules:

For this to work perfectly, you must follow the following rules:

  • A first key always need to be set. ex: title[0] or title. In both cases the first key is title
  • Each sub key need to be separate by brackets [ ] or dot . (depends of your options)
  • For mixed or mixedDot options, brackets [] is for list, and dot . is for object
  • For mixedDot options, is look like mixed but with dot when array is follow a object
  • Don't put spaces between separators.
  • By default, you can't set duplicates keys (see options)

How it works:

Attributes where sub keys are full numbers only are automatically converted into lists:

	data = {
		'title[0]': 'my-value',
		'title[1]': 'my-second-value'
	}
	output = {
		'title': [
			'my-value',
			'my-second-value'
		]
	}

	# Be aware of the fact that you have to respect the order of the indices for arrays, thus 
    	'title[2]': 'my-value' # Invalid (you have to set title[0] and title[1] before)

    # Also, you can't create an array on a key already set as a prinitive value (int, boolean or string):
		'title': 42,
		'title[object]': 42 # Invalid

Attributes where sub keys are other than full numbers are converted into Python dictionary:

	data = {
		'title[key0]': 'my-value',
		'title[key7]': 'my-second-value'
	}
	output = {
		'title': {
			'key0': 'my-value',
			'key7': 'my-second-value'
		}
	}
    
    # You have no limit for chained key:
	data = {
		'the[0][chained][key][0][are][awesome][0][0]': 'im here !!'
	}
	# With "dot" separator option:
	data = {
		'the.0.chained.key.0.are.awesome.0.0': 'im here !!'
	}
	# with "mixed" separator option:
	data = {
		'the[0]chained.key[0]are.awesome[0][0]': 'im here !!'
	}
	# with "mixedDot" separator option (same as 'mixed' but with dot after list to object):
	data = {
		'the[0].chained.key[0].are.awesome[0][0]': 'im here !!'
	}

Parser usage

const NestedParser = require('multipart-object')

// options is optional
const options = {
    separator: "dot"
}

const parser = new NestedParser(data, options)
if (parser.isValid()) {
    const validateData = parser.validateData

} else {
    console.error(parser.errors)
}

Options

const options = {
	/*
		Separators:
		with bracket:  article[0][title][authors][0]: "jhon doe"
		with dot:      article.0.title.authors.0: "jhon doe"
		with mixed:      article[0].title.authors[0]: "jhon doe"
		with mixedDot:      article[0]title.authors[0]: "jhon doe"
	*/
	separator: 'bracket' or 'dot' or 'mixed' or 'mixedDot', // default is bracket

    /*
    raise a expections when you have duplicate keys
	    ex :
	    {
		    "article": 42,
	    	"article[title]": 42,
	    } 
    */
	throwDuplicate: true,

	/*
    overide the duplicate keys, you need to set "throwDuplicate" to False
	 ex :
	 {
		"article": 42,
		"article[title]": 42,
	 }
	 the out is
	 ex :
	 {
		"article"{
	 		"title": 42,
		}
	 }
     */
	assignDuplicate: false
}

License

MIT