Skip to content

Commit

Permalink
Refactor: Remove LengthOfArrayLike
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken committed Nov 1, 2021
1 parent 0827464 commit adad0e6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
15 changes: 10 additions & 5 deletions src/Float16Array.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ import {
} from "./_util/primordials.mjs";
import {
IsDetachedBuffer,
LengthOfArrayLike,
SpeciesConstructor,
ToIntegerOrInfinity,
ToLength,
defaultCompare,
} from "./_util/spec.mjs";

Expand Down Expand Up @@ -331,7 +331,7 @@ export class Float16Array {
}
} else { // ArrayLike
list = /** @type {ArrayLike<unknown>} */ (input);
length = LengthOfArrayLike(input);
length = ToLength(list.length);
}
float16bitsArray = ReflectConstruct(NativeUint16Array, [length], new.target);
}
Expand Down Expand Up @@ -421,8 +421,13 @@ export class Float16Array {
length = list.length;
}
} else { // ArrayLike
list = src;
length = LengthOfArrayLike(src);
if (src == null) {
throw NativeTypeError(
CANNOT_CONVERT_UNDEFINED_OR_NULL_TO_OBJECT
);
}
list = NativeObject(src);
length = ToLength(list.length);
}

const array = new Constructor(length);
Expand Down Expand Up @@ -831,7 +836,7 @@ export class Float16Array {
const targetLength = TypedArrayPrototypeGetLength(float16bitsArray);

const src = NativeObject(input);
const srcLength = LengthOfArrayLike(src);
const srcLength = ToLength(src.length);

if (targetOffset === Infinity || srcLength + targetOffset > targetLength) {
throw NativeRangeError(OFFSET_IS_OUT_OF_BOUNDS);
Expand Down
21 changes: 5 additions & 16 deletions src/_util/spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
SymbolSpecies,
} from "./primordials.mjs";

const MAX_SAFE_INTEGER = NativeNumber.MAX_SAFE_INTEGER;

/**
* @see https://tc39.es/ecma262/#sec-tointegerorinfinity
* @param {unknown} target
Expand All @@ -38,28 +40,15 @@ export function ToIntegerOrInfinity(target) {
* @param {unknown} target
* @returns {number}
*/
function ToLength(target) {
export function ToLength(target) {
const length = ToIntegerOrInfinity(target);
if (length < 0) {
return 0;
}

return length < NativeNumber.MAX_SAFE_INTEGER
return length < MAX_SAFE_INTEGER
? length
: NativeNumber.MAX_SAFE_INTEGER;
}

/**
* @see https://tc39.es/ecma262/#sec-lengthofarraylike
* @param {object} arrayLike
* @returns {number}
*/
export function LengthOfArrayLike(arrayLike) {
if (!isObject(arrayLike)) {
throw NativeTypeError(THIS_IS_NOT_AN_OBJECT);
}

return ToLength(/** @type {any} */ (arrayLike).length);
: MAX_SAFE_INTEGER;
}

/**
Expand Down
9 changes: 5 additions & 4 deletions test/Float16Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,10 +501,11 @@ describe("Float16Array", () => {
});

it("input empty or primitive", () => {
assert.doesNotThrow(() => Float16Array.from(0));
assert.doesNotThrow(() => Float16Array.from(4));
assert.doesNotThrow(() => Float16Array.from(-1));
assert.doesNotThrow(() => Float16Array.from(Symbol()));
assert.equalFloat16ArrayValues(Float16Array.from(0), []);
assert.equalFloat16ArrayValues(Float16Array.from(4), []);
assert.equalFloat16ArrayValues(Float16Array.from(-1), []);
assert.equalFloat16ArrayValues(Float16Array.from(Symbol()), []);
assert.equalFloat16ArrayValues(Float16Array.from("123"), [1, 2, 3]);

assert.throws(() => Float16Array.from(), TypeError);
assert.throws(() => Float16Array.from(null), TypeError);
Expand Down

0 comments on commit adad0e6

Please sign in to comment.