├── .gitignore ├── .jshintrc ├── .travis.yml ├── LICENSE.md ├── README.md ├── package.json ├── prr.js └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "predef": [ ] 3 | , "bitwise": false 4 | , "camelcase": false 5 | , "curly": false 6 | , "eqeqeq": false 7 | , "forin": false 8 | , "immed": false 9 | , "latedef": false 10 | , "newcap": true 11 | , "noarg": true 12 | , "noempty": true 13 | , "nonew": true 14 | , "plusplus": false 15 | , "quotmark": true 16 | , "regexp": false 17 | , "undef": true 18 | , "unused": true 19 | , "strict": false 20 | , "trailing": true 21 | , "maxlen": 120 22 | , "asi": true 23 | , "boss": true 24 | , "debug": true 25 | , "eqnull": true 26 | , "es5": true 27 | , "esnext": true 28 | , "evil": true 29 | , "expr": true 30 | , "funcscope": false 31 | , "globalstrict": false 32 | , "iterator": false 33 | , "lastsemic": true 34 | , "laxbreak": true 35 | , "laxcomma": true 36 | , "loopfunc": true 37 | , "multistr": false 38 | , "onecase": false 39 | , "proto": false 40 | , "regexdash": false 41 | , "scripturl": true 42 | , "smarttabs": false 43 | , "shadow": false 44 | , "sub": true 45 | , "supernew": false 46 | , "validthis": true 47 | , "browser": true 48 | , "couch": false 49 | , "devel": false 50 | , "dojo": false 51 | , "mootools": false 52 | , "node": true 53 | , "nonstandard": true 54 | , "prototypejs": false 55 | , "rhino": false 56 | , "worker": true 57 | , "wsh": false 58 | , "nomen": false 59 | , "onevar": true 60 | , "passfail": false 61 | } -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 0.8 4 | - "0.10" 5 | branches: 6 | only: 7 | - master 8 | notifications: 9 | email: 10 | - rod@vagg.org -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | ===================== 3 | 4 | Copyright (c) 2014 Rod Vagg 5 | --------------------------- 6 | 7 | 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: 8 | 9 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 10 | 11 | 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. 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # prr [![Build Status](https://secure.travis-ci.org/rvagg/prr.png)](http://travis-ci.org/rvagg/prr) 2 | 3 | An sensible alternative to `Object.defineProperty()`. Available in npm and Ender as **prr**. 4 | 5 | ## Usage 6 | 7 | Set the property `'foo'` (`obj.foo`) to have the value `'bar'` with default options (`'enumerable'`, `'configurable'` and `'writable'` are all `false`): 8 | 9 | ```js 10 | prr(obj, 'foo', 'bar') 11 | ``` 12 | 13 | Adjust the default options: 14 | 15 | ```js 16 | prr(obj, 'foo', 'bar', { enumerable: true, writable: true }) 17 | ``` 18 | 19 | Do the same operation for multiple properties: 20 | 21 | ```js 22 | prr(obj, { one: 'one', two: 'two' }) 23 | // or with options: 24 | prr(obj, { one: 'one', two: 'two' }, { enumerable: true, writable: true }) 25 | ``` 26 | 27 | ### Simplify! 28 | 29 | But obviously, having to write out the full options object makes it nearly as bad as the original `Object.defineProperty()` so we can simplify. 30 | 31 | As an alternative method we can use an options string where each character represents a option: `'e'=='enumerable'`, `'c'=='configurable'` and `'w'=='writable'`: 32 | 33 | ```js 34 | prr(obj, 'foo', 'bar', 'ew') // enumerable and writable but not configurable 35 | // muliple properties: 36 | prr(obj, { one: 'one', two: 'two' }, 'ewc') // configurable too 37 | ``` 38 | 39 | ## Where can I use it? 40 | 41 | Anywhere! For pre-ES5 environments *prr* will simply fall-back to an `object[property] = value` so you can get close to what you want. 42 | 43 | *prr* is Ender-compatible so you can include it in your Ender build and `$.prr(...)` or `var prr = require('prr'); prr(...)`. 44 | 45 | ## Licence 46 | 47 | prr is Copyright (c) 2013 Rod Vagg [@rvagg](https://twitter.com/rvagg) and licensed under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE.md file for more details. 48 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "prr", 3 | "description": "A better Object.defineProperty()", 4 | "version": "1.0.1", 5 | "homepage": "https://github.com/rvagg/prr", 6 | "author": "Rod Vagg (https://github.com/rvagg)", 7 | "keywords": [ 8 | "property", 9 | "properties", 10 | "defineProperty", 11 | "ender" 12 | ], 13 | "main": "./prr.js", 14 | "repository": { 15 | "type": "git", 16 | "url": "https://github.com/rvagg/prr.git" 17 | }, 18 | "dependencies": {}, 19 | "devDependencies": { 20 | "tap": "*" 21 | }, 22 | "scripts": { 23 | "test": "node ./test.js" 24 | }, 25 | "license": "MIT" 26 | } 27 | -------------------------------------------------------------------------------- /prr.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * prr 3 | * (c) 2013 Rod Vagg 4 | * https://github.com/rvagg/prr 5 | * License: MIT 6 | */ 7 | 8 | (function (name, context, definition) { 9 | if (typeof module != 'undefined' && module.exports) 10 | module.exports = definition() 11 | else 12 | context[name] = definition() 13 | })('prr', this, function() { 14 | 15 | var setProperty = typeof Object.defineProperty == 'function' 16 | ? function (obj, key, options) { 17 | Object.defineProperty(obj, key, options) 18 | return obj 19 | } 20 | : function (obj, key, options) { // < es5 21 | obj[key] = options.value 22 | return obj 23 | } 24 | 25 | , makeOptions = function (value, options) { 26 | var oo = typeof options == 'object' 27 | , os = !oo && typeof options == 'string' 28 | , op = function (p) { 29 | return oo 30 | ? !!options[p] 31 | : os 32 | ? options.indexOf(p[0]) > -1 33 | : false 34 | } 35 | 36 | return { 37 | enumerable : op('enumerable') 38 | , configurable : op('configurable') 39 | , writable : op('writable') 40 | , value : value 41 | } 42 | } 43 | 44 | , prr = function (obj, key, value, options) { 45 | var k 46 | 47 | options = makeOptions(value, options) 48 | 49 | if (typeof key == 'object') { 50 | for (k in key) { 51 | if (Object.hasOwnProperty.call(key, k)) { 52 | options.value = key[k] 53 | setProperty(obj, k, options) 54 | } 55 | } 56 | return obj 57 | } 58 | 59 | return setProperty(obj, key, options) 60 | } 61 | 62 | return prr 63 | }) -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const test = require('tap').test 2 | , prr = require('./') 3 | 4 | test('test prr(o, key, value) form', function (t) { 5 | t.plan(2) 6 | 7 | var o = {} 8 | prr(o, 'foo', 'bar') 9 | t.equal(o.foo, 'bar', 'correct value') 10 | t.deepEqual( 11 | Object.getOwnPropertyDescriptor(o, 'foo') 12 | , { 13 | enumerable : false 14 | , configurable : false 15 | , writable : false 16 | , value : 'bar' 17 | } 18 | , 'correct property descriptor' 19 | ) 20 | t.end() 21 | }) 22 | 23 | test('test prr(o, { key: value }) form', function (t) { 24 | t.plan(2) 25 | 26 | var o = {} 27 | prr(o, { foo: 'bar' }) 28 | 29 | t.equal(o.foo, 'bar', 'correct value') 30 | t.deepEqual( 31 | Object.getOwnPropertyDescriptor(o, 'foo') 32 | , { 33 | enumerable : false 34 | , configurable : false 35 | , writable : false 36 | , value : 'bar' 37 | } 38 | , 'correct property descriptor' 39 | ) 40 | t.end() 41 | }) 42 | 43 | test('test multiple key:value pairs', function (t) { 44 | var o = { foo: 'bar' } 45 | 46 | prr(o, { one: 'ONE', two: 'TWO', obj: { o: 'o' }}) 47 | 48 | t.deepEqual(o, { foo: 'bar' }, 'properties are not enumerable') 49 | t.equal(o.one, 'ONE', 'correctly set property') 50 | t.equal(o.two, 'TWO', 'correctly set property') 51 | t.deepEqual(o.obj, { o: 'o' }, 'correctly set property') 52 | 53 | ;[ 'one', 'two', 'obj' ].forEach(function (p) { 54 | t.deepEqual( 55 | Object.getOwnPropertyDescriptor(o, p) 56 | , { 57 | enumerable : false 58 | , configurable : false 59 | , writable : false 60 | , value : p == 'obj' ? { o: 'o' } : p.toUpperCase() 61 | } 62 | , 'correct property descriptor' 63 | ) 64 | }) 65 | 66 | t.end() 67 | }) 68 | 69 | test('test descriptor options', function (t) { 70 | var o = {} 71 | 72 | prr(o, 'foo', 'bar', { 73 | enumerable : true 74 | , configurable : false 75 | }) 76 | t.equal(o.foo, 'bar', 'correct value') 77 | t.deepEqual( 78 | Object.getOwnPropertyDescriptor(o, 'foo') 79 | , { 80 | enumerable : true 81 | , configurable : false 82 | , writable : false 83 | , value : 'bar' 84 | } 85 | , 'correct property descriptor' 86 | ) 87 | 88 | prr(o, 'foo2', 'bar2', { 89 | enumerable : true 90 | , configurable : true 91 | , writable : false 92 | }) 93 | t.equal(o.foo2, 'bar2', 'correct value') 94 | t.deepEqual( 95 | Object.getOwnPropertyDescriptor(o, 'foo2') 96 | , { 97 | enumerable : true 98 | , configurable : true 99 | , writable : false 100 | , value : 'bar2' 101 | } 102 | , 'correct property descriptor' 103 | ) 104 | 105 | prr(o, 'foo3', 'bar3', { 106 | enumerable : true 107 | , configurable : true 108 | , writable : true 109 | }) 110 | t.equal(o.foo3, 'bar3', 'correct value') 111 | t.deepEqual( 112 | Object.getOwnPropertyDescriptor(o, 'foo3') 113 | , { 114 | enumerable : true 115 | , configurable : true 116 | , writable : true 117 | , value : 'bar3' 118 | } 119 | , 'correct property descriptor' 120 | ) 121 | 122 | t.end() 123 | }) 124 | 125 | 126 | test('test descriptor options, string form', function (t) { 127 | var o = {} 128 | 129 | prr(o, 'foo', 'bar', 'e') 130 | t.equal(o.foo, 'bar', 'correct value') 131 | t.deepEqual( 132 | Object.getOwnPropertyDescriptor(o, 'foo') 133 | , { 134 | enumerable : true 135 | , configurable : false 136 | , writable : false 137 | , value : 'bar' 138 | } 139 | , 'correct property descriptor' 140 | ) 141 | 142 | prr(o, 'foo2', 'bar2', 'ec') 143 | t.equal(o.foo2, 'bar2', 'correct value') 144 | t.deepEqual( 145 | Object.getOwnPropertyDescriptor(o, 'foo2') 146 | , { 147 | enumerable : true 148 | , configurable : true 149 | , writable : false 150 | , value : 'bar2' 151 | } 152 | , 'correct property descriptor' 153 | ) 154 | 155 | prr(o, 'foo3', 'bar3', 'ecw') 156 | t.equal(o.foo3, 'bar3', 'correct value') 157 | t.deepEqual( 158 | Object.getOwnPropertyDescriptor(o, 'foo3') 159 | , { 160 | enumerable : true 161 | , configurable : true 162 | , writable : true 163 | , value : 'bar3' 164 | } 165 | , 'correct property descriptor' 166 | ) 167 | 168 | t.end() 169 | }) 170 | --------------------------------------------------------------------------------