Skip to content

Commit

Permalink
Functions now observable; more tests; closes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxArt2501 committed Feb 11, 2015
1 parent 80df2d4 commit f970512
Show file tree
Hide file tree
Showing 9 changed files with 393 additions and 128 deletions.
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## v0.2.2

2015-02-11

* Uses `Object.getOwnPropertyNames` instead of `Object.keys` (issue #1)
* Functions can now be observed
* More tests

## v0.2.1

2015-01-28
Expand Down
50 changes: 37 additions & 13 deletions dist/object-observe-lite.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Object.observe "lite" polyfill - v0.2.1
* Object.observe "lite" polyfill - v0.2.2
* by Massimo Artizzu (MaxArt2501)
*
* https://github.com/MaxArt2501/object-observe
Expand Down Expand Up @@ -164,18 +164,42 @@ Object.observe || (function(O, A, root) {
} : function() { return new Map(); },

/**
* Simple shim for Object.keys is not available
* Misses checks on object, don't use as a replacement of Object.keys
* @function getKeys
* Simple shim for Object.getOwnPropertyNames when is not available
* Misses checks on object, don't use as a replacement of Object.keys/getOwnPropertyNames
* @function getProps
* @param {Object} object
* @returns {String[]}
*/
getKeys = O.keys || function(object) {
var keys = [], prop;
getProps = O.getOwnPropertyNames ? (function() {
var func = O.getOwnPropertyNames;
try {
arguments.callee;
} catch (e) {
// Strict mode is supported

// In strict mode, we can't access to "arguments", "caller" and
// "callee" properties of functions. Object.getOwnPropertyNames
// returns [ "prototype", "length", "name" ] in Firefox; it returns
// "caller" and "arguments" too in Chrome and in Internet
// Explorer, so those values must be filtered.
var avoid = (func(inArray).join(" ") + " ").replace(/prototype |length |name /g, "").slice(0, -1).split(" ");
if (avoid.length) func = function(object) {
var props = O.getOwnPropertyNames(object);
if (typeof object === "function")
for (var i = 0, j; i < avoid.length;)
if ((j = inArray(avoid[i++], props)) > -1)
props.splice(j, 1);

return props;
};
}
return func;
})() : function(object) {
var props = [], prop;
for (prop in object)
if (object.hasOwnProperty(prop))
keys.push(prop);
return keys;
props.push(prop);
return props;
},

/**
Expand Down Expand Up @@ -225,7 +249,7 @@ Object.observe || (function(O, A, root) {
* @param {Object} object
*/
createObjectData = function(object, data) {
var props = isNode(object) ? [] : getKeys(object),
var props = isNode(object) ? [] : getProps(object),
values = [], descs, i = 0,
data = {
handlers: createMap(),
Expand Down Expand Up @@ -262,7 +286,7 @@ Object.observe || (function(O, A, root) {

props = data.properties.slice();
proplen = props.length;
keys = getKeys(object);
keys = getProps(object);

// Check for value additions/changes
while (i < keys.length) {
Expand Down Expand Up @@ -460,7 +484,7 @@ Object.observe || (function(O, A, root) {
* @returns {Object} The observed object
*/
O.observe = function observe(object, handler, acceptList) {
if (object === null || typeof object !== "object")
if (object === null || typeof object !== "object" && typeof object !== "function")
throw new TypeError("Object.observe cannot observe non-object");

if (typeof handler !== "function")
Expand All @@ -486,7 +510,7 @@ Object.observe || (function(O, A, root) {
* @returns {Object} The given object
*/
O.unobserve = function unobserve(object, handler) {
if (object === null || typeof object !== "object")
if (object === null || typeof object !== "object" && typeof object !== "function")
throw new TypeError("Object.unobserve cannot unobserve non-object");

if (typeof handler !== "function")
Expand Down Expand Up @@ -524,7 +548,7 @@ Object.observe || (function(O, A, root) {
* @returns {Notifier}
*/
O.getNotifier = function getNotifier(object) {
if (object === null || typeof object !== "object")
if (object === null || typeof object !== "object" && typeof object !== "function")
throw new TypeError("Object.getNotifier cannot getNotifier non-object");

if (O.isFrozen && O.isFrozen(object)) return null;
Expand Down
18 changes: 9 additions & 9 deletions dist/object-observe-lite.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 37 additions & 13 deletions dist/object-observe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Object.observe polyfill - v0.2.1
* Object.observe polyfill - v0.2.2
* by Massimo Artizzu (MaxArt2501)
*
* https://github.com/MaxArt2501/object-observe
Expand Down Expand Up @@ -168,18 +168,42 @@ Object.observe || (function(O, A, root) {
} : function() { return new Map(); },

/**
* Simple shim for Object.keys is not available
* Misses checks on object, don't use as a replacement of Object.keys
* @function getKeys
* Simple shim for Object.getOwnPropertyNames when is not available
* Misses checks on object, don't use as a replacement of Object.keys/getOwnPropertyNames
* @function getProps
* @param {Object} object
* @returns {String[]}
*/
getKeys = O.keys || function(object) {
var keys = [], prop;
getProps = O.getOwnPropertyNames ? (function() {
var func = O.getOwnPropertyNames;
try {
arguments.callee;
} catch (e) {
// Strict mode is supported

// In strict mode, we can't access to "arguments", "caller" and
// "callee" properties of functions. Object.getOwnPropertyNames
// returns [ "prototype", "length", "name" ] in Firefox; it returns
// "caller" and "arguments" too in Chrome and in Internet
// Explorer, so those values must be filtered.
var avoid = (func(inArray).join(" ") + " ").replace(/prototype |length |name /g, "").slice(0, -1).split(" ");
if (avoid.length) func = function(object) {
var props = O.getOwnPropertyNames(object);
if (typeof object === "function")
for (var i = 0, j; i < avoid.length;)
if ((j = inArray(avoid[i++], props)) > -1)
props.splice(j, 1);

return props;
};
}
return func;
})() : function(object) {
var props = [], prop;
for (prop in object)
if (object.hasOwnProperty(prop))
keys.push(prop);
return keys;
props.push(prop);
return props;
},

/**
Expand Down Expand Up @@ -248,7 +272,7 @@ Object.observe || (function(O, A, root) {
* @param {Object} object
*/
createObjectData = function(object, data) {
var props = isNode(object) ? [] : getKeys(object),
var props = isNode(object) ? [] : getProps(object),
values = [], descs, i = 0,
data = {
handlers: createMap(),
Expand Down Expand Up @@ -387,7 +411,7 @@ Object.observe || (function(O, A, root) {

props = data.properties.slice();
proplen = props.length;
keys = getKeys(object);
keys = getProps(object);

if (descs) {
while (i < keys.length) {
Expand Down Expand Up @@ -623,7 +647,7 @@ Object.observe || (function(O, A, root) {
* @returns {Object} The observed object
*/
O.observe = function observe(object, handler, acceptList) {
if (object === null || typeof object !== "object")
if (object === null || typeof object !== "object" && typeof object !== "function")
throw new TypeError("Object.observe cannot observe non-object");

if (typeof handler !== "function")
Expand All @@ -649,7 +673,7 @@ Object.observe || (function(O, A, root) {
* @returns {Object} The given object
*/
O.unobserve = function unobserve(object, handler) {
if (object === null || typeof object !== "object")
if (object === null || typeof object !== "object" && typeof object !== "function")
throw new TypeError("Object.unobserve cannot unobserve non-object");

if (typeof handler !== "function")
Expand Down Expand Up @@ -687,7 +711,7 @@ Object.observe || (function(O, A, root) {
* @returns {Notifier}
*/
O.getNotifier = function getNotifier(object) {
if (object === null || typeof object !== "object")
if (object === null || typeof object !== "object" && typeof object !== "function")
throw new TypeError("Object.getNotifier cannot getNotifier non-object");

if (O.isFrozen && O.isFrozen(object)) return null;
Expand Down
Loading

0 comments on commit f970512

Please sign in to comment.