UNPKG

6.17 kBMarkdownView Raw
1<a name="Range"></a>
2## Range
3* [new Range(start, end, [step])](#new_Range_new)
4* _instance_
5 * [.size()](#Range+size) ⇒ <code>Array.&lt;number&gt;</code>
6 * [.min()](#Range+min) ⇒ <code>number</code> &#124; <code>undefined</code>
7 * [.max()](#Range+max) ⇒ <code>number</code> &#124; <code>undefined</code>
8 * [.forEach(callback)](#Range+forEach)
9 * [.map(callback)](#Range+map) ⇒ <code>Array</code>
10 * [.toArray()](#Range+toArray) ⇒ <code>Array</code>
11 * [.valueOf()](#Range+valueOf) ⇒ <code>Array</code>
12 * [.format([options])](#Range+format) ⇒ <code>string</code>
13 * [.toString()](#Range+toString) ⇒ <code>string</code>
14 * [.toJSON()](#Range+toJSON) ⇒ <code>Object</code>
15* _static_
16 * [.parse(str)](#Range.parse) ⇒ <code>[Range](#Range)</code> &#124; <code>null</code>
17 * [.fromJSON(json)](#Range.fromJSON) ⇒ <code>[Range](#Range)</code>
18
19<a name="new_Range_new"></a>
20### new Range(start, end, [step])
21Create a range. A range has a start, step, and end, and contains functions
22to iterate over the range.
23
24A range can be constructed as:
25
26```js
27const range = new Range(start, end)
28const range = new Range(start, end, step)
29```
30
31To get the result of the range:
32
33```js
34range.forEach(function (x) {
35 console.log(x)
36})
37range.map(function (x) {
38 return math.sin(x)
39})
40range.toArray()
41```
42
43Example usage:
44
45```js
46const c = new Range(2, 6) // 2:1:5
47c.toArray() // [2, 3, 4, 5]
48const d = new Range(2, -3, -1) // 2:-1:-2
49d.toArray() // [2, 1, 0, -1, -2]
50```
51
52| Param | Type | Description |
53| --- | --- | --- |
54| start | <code>number</code> | included lower bound |
55| end | <code>number</code> | excluded upper bound |
56| [step] | <code>number</code> | step size, default value is 1 |
57
58<a name="Range+size"></a>
59### range.size() ⇒ <code>Array.&lt;number&gt;</code>
60Retrieve the size of the range.
61Returns an array containing one number, the number of elements in the range.
62
63**Kind**: instance method of <code>[Range](#Range)</code>
64**Returns**: <code>Array.&lt;number&gt;</code> - size
65<a name="Range+min"></a>
66### range.min() ⇒ <code>number</code> &#124; <code>undefined</code>
67Calculate the minimum value in the range
68
69**Kind**: instance method of <code>[Range](#Range)</code>
70**Returns**: <code>number</code> &#124; <code>undefined</code> - min
71<a name="Range+max"></a>
72### range.max() ⇒ <code>number</code> &#124; <code>undefined</code>
73Calculate the maximum value in the range
74
75**Kind**: instance method of <code>[Range](#Range)</code>
76**Returns**: <code>number</code> &#124; <code>undefined</code> - max
77<a name="Range+forEach"></a>
78### range.forEach(callback)
79Execute a callback function for each value in the range.
80
81**Kind**: instance method of <code>[Range](#Range)</code>
82
83| Param | Type | Description |
84| --- | --- | --- |
85| callback | <code>function</code> | The callback method is invoked with three parameters: the value of the element, the index of the element, and the Range being traversed. |
86
87<a name="Range+map"></a>
88### range.map(callback) ⇒ <code>Array</code>
89Execute a callback function for each value in the Range, and return the
90results as an array
91
92**Kind**: instance method of <code>[Range](#Range)</code>
93**Returns**: <code>Array</code> - array
94
95| Param | Type | Description |
96| --- | --- | --- |
97| callback | <code>function</code> | The callback method is invoked with three parameters: the value of the element, the index of the element, and the Matrix being traversed. |
98
99<a name="Range+toArray"></a>
100### range.toArray() ⇒ <code>Array</code>
101Create an Array with a copy of the Ranges data
102
103**Kind**: instance method of <code>[Range](#Range)</code>
104**Returns**: <code>Array</code> - array
105<a name="Range+valueOf"></a>
106### range.valueOf() ⇒ <code>Array</code>
107Get the primitive value of the Range, a one dimensional array
108
109**Kind**: instance method of <code>[Range](#Range)</code>
110**Returns**: <code>Array</code> - array
111<a name="Range+format"></a>
112### range.format([options]) ⇒ <code>string</code>
113Get a string representation of the range, with optional formatting options.
114Output is formatted as 'start:step:end', for example '2:6' or '0:0.2:11'
115
116**Kind**: instance method of <code>[Range](#Range)</code>
117**Returns**: <code>string</code> - str
118
119| Param | Type | Description |
120| --- | --- | --- |
121| [options] | <code>Object</code> &#124; <code>number</code> &#124; <code>function</code> | Formatting options. See lib/utils/number:format for a description of the available options. |
122
123<a name="Range+toString"></a>
124### range.toString() ⇒ <code>string</code>
125Get a string representation of the range.
126
127**Kind**: instance method of <code>[Range](#Range)</code>
128<a name="Range+toJSON"></a>
129### range.toJSON() ⇒ <code>Object</code>
130Get a JSON representation of the range
131
132**Kind**: instance method of <code>[Range](#Range)</code>
133**Returns**: <code>Object</code> - Returns a JSON object structured as:
134 `{"mathjs": "Range", "start": 2, "end": 4, "step": 1}`
135<a name="Range.parse"></a>
136### Range.parse(str) ⇒ <code>[Range](#Range)</code> &#124; <code>null</code>
137Parse a string into a range,
138The string contains the start, optional step, and end, separated by a colon.
139If the string does not contain a valid range, null is returned.
140For example str='0:2:11'.
141
142**Kind**: static method of <code>[Range](#Range)</code>
143**Returns**: <code>[Range](#Range)</code> &#124; <code>null</code> - range
144
145| Param | Type |
146| --- | --- |
147| str | <code>string</code> |
148
149<a name="Range.fromJSON"></a>
150### Range.fromJSON(json) ⇒ <code>[Range](#Range)</code>
151Instantiate a Range from a JSON object
152
153**Kind**: static method of <code>[Range](#Range)</code>
154
155| Param | Type | Description |
156| --- | --- | --- |
157| json | <code>Object</code> | A JSON object structured as: `{"mathjs": "Range", "start": 2, "end": 4, "step": 1}` |
158