From 99e5007e0f1cb0fc65db35f4cee19b674eaa3a11 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 30 Nov 2010 01:14:36 -0700 Subject: [PATCH] updated to use the Flanagan / Miller device --- README.md | 6 ++++++ lib/remedial.js | 4 +--- tests/array-wtf.js | 9 +++++---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 9a6e2c7..25e578b 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,12 @@ This works in both the Browser and SSJS. require('remedial'); +Notes +---- + +This uses the ["Flanagan / Miller device"](http://groups.google.com/group/nodejs/msg/0670a986a2906aeb) rather than the Crockford's original. + +There is [a more specific typeof()](http://rolandog.com/archives/2007/01/18/typeof-a-more-specific-typeof/) implementation worthy of consideration. Globals ==== diff --git a/lib/remedial.js b/lib/remedial.js index f4187a7..21bece8 100644 --- a/lib/remedial.js +++ b/lib/remedial.js @@ -5,9 +5,7 @@ var s = typeof value; if (s === 'object') { if (value) { - if (typeof value.length === 'number' && - !(value.propertyIsEnumerable('length')) && - typeof value.splice === 'function') { + if ((/array/i).test(({}).toString.call(value))) { s = 'array'; } } else { diff --git a/tests/array-wtf.js b/tests/array-wtf.js index 352eeba..cf860f7 100644 --- a/tests/array-wtf.js +++ b/tests/array-wtf.js @@ -1,6 +1,6 @@ // Firefox was tested using persevere's global-es5 for es5 emulation (function () { - require('remedial'); + require('../lib/remedial'); a = []; a[2] = 27; @@ -24,7 +24,8 @@ // Expected: 3 // Node/V8/FF: 0 console.log(typeOf(b)); - // array + // Expected: Object + // Node/V8/FF: array (with Crockford's original) c = Object.create([]); @@ -39,7 +40,7 @@ // Expected: 3 // Node/V8/FF: 1 console.log(typeOf(c)); - // Expected: array + // Expected: object // Node/V8/FF: object @@ -53,7 +54,7 @@ // Expected: 3 // Node/V8/FF: 1 console.log(typeOf(d)); - // Expected: array + // Expected: object // Node/V8: object }());