all files / montage/core/extras/ object.js

37.14% Statements 13/35
27.78% Branches 5/18
40% Functions 2/5
37.14% Lines 13/35
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128                            11×     11× 13×     11×                                                                                                                                                                                                  
/**
    Defines extensions to intrinsic `Object`.
    @see {external:Object}
    @module montage/core/extras/object
*/
 
/**
 * Returns the descriptor object for an object's property.
 * @param {Object} anObject The object containing the property.
 * @param {string} propertyName The name of the property.
 * @returns {Object} The object's property descriptor.
 * @function external:Object.getPropertyDescriptor
*/
Object.defineProperty(Object, "getPropertyDescriptor", {
    value: function (anObject, propertyName) {
        var current = anObject,
            currentDescriptor;
 
        do {
            currentDescriptor = Object.getOwnPropertyDescriptor(current, propertyName);
        } while (!currentDescriptor && (current = current.__proto__ || Object.getPrototypeOf(current)));
 
        return currentDescriptor;
    },
    writable: true,
    configurable: true
});
 
/**
 * Returns the prototype object and property descriptor for a property
 * belonging to an object.
 * @param {Object} anObject The object to return the prototype for.
 * @param {string} propertyName The name of the property.
 * @returns {Object} An object containing two properties named `prototype` and
 * `propertyDescriptor` that contain the object's prototype object and property
 * descriptor, respectively.  @function
 * external:Object.getPrototypeAndDescriptorDefiningProperty
 */
Object.defineProperty(Object, "getPrototypeAndDescriptorDefiningProperty", {
    value: function (anObject, propertyName) {
        var current = anObject,
            currentDescriptor;
        if (propertyName) {
 
            do {
                currentDescriptor = Object.getOwnPropertyDescriptor(current, propertyName);
            } while (!currentDescriptor && (current = current.__proto__ || Object.getPrototypeOf(current)));
 
            return {
                prototype: current,
                propertyDescriptor: currentDescriptor
            };
        }
    },
    writable: true,
    configurable: true
});
 
/**
 * Removes all properties owned by this object making the object suitable for
 * reuse.
 *
 * @function external:Object#clear
 * @returns this
 */
Object.defineProperty(Object.prototype, "clear", {
    value: function () {
        var keys = Object.keys(this),
            i = keys.length;
 
        while (i) {
            i--;
            delete this[keys[i]];
        }
 
        return this;
    },
    writable: true,
    configurable: true
});
 
Eif (Object.hasOwnProperty('defineBinding') === false) {
 
    Object.defineProperty(Object, "defineBinding", {
        value: function (target, targetPath, descriptor) {
            var depth = Error.stackTraceLimit,
                warningMsg = (['Object.defineBinding deprecated, replace Object.defineBinding with: ',
                'Import Bindings from "montage/core/core"',
                'Bindings.defineBinding(target, targetPath, descriptor);',
                '- Use "<-", "<->", and "source" in place of "boundObjectPropertyPath", "oneway", and "boundObject".',
                '- Use "convert" or "converter.convert" in place of "boundValueMutator".']).join("\r\n");
 
            Error.stackTraceLimit = 2;
            console.warn(warningMsg + new Error("deprecated").stack);
            Error.stackTraceLimit = depth;
 
            var Bindings = require("frb");
 
            descriptor.source = descriptor.boundObject;
            if (descriptor.oneway) {
                descriptor["<-"] = descriptor.boundObjectPropertyPath;
            } else {
                descriptor["<->"] = descriptor.boundObjectPropertyPath;
            }
 
            if (descriptor.boundValueMutator) {
                descriptor.convert = descriptor.boundValueMutator;
            }
 
            Bindings.defineBinding(target, targetPath, descriptor);
        },
        writable: true,
        configurable: true
    });
}
 
Eif (Object.hasOwnProperty('deleteBinding') === false) {
    Object.defineProperty(Object, "deleteBinding", {
        value: function (target, targetPath) {
            var Bindings = require("frb");
            Bindings.cancelBinding(target, targetPath);
        },
        writable: true,
        configurable: true
    });
}