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

obj-to-property-string

v1.0.2

Published

Convert a JavaScript object's key:value pairs into a [customizable] delimited property string

Downloads

8

Readme

obj-to-property-string

Convert a JavaScript object's key:value pairs into a [customizable] delimited property string

Build Status

Print out the key:values of any javascript object into a customizable property string. The default behavior is to print the objects key:value pairs into a string that can be used on any HTML object, but you can customize any of the property string's delimiters to get any custom string output you need. See the examples below to explore the numerous options and output formats. Pull requests welcome.

Install

$ npm install --save obj-to-property-string

Usage

const objToPropertyString = require('obj-to-property-string');

objToPropertyString({class: 'customClass', style: 'display:none;', id: '1'});
//=> 'class="customClass" style="display:none;" id="1"'

API

objToPropertyString(object, [options])

object

Type: object

The object with key:values you want to convert to a property string.

options

spacer

Type: String Default: ' '

The character you want to use in between printed values. Defaults to a single space.

objToPropertyString({alpha: '1', beta: '2'}, {spacer: '::'});
//=> 'alpha="1"::beta="2"'

Note: The spacer value will not print after the final key:value pair

assignment

Type: String Default: '='

The assignment operator or character you want to use to separate the key from the value. Defaults to the assignment operator = (the equals sign).

objToPropertyString({alpha: '1', beta: '2'}, {assignment: ':'});
//=> 'alpha:"1" beta:"2"'
quoteValues

Type: Boolean Default: true

Set this value to false if you don't want the quoteString value to wrap the value. For example, if you would prefer your string to print like foo=bar instead of foo="bar".

objToPropertyString({alpha: '1', beta: '2'}, {quoteValues: false});
//=> 'alpha=1 beta=2'

objToPropertyString({alpha: '1', beta: '2'}, {quoteValues: false, assignment: ':'});
//=> 'alpha:1 beta:2'
quoteString

Type: String Default: '"' (double quote)

The character(s) you want to use to wrap the value in. Defaults to the double-quote character ".

objToPropertyString({alpha: '1', beta: '2'}, {quoteString: '|'});
//=> 'alpha=|1| beta=|2|'
endLineChar

Type: String Default: ''

Use this if you want to force a character to the end of your property string. For example, you could add a newline character to help add some formatting to your property string. Defaults to no value (empty string).

objToPropertyString({alpha: '1', beta: '2', gamma: '3'}, {endLineChar: '\n'});
//=> 'alpha="1" \nbeta="2" \ngamma: "3"'

Note: The endLineChar value will not print after the final key:value pair

quoteKeys

Type: Boolean Default: false

Set this value to true if you want to wrap the key (in key:value) with the quoteString character(s).

objToPropertyString({alpha: '1', beta: '2'}, {quoteKeys: true});
//=> '"alpha"="1" "beta"="2"'

License

MIT © Michael Wuergler