Skip to content

Commit 050f19a

Browse files
committed
add some info about asIndex et al
1 parent c6615ba commit 050f19a

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

breakingchanges.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,9 @@
171171
- `getSetDict`
172172

173173
**`Sk.misceval.`**
174-
- `asIndexOrThrow`
174+
- `asIndex` - will return the internal representation of the integer - or undefined if tho indexable - could be a number or a bigint (JSBI) only used
175+
- `asIndexOrThrow` - does `asIndex` but throws an error if the number is not indexable - with an optional message parameter.
176+
- `asIndexSized` - throws an error if the object is not indexable, returns a Number always, Option to throw an error if the index is larger than `Number.MAX_SAFE_INTEGER`. This is the goto method for most buitins now.
175177
- `Iterator` - a python class that easily wraps an iterator
176178
- `arrayFromIterable` - optional canSuspend implementation that returns an array from a python iterator
177179

src/misceval.js

+22
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,30 @@ function asIndexOrThrow(index, msg) {
9797
throw new Sk.builtin.TypeError(msg);
9898
}
9999

100+
/**
101+
*
102+
* @param {*} index
103+
*
104+
* @description
105+
* will return an integer javascript number
106+
* if the value is larger than Number.MAX_SAFE_INTEGER will return a BigInt
107+
* if the object passed is not a valid indexable object then it will return undefined
108+
* if you want to throw an error instead of returning undefined use {@link Sk.misceval.asIndexOrThrow}
109+
* If you know you want a number and not a BigInt - use {@link Sk.misceval.asIndexSized}
110+
*/
100111
Sk.misceval.asIndex = asIndex;
101112

113+
114+
/**
115+
*
116+
* @param {*} index
117+
* @param {Sk.builtin.Exception=} Err provided an excption type if you wish to throw an exception
118+
* @param {string} msg an option message if the index passed is not a valid indexable object
119+
*
120+
* @description
121+
* this function will always return a `Number` whose size is less than `Number.MAX_SAFE_INTEGER`
122+
* If you provide an err then this function will throw an error if the index is larger than `Number.MAX_SAFE_INTEGER`
123+
*/
102124
Sk.misceval.asIndexSized = function (index, Err, msg) {
103125
const i = asIndexOrThrow(index, msg);
104126
if (typeof i === "number") {

0 commit comments

Comments
 (0)