This is an object test which is not *necessarily* about collections.
// file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
import { call, createCallableFunction } from "./function.js";
+import { isConcatSpreadableObject } from "./object.js";
import { toIndex, type } from "./value.js";
const { prototype: arrayPrototype } = Array;
"indices",
);
-
/**
* Returns whether the provided object is a collection.
*
} else {
try {
toIndex($.length); // will throw if `length` is not an index
- return isConcatSpreadable($);
+ return isConcatSpreadableObject($);
} catch {
return false;
}
}
};
-/**
- * Returns whether the provided value is spreadable during array
- * concatenation.
- *
- * This is also used to determine which things should be treated as
- * collections.
- */
-export const isConcatSpreadable = ($) => {
- if (type($) !== "object") {
- // The provided value is not an object.
- return false;
- } else {
- // The provided value is an object.
- const spreadable = $[Symbol.isConcatSpreadable];
- return spreadable !== undefined ? !!spreadable : isArray($);
- }
-};
-
/**
* Returns an iterator over the items in the provided value according
* to the algorithm of `Array::values`.
it,
spy,
} from "./dev-deps.js";
-import {
- findIndexedEntry,
- isCollection,
- isConcatSpreadable,
-} from "./collection.js";
+import { findIndexedEntry, isCollection } from "./collection.js";
describe("findIndexedEntry", () => {
it("[[Call]] returns undefined if no matching entry exists", () => {
);
});
});
-
-describe("isConcatSpreadable", () => {
- it("[[Call]] returns false for primitives", () => {
- assertStrictEquals(isConcatSpreadable("failure"), false);
- });
-
- it("[[Call]] returns false if [Symbol.isConcatSpreadable] is null or false", () => {
- assertStrictEquals(
- isConcatSpreadable(
- Object.assign([], { [Symbol.isConcatSpreadable]: null }),
- ),
- false,
- );
- assertStrictEquals(
- isConcatSpreadable(
- Object.assign([], { [Symbol.isConcatSpreadable]: false }),
- ),
- false,
- );
- });
-
- it("[[Call]] returns true if [Symbol.isConcatSpreadable] is undefined and the object is an array", () => {
- assertStrictEquals(
- isConcatSpreadable(
- Object.assign([], { [Symbol.isConcatSpreadable]: undefined }),
- ),
- true,
- );
- });
-
- it("[[Call]] returns true if [Symbol.isConcatSpreadable] is true", () => {
- assertStrictEquals(
- isConcatSpreadable({ [Symbol.isConcatSpreadable]: true }),
- true,
- );
- });
-});
toFunctionName,
} from "./function.js";
import {
+ IS_CONCAT_SPREADABLE,
ITERATOR,
SPECIES,
toLength,
}
};
+export const {
+ /**
+ * Returns whether the provided value is spreadable during array
+ * concatenation.
+ *
+ * This is also used to determine which things should be treated as
+ * collections.
+ */
+ isConcatSpreadableObject,
+} = (() => {
+ const { isArray } = Array;
+
+ return {
+ isConcatSpreadableObject: ($) => {
+ if (type($) !== "object") {
+ // The provided value is not an object.
+ return false;
+ } else {
+ // The provided value is an object.
+ const spreadable = $[IS_CONCAT_SPREADABLE];
+ return spreadable !== undefined ? !!spreadable : isArray($);
+ }
+ },
+ };
+})();
+
/**
* Returns whether the provided object is extensible.
*
hasOwnProperty,
hasProperty,
isArraylikeObject,
+ isConcatSpreadableObject,
isExtensibleObject,
isUnfrozenObject,
isUnsealedObject,
});
});
+describe("isConcatSpreadableObject", () => {
+ it("[[Call]] returns false for primitives", () => {
+ assertStrictEquals(isConcatSpreadableObject("failure"), false);
+ });
+
+ it("[[Call]] returns false if [Symbol.isConcatSpreadable] is null or false", () => {
+ assertStrictEquals(
+ isConcatSpreadableObject(
+ Object.assign([], { [Symbol.isConcatSpreadable]: null }),
+ ),
+ false,
+ );
+ assertStrictEquals(
+ isConcatSpreadableObject(
+ Object.assign([], { [Symbol.isConcatSpreadable]: false }),
+ ),
+ false,
+ );
+ });
+
+ it("[[Call]] returns true if [Symbol.isConcatSpreadable] is undefined and the object is an array", () => {
+ assertStrictEquals(
+ isConcatSpreadableObject(
+ Object.assign([], { [Symbol.isConcatSpreadable]: undefined }),
+ ),
+ true,
+ );
+ });
+
+ it("[[Call]] returns true if [Symbol.isConcatSpreadable] is true", () => {
+ assertStrictEquals(
+ isConcatSpreadableObject({ [Symbol.isConcatSpreadable]: true }),
+ true,
+ );
+ });
+
+ it("[[Construct]] throws an error", () => {
+ assertThrows(() => new isConcatSpreadableObject({}));
+ });
+
+ describe(".length", () => {
+ it("[[Get]] returns the correct length", () => {
+ assertStrictEquals(isConcatSpreadableObject.length, 1);
+ });
+ });
+
+ describe(".name", () => {
+ it("[[Get]] returns the correct name", () => {
+ assertStrictEquals(
+ isConcatSpreadableObject.name,
+ "isConcatSpreadableObject",
+ );
+ });
+ });
+});
+
describe("isExtensibleObject", () => {
it("[[Call]] returns true for extensible objects", () => {
assertStrictEquals(isExtensibleObject({}), true);