Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

define-prop

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

define-prop

Easy and type safe custom object property define

  • 0.0.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

#define-prop

###Easy and type safe custom object property define

  • object descriptors in a space seperated string for fast inline value creation
  • deep freezes non-writable object values
  • doesn't throw, but logs on invalid input
  • supports custom log so you can throw yourself if needed
  • dynamically type checked

###Usage:

<object> defineProperty( <object> obj, <string>/<number> key, <any type> value, <string> descriptorNames )

Default, the enumerable, configurable and writable descriptor values are set to false, which is the default behaviour of Object.defineProperty. You can set them individually to true by passing their names as last arguments or in a space seperated string.

var defineProp= require( 'define-prop' );


var obj= {};

// define obj.hello and give it the value 'world!', it will be
// immutable by default, we only set enumerable to be able to log it
defineProp( obj, 'hello', 'world!', 'enumerable' );
obj.hello= 'cannot be set..';

console.log( obj );
//	{ hello: 'world!' }


// define a getter and setter similar to native Object.defineProperty
defineProp( obj, 'count', ( () => {
	var count= 0;
	return {
		 get: () => count
		,set: ( value ) => count= value
	};
})(), 'enumerable' );

obj.count++;
console.log( obj.count );
// 1


// define an object to be deeply immutable
defineProp( obj, 'protected', {
	 notWritable	: 'this is a frozen object'
	,test			: true
}, 'enumerable' );

obj.protected.cannotAssign	= 'when "writable" is not set';
obj.protected.test			= 'futile';

console.log( obj );
// { hello: 'world!',
//   count: [Getter/Setter],
//   protected: { notWritable: 'this is a frozen object', test: true } }


// have custom input error handler
defineProp.log= ( err ) => {
	// do something with err?
};

change log

--

0.0.5

  • adds support for making object values deeply immutable if descriptor is not set to 'writable'
  • adds optional input error logging (default on)

--

0.0.4

  • adds support for defining a non-getter/setter object

--

0.0.3

  • changed license to MIT

--

###license

MIT

Keywords

FAQs

Package last updated on 25 Sep 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc