This commit is contained in:
AJ ONeal 2018-05-09 18:14:57 -06:00
parent 51e539243d
commit ba3f718ea5
4 changed files with 102 additions and 8 deletions

View File

@ -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.

View File

@ -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 {

View File

@ -3,7 +3,9 @@
"contributors": [ "Douglas Crockford <douglas@crockford.com>" ],
"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"
}

87
tests/array-wtf.js Normal file
View File

@ -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
*/