New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

parkplace

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

parkplace

A clean wrapper around the Object.defineProperty method with some convenient magic. It's called parkplace 'cause that's a good definition of a property.

latest
Source
npmnpm
Version
0.1.0
Version published
Maintainers
1
Created
Source

parkplace

A property definer. (parkplace is a good definition of a property.)

npm install parkplace --save

At a base level, parkplace is a wrapper around the ES5 Object.defineProperty function (it will fall back to simply assigning properties in a pre-ES5 environment).

# coffeescript:
pp = require 'parkplace'
someObjectToDefinePropertiesOn = {}
pp.define 'someProp', 'someValue', {
    enumerable: true
    writable: true
    configurable: true
}, someObjectToDefinePropertiesOn

# javascript:
pp = require('parkplace');
someObjectToDefinePropertiesOn = {};
pp.define('someProperty', 100, {
    enumerable: true,
    writable: true,
    configurable: true
}, someObjecToDefinePropertiesOn);

by calling scope, we get a copy of the base parkplace with some additional convenience methods (get and has) but we no longer have to specify which object we are defining properties on (the last parameter of define, above):

# coffeescript:
scoped = pp.scope someObject
# (I like to use a triple-underscore, but you do you.)
___ = pp.scope someObject
___.define 'someProp', 'someValue', {
    enumerable: true
    writable: true
    configurable: true
}
# the last parameter can now be omitted (as compared to the earlier example, above)

# javascript
___ = pp.scope(someObject);
___.define('someProp', 'someValue', {
    enumerable: true,
    writable: true,
    configurable: true
}/* omitted param would go here */);

In addition to the base define function, there are nearly the permutations for the the second-to-last parameter of define above:

  • mutable - enumerable, writable, configurable (essentially an alias of define)
  • secret - non-enumerable, writable, non-configurable
  • open - enumerable, non-writable, configurable
  • guarded - non-enumerable, non-writable, configurable
  • readable - enumerable, non-writable, non-configurable
  • writable - enumerable, writable, non-configurable
  • constant - non-enumerable, non-writable, non-configurable

(I didn't see much purpose in a non-enumerable, writable, configurable property, as it feels like a secret API, but perhaps one can be added in the future. And if you want a truly private context, use hidden below.)

So, all together now:

# coffeescript
"use strict"
someObject = {}
# let's make a definer:
____ = require('./lib/parkplace').scope someObject
____.constant 'PI', Math.PI
console.log someObject.PI is Math.PI       # prints true
____.secret 'license', "KFBR392"
console.log Object.keys someObject         # prints []
____.open 'name', 'publizity'
console.log Object.keys someObject         # prints ['name']
# maybe in some other file, down the line
____.mutable 'license', "somenewvalue"   # throws TypeError: Cannot redefine property: license

# javascript
"use strict";
var someObject = {};
var ____ = require('./lib/parkplace').scope(someObject);
____.constant('PI', Math.PI);
console.log(someObject.PI === Math.PI);    // prints true
____.secret('license', "KFBR392");
console.log(Object.keys(someObject));      // prints []
____.open('name', 'publizity');          
console.log(Object.keys(someObject));      // prints ['name']
____.mutable('license', "somenewvalue"); // throws TypeError: Cannot redefine property: license

The scoped definer also has the convenience methods has which returns a boolean (true if property is defined) and get, which returns the matched value or null:

console.log ___.has 'license'                        # prints true
console.log ___.get 'license'                        # prints 'KFBR392'

console.log ___.get('license') is someObject.license # prints true

Keywords

object

FAQs

Package last updated on 30 Mar 2015

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