define-prop
Advanced tools
Comparing version 0.0.3 to 0.0.5
@@ -1,55 +0,43 @@ | ||
// | ||
// define-prop | ||
// | ||
// Easy and type safe custom object property define | ||
// | ||
// MIT License | ||
// | ||
// Copyright (c) 2015 Dennis Raymondo van der Sluis | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
// | ||
// Generated by CoffeeScript 1.10.0 | ||
(function() { | ||
var deepFreeze, defineProp, types, | ||
slice = [].slice; | ||
var types= require( 'types.js' ); | ||
types = require('types.js'); | ||
deepFreeze = require('deep-freeze'); | ||
defineProp = function() { | ||
var descriptor, key, object, settings, value; | ||
object = arguments[0], key = arguments[1], value = arguments[2], settings = 4 <= arguments.length ? slice.call(arguments, 3) : []; | ||
if (!(key = types.forceString(key))) { | ||
defineProp.log('define-prop: error, invalid or missing key!'); | ||
return; | ||
} | ||
if (types.notFunction(object) && (typeof object !== 'object')) { | ||
defineProp.log('define-prop: error, invalid or missing object!'); | ||
return; | ||
} | ||
descriptor = { | ||
enumerable: ~settings.indexOf('enumerable') | ||
}; | ||
if ((types.isObject(value)) && (types.hasFunction(value.get, value.set))) { | ||
descriptor.get = types.forceFunction(value.get); | ||
descriptor.set = types.forceFunction(value.set); | ||
} else { | ||
descriptor.value = value; | ||
descriptor.configurable = ~settings.indexOf('configurable'); | ||
descriptor.writable = ~settings.indexOf('writable'); | ||
if (!descriptor.writable) { | ||
descriptor.value = deepFreeze(descriptor.value); | ||
} | ||
} | ||
Object.defineProperty(object, key, descriptor); | ||
return object; | ||
}; | ||
var defineProperty= function( obj, key, value, settings ){ | ||
defineProp.log = typeof console !== "undefined" && console !== null ? console.log : void 0; | ||
obj = types.forceObject( obj ); | ||
settings = settings || ''; | ||
module.exports = defineProp; | ||
var descriptor= { | ||
enumerable : ~settings.indexOf( 'enumerable' ) | ||
}; | ||
if ( types.isObject(value) ){ | ||
descriptor.get= types.forceFunction( value.get ); | ||
descriptor.set= types.forceFunction( value.set ); | ||
} else { | ||
descriptor.value = value; | ||
descriptor.configurable = ~settings.indexOf( 'configurable' ); | ||
descriptor.writable = ~settings.indexOf( 'writable' ); | ||
} | ||
Object.defineProperty( obj, types.forceString(key), descriptor ); | ||
return obj; | ||
}; | ||
module.exports= defineProperty; | ||
}).call(this); |
@@ -1,1 +0,1 @@ | ||
var types=require("types.js"),defineProperty=function(e,t,r,n){e=types.forceObject(e),n=n||"";var i={enumerable:~n.indexOf("enumerable")};return types.isObject(r)?(i.get=types.forceFunction(r.get),i.set=types.forceFunction(r.set)):(i.value=r,i.configurable=~n.indexOf("configurable"),i.writable=~n.indexOf("writable")),Object.defineProperty(e,types.forceString(t),i),e};module.exports=defineProperty; | ||
(function(){var e,n,o,r=[].slice;o=require("types.js"),e=require("deep-freeze"),n=function(){var i,t,l,u,a;return l=arguments[0],t=arguments[1],a=arguments[2],u=4<=arguments.length?r.call(arguments,3):[],(t=o.forceString(t))?o.notFunction(l)&&"object"!=typeof l?void n.log("define-prop: error, invalid or missing object!"):(i={enumerable:~u.indexOf("enumerable")},o.isObject(a)&&o.hasFunction(a.get,a.set)?(i.get=o.forceFunction(a.get),i.set=o.forceFunction(a.set)):(i.value=a,i.configurable=~u.indexOf("configurable"),i.writable=~u.indexOf("writable"),i.writable||(i.value=e(i.value))),Object.defineProperty(l,t,i),l):void n.log("define-prop: error, invalid or missing key!")},n.log="undefined"!=typeof console&&null!==console?console.log:void 0,module.exports=n}).call(this); |
{ | ||
"name": "define-prop", | ||
"version": "0.0.3", | ||
"version": "0.0.5", | ||
"description": "Easy and type safe custom object property define", | ||
"main": "define-prop.min.js", | ||
"dependencies": { | ||
"types.js": "latest" | ||
"types.js": "1.6.3", | ||
"deep-freeze": "0.0.1" | ||
}, | ||
@@ -9,0 +10,0 @@ "scripts": { |
@@ -6,2 +6,5 @@ #define-prop | ||
- 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 | ||
@@ -16,3 +19,3 @@ | ||
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 argument in a space seperated string. | ||
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. | ||
@@ -23,8 +26,9 @@ | ||
var obj= {}; | ||
// let's 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 now | ||
defineProperty( obj, 'hello', 'world!', 'enumerable' ); | ||
obj.hello= 'into a black hole'; | ||
// 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..'; | ||
@@ -34,11 +38,36 @@ console.log( obj ); | ||
// define a getter and setter as you would with Object.defineProperty | ||
defineProperty( obj, 'count', { | ||
get: () => obj.value | ||
,set: ( value ) => obj.value= value | ||
}); | ||
obj.count= 0; | ||
console.log( ++obj.count ); | ||
// 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? | ||
}; | ||
``` | ||
@@ -51,2 +80,15 @@ | ||
**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** | ||
@@ -53,0 +95,0 @@ |
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
8318
7
100
2
37
+ Addeddeep-freeze@0.0.1
+ Addeddeep-freeze@0.0.1(transitive)
+ Addedtypes.js@1.6.3(transitive)
- Removedtypes.js@2.0.1(transitive)
Updatedtypes.js@1.6.3