1. object.getownpropertydescriptors
ES2017 spec-compliant shim for `Object.getOwnPropertyDescriptors` that works in ES5.
object.getownpropertydescriptors
Package: object.getownpropertydescriptors
Created by: es-shims
Last modified: Wed, 20 Mar 2024 05:41:59 GMT
Version: 2.1.8
License: MIT
Downloads: 44,621,337
Repository: https://github.com/es-shims/object.getownpropertydescriptors

Install

npm install object.getownpropertydescriptors
yarn add object.getownpropertydescriptors

object.getownpropertydescriptors Version Badge

github actions
coverage
dependency status
dev dependency status
License
Downloads

npm badge

An ES2017 spec-compliant shim for Object.getOwnPropertyDescriptors that works in ES5.
Invoke its "shim" method to shim Object.getOwnPropertyDescriptors if it is unavailable, and if Object.getOwnPropertyDescriptor is available.

This package implements the es-shim API interface. It works in an ES3-supported environment and complies with the spec.

Example

 var getDescriptors = require('object.getownpropertydescriptors');
var assert = require('assert');
var obj = { normal: Infinity };
var enumDescriptor = {
	enumerable: false,
	writable: false,
	configurable: true,
	value: true
};
var writableDescriptor = {
	enumerable: true,
	writable: true,
	configurable: true,
	value: 42
};
var symbol = Symbol();
var symDescriptor = {
	enumerable: true,
	writable: true,
	configurable: false,
	value: [symbol]
};

Object.defineProperty(obj, 'enumerable', enumDescriptor);
Object.defineProperty(obj, 'writable', writableDescriptor);
Object.defineProperty(obj, 'symbol', symDescriptor);

var descriptors = getDescriptors(obj);

assert.deepEqual(descriptors, {
	normal: {
		enumerable: true,
		writable: true,
		configurable: true,
		value: Infinity
	},
	enumerable: enumDescriptor,
	writable: writableDescriptor,
	symbol: symDescriptor
});
 var getDescriptors = require('object.getownpropertydescriptors');
var assert = require('assert');
/* when Object.getOwnPropertyDescriptors is not present */
delete Object.getOwnPropertyDescriptors;
var shimmedDescriptors = getDescriptors.shim();
assert.equal(shimmedDescriptors, getDescriptors);
assert.deepEqual(shimmedDescriptors(obj), getDescriptors(obj));
 var getDescriptors = require('object.getownpropertydescriptors');
var assert = require('assert');
/* when Object.getOwnPropertyDescriptors is present */
var shimmedDescriptors = getDescriptors.shim();
assert.notEqual(shimmedDescriptors, getDescriptors);
assert.deepEqual(shimmedDescriptors(obj), getDescriptors(obj));

Tests

Simply clone the repo, npm install, and run npm test

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code