I'm looking for a critique of an API.  I built this some months ago,
and
might need it again, and would love to have other eyes on it.

On a recent project I found myself in need of some configuration data
that could be easily changed, data in which one value was based upon
another.  And I created a generic constructor function to do it.  I
have no idea if this would be useful to anyone else, but just in case,
I thought I'd share.

The code is at

    http://scott.sauyet.com/Javascript/Test/ConfigObject/code

A test suite is at

    http://scott.sauyet.com/Javascript/Test/ConfigObject/test

I'm looking for any critique you might have.  I'm most interested in
comments on the API, as the implementation was quickly thrown
together. But any comments are welcome.

The API is fairly simple, as shown by this test code:

    test("Test 1", function() {
      var conf = new ConfigObject({
        host: "http://localhost";,
        path: "/somepath/to/content",
        url: "${host}${path}"
      });
      equals(conf.url, "http://localhost/somepath/to/content";);
    });

The object created by new ConfigObject(nameValues) is as simple as I
can make it.  It is just the name-value pairs supplied to the
constructor, with all token values appropriately replaced.  There are
no additional properties, hence no object methods.  But the
constructor function does have a few, so you can, for instance, use
the following to update the values of our configuration object:
    test("Test 2", function() {
      var conf = new ConfigObject({
        host: "http://localhost";,
        path: "/somepath/to/content",
        url: "${host}${path}"
      });
      equals(conf.url, "http://localhost/somepath/to/content";);

      ConfigObject.update(conf, {path: "/a/different/path"});
      equals(conf.url, "http://localhost/a/different/path";);
    });

You can also override the default tokens:

    test("Test 3", function() {
      var conf = new ConfigObject({
        host: "http://localhost";,
        path: "/somepath/to/content",
        url: "<~host~><~path~>"
      }, {prefix: "<~", suffix: "~>"});
      equals(conf.url, "http://localhost/somepath/to/content";);
    });

And you can nest tokens:

  test("Nested tokens are replaced", function() {
    var conf = new ConfigObject({
      config: "prod",
      // config: "dev",
      host: "${${config}.host}",
      port: "${${config}.port}",
      path: "/MyAppPath/login",
      url: "http://${host}:${port}${path}";,
      "prod.host": "myprodhost",
      "prod.port": "7453",
      "dev.host": "mydevhost",
      "dev.port": "8080"
    });
    equals(conf.url, "http://myprodhost:7453/MyAppPath/login";);
    ConfigObject.update(conf, {"config": "dev"});
    equals(conf.url, "http://mydevhost:8080/MyAppPath/login";);
  });

There is one implementation concern:  These objects cannot be garbage-
collected as the constructor function  itself retains a reference to
the items.  Since my need is for objects that last the life of the
page, this doesn't concern me.  But if it is an issue, it would not be
hard to add a "delete" function to the constructor... and perhaps a
"clone" function as well.

So what do you think?  Is this API reasonable?  Is it something you
could imagine using?  How about the implementation.  Have I done
anything outrageous?

Thanks for any feedback you can offer,

  -- Scott

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to