Skip to content

Commit c4e751e

Browse files
committed
Add Float16Array#filterReject
1 parent b7a707a commit c4e751e

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

src/Float16Array.mjs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,29 @@ export class Float16Array {
606606
return /** @type {any} */ (array);
607607
}
608608

609+
/** @see https://tc39.es/proposal-array-filtering/ */
610+
filterReject(callback, ...opts) {
611+
assertFloat16Array(this);
612+
const float16bitsArray = getFloat16BitsArray(this);
613+
614+
const length = TypedArrayPrototypeGetLength(float16bitsArray);
615+
const thisArg = opts[0];
616+
617+
const kept = [];
618+
for (let i = 0; i < length; ++i) {
619+
const val = convertToNumber(float16bitsArray[i]);
620+
if (!ReflectApply(callback, thisArg, [val, i, this])) {
621+
ArrayPrototypePush(kept, val);
622+
}
623+
}
624+
625+
const Constructor = SpeciesConstructor(float16bitsArray, Float16Array);
626+
const array = new Constructor(kept);
627+
assertSpeciesTypedArray(array);
628+
629+
return /** @type {any} */ (array);
630+
}
631+
609632
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.reduce */
610633
reduce(callback, ...opts) {
611634
assertFloat16Array(this);

test/Float16Array.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,86 @@ describe("Float16Array", () => {
10161016
});
10171017
});
10181018

1019+
describe("#filterReject()", () => {
1020+
it("property `name` is 'filterReject'", () => {
1021+
assert(Float16Array.prototype.filterReject.name === "filterReject");
1022+
});
1023+
1024+
it("property `length` is 1", () => {
1025+
assert(Float16Array.prototype.filterReject.length === 1);
1026+
});
1027+
1028+
it("check callback arguments", () => {
1029+
const float16 = new Float16Array([1]);
1030+
const thisArg = {};
1031+
1032+
float16.filterReject(function (val, key, f16) {
1033+
assert(val === 1);
1034+
assert(key === 0);
1035+
assert(f16 === float16);
1036+
assert(this === thisArg);
1037+
}, thisArg);
1038+
});
1039+
1040+
it("reject even value", () => {
1041+
const float16_1 = new Float16Array([1, 2, 3, 4]);
1042+
const float16_2 = float16_1.filterReject((val) => val % 2 === 0);
1043+
1044+
assert(float16_2 instanceof Float16Array);
1045+
assert.equalFloat16ArrayValues(float16_2, [1, 3]);
1046+
});
1047+
1048+
it("respect @@species", () => {
1049+
let step = 0;
1050+
class Foo extends Float16Array {
1051+
constructor(...args) {
1052+
super(...args);
1053+
if (step === 0) {
1054+
assert(args.length === 1);
1055+
assert.deepStrictEqual(args[0], [1, 2, 3, 4]);
1056+
++step;
1057+
} else {
1058+
assert(args.length === 1);
1059+
assert.deepStrictEqual(args[0], [1, 2, 3, 4]);
1060+
}
1061+
}
1062+
}
1063+
const foo = new Foo([1, 2, 3, 4]).filterReject(() => false);
1064+
assert(foo instanceof Foo);
1065+
assert.equalFloat16ArrayValues(foo, [1, 2, 3, 4]);
1066+
1067+
class Bar extends Float16Array {
1068+
static get [Symbol.species]() {
1069+
return Float16Array;
1070+
}
1071+
}
1072+
const bar = new Bar([1, 2, 3, 4]).filterReject(() => false);
1073+
assert(!(bar instanceof Bar));
1074+
assert(bar instanceof Float16Array);
1075+
assert.equalFloat16ArrayValues(bar, [1, 2, 3, 4]);
1076+
});
1077+
1078+
it("SpeciesConstructor must return a TypedArray of the same Content Type", function () {
1079+
class Foo extends Float16Array {
1080+
static get [Symbol.species]() {
1081+
return Array;
1082+
}
1083+
}
1084+
const foo = new Foo([1, 2, 3, 4]);
1085+
assert.throws(() => foo.filterReject(() => true), TypeError);
1086+
1087+
if (typeof BigUint64Array !== "undefined") {
1088+
class Bar extends Float16Array {
1089+
static get [Symbol.species]() {
1090+
return BigUint64Array;
1091+
}
1092+
}
1093+
const bar = new Bar([1, 2, 3, 4]);
1094+
assert.throws(() => bar.filterReject(() => true), TypeError);
1095+
}
1096+
});
1097+
});
1098+
10191099
describe("#reduce()", () => {
10201100
it("property `name` is 'reduce'", () => {
10211101
assert(Float16Array.prototype.reduce.name === "reduce");

0 commit comments

Comments
 (0)