Numerous Philanthropic Misanthropes
    Wondering what’s next for npm?Check out our public roadmap! »

    easydot

    1.1.0 • Public • Published

    easydot

    MIT License

    easydot provides "dot-string access" for target object.

    e.g) console.log(easydot({a:{b:{c:42}}})["a.b.c"]); // => 42

    Installing

    For Browser (CDN)

    <script src="https://cdn.jsdelivr.net/gh/matsuby/easydot/dist/easydot.min.js"></script>
    

    For Node.js

    // Install from npm
    npm install --save easydot
    
    // Install from yarn
    yarn add easydot
    

    and

    // Use ES Modules
    import easydot from "easydot";
    
    // Use Common JS
    const easydot = require("easydot");
    

    Usage

    const target = {
      what: {
        is: {
          easydot: 42
        }
      }
    };
    
    // create proxy that implements `dot-string acccess`
    const proxy = easydot(target);
    
    // get nested property
    console.log(proxy["what.is.easydot"]);
    // => 42
    
    // set nested property
    proxy["what.is.easydot"] = "set value";
    console.log(proxy.what.is.easydot);
    // => "set value"
    
    // this is a just proxy, so target status was affected
    proxy["what.is.name"] = "john";
    console.log(JSON.stringify(target, null, 2));
    // =>
    // {
    //  "what": {
    //    "is": {
    //      "easydot": "set value",
    //      "name": "john"
    //    }
    //  }
    // }
    

    Over jump access

    const target = {
      what: {
        is: {
          easydot: 42
        }
      }
    };
    
    // unsafe over jump access(default)
    const unsafeProxy = easydot(target);
    try {
      unsafeProxy["not.exist.property"];
    } catch (e) {
      console.error("error");
    }
    
    
    // safe over jump access(optional)
    const safeProxy = easydot(target, true);
    safeProxy["not.exist.property"] = "safe";
    

    Install

    npm i easydot

    DownloadsWeekly Downloads

    2

    Version

    1.1.0

    License

    MIT

    Unpacked Size

    11.6 kB

    Total Files

    13

    Last publish

    Collaborators

    • avatar