array-index
Advanced tools
Comparing version
50
index.js
@@ -6,2 +6,3 @@ | ||
var util = require('util') | ||
var debug = require('debug')('array-index') | ||
@@ -56,2 +57,49 @@ | ||
/** | ||
* Converts this array class into a real JavaScript Array. Note that this | ||
* is a "flattened" array and your defined getters and setters won't be invoked | ||
* when you interact with the returned Array. This function will call the | ||
* getter on every array index of the object. | ||
* | ||
* @return {Array} The flattened array | ||
* @api public | ||
*/ | ||
ArrayIndex.prototype.toArray = function toArray () { | ||
var i = 0, l = this.length, array = new Array(l) | ||
for (; i < l; i++) { | ||
array[i] = this[i] | ||
} | ||
return array | ||
} | ||
/** | ||
* Basic support for `JSON.stringify()`. | ||
*/ | ||
ArrayIndex.prototype.toJSON = function toJSON () { | ||
return this.toArray() | ||
} | ||
/** | ||
* toString() override. Use Array.prototype.toString(). | ||
*/ | ||
ArrayIndex.prototype.toString = function toString () { | ||
var a = this.toArray() | ||
return a.toString.apply(a, arguments) | ||
} | ||
/** | ||
* inspect() override. For the REPL. | ||
*/ | ||
ArrayIndex.prototype.inspect = function inspect () { | ||
var a = this.toArray() | ||
Object.keys(this).forEach(function (k) { | ||
a[k] = this[k] | ||
}, this) | ||
return util.inspect(a) | ||
} | ||
/** | ||
* Getter for the "length" property. | ||
@@ -95,3 +143,3 @@ * Returns the value of the "__length" property. | ||
debug('calling Object.defineProperties() with %d entries', num) | ||
Object.defineProperties(ArrayIndex.prototype, desc); | ||
Object.defineProperties(ArrayIndex.prototype, desc) | ||
debug('finished Object.defineProperties()') | ||
@@ -98,0 +146,0 @@ ArrayIndex.prototype.__length__ = length |
@@ -10,3 +10,3 @@ { "name": "array-index" | ||
] | ||
, "version": "0.0.2" | ||
, "version": "0.0.3" | ||
, "author": "Nathan Rajlich <nathan@tootallnate.net> (http://tootallnate.net)" | ||
@@ -13,0 +13,0 @@ , "repository": { "type": "git", "url": "git://github.com/TooTallNate/array-index.git" } |
@@ -72,1 +72,6 @@ | ||
assert.equal(11, a.length) | ||
a[4] = 20 | ||
a[6] = 5.55432 | ||
var b = [0, 1, 2, 3, 80, 5, 33.325919999999996, 7, 8, 9, 30] | ||
assert.equal(JSON.stringify(b), JSON.stringify(a)) |
7726
18.7%197
28.76%