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

self-referenced-object

v2.0.0

Published

resolves objects with values that self-reference other properties in the same object

Downloads

761

Readme

self-referenced-object Build Status Coverage Status

Build and resolve objects with self-referencing properties.

I've found this especially useful when defining some kinds of config files in JS. Specifically you may sometimes want to define properties that either reference or are calculated from other properties in the same config file. Normally doing so would require adding calculated or self-referencing properties as extra statements after the initial object definition. This package allows you to define all your properties in a single statement in any order.

Usage

self-referenced-object behaves just like regular template literal expressions, except that inside ${} expressions it allows the use of this.[key] to reference other properties inside the same object, and uses regular strings (' or ") instead of template strings (`).

$ npm install self-referenced-object 

const sro = require('self-referenced-object');

// basic usage
sro({
  a: 'a',
  b: '${this.a}',
});

// calculated values
sro({
  a: 'a',
  b: '${this.a + this.a}',
});

// non-primative types
sro({
  a: [1, 2, 3],
  b: '${(this.a).concat([4])}',
});

// nested values
sro({
  a: {
    a: 'a',
  },
  b: {
    a: '${this.a.a}',
  }
});

Circular references and undefined references

self-referenced-object will throw errors if circular references or undefined references are found. It uses a backtracking DFS to track circular references.

Non-string values

The other way in which self-referenced-object differs from regular template strings is it's support for returning non-string values. In order to support self-references that might be numbers, arrays, objects etc. self-referenced-object will avoid casting the value to a string if the template is only a single ${} expression. Ie in { a: [1,2,3], b: "${this.a}" } b would be an Array, while in { a: [1,2,3], b: "${this.a} go" }, b would be the string "1,2,3 go"

Nested objects and values

self-referenced-object supports both nested references (ie ${this.x.x}) and nested objects (ie it will recursively traverse any children objects up to any depth);

Escape characters

If you need to use } inside your template literal expressions, they can be escaped by adding a \ in front of them just like in regular template literals.

Security

self-referenced-object evaluates any expressions inside template literals by calling Function('"use strict"; return `' + expression + "`;")() which is a marginally safer version of eval (ie still incredibly unsafe), so you should avoid passing any untrusted data into an object evaluated by sro (or at least don't self-reference untrusted data in an sro evaluated object).