array-index
Advanced tools
Comparing version
0.9.0 / 2015-12-27 | ||
================== | ||
* add backwards compat logic with deprecate message | ||
* add LICENSE field and entry in package.json | ||
* convert to using es6 Symbols | ||
* remove extraneous debug() calls | ||
* travis: test moar Node.js versions | ||
0.2.0 / 2015-12-02 | ||
@@ -3,0 +12,0 @@ ================== |
176
index.js
@@ -6,5 +6,10 @@ | ||
var util = require('util') | ||
var debug = require('debug')('array-index') | ||
var Symbol = require('es6-symbol'); | ||
var deprecate = require('util-deprecate'); | ||
var debug = require('debug')('array-index'); | ||
var get = Symbol('get'); | ||
var set = Symbol('set'); | ||
var length = Symbol('length'); | ||
/** | ||
@@ -15,3 +20,3 @@ * JavaScript Array "length" is bound to an unsigned 32-bit int. | ||
var MAX_LENGTH = Math.pow(2, 32) | ||
var MAX_LENGTH = Math.pow(2, 32); | ||
@@ -22,3 +27,5 @@ /** | ||
module.exports = ArrayIndex | ||
module.exports = ArrayIndex; | ||
ArrayIndex.get = get; | ||
ArrayIndex.set = set; | ||
@@ -29,3 +36,3 @@ /** | ||
function ArrayIndex (length) { | ||
function ArrayIndex (_length) { | ||
Object.defineProperty(this, 'length', { | ||
@@ -36,13 +43,8 @@ get: getLength, | ||
configurable: true | ||
}) | ||
}); | ||
Object.defineProperty(this, '__length', { | ||
value: 0, | ||
writable: true, | ||
enumerable: false, | ||
configurable: true | ||
}) | ||
this[length] = 0; | ||
if (arguments.length > 0) { | ||
this.length = length | ||
setLength.call(this, _length); | ||
} | ||
@@ -52,17 +54,55 @@ } | ||
/** | ||
* You overwrite the "__get__" function in your subclass. | ||
* You overwrite the "get" Symbol in your subclass. | ||
*/ | ||
ArrayIndex.prototype.__get__ = function () { | ||
throw new Error('you must implement the __get__ function') | ||
} | ||
ArrayIndex.prototype[ArrayIndex.get] = function () { | ||
throw new Error('you must implement the `ArrayIndex.get` Symbol'); | ||
}; | ||
/** | ||
* You overwrite the "__set__" function in your subclass. | ||
* You overwrite the "set" Symbol in your subclass. | ||
*/ | ||
ArrayIndex.prototype.__set__ = function () { | ||
throw new Error('you must implement the __set__ function') | ||
} | ||
ArrayIndex.prototype[ArrayIndex.set] = function () { | ||
throw new Error('you must implement the `ArrayIndex.set` Symbol'); | ||
}; | ||
// XXX: remove for v1.0.0 | ||
var deprecatedGet = deprecate( | ||
function (v) { | ||
if ('function' === typeof v) { | ||
return this[ArrayIndex.get] = v; | ||
} else { | ||
return this[ArrayIndex.get]; | ||
} | ||
}, | ||
'`__get__` is deprecated, use `ArrayIndex.get` Symbol instead' | ||
); | ||
Object.defineProperty(ArrayIndex.prototype, '__get__', { | ||
get: deprecatedGet, | ||
set: deprecatedGet, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
var deprecatedSet = deprecate( | ||
function (v) { | ||
if ('function' === typeof v) { | ||
return this[ArrayIndex.set] = v; | ||
} else { | ||
return this[ArrayIndex.set]; | ||
} | ||
}, | ||
'`__set__` is deprecated, use `ArrayIndex.set` Symbol instead' | ||
); | ||
Object.defineProperty(ArrayIndex.prototype, '__set__', { | ||
get: deprecatedSet, | ||
set: deprecatedSet, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
/** | ||
@@ -79,8 +119,10 @@ * Converts this array class into a real JavaScript Array. Note that this | ||
ArrayIndex.prototype.toArray = function toArray () { | ||
var i = 0, l = this.length, array = new Array(l) | ||
var i = 0; | ||
var l = this.length; | ||
var array = new Array(l); | ||
for (; i < l; i++) { | ||
array[i] = this[i] | ||
array[i] = this[i]; | ||
} | ||
return array | ||
} | ||
return array; | ||
}; | ||
@@ -92,4 +134,4 @@ /** | ||
ArrayIndex.prototype.toJSON = function toJSON () { | ||
return this.toArray() | ||
} | ||
return this.toArray(); | ||
}; | ||
@@ -101,5 +143,5 @@ /** | ||
ArrayIndex.prototype.toString = function toString () { | ||
var a = this.toArray() | ||
return a.toString.apply(a, arguments) | ||
} | ||
var a = this.toArray(); | ||
return a.toString.apply(a, arguments); | ||
}; | ||
@@ -111,28 +153,28 @@ /** | ||
ArrayIndex.prototype.inspect = function inspect () { | ||
var a = this.toArray() | ||
var a = this.toArray(); | ||
Object.keys(this).forEach(function (k) { | ||
a[k] = this[k] | ||
}, this) | ||
return util.inspect(a) | ||
} | ||
a[k] = this[k]; | ||
}, this); | ||
return a; | ||
}; | ||
/** | ||
* Getter for the "length" property. | ||
* Returns the value of the "__length" property. | ||
* Returns the value of the "length" Symbol. | ||
*/ | ||
function getLength () { | ||
debug('getting "length": %o', this.__length) | ||
return this.__length | ||
} | ||
debug('getting "length": %o', this[length]); | ||
return this[length]; | ||
}; | ||
/** | ||
* Setter for the "length" property. | ||
* Calls "ensureLength()", then sets the "__length" property. | ||
* Calls "ensureLength()", then sets the "length" Symbol. | ||
*/ | ||
function setLength (v) { | ||
debug('setting "length": %o', v) | ||
return this.__length = ensureLength(this, v) | ||
} | ||
debug('setting "length": %o', v); | ||
return this[length] = ensureLength(this, v); | ||
}; | ||
@@ -146,25 +188,23 @@ /** | ||
function ensureLength (self, _length) { | ||
var length | ||
if (_length > MAX_LENGTH) { | ||
length = MAX_LENGTH | ||
function ensureLength (self, _newLength) { | ||
var newLength; | ||
if (_newLength > MAX_LENGTH) { | ||
newLength = MAX_LENGTH; | ||
} else { | ||
length = _length | 0 | ||
newLength = _newLength | 0; | ||
} | ||
var proto = Object.getPrototypeOf(self) | ||
var cur = proto.__length__ | 0 | ||
var num = length - cur | ||
var proto = Object.getPrototypeOf(self); | ||
var cur = proto[length] | 0; | ||
var num = newLength - cur; | ||
if (num > 0) { | ||
var desc = {} | ||
debug('creating a descriptor object with %o entries', num) | ||
for (var i = cur; i < length; i++) { | ||
desc[i] = setup(i) | ||
var desc = {}; | ||
debug('creating a descriptor object with %o entries', num); | ||
for (var i = cur; i < newLength; i++) { | ||
desc[i] = setup(i); | ||
} | ||
debug('done creating descriptor object') | ||
debug('calling `Object.defineProperties()` with %o entries', num) | ||
Object.defineProperties(proto, desc) | ||
debug('finished `Object.defineProperties()`') | ||
proto.__length__ = length | ||
debug('calling `Object.defineProperties()` with %o entries', num); | ||
Object.defineProperties(proto, desc); | ||
proto[length] = newLength; | ||
} | ||
return length | ||
return newLength; | ||
} | ||
@@ -181,13 +221,13 @@ | ||
function get () { | ||
return this.__get__(index) | ||
return this[ArrayIndex.get](index); | ||
} | ||
function set (v) { | ||
return this.__set__(index, v) | ||
return this[ArrayIndex.set](index, v); | ||
} | ||
return { | ||
enumerable: true | ||
, configurable: true | ||
, get: get | ||
, set: set | ||
} | ||
enumerable: true, | ||
configurable: true, | ||
get: get, | ||
set: set | ||
}; | ||
} |
@@ -11,3 +11,4 @@ { | ||
], | ||
"version": "0.2.0", | ||
"version": "0.9.0", | ||
"license": "MIT", | ||
"author": "Nathan Rajlich <nathan@tootallnate.net> (http://tootallnate.net)", | ||
@@ -23,3 +24,5 @@ "repository": { | ||
"dependencies": { | ||
"debug": "*" | ||
"debug": "*", | ||
"es6-symbol": "^3.0.2", | ||
"util-deprecate": "^1.0.2" | ||
}, | ||
@@ -26,0 +29,0 @@ "engines": { |
14
test.js
@@ -1,2 +0,1 @@ | ||
var ArrayIndex = require('./') | ||
@@ -22,10 +21,11 @@ var inherits = require('util').inherits | ||
var a = new Arrayish(11) | ||
assert.equal(a.length, 11); | ||
assert.throws(function () { | ||
a[0] | ||
}, /__get__/) | ||
}, /you must implement the `ArrayIndex.get` Symbol/) | ||
assert.throws(function () { | ||
a[0] = 0 | ||
}, /__set__/) | ||
}, /you must implement the `ArrayIndex.set` Symbol/) | ||
@@ -39,3 +39,3 @@ | ||
Arrayish.prototype.__get__ = function get (index) { | ||
Arrayish.prototype[ArrayIndex.get] = function get (index) { | ||
if (index in this.sets) { | ||
@@ -52,3 +52,3 @@ return +this.sets[index] * index | ||
Arrayish.prototype.__set__ = function set (index, value) { | ||
Arrayish.prototype[ArrayIndex.set] = function set (index, value) { | ||
this.sets[index] = value | ||
@@ -99,3 +99,3 @@ } | ||
f.__get__ = function (index) { | ||
f[ArrayIndex.get] = function (index) { | ||
return index * 2; | ||
@@ -109,3 +109,3 @@ }; | ||
f.__set__ = function (index, value) { | ||
f[ArrayIndex.set] = function (index, value) { | ||
this['foo' + index] = value; | ||
@@ -112,0 +112,0 @@ }; |
Sorry, the diff of this file is not supported yet
14901
20.07%11
10%291
13.67%3
200%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added