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

property-4-json

v0.6.0

Published

Automatically enrich files containing JavaScript objects with new properties

Downloads

4

Readme

Property 4 JSON

Build script to automatically enrich static files containing JavaScript objects with auto-incremented properties without the need of a database.

What this library can do:

  • add (autoincrement) properties to each object in a JSON-like structure
  • quickly process large folders containing many static files
  • match specific file types by file pattern
  • unify unordered or scattered property values to a sequential order across objects of the same level

Why this library is useful:

  • it works with any file type, i.e. .js, .ts or .json
  • it does not reformat your file and it preserves whitespaces and respects your indentation
  • it does not require valid JSON structures as it is RegExp based
  • it is idempotent (multiple executions will not have any other effect as the initial execution)

Setup

Install the lib via npm i property-4-json -D as dev dependency and add the following line to your npm scripts:

"scripts": {
    "autoincrement-ids": "node node_modules/property-4-json --folder=dir/subdir --filetype=.json"
}

Now you can run the the script using

npm run autoincrement-ids

Options

Pass each option using following syntax --option=value

  • folder directory of the source files relative to your package.json, i.e. --folder=assets/files
  • filetype optional, change the file type, i.e. .js, .ts, .json etc. of which JSON is default
  • prop optional, the property name that should be auto incremented, id is default
  • quotation optional, quotation type in which the parameter should be wrapped, possible values are none, single and double (default)
  • stopwords optional, list of comma separated stopwords, i.e. --stopwords=import,export - will not match an object if a stopword is ahead the opening bracket
  • startvalue optional, start value of incremental variable other than 0 - the value will keep incrementing across multiple matched files
  • delimiter optional, changes the key-value delimiter from colon to custom character, i.e. setting --delimiter='=' will make use of = symbol

Examples

Running the algorithm on the following JSON file containing an array of objects

[
  {
    "foo": { },
    "bar": []
  },
  {
    "bar" : {
      "baz" : false
    }
  }
]

will enrich each object with an incremental id after the opening bracket

[
  {
    "id": 0, // <- here
    "foo": { },
    "bar": []
  },
  {
    "id": 1, // <- and here
    "bar" : {
      "baz" : false
    }
  }
]

You are not limited to JSON files, the algorithm can be applied to any structure wrapped in curly brackets. Using the algorithm with with following parameters on a typescript file

node node_modules/property-4-json --folder=dir/subdir --filetype=.ts --prop=count --quotation=none --delimiter=' =' --stopwords=interface,export --endline=semicolon
class Car {
  engine: Engine;
  cylinders: number;
}
export { Car };

will result in

class Car {
  count = 0; // <- here
  engine: Engine;
  cylinders: number;
}
export { Car };

regardless how many class objects are contained across all files, each will be incremented with its own unique count variable. Curly braced objects after statements like export, interface etc. will be omitted.

Updating existing properties

If your object already contains the property that you want to increment, the script will recognize the property and update it with a new value. Multiple executions of the script on the same file will not have any negative effect.

{"id": 999, "car": true}, {"vehicle": false, "id": 155}

would be updated by id while keeping the formatting as

{"id": 0, "car": true}, {"vehicle": false, "id": 1}

no matter how often the script is applied on that file.

Limitations

  • this lib is not meant to replace a database autoincrement feature, it was developed to clean up static data before a production build
  • this lib can be used for enriching existent dummy data with incremental properties, if you look for a solution to maintain JSON data try TODO json-server
  • if you look for a way to create dummy data that already contains id fields, try Mockaroo

Upcoming features

  • add new properties at lower nesting levels than 1 (currently the RegExp will always match the first opening bracket of the object)
  • pass one filename instead of a file pattern or folder name

Contributing

Please report bugs in the issues section

Author & License

  • Jan Suwart | MIT License