3 ### Invoke getter/setter functions on array-like objects
4 [![Build Status](https://secure.travis-ci.org/TooTallNate/array-index.svg)](http://travis-ci.org/TooTallNate/array-index)
7 This little module provides an `ArrayIndex` constructor function that you can
8 inherit from with your own objects. When a numbered property gets read, then the
9 `ArrayIndex.get` function on the object will be invoked. When a numbered property gets
10 set, then the `ArrayIndex.set` function on the object will be invoked.
19 $ npm install array-index
26 A quick silly example, using `Math.sqrt()` for the "getter":
29 var ArrayIndex = require('array-index');
31 // let's just create a singleton instance.
32 var a = new ArrayIndex();
34 // the "ArrayIndex.get" function is invoked for each "a[n]" access.
35 // it is given a single argument, the "index" currently being accessed.
36 // so here, we're passing in the `Math.sqrt()` function, so accessing
37 // "a[9]" will return `Math.sqrt(9)`.
38 a[ArrayIndex.get] = Math.sqrt;
40 // the "ArrayIndex.get" and "ArrayIndex.set" functions are only invoked up
41 // to "a.length", so we must set that manually.
47 // 1.4142135623730951,
48 // 1.7320508075688772,
52 // 2.6457513110645907,
53 // 2.8284271247461903,
57 Here's an example of creating a subclass of `ArrayIndex` using `util.inherits()`:
60 var ArrayIndex = require('array-index');
61 var inherits = require('util').inherits;
63 function MyArray (length) {
64 // be sure to call the ArrayIndex constructor in your own constructor
65 ArrayIndex.call(this, length);
67 // the "set" object will contain values at indexes previously set,
68 // so that they can be returned in the "getter" function. This is just a
69 // silly example, your subclass will have more meaningful logic.
70 Object.defineProperty(this, 'set', {
71 value: Object.create(null),
76 // inherit from the ArrayIndex's prototype
77 inherits(MyArray, ArrayIndex);
79 MyArray.prototype[ArrayIndex.get] = function (index) {
80 if (index in this.set) return this.set[index];
84 MyArray.prototype[ArrayIndex.set] = function (index, v) {
89 // and now you can create some instances
90 var a = new MyArray(15);
91 a[9] = a[10] = a[14] = '_';
95 // [ 'nate', 2, 4, 6, 8, 10, 12, 14, 16, '_', '_', 22, 24, 26, '_' ]
101 The `ArrayIndex` base class is meant to be subclassed, but it also has a few
102 convenient functions built-in.
104 ### "length" → Number
106 The length of the ArrayIndex instance. The `ArrayIndex.get` and `ArrayIndex.set` functions will
107 only be invoked on the object up to this "length". You may set this length at any
108 time to adjust the amount range where the getters/setters will be invoked.
110 ### "toArray()" → Array
112 Returns a new regular Array instance with the same values that this ArrayIndex
113 class would have. This function calls the `ArrayIndex.get` function repeatedly from
114 `0...length-1` and returns the "flattened" array instance.
116 ### "toJSON()" → Array
118 All `ArrayIndex` instances get basic support for `JSON.stringify()`, which is
119 the same as a "flattened" Array being stringified.
121 ### "toString()" → String
123 The `toString()` override is basically just `array.toArray().toString()`.
125 ### "format()" → String
127 The `inspect()` implementation for the REPL attempts to mimic what a regular
128 Array looks like in the REPL.
136 Copyright (c) 2012 Nathan Rajlich <nathan@tootallnate.net>
138 Permission is hereby granted, free of charge, to any person obtaining
139 a copy of this software and associated documentation files (the
140 'Software'), to deal in the Software without restriction, including
141 without limitation the rights to use, copy, modify, merge, publish,
142 distribute, sublicense, and/or sell copies of the Software, and to
143 permit persons to whom the Software is furnished to do so, subject to
144 the following conditions:
146 The above copyright notice and this permission notice shall be
147 included in all copies or substantial portions of the Software.
149 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
150 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
151 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
152 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
153 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
154 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
155 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.