]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/node-gyp/node_modules/path-array/node_modules/array-index/README.md
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / node-gyp / node_modules / path-array / node_modules / array-index / README.md
1 array-index
2 ===========
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)
5
6
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.
11
12
13 Installation
14 ------------
15
16 Install with `npm`:
17
18 ``` bash
19 $ npm install array-index
20 ```
21
22
23 Examples
24 --------
25
26 A quick silly example, using `Math.sqrt()` for the "getter":
27
28 ``` js
29 var ArrayIndex = require('array-index');
30
31 // let's just create a singleton instance.
32 var a = new ArrayIndex();
33
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;
39
40 // the "ArrayIndex.get" and "ArrayIndex.set" functions are only invoked up
41 // to "a.length", so we must set that manually.
42 a.length = 10;
43
44 console.log(a);
45 // [ 0,
46 //   1,
47 //   1.4142135623730951,
48 //   1.7320508075688772,
49 //   2,
50 //   2.23606797749979,
51 //   2.449489742783178,
52 //   2.6457513110645907,
53 //   2.8284271247461903,
54 //   3 ]
55 ```
56
57 Here's an example of creating a subclass of `ArrayIndex` using `util.inherits()`:
58
59 ``` js
60 var ArrayIndex = require('array-index');
61 var inherits = require('util').inherits;
62
63 function MyArray (length) {
64   // be sure to call the ArrayIndex constructor in your own constructor
65   ArrayIndex.call(this, length);
66
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),
72     enumerable: false
73   });
74 }
75
76 // inherit from the ArrayIndex's prototype
77 inherits(MyArray, ArrayIndex);
78
79 MyArray.prototype[ArrayIndex.get] = function (index) {
80   if (index in this.set) return this.set[index];
81   return index * 2;
82 };
83
84 MyArray.prototype[ArrayIndex.set] = function (index, v) {
85   this.set[index] = v;
86 };
87
88
89 // and now you can create some instances
90 var a = new MyArray(15);
91 a[9] = a[10] = a[14] = '_';
92 a[0] = 'nate';
93
94 console.log(a);
95 // [ 'nate', 2, 4, 6, 8, 10, 12, 14, 16, '_', '_', 22, 24, 26, '_' ]
96 ```
97
98 API
99 ---
100
101 The `ArrayIndex` base class is meant to be subclassed, but it also has a few
102 convenient functions built-in.
103
104 ### "length" → Number
105
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.
109
110 ### "toArray()" → Array
111
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.
115
116 ### "toJSON()" → Array
117
118 All `ArrayIndex` instances get basic support for `JSON.stringify()`, which is
119 the same as a "flattened" Array being stringified.
120
121 ### "toString()" → String
122
123 The `toString()` override is basically just `array.toArray().toString()`.
124
125 ### "format()" → String
126
127 The `inspect()` implementation for the REPL attempts to mimic what a regular
128 Array looks like in the REPL.
129
130
131 License
132 -------
133
134 (The MIT License)
135
136 Copyright (c) 2012 Nathan Rajlich <nathan@tootallnate.net>
137
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:
145
146 The above copyright notice and this permission notice shall be
147 included in all copies or substantial portions of the Software.
148
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.