Recently I have somehow found myself in need of using JavaScript dot notation to represent values a bit too often to bother constantly rewriting the code for it. Turns out, there's no real (and nice) way to do this in the way that I wanted. Perhaps I'm conforming to some weird standard, but it appears that most of the existing modules (specifically on npm) all use some bizarre form of the notation I want. I simply wanted to use the same way you would access an Object's properties in JavaScript.

Due to this, I decided to write dot-notes, a simple npm module for converting an Object to it's dotted form (and back again). This library has full coverage, and is pretty easy to use. It uses the following syntax for notation:

  • All non-special keys are separated by a period .
  • Keys containing special characters are placed in square brackets and wrapped in quotes ['key'], and are not preceeded by a period. Such keys are also followed by a period only if the following key is a non-special key.
  • Arrays are also traversed, and are represented by an index in square brackets [0]. These keys are followed by a period only if the following key is a non-special key.

Maybe that's just my style, but I think it's pretty typical to expect something like this when using JavaScript. Below are a few examples of the syntax:

// Any key may be reference via dot separators
test.one;

// Array elements must be wrapped in square brackets
test.one[2];

// Keys with special characters must go in quotes, in square brackets
test.one[2]['my.test'];

// Quotes can be either double or single, as long as they match
test.one[2]["my.test"];

// Should you wish to, you can place non-special keys in this form, it won't harm anything
test['one'];  

You can use dot-notes to translate an Object into a noted form, a noted Object back into the original, and a String following the above syntax to create an Object. All information around how to do this can be found on npm and the GitHub repo.

I don't expect many people will find a need for this (even my needs are quite specific to a project), but it's always better to have something for it than not!

Oh and if there's something I'm missing with regards to syntax, or if there's a syntax which is more widely accepted that I'm somehow missing, feel free to let me know and I'll consider switching over (or adding support, whichever).