Use PHP functions in JavaScript

JavaScript aggregate_properties_by_list

!No description available for aggregate_properties_by_list. @php.js developers: Please update the function summary text file.

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
5556
57
58
59
6061
62
63
64
6566
67
68
69
7071
72
73
74
7576
77
78
79
8081
82
83
84
8586
87
88
89
9091
92
93
94
9596
97
98
function aggregate_properties_by_list (obj, class_name, properties_list, exclude) {
    // !No description available for aggregate_properties_by_list. @php.js developers: Please update the function summary text file.
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/aggregate_properties_by_list    // +   original by: Brett Zamir (http://brett-zamir.me)
    // %          note 1: We can't copy instance properties, as those require instantiation (with potential side-effects when called)
    // %          note 1: We've chosen not to assign to or create a prototype object on the destination object even if the original object had the properties on its prototype
    // *     example 1: var A = function () {};
    // *     example 1: A.prototype.prop = 10;    // *     example 1: var b = {};
    // *     example 1: aggregate_properties_by_list(b, 'A', ['prop'], false);
    // *     returns 1: undefined
 
    var p = '', i=0, record={}, pos=-1,        indexOf = function (value) {
            for (var i = 0, length=this.length; i < length; i++) {
                if (this[i] === value) {
                    return i;
                }            }
            return -1;
        };
        
    if (!properties_list.indexOf) {        properties_list.indexOf = indexOf;
    }
 
    if (typeof class_name === 'string') { // PHP behavior
        class_name = this.window[class_name];    }
 
    // BEGIN REDUNDANT
    this.php_js = this.php_js || {};
    this.php_js.aggregateKeys = this.php_js.aggregateKeys || [];    this.php_js.aggregateRecords = this.php_js.aggregateRecords || []; // Needed to allow deaggregate() and aggregate_info()
    this.php_js.aggregateClasses = this.php_js.aggregateClasses || [];
    var getFuncName = function (fn) {
        var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
        if (!name) {            return '(Anonymous)';
        }
        return name[1];
    };
    // END REDUNDANT    this.php_js.aggregateClasses.push(getFuncName(class_name));
 
    if (exclude) {
        for (p in class_name) {
            if (!(p in obj) &&                    typeof class_name[p] !== 'function' &&
                        p[0] !== '_' &&
                            properties_list.indexOf(p) === -1) { // Static (non-private) class properties
                obj[p] = class_name[p];
                record[p] = class_name[p];            }
        }
        for (p in class_name.prototype) {
            if (!(p in obj) &&
                    typeof class_name.prototype[p] !== 'function' &&                        p[0] !== '_' &&
                            properties_list.indexOf(p) === -1) { // Prototype (non-private) default properties
                obj[p] = class_name.prototype[p];
                record[p] = class_name.prototype[p];
            }        }
    } else {
        for (i=0; i < properties_list.length; i++) {
            p = properties_list[i];
            if (!(p in obj) &&                    p in class_name &&
                        p[0] !== '_' &&
                            typeof class_name.prototype[p] !== 'function') { // Static (non-private) class properties
                obj[p] = class_name[p];
                record[p] = class_name[p];            } else if (!(p in obj) &&
                    p in class_name.prototype &&
                        p[0] !== '_' &&
                            typeof class_name.prototype[p] !== 'function') { // Prototype (non-private) default properties
                obj[p] = class_name.prototype[p];                record[p] = class_name.prototype[p];
            }
        }
    }
    if (!this.php_js.aggregateKeys.indexOf) {        this.php_js.aggregateKeys.indexOf = indexOf;
    }
    pos = this.php_js.aggregateKeys.indexOf(obj);
    if (pos !== -1) {
        this.php_js.aggregateRecords[pos].push(record);        this.php_js.aggregateClasses[pos].push(getFuncName(class_name));
    } else {
        this.php_js.aggregateKeys.push(obj);
        this.php_js.aggregateRecords.push([record]);
        this.php_js.aggregateClasses[0] = [];        this.php_js.aggregateClasses[0].push(getFuncName(class_name));
    }
}
external links: original PHP docs | raw js source

Examples

Running

1
2
3
4
var A = function () {};
A.prototype.prop = 10;
var b = {};
aggregate_properties_by_list(b, 'A', ['prop'], false);

Should return

1
undefined

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have aggregate_properties_by_list goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

No comments yet. Be the first!


Contribute a New function

More functions

In this category

aggregate
aggregate_info
aggregate_methods
aggregate_methods_by_list
aggregate_methods_by_regexp
aggregate_properties
» aggregate_properties_by_list
aggregate_properties_by_regexp
aggregation_info
deaggregate

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: @HalfWinter, Paulo Freitas, Andros Peña Romo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !