]> Lady’s Gitweb - Pisces/blobdiff - object.js
Add methods for own entries and values to object.js
[Pisces] / object.js
index bd310ca87946e2990a2fb2a5731198c1f6f043fa..3221e1da360305d4da5fa5582355cee6cf896e03 100644 (file)
--- a/object.js
+++ b/object.js
@@ -10,6 +10,7 @@
 import {
   IS_CONCAT_SPREADABLE,
   isAccessorDescriptor,
+  isDataDescriptor,
   SPECIES,
   toFunctionName,
   toLength,
@@ -17,6 +18,7 @@ import {
   UNDEFINED,
 } from "./value.js";
 
+const createArray = Array;
 const { isArray } = Array;
 const object = Object;
 const {
@@ -357,6 +359,8 @@ export const getOwnPropertyDescriptors = (O) => {
   const keys = ownKeys(obj);
   const descriptors = {};
   for (let k = 0; k < keys.length; ++k) {
+    // Iterate over the keys of the provided object and collect its
+    // descriptors.
     const key = keys[k];
     defineOwnDataProperty(
       descriptors,
@@ -368,7 +372,29 @@ export const getOwnPropertyDescriptors = (O) => {
 };
 
 /**
- * Returns an array of property keys on the provided value.
+ * Returns an array of own property entries on the provided value,
+ * using the provided receiver if given.
+ */
+export const getOwnPropertyEntries = (O, Receiver = O) => {
+  const obj = toObject(O);
+  const keys = ownKeys(obj);
+  const target = Receiver === UNDEFINED ? obj : toObject(Receiver);
+  const result = createArray(keys.length);
+  for (let k = 0; k < keys.length; ++k) {
+    // Iterate over each key and add the corresponding entry to the
+    // result.
+    const key = keys[k];
+    defineOwnDataProperty(
+      result,
+      k,
+      [key, getOwnPropertyValue(obj, keys[k], target)],
+    );
+  }
+  return result;
+};
+
+/**
+ * Returns an array of own property keys on the provided value.
  *
  * ※ This is effectively an alias for `Reflect.ownKeys`, except that
  * it does not require that the argument be an object.
@@ -397,6 +423,53 @@ export const getOwnPropertyStrings = (O) => getOwnPropertyNames(O);
 export const getOwnPropertySymbols = (O) =>
   objectGetOwnPropertySymbols(O);
 
+/**
+ * Returns the value of the provided own property on the provided
+ * value using the provided receiver, or undefined if the provided
+ * property is not an own property on the provided value.
+ *
+ * ※ If the receiver is not provided, it defaults to the provided
+ * value.
+ */
+export const getOwnPropertyValue = (O, P, Receiver = UNDEFINED) => {
+  const obj = toObject(O);
+  const desc = getOwnPropertyDescriptor(O, P);
+  if (desc === UNDEFINED) {
+    // The provided property is not an own property on the provided
+    // value; return undefined.
+    return UNDEFINED;
+  }
+  if (isDataDescriptor(desc)) {
+    // The provided property is a data property; return its value.
+    return desc.value;
+  } else {
+    // The provided property is an accessor property; get its value
+    // using the appropriate receiver.
+    const target = Receiver === UNDEFINED ? obj : toObject(Receiver);
+    return call(desc.get, target, []);
+  }
+};
+
+/**
+ * Returns an array of own property values on the provided value, using
+ * the provided receiver if given.
+ */
+export const getOwnPropertyValues = (O, Receiver = O) => {
+  const obj = toObject(O);
+  const keys = ownKeys(obj);
+  const target = Receiver === UNDEFINED ? obj : toObject(Receiver);
+  const result = createArray(keys.length);
+  for (let k = 0; k < keys.length; ++k) {
+    // Iterate over each key and collect the values.
+    defineOwnDataProperty(
+      result,
+      k,
+      getOwnPropertyValue(obj, keys[k], target),
+    );
+  }
+  return result;
+};
+
 /**
  * Returns the value of the provided property key on the provided
  * value.
This page took 0.024058 seconds and 4 git commands to generate.