]> Lady’s Gitweb - Pisces/blobdiff - value.js
Move index string functions back into value.js
[Pisces] / value.js
index 6956b6fc6d45945e52f5234ae43b35ebb3d73da3..341ecdf9dd492653f0397d31317c424c98dc12af 100644 (file)
--- a/value.js
+++ b/value.js
@@ -178,6 +178,25 @@ export const POSITIVE_ZERO = 0;
 /** The undefined primitive. */
 export const UNDEFINED = undefined;
 
+/**
+ * Returns −0 if the provided argument is `"-0"´; returns a number
+ * representing the index if the provided argument is a canonical
+ * numeric index string; otherwise, returns undefined.
+ *
+ * There is no clamping of the numeric index, but note that numbers
+ * above 2^53 − 1 are not safe nor valid integer indices.
+ */
+export const canonicalNumericIndexString = ($) => {
+  if (typeof $ !== "string") {
+    return UNDEFINED;
+  } else if ($ === "-0") {
+    return -0;
+  } else {
+    const n = +$;
+    return $ === `${n}` ? n : UNDEFINED;
+  }
+};
+
 /**
  * Completes the provided property descriptor by setting missing values
  * to their defaults.
@@ -243,6 +262,21 @@ export const completePropertyDescriptor = (Desc) => {
 export const isAccessorDescriptor = (Desc) =>
   Desc !== UNDEFINED && ("get" in Desc || "set" in Desc);
 
+/** Returns whether the provided value is an array index. */
+export const isArrayIndexString = ($) => {
+  const value = canonicalNumericIndexString($);
+  if (value !== UNDEFINED) {
+    // The provided value is a canonical numeric index string.
+    //
+    // Return whether it is in range for array indices.
+    return sameValue(value, 0)
+      || value === toLength(value) && value > 0 && value < -1 >>> 0;
+  } else {
+    // The provided value is not a canonical numeric index string.
+    return false;
+  }
+};
+
 /** Gets whether the provided value is a data descrtiptor. */
 export const isDataDescriptor = (Desc) =>
   Desc !== UNDEFINED && ("value" in Desc || "writable" in Desc);
@@ -266,6 +300,21 @@ export const isGenericDescriptor = (Desc) =>
   && !("get" in Desc || "set" in Desc || "value" in Desc
     || "writable" in Desc);
 
+/** Returns whether the provided value is an integer index string. */
+export const isIntegerIndexString = ($) => {
+  const value = canonicalNumericIndexString($);
+  if (value !== UNDEFINED) {
+    // The provided value is a canonical numeric index string.
+    //
+    // Return whether it is in range for integer indices.
+    return sameValue(value, 0)
+      || value === toLength(value) && value > 0;
+  } else {
+    // The provided value is not a canonical numeric index string.
+    return false;
+  }
+};
+
 export const {
   /**
    * Returns the primitive value of the provided object per its
@@ -420,7 +469,7 @@ export const {
   const { isNaN: isNan } = Number;
   const { is } = Object;
   return {
-    sameValue: (a, b) => is(a, b),
+    sameValue: ($1, $2) => is($1, $2),
     sameValueZero: ($1, $2) => {
       const type1 = type($1);
       const type2 = type($2);
This page took 0.021811 seconds and 4 git commands to generate.