]> Lady’s Gitweb - Pisces/commitdiff
Move isConcatSpreadable[Object] into object.js
authorLady <redacted>
Sun, 19 Nov 2023 16:05:03 +0000 (11:05 -0500)
committerLady <redacted>
Wed, 22 Nov 2023 02:26:59 +0000 (21:26 -0500)
This is an object test which is not *necessarily* about collections.

collection.js
collection.test.js
object.js
object.test.js

index 03326a6cd0de048259206c4310cec6f73d8d2d9e..d340709750c26f1ca902f0ee6d0d8eceae33b483 100644 (file)
@@ -8,6 +8,7 @@
 // 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;
@@ -193,7 +194,6 @@ export const indices = createCallableFunction(
   "indices",
 );
 
-
 /**
  * Returns whether the provided object is a collection.
  *
@@ -215,31 +215,13 @@ export const isCollection = ($) => {
   } 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`.
index 779b4ca506eee40f02c60e2ae4134e254f842362..c0669325f1fc67a87cfd0288ec8a03f9d9b95719 100644 (file)
@@ -16,11 +16,7 @@ import {
   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", () => {
@@ -144,40 +140,3 @@ describe("isCollection", () => {
     );
   });
 });
-
-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,
-    );
-  });
-});
index 9c0c973b6fd11c9bc6b9871035c4081a95a878a4..ee3d51da336d6f1c5a09123cd41b8ec921550f80 100644 (file)
--- a/object.js
+++ b/object.js
@@ -14,6 +14,7 @@ import {
   toFunctionName,
 } from "./function.js";
 import {
+  IS_CONCAT_SPREADABLE,
   ITERATOR,
   SPECIES,
   toLength,
@@ -810,6 +811,32 @@ export const isArraylikeObject = ($) => {
   }
 };
 
+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.
  *
index fc62be345f89921dc088e3402ca9db7fd26001ff..a2e718b189a93ba31179f29dc12b7f9086752f35 100644 (file)
@@ -35,6 +35,7 @@ import {
   hasOwnProperty,
   hasProperty,
   isArraylikeObject,
+  isConcatSpreadableObject,
   isExtensibleObject,
   isUnfrozenObject,
   isUnsealedObject,
@@ -1588,6 +1589,62 @@ describe("isArraylikeObject", () => {
   });
 });
 
+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);
This page took 0.030576 seconds and 4 git commands to generate.