Nutella Peanut-Butter Marshmallow

    self-referenced-object

    2.0.0 • Public • Published

    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).

    Install

    npm i self-referenced-object

    DownloadsWeekly Downloads

    211

    Version

    2.0.0

    License

    ISC

    Unpacked Size

    9.79 kB

    Total Files

    7

    Last publish

    Collaborators

    • alex-e-leon