Skip to content

Commit 02c09a3

Browse files
committed
feat: add slice/ctor
1 parent 775de05 commit 02c09a3

11 files changed

Lines changed: 3241 additions & 0 deletions

File tree

Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# Slice
22+
23+
> Slice constructor.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var Slice = require( '@stdlib/slice/ctor' );
41+
```
42+
43+
<a name="main"></a>
44+
45+
#### Slice( \[start, ]stop\[, step] )
46+
47+
Returns a `Slice` instance.
48+
49+
```javascript
50+
var s = new Slice( 0, 10, 1 );
51+
// returns <Slice>
52+
```
53+
54+
The constructor has the following parameters:
55+
56+
- **start**: starting index (inclusive). Default: `null`.
57+
- **stop**: ending index (exclusive).
58+
- **step**: index increment. Default: `null`.
59+
60+
Slice arguments may be either integers or `null`, where the latter indicates a slice parameter which should be determined based on the slice context (e.g., when used to index into an [`ndarray`][@stdlib/ndarray/ctor]).
61+
62+
* * *
63+
64+
### Properties
65+
66+
<a name="static-prop-name"></a>
67+
68+
#### Slice.name
69+
70+
String value of the `Slice` constructor name.
71+
72+
```javascript
73+
var str = Slice.name;
74+
// returns 'Slice'
75+
```
76+
77+
<a name="prop-start"></a>
78+
79+
#### Slice.prototype.start
80+
81+
**Read-only** property returning the slice starting index.
82+
83+
```javascript
84+
var s = new Slice( 10 );
85+
// returns <Slice>
86+
87+
var start = s.start;
88+
// returns null
89+
90+
s = new Slice( 2, 10 );
91+
// returns <Slice>
92+
93+
start = s.start;
94+
// returns 2
95+
```
96+
97+
<a name="prop-stop"></a>
98+
99+
#### Slice.prototype.stop
100+
101+
**Read-only** property returning the slice ending index.
102+
103+
<!-- eslint-disable no-global-assign, stdlib/no-redeclare -->
104+
105+
```javascript
106+
var s = new Slice( null );
107+
// returns <Slice>
108+
109+
var stop = s.stop;
110+
// returns null
111+
112+
s = new Slice( 10 );
113+
// returns <Slice>
114+
115+
stop = s.stop;
116+
// returns 10
117+
118+
s = new Slice( 2, 10 );
119+
// returns <Slice>
120+
121+
stop = s.stop;
122+
// returns 10
123+
```
124+
125+
<a name="prop-step"></a>
126+
127+
#### Slice.prototype.step
128+
129+
**Read-only** property returning the slice index increment.
130+
131+
```javascript
132+
var s = new Slice( 10 );
133+
// returns <Slice>
134+
135+
var step = s.step;
136+
// returns null
137+
138+
s = new Slice( 2, 10 );
139+
// returns <Slice>
140+
141+
step = s.step;
142+
// returns null
143+
144+
s = new Slice( 2, 10, 1 );
145+
// returns <Slice>
146+
147+
step = s.step;
148+
// returns 1
149+
```
150+
151+
* * *
152+
153+
### Methods
154+
155+
<a name="method-to-string"></a>
156+
157+
#### Slice.prototype.toString()
158+
159+
Serializes a `Slice` as a string.
160+
161+
```javascript
162+
var s = new Slice( 10 );
163+
// returns <Slice>
164+
165+
var str = s.toString();
166+
// returns 'Slice(null,10,null)'
167+
168+
s = new Slice( 2, 10, 1 );
169+
// returns <Slice>
170+
171+
str = s.toString();
172+
// returns 'Slice(2,10,1)'
173+
```
174+
175+
<a name="method-to-json"></a>
176+
177+
#### ndarray.prototype.toJSON()
178+
179+
Serializes a `Slice` as a [JSON][json] object.
180+
181+
```javascript
182+
var s = new Slice( 10 );
183+
// returns <Slice>
184+
185+
var o = s.toJSON();
186+
// returns { 'type': 'Slice', 'data': [ null, 10, null ] }
187+
188+
s = new Slice( 2, 10, 1 );
189+
// returns <Slice>
190+
191+
o = s.toJSON();
192+
// returns { 'type': 'Slice', 'data': [ 2, 10, 1 ] }
193+
```
194+
195+
`JSON.stringify()` implicitly calls this method when stringifying a `Slice` instance.
196+
197+
</section>
198+
199+
<!-- /.usage -->
200+
201+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
202+
203+
* * *
204+
205+
<section class="notes">
206+
207+
## Notes
208+
209+
- Slice instances have no explicit functionality; however, they are used by [`ndarray`][@stdlib/ndarray] and other packages for creating views into multi-dimensional data structures.
210+
211+
</section>
212+
213+
<!-- /.notes -->
214+
215+
<!-- Package usage examples. -->
216+
217+
<section class="examples">
218+
219+
* * *
220+
221+
## Examples
222+
223+
<!-- eslint no-undef: "error" -->
224+
225+
<!-- eslint-disable no-global-assign, stdlib/no-redeclare -->
226+
227+
```javascript
228+
var Slice = require( '@stdlib/slice/ctor' );
229+
230+
var s = new Slice( 9, -10, -1 );
231+
// returns <Slice>
232+
233+
var start = s.start;
234+
console.log( 'Start: %d', start );
235+
// => 'Start: 9'
236+
237+
var stop = s.stop;
238+
console.log( 'Stop: %d', stop );
239+
// => 'Stop: -10'
240+
241+
var step = s.step;
242+
console.log( 'Step: %d', step );
243+
// => 'Step: -1'
244+
245+
var str = s.toString();
246+
console.log( str );
247+
// => 'Slice(9,-10,-1)'
248+
249+
var o = s.toJSON();
250+
console.log( JSON.stringify( o ) );
251+
// => '{"type":"Slice","data":[9,-10,-1]}'
252+
```
253+
254+
</section>
255+
256+
<!-- /.examples -->
257+
258+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
259+
260+
<section class="references">
261+
262+
</section>
263+
264+
<!-- /.references -->
265+
266+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
267+
268+
<section class="related">
269+
270+
</section>
271+
272+
<!-- /.related -->
273+
274+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
275+
276+
<section class="links">
277+
278+
[json]: http://www.json.org/
279+
280+
[@stdlib/ndarray]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray
281+
282+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
283+
284+
</section>
285+
286+
<!-- /.links -->

0 commit comments

Comments
 (0)