You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lib/node_modules/@stdlib/slice/base/seq2slice/README.md
+43-30Lines changed: 43 additions & 30 deletions
Original file line number
Diff line number
Diff line change
@@ -42,12 +42,12 @@ var seq2slice = require( '@stdlib/slice/base/seq2slice' );
42
42
43
43
<aname="main"></a>
44
44
45
-
#### seq2slice( str, len )
45
+
#### seq2slice( str, len, strict )
46
46
47
47
Converts a subsequence string to a [`Slice`][@stdlib/slice/ctor] object, where `len` specifies the maximum number of elements allowed in the slice.
48
48
49
49
```javascript
50
-
var s =seq2slice( ':5', 10 );
50
+
var s =seq2slice( ':5', 10, false );
51
51
// returns <Slice>
52
52
53
53
var v =s.start;
@@ -77,7 +77,7 @@ where
77
77
- The `end` keyword resolves to the provided length `len`. Thus, `:-1` is equivalent to `:end-1`, `:-2` is equivalent to `:end-2`, and so on and so forth. The exception is when performing a division operation when the `increment` is less than zero; in which case, `end` is equal to `len-1` in order to preserve user expectations when `end/d` equals a whole number and slicing from right-to-left. The result from a division operation is **rounded down** to the nearest integer value.
78
78
79
79
```javascript
80
-
var s =seq2slice( 'end:2:-1', 10 );
80
+
var s =seq2slice( 'end:2:-1', 10, false );
81
81
// returns <Slice>
82
82
83
83
var v =s.start;
@@ -89,7 +89,7 @@ v = s.stop;
89
89
v =s.step;
90
90
// returns -1
91
91
92
-
s =seq2slice( 'end-2:2:-1', 10 );
92
+
s =seq2slice( 'end-2:2:-1', 10, false );
93
93
// returns <Slice>
94
94
95
95
v =s.start;
@@ -101,7 +101,7 @@ v = s.stop;
101
101
v =s.step;
102
102
// returns -1
103
103
104
-
s =seq2slice( 'end/2:2:-1', 10 );
104
+
s =seq2slice( 'end/2:2:-1', 10, false );
105
105
// returns <Slice>
106
106
107
107
v =s.start;
@@ -114,13 +114,26 @@ v = s.step;
114
114
// returns -1
115
115
```
116
116
117
-
The function returns `null` if provided in invalid subsequence string.
117
+
The function returns an error object if provided in invalid subsequence string.
118
118
119
119
```javascript
120
-
var s =seq2slice( '1:2:3:4', 10 );
121
-
// returns null
120
+
var s =seq2slice( '1:2:3:4', 10, false );
121
+
// returns { 'code': 'ERR_INVALID_SUBSEQUENCE' }
122
122
```
123
123
124
+
When `strict` is `true`, the function returns an error object if a subsequence string resolves to a slice exceeding index bounds.
125
+
126
+
```javascript
127
+
var s =seq2slice( '10:20', 10, true );
128
+
// returns { 'code': 'ERR_OUT_OF_BOUNDS' }
129
+
```
130
+
131
+
A returned error object may have one of the following error codes:
132
+
133
+
-**ERR_INVALID_SUBSEQUENCE**: a subsequence string is invalid.
134
+
-**ERR_INVALID_INCREMENT**: a subsequence string must have a non-zero increment.
135
+
-**ERR_OUT_OF_BOUNDS**: a subsequence string resolves to a slice exceeding index bounds.
136
+
124
137
</section>
125
138
126
139
<!-- /.usage -->
@@ -132,8 +145,8 @@ var s = seq2slice( '1:2:3:4', 10 );
132
145
## Notes
133
146
134
147
- When `len` is zero, the function always returns a Slice object equivalent to `0:0:<increment>`.
135
-
-The resolved slice start is clamped to the slice index bounds (i.e., `[0, len)`).
136
-
-The resolved slice end is upper bound clamped to `len` (i.e., one greater than the last possible index).
148
+
-When `strict` is `false`, the resolved slice start is clamped to the slice index bounds (i.e., `[0, len)`).
149
+
-When `strict` is `false`, the resolved slice end is upper bound clamped to `len` (i.e., one greater than the last possible index).
137
150
- When the increment is negative, the resolved slice end value may be `null`, thus indicating that a non-empty slice should include the first index.
138
151
- The function ensures that results satisfy the convention that `:n` combined with `n:` is equivalent to `:` (i.e., selecting all elements). This convention matches Python slice semantics, but diverges from the MATLAB convention where `:n` and `n:` overlap by one element.
139
152
- Unlike MATLAB, but like Python, the subsequence string is upper-bound exclusive. For example, in Python, `0:2` corresponds to the sequence `{0,1}`. In MATLAB, `1:3` corresponds to `{1,2,3}`.
@@ -153,83 +166,83 @@ var s = seq2slice( '1:2:3:4', 10 );
153
166
```javascript
154
167
var seq2slice =require( '@stdlib/slice/base/seq2slice' );
* Converts a subsequence string to a Slice object.
27
42
*
@@ -43,24 +58,27 @@ import { Slice } from '@stdlib/types/slice';
43
58
* - Both `start` and `stop` can use the `end` keyword (e.g., `end-2::2`, `end-3:`, etc), which supports basic subtraction and division.
44
59
* - The `end` keyword resolves to the provided length `len`. Thus, `:-1` is equivalent to `:end-1`, `:-2` is equivalent to `:end-2`, and so on and so forth. The exception is when performing a division operation when the `increment` is less than zero; in which case, `end` is equal to `len-1` in order to preserve user expectations when `end/d` equals a whole number and slicing from right-to-left. The result from a division operation is **rounded down** to the nearest integer value.
45
60
*
46
-
* - The resolved slice start is clamped to the slice index bounds (i.e., `[0, len)`).
61
+
* - When `strict` is `false`, the resolved slice start is clamped to the slice index bounds (i.e., `[0, len)`).
47
62
*
48
-
* - The resolved slice end is upper bound clamped to `len` (i.e., one greater than the last possible index).
63
+
* - When `strict` is `false`, the resolved slice end is upper bound clamped to `len` (i.e., one greater than the last possible index).
49
64
*
50
65
* - When the increment is negative, the resolved slice end value may be `null`, thus indicating that a non-empty slice should include the first index.
51
66
*
52
67
* - The function ensures that results satisfy the convention that `:n` combined with `n:` is equivalent to `:` (i.e., selecting all elements).
53
68
*
54
69
* - When `len` is zero, the function always returns a Slice object equivalent to `0:0:<increment>`.
55
70
*
56
-
* - The function returns `null` if provided in invalid subsequence string.
71
+
* - The function returns an error object if provided in invalid subsequence string.
72
+
*
73
+
* - If `strict` is `true`, the function returns an error object if provided a subsequence string which exceeds index bounds.
57
74
*
58
75
* @param str - input string
59
76
* @param len - maximum number of elements allowed in the slice
0 commit comments