From ba3f718ea59d36ad47e569cd97d139e497e606dc Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 9 May 2018 18:14:57 -0600 Subject: [PATCH] v1.0.2 --- README.md | 13 +++++-- lib/remedial.js | 4 +-- package.json | 6 ++-- tests/array-wtf.js | 87 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 8 deletions(-) create mode 100644 tests/array-wtf.js diff --git a/README.md b/README.md index b9e0ee8..25aec9b 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,18 @@ This works in both the Browser and SSJS. require('remedial'); +Notes +---- -Usage / API +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 ==== typeOf(o) +---- Since JavaScript is a loosely-typed language, it is sometimes necessary to examine a value to determine its type. (This is sometimes necessary in strongly typed languages as well.) JavaScript provides a typeof operator to facilitate this, but typeof has problems. @@ -36,8 +43,8 @@ isEmpty(v) isEmpty(v) returns true if v is an object containing no enumerable members. -**String Methods** ----- +String Methods +==== JavaScript provides some useful methods for strings, but leaves out some important ones. Fortunately, JavaScript allows us to add new methods to the basic types. 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/package.json b/package.json index 69b0aed..2e6c1af 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,9 @@ "contributors": [ "Douglas Crockford " ], "dependencies": {}, "description": "Deprecated. Utilities for ES3, most of which have been adopted or superseded in ES5.1. Adapted from Douglas Crockford's Remedial JavaScript", - "engines": { "node": "*" }, + "engines": { + "node": "*" + }, "keywords": [ "util", "isEmpty", @@ -17,5 +19,5 @@ "main": "./lib/remedial", "name": "remedial", "homepage": "https://git.coolaj86.com/coolaj86/remedial.js", - "version": "1.0.1" + "version": "1.0.2" } diff --git a/tests/array-wtf.js b/tests/array-wtf.js new file mode 100644 index 0000000..cf860f7 --- /dev/null +++ b/tests/array-wtf.js @@ -0,0 +1,87 @@ +// Firefox was tested using persevere's global-es5 for es5 emulation +(function () { + require('../lib/remedial'); + + a = []; + a[2] = 27; + console.log(a); + // Expected: [undefined, undefined, 27]; + // Node/V8: [ 27 ]; + // FF3: [undefined, undefined, 27]; + console.log(a.length); + // 3 + console.log(typeOf(a)); + // array + + + + b = Object.create([]); + b[2] = 27; + console.log(b); + // Expected: [undefined, undefined, 27]; + // Node/V8: [ 27 ] + console.log(b.length); + // Expected: 3 + // Node/V8/FF: 0 + console.log(typeOf(b)); + // Expected: Object + // Node/V8/FF: array (with Crockford's original) + + + c = Object.create([]); + c.push(); + c.push(); + c.push(27); + console.log(c); + // Expected: [undefined, undefined, 27] + // Node: [ 27, length: 1 ] + // FF: [] + console.log(c.length); + // Expected: 3 + // Node/V8/FF: 1 + console.log(typeOf(c)); + // Expected: object + // Node/V8/FF: object + + + d = Object.create(Array); + // FF: Error: typeof prototype[function] != 'object' + d[2] = 27; + console.log(d); + // Expected: [undefined, undefined, 27]; + // Node/V8: { '2': 27 } + console.log(d.length); + // Expected: 3 + // Node/V8/FF: 1 + console.log(typeOf(d)); + // Expected: object + // Node/V8: object +}()); + +/* + Exact Node Output: + [ 27 ] + 3 + array + [ 27 ] + 0 + array + [ 27, length: 1 ] + 1 + object + + Exact FF (firebug) Output (expanded): + [undefined, undefined, 27] + > 2 27 + 3 + array + [] + > 2 27 + 0 + array + [27] + > 0 27 + length 1 + 1 + object +*/