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

esformatter-collapse-objects

v0.5.1

Published

esformatter plugin: conditionally collapses objects and array literals

Downloads

254

Readme

esformatter-collapse-objects

esformatter plugin for conditionally collapsing object and array literals.

Features

  • Conditionally formats literals on a single line, while leaving others expanded
  • Respects your original esformatter whitespace settings
  • Conditions include a max line-length, a max number of keys/elements in the literal, a max depth of the literal (when it contains other objects or arrays), or when it contains complex expressions like inline function expressions.

Usage

install it:

npm install esformatter-collapse-objects

and something like this to your esformatter config file:

{
  "plugins": [
    "esformatter-collapse-objects"
  ]
}

Important: Update your esformatter config

This plugin works by relying on esformatter expanding the relevant expressions, and conditionally collapsing them back down to a single line. Therefore, you need to have esformatter expand them in the first place (esformatter defaults to expanding object literals, but not array literals).

Add the following to your esformatter config when collapsing arrays:

"lineBreak": {
  "before": {
    "ArrayExpressionClosing": 1
  },
  "after": {
    "ArrayExpressionOpening": 1,
    "ArrayExpressionComma": 1
  }
},

Since expressions were collapsed, you might be surprised that some appear as {a:b,c:d}. This is because esformatter defaults are tuned toward expanded expressions. Try merging the following into your config for whitespace before each property in an object:

{
  "whiteSpace": {
    "before": {
      "PropertyName": 1
    }
}

Options

The following is the default configuration for the plugin, which can be reproduced and modified in your .esformatter config:


{
  "collapseObjects": {
    "ObjectExpression": {
      "maxLineLength": 80,
      "maxKeys": 3,
      "maxDepth": 2,
      "forbidden": [
        "FunctionExpression"
      ]
    },
    "ArrayExpression": {
      "maxLineLength": 80,
      "maxKeys": 3,
      "maxDepth": 2,
      "forbidden": [
        "FunctionExpression"
      ]
    }
  }
}

Options map esprima AST Node types (in this case both ObjectExpression and ArrayExpression) to their respective options, just like indentation in esformatter.

Set either expression's value to -1 to opt out of collapsing those nodes.

maxLineLength (int)

If the literal exceeds a certain number of columns collapsed, it will not be collapsed.

Use a maxLineLength of -1 to ignore this option.

forbidden (Array)

You can also avoid collapsing literals under certain conditions like a maximum number of keys, or when they contain other nodes like FunctionExpression.

[function foo() { return 'bar' }]

for example, could never occur since FunctionExpression is forbidden when trying to collapse a literal if this is set.

Use a forbidden of [] to ignore this option.

maxDepth (int)

You can also limit the depth of nested literals. All literals begin at a depth of 1, and for performance reasons setting a maxDepth of greater than 3 is ignored. For example, {foo: { bar: 'baz' }} has a depth of two and would be collapsed if the maxDepth is 2 or greater.

Use a maxDepth of -1 to opt out of this functionality.

JavaScript API

Register the plugin and call esformatter like so:

// register plugin
esformatter.register(require('esformatter-collapse-objects'));
// pass options as second argument
var output = esformatter.format(str, options);

License

Released under the MIT License.

Credits

Huge thanks to Jörn Zaefferer, who published an MIT-licensed gist which serves as the foundation for this module.