Files
Mozilla/mozilla/zap/base/ObjectUtils.js
alex%croczilla.com feb28e3799 Change JS modules from rel: to gre: locations as necessitated by
bug#321237.


git-svn-id: svn://10.0.0.236/branches/ZAP_20050610_BRANCH@186547 18797224-902f-48f8-a5cc-f745e15eee43
2005-12-23 23:22:18 +00:00

214 lines
6.7 KiB
JavaScript

/* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 moz-jssh-buffer-globalobj: "Components.utils.importModule('gre:ObjectUtils.js', null)" -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Mozilla SIP client project.
*
* The Initial Developer of the Original Code is 8x8 Inc.
* Portions created by the Initial Developer are Copyright (C) 2005
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Alex Fritze <alex@croczilla.com> (original author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
EXPORTED_SYMBOLS = [ "hasproperty",
"hasgettersetter",
"propcopy",
"objclone",
"objmerge",
"hashget",
"hashhas",
"hashset",
"hashdel",
"hashkeys",
"hashmap"];
// object to hold module's documentation:
var _doc_ = {};
//----------------------------------------------------------------------
// hasproperty
_doc_.hasproperty = "\
Returns true iff 'obj' has a property, getter or setter with name \n\
'prop'; false otherwise. ";
function hasproperty(obj, prop) {
return (hasgettersetter(obj, prop) ||
obj[prop] !== undefined);
}
//----------------------------------------------------------------------
// hasgettersetter
_doc_.hasgettersetter ="\
Returns true iff 'obj' has a getter or getter/setter with name \n\
'prop'; false otherwise. ";
function hasgettersetter(obj, prop) {
return (obj.__lookupGetter__(prop) ||
obj.__lookupSetter__(prop));
}
//----------------------------------------------------------------------
// propcopy
_doc_.propcopy = "\
Copies property 'prop' from object 'src' to object 'dest'. 'prop' \n\
can name a 'normal' property or a getter/setter. \n\
Returns true. ";
function propcopy(dest, src, prop) {
// src[prop] might be a getter and/or setter:
var getter = src.__lookupGetter__(prop);
var setter = src.__lookupSetter__(prop);
if (getter || setter) {
if (getter)
dest.__defineGetter__(prop, getter);
if (setter)
dest.__defineSetter__(prop, setter);
}
else
dest[prop] = src[prop];
return true;
}
//----------------------------------------------------------------------
// objclone
_doc_.objclone = "\
Returns a shallow copy of the given object. The copy includes all \n\
own and inherited properties of 'src' as well as any getters and \n\
setters. ";
function objclone(src) {
if (src == null) return null;
var dest = {};
for (var p in src) {
propcopy(dest, src, p);
}
return dest;
}
//----------------------------------------------------------------------
// objmerge
_doc_.objmerge = "\
Merge properties from object 'src' into object 'dest'. Only copy \n\
properties that aren't present in 'dest'. Merging can be customized \n\
by providing an optional function mergefun(dest, src, prop) which \n\
will be called for each property in turn. If this function returns \n\
true, the given property will not be copied by objmerge. ";
function objmerge(dest, src, /*[opt]*/ mergefun) {
for (var p in src) {
if (mergefun && mergefun(dest, src, p))
continue; // merging handled by mergefun
if (!hasproperty(dest, p))
propcopy(dest, src, p);
}
}
//----------------------------------------------------------------------
// hashget
_doc_.hashget = "\
Normal JS objects can be used as hashes, but we need to take care to\n\
avoid reserved names such as __prototype__, etc. as key values. \n\
hashget(obj, key), hashhas(obj, key), hashset(obj, key, value), \n\
hashdel(obj, key) translate key values appropriately. \n\
hashkeys(obj) returns an array of all keys in the hash. \n\
hashmap(obj, fct) can be used to map 'fct(key,value)' over all \n\
hash elements. Mapping stops when 'fct' returns a value != false \n\
and that value will be returned by the hashmap(). ";
function hashget(obj, key) {
return obj["$"+key];
}
//----------------------------------------------------------------------
// hashhas
_doc_hashhas = _doc_.hashget;
function hashhas(obj, key) {
return obj["$"+key] !== undefined;
}
//----------------------------------------------------------------------
// hashset
_doc_hashset = _doc_.hashget;
function hashset(obj, key, value) {
return obj["$"+key] = value;
}
//----------------------------------------------------------------------
// hashdel
_doc_hashdel = _doc_.hashget;
function hashdel(obj, key) {
delete obj["$"+key];
}
//----------------------------------------------------------------------
// hashkeys
_doc_hashkeys = _doc_.hashget;
var REGEXP_HASHKEY = /^\$(.*)$/;
function hashkeys(obj) {
var rv = [];
var match;
for (var k in obj) {
if ((match = REGEXP_HASHKEY(k)))
rv.push(match[1]);
}
return rv;
}
//----------------------------------------------------------------------
// hashmap
_doc_hashmap = _doc_.hashfind;
function hashmap(obj, fct) {
var match;
var rv;
for (var k in obj) {
if ((match = REGEXP_HASHKEY(k))) {
if ((rv = fct(match[1], obj[k])))
return rv;
}
}
return null;
}