From f48adff6c70e7a84e264feb18ce9bdd53df93f8f Mon Sep 17 00:00:00 2001 From: rolux Date: Fri, 25 May 2012 09:32:11 +0200 Subject: [PATCH] move Ox.getset and Ox.makeObject to Object.js --- source/Ox/js/Object.js | 89 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/source/Ox/js/Object.js b/source/Ox/js/Object.js index 3a7a8c8a..c8253781 100644 --- a/source/Ox/js/Object.js +++ b/source/Ox/js/Object.js @@ -14,6 +14,68 @@ Ox.extend = function(obj) { return obj; }; +/*@ +Ox.getset Generic getter and setter function + See examples for details. + # Usage -------------------------------------------------------------------- + Ox.getset(options, args=[]) -> all options + Ox.getset(options, args=[key]) -> <*> options[key] + Ox.getset(options, args=[key, value], callback, that) -> context + sets options[key] to value and calls fn(key, value) + if the key/value pair was added or modified + Ox.getset(options, args=[{key: value}], callback, that) -> that + sets multiple options and calls fn(key, value) + for every key/value pair that was added or modified + # Arguments ---------------------------------------------------------------- + options Options object (key/value pairs) + args The arguments "array" of the caller function + callback Callback function + The callback is called for every key/value pair that was added or + modified. + key Key + value <*> Value + that The this object of the caller function (for chaining) + # Examples ----------------------------------------------------------------- + + > Ox.test.object.options("key", "val").options("key") + "val" + > Ox.test.object.options({foo: "foo", bar: "bar"}).options() + {"key": "val", "foo": "foo", "bar": "bar"} +@*/ +Ox.getset = function(object, args, callback, that) { + var object_ = Ox.clone(object), ret; + if (args.length == 0) { + // [] + ret = object_; + } else if (args.length == 1 && !Ox.isObject(args[0])) { + // [key] + ret = Ox.clone(object[args[0]]); + } else { + // [key, val] or [{key: val, ...}] + args = Ox.makeObject(args); + object = Ox.extend(object, args); + Ox.forEach(args, function(value, key) { + if (!object_ || !Ox.isEqual(object_[key], value)) { + callback && callback(key, value); + } + }); + ret = that; + } + return ret; +}; + Ox.hasOwn = function(obj, val) { return Object.prototype.hasOwnProperty.call(obj, val) }; @@ -28,12 +90,37 @@ Ox.keyOf = function(obj, val) { Ox.forEach(obj, function(v, k) { if (v === val) { key = k; - Ox.Break()(); + Ox.Break(); } }); return key; }; +/*@ +Ox.makeObject Takes an array and returns an object + Ox.makeObject is a helper for functions with two alternative + signatures like ('key', 'val') and ({key: 'val'}). + > (function() { return Ox.makeObject(arguments); }({foo: 1, bar: 2})) + {foo: 1, bar: 2} + > (function() { return Ox.makeObject(arguments); }('foo', 1)) + {foo: 1} + > (function() { return Ox.makeObject(arguments); }('foo')) + {foo: void 0} + > (function() { return Ox.makeObject(arguments); }()) + {} +@*/ +Ox.makeObject = function(array) { + var ret = {}; + if (Ox.isObject(array[0])) { + // ({foo: 'bar'}) + ret = array[0]; + } else if (array.length) { + // ('foo', 'bar') + ret[array[0]] = array[1]; + } + return ret; +}; + /*@ Ox.methods Returns a sorted list of all method names of an object > Ox.methods({a: [], b: false, f: function() {}, n: 0, o: {}, s: ''})