Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Commit

Permalink
[ObjDefine] support phantomjs dispite ariya/phantomjs#11856
Browse files Browse the repository at this point in the history
  • Loading branch information
Spencer Alger committed Apr 30, 2015
1 parent fd76370 commit 71ef8e4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
42 changes: 40 additions & 2 deletions src/kibana/utils/obj_define.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ define(function (require) {
this.prototype = prototype || Object.prototype;
}

ObjDefine.REDEFINE_SUPPORTED = (function () {
var a = Object.create(Object.prototype, {
prop: {
configurable: true,
value: 1
}
});

Object.defineProperty(a, 'prop', {
configurable: true,
value: 2
});

return a.prop === 2;
}());

/**
* normal value, writable and exported in JSON
*
Expand Down Expand Up @@ -51,7 +67,20 @@ define(function (require) {
* @return {object} - created object
*/
ObjDefine.prototype.create = function () {
return this.obj = Object.create(this.prototype, this.descs);
var self = this;
self.obj = Object.create(this.prototype, self.descs);

if (!Object.REDEFINE_SUPPORTED && !self.prototype.toJSON) {
self.obj.toJSON = function () {
return _.transform(self.obj, function (json, val, key) {
var desc = self.descs[key];
if (desc && desc.enumerable && val == null) return;
json[key] = val;
}, {});
};
}

return self.obj;
};


Expand All @@ -68,7 +97,7 @@ define(function (require) {
var self = this;
var exists = val != null;

if (exported) {
if (exported && ObjDefine.REDEFINE_SUPPORTED) {
return {
enumerable: exists,
configurable: true,
Expand All @@ -85,6 +114,15 @@ define(function (require) {
};
}

if (exported && !ObjDefine.REDEFINE_SUPPORTED) {
return {
enumerable: true,
configurable: true,
writable: changeable,
value: val
};
}

return {
enumerable: false,
writable: changeable,
Expand Down
6 changes: 1 addition & 5 deletions test/unit/specs/utils/obj_define.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ define(function (require) {
});

it('switched to exporting if a value is written', function () {
if (/PhantomJS/.test(window.navigator.userAgent)) {
// disabled in phantom until https://github.com/ariya/phantomjs/issues/11856 is resolved
return;
}

var def = new ObjDefine();
def.writ('name');

Expand All @@ -63,6 +58,7 @@ define(function (require) {
obj.name = null;
expect(flatten(obj)).to.not.have.property('name');
});

});

describe('#fact', function () {
Expand Down

0 comments on commit 71ef8e4

Please sign in to comment.