Skip to content

Commit 680a0d4

Browse files
committed
feat: add slice/base/normalize-multi-slice
1 parent f04c363 commit 680a0d4

File tree

11 files changed

+1247
-0
lines changed

11 files changed

+1247
-0
lines changed
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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+
# normalizeMultiSlice
22+
23+
> Normalize a [`MultiSlice`][@stdlib/slice/multi] object.
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 normalizeMultiSlice = require( '@stdlib/slice/base/normalize-multi-slice' );
41+
```
42+
43+
<a name="main"></a>
44+
45+
#### normalizeMultiSlice( slice, shape, strict )
46+
47+
Normalizes a [`MultiSlice`][@stdlib/slice/multi] object, where `shape` specifies the maximum allowed slice shape.
48+
49+
<!-- eslint-disable stdlib/no-redeclare, no-global-assign -->
50+
51+
```javascript
52+
var Slice = require( '@stdlib/slice/ctor' );
53+
var MultiSlice = require( '@stdlib/slice/multi' );
54+
55+
var shape = [ 10, 10, 10 ];
56+
57+
var s1 = new MultiSlice( new Slice( 2, null, 2 ), null, -4 );
58+
var s2 = normalizeMultiSlice( s1, shape, false );
59+
// returns <MultiSlice>
60+
61+
var d = s2.data;
62+
// returns [ <Slice>, <Slice>, <Slice> ]
63+
64+
var v = d[ 0 ];
65+
// returns <Slice>
66+
67+
var start = v.start;
68+
// returns 2
69+
70+
var stop = v.stop;
71+
// returns 10
72+
73+
var step = v.step;
74+
// returns 2
75+
76+
v = d[ 1 ];
77+
// returns <Slice>
78+
79+
start = v.start;
80+
// returns 0
81+
82+
stop = v.stop;
83+
// returns 10
84+
85+
step = v.step;
86+
// returns 1
87+
88+
v = d[ 2 ];
89+
// returns <Slice>
90+
91+
start = v.start;
92+
// returns 6
93+
94+
stop = v.stop;
95+
// returns 7
96+
97+
step = v.step;
98+
// returns 1
99+
```
100+
101+
When `strict` is `true`, the function returns an error object if an input slice exceeds index bounds.
102+
103+
```javascript
104+
var Slice = require( '@stdlib/slice/ctor' );
105+
var MultiSlice = require( '@stdlib/slice/multi' );
106+
107+
var s1 = new MultiSlice( new Slice( -20, 20, 1 ) );
108+
var s2 = normalizeMultiSlice( s1, [ 10 ], true );
109+
// returns { 'code': 'ERR_OUT_OF_BOUNDS' }
110+
```
111+
112+
A returned error object may have one of the following error codes:
113+
114+
- **ERR_OUT_OF_BOUNDS**: a slice exceeds index bounds.
115+
116+
</section>
117+
118+
<!-- /.usage -->
119+
120+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
121+
122+
<section class="notes">
123+
124+
</section>
125+
126+
<!-- /.notes -->
127+
128+
<!-- Package usage examples. -->
129+
130+
<section class="examples">
131+
132+
## Examples
133+
134+
<!-- eslint no-undef: "error" -->
135+
136+
<!-- eslint-disable new-cap -->
137+
138+
```javascript
139+
var S = require( '@stdlib/slice/ctor' );
140+
var MultiSlice = require( '@stdlib/slice/multi' );
141+
var normalizeMultiSlice = require( '@stdlib/slice/base/normalize-multi-slice' );
142+
143+
var s1 = new MultiSlice( null, S(), -1 );
144+
var s2 = normalizeMultiSlice( s1, [ 5, 10, 7 ], false );
145+
console.log( '%s => %s', s1.toString(), s2.toString() );
146+
147+
s1 = new MultiSlice( null );
148+
s2 = normalizeMultiSlice( s1, [ 5 ], false );
149+
console.log( '%s => %s', s1.toString(), s2.toString() );
150+
151+
s1 = new MultiSlice( S( -1, null, -1 ), 3 );
152+
s2 = normalizeMultiSlice( s1, [ 5, 5 ], false );
153+
console.log( '%s => %s', s1.toString(), s2.toString() );
154+
155+
s1 = new MultiSlice( 2, S( -10, -2, 2 ), 3, null );
156+
s2 = normalizeMultiSlice( s1, [ 10, 10, 10, 10 ], false );
157+
console.log( '%s => %s', s1.toString(), s2.toString() );
158+
159+
s1 = new MultiSlice( S( 1, 20, 2 ), S( null, null, -1 ) );
160+
s2 = normalizeMultiSlice( s1, [ 10, 10 ], false );
161+
console.log( '%s => %s', s1.toString(), s2.toString() );
162+
```
163+
164+
</section>
165+
166+
<!-- /.examples -->
167+
168+
<!-- 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. -->
169+
170+
<section class="references">
171+
172+
</section>
173+
174+
<!-- /.references -->
175+
176+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
177+
178+
<section class="related">
179+
180+
</section>
181+
182+
<!-- /.related -->
183+
184+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
185+
186+
<section class="links">
187+
188+
[@stdlib/slice/multi]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/slice/multi
189+
190+
</section>
191+
192+
<!-- /.links -->

0 commit comments

Comments
 (0)