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

auto-object

v1.1.1

Published

Create auto properties object and class in Node.js.

Downloads

4

Readme

auto-object

Version Downloads License TravisCI Dependency

Create an object or a class that may accessed by any property name just like Proxy.

eg.

const myObject = autoObject.createObject(function(name) {
    if(name.startsWith("say")) {
        return function() {
            return name.substr(3);
        }
    }

    return name;
});

myObject.foo;       ///< "foo"
myObject.sayFoo();  ///< "Foo"
myObject.sayBar();  ///< "Bar"

Installation

$ npm install --save auto-object

API

Require auto-object at first:

const autoObject = require("auto-object");

createClass

Create an auto-object class and get its constructor function:

autoObject.createClass([className])
  • className: the class name, default to AutoClass. (optional)
  • return: the special constructor function

Your own constructor function should attached on the prototype chain named $$constructor. And the access function should attached on the prototype chain named $$access. Both of $$constructor and $$access are optional.

What's $$constructor

We usually create a class constructor function like this:

function MyClass(val) {
    this.$val = val;
}

But createClass exactly returns a constructor function which you can't redefine. So we make $$constructor as your own constructor function.

eg.

const MyClass = autoObject.createClass("MyClass");

MyClass.prototype.$$constructor = function(val) {
    this.$$val = val;
};

const ret = new MyClass(233); ///< MyClass { $$val: 233 }

What's $$access

$$access is the access function to deal with property accessing. This function owns a single parameter which means the property name. You should return a result for the accessing.

eg.

const MyClass = autoObject.createClass("MyClass");

MyClass.prototype.$$constructor = function(val) {
    this.$$val = val;
};

MyClass.prototype.$$access = function(property) {
    const self = this;
    if(property.match(/^plus\d+$/)) {
        return function() {
            self.$$val += parseInt(property.substr(4));
            return $$val;
        };
    } else if(property.match(/^minus\d+$/)) {
        return function() {
            self.$$val -= parseInt(property.substr(5));
            return $$val;
        };
    }

    return "You're hacked";
};

const ret = new MyClass(233);   ///< MyClass { $$val: 233 }
ret.plus5();                    ///< 238
ret.plus2();                    ///< 240
ret.minus10();                  ///< 230
ret.foo;                        ///< "You're hacked"

Inherits

The result of createClass almost like the normal constructor function, it may inherit from some other class by using util.inherits too.

eg.

const MyClass = autoObject.createClass("MyClass");

MyClass.prototype.$$constructor = function() {
    EventEmitter.call(this);
};

util.inherits(MyClass, EventEmitter);

MyClass.prototype.$$access = function(name) {
    const self = this;
    return function(val) {
        self.emit(name, val);
    };
}

const ret = new MyClass();
ret.on("test", function(val) {
    console.log(val);
});
ret.test("Hello world");        // emit("test", "Hello world")

createObject

Create and get an auto-object:

autoObject.createObject([access]);
  • access: the access function. (optional)
  • return: the special auto-object

If you want to change access function lately, you just assign the function to the $$access property of the returned object.

What's $$access

$$access is the access function to deal with property accessing. This function owns a single parameter which means the property name. You should return a result for the accessing.

eg.

const obj = autoObject.createObject(function(name) {
    return `You're ${name}`;
});

obj.foo;        ///< "You're foo"
obj.bar;        ///< "You're bar"

You can change $$access at any time you want to do this.

eg.

const obj = autoObject.createObject();

obj.foo;                        ///< undefined
obj.$$access = function(name) {
    return `You're ${name}`;
};
obj.foo;                        ///< "You're foo"
obj.bar;                        ///< "You're bar"

Reserved Properties and Overwrite Rule

This is important!

Here's a reserved properties list that won't go through the access function:

[
  "constructor",
  "hasOwnProperty",
  "isPrototypeOf",
  "propertyIsEnumerable",
  "toLocaleString",
  "toString",
  "valueOf",
  "__defineGetter__",
  "__defineSetter__",
  "__lookupGetter__",
  "__lookupSetter__",
  "__proto__",
  "inspect",
  "$$constructor",
  "$$access"
]

And if you define a property manually in your auto-object, that certain property won't go through the access function too.

eg.

const obj = autoObject.createObject(function() {
    return "You're hacked";
});

obj.foo;                ///< "You're hacked"
obj.foo = "I'm free";
obj.foo;                ///< "I'm free"
delete obj.foo;
obj.foo;                ///< "You're hacked"

Hacking util.inherits*

If you're using Node.js v4.x or you're considering to backward compatible v4.x in your program, you should not use util.inherits to inherit your Auto-Object class.

As a substitute, you should use autoObject.inherits instead. This function is same as util.inherits when you're using v6.x or higher. But if you're using v4.x, it's a hacking.

The code is like this under v4.x:

for(const key in superCtor.prototype) {
    ctor.prototype[key] = superCtor.prototype[key];
}

The behavior is not 100% same as util.inherits. So in fact I don't recommend you use this package under v4.x.

Contribution

You're welcome to fork and make pull requests!

「雖然我覺得不怎麼可能有人會關注我」