Skip to Content
  • Website
  • Login
Codsen
  • Home
  • Open Source
  • Articles
  • About

 

So-called object-path notation

Book Node.js Design Patterns, 3rd edition Nearly a decade ago, Mario Casciaro, the author of Node.js Design Patternsopens in a new tab book, published an npm package object-pathopens in a new tab.

It’s a getter/setter helper which works on any data structures:

import { get } from "object-path";
const testObj1 = {
  foo: {
    bar: "value",
  },
};
const testObj2 = {};

console.log(get(testObj1, "foo.bar"));
// => "value"

console.log(get(testObj2, "foo.bar"));
// => null

The optional chainingopens in a new tab is a substitute for get (provided you put question marks correct in front of every dot), but there are eight more methods in object-path which don’t have native JS equivalents.

But object-path has one peculiar detail — it marks the array paths also using dots, not brackets (as in native JS):

import { get } from "object-path";
const testArr1 = [["value"]];
const testArr2 = [];

console.log(get(testArr1, "0.0"));
// => "value"

console.log(get(testArr2, "0.0"));
// => null

Practically, this dot-based path notation makes the life easier, both for program authors and program users. Just compare somePath.split(".").some(...) with… ahem, whatever the bracket-aware equivalent would be…

When objects and arrays are nested, we get paths like foo.1.bar.3.value (as opposed to native JS foo[1].bar[3].value).

Many packages our ours use this “dot notation”:

  • ast-get-values-by-key
  • The ast-monkey package family:
    ast-monkey, ast-monkey-traverse, ast-monkey-traverse-with-lookahead and ast-monkey-util
  • check-types-mini
  • edit-package-json
  • json-comb-core
  • json-variables
  • object-fill-missing-keys
  • object-no-new-keys

Codsen the company

UK Companies House 🏢 assigned Codsen Ltd a number #9204532 back on Friday, September 5th, 2014. That makes Codsen 10 years, 10 months and 25 days old.

Cookies

We don’t use cookies. This website only tracks anonymised website traffic via Fathomopens in a new tab which is GDPR compliant and all its data is housed exclusively in EU. Our analytics are publicopens in a new tab, see our Privacy Policy.

Activities

🐛 See a bug? Raise an issueopens in a new tab
💘 Check out the Indiewebopens in a new tab and Libera manifestoopens in a new tab

Trivia

Built with 💿 Remixopens in a new tab framework. See more in Trivia section.
Page performance: TTFBopens in a new tab loading FCPopens in a new tab loading.

Copyright

All rights reserved © Codsen Ltd 2022
All our open source packages are under MIT licenceopens in a new tab