Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/brechtcs/stdprop
Define object properties without boilerplate
https://github.com/brechtcs/stdprop
Last synced: about 11 hours ago
JSON representation
Define object properties without boilerplate
- Host: GitHub
- URL: https://github.com/brechtcs/stdprop
- Owner: brechtcs
- License: apache-2.0
- Created: 2020-01-15T07:10:22.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-05-05T20:25:27.000Z (over 4 years ago)
- Last Synced: 2024-11-09T22:03:50.194Z (7 days ago)
- Language: JavaScript
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# stdprop
Define object properties without boilerplate
## Usage
With this modules, you can define object properties using the following shorthand (original longhand included for reference):
```js
var prop = require('stdprop')
var obj = {}// non-enumerable and non-writable
// with stdprop:
prop(obj, 'one', 'stuff')// previously:
Object.defineProperty(obj, 'stuff', {
value: 'stuff'
})// enumerable and non-writable
// with stdprop:
prop(obj, 'one', 'stuff', 'e')// previously:
Object.defineProperty(obj, 'stuff', {
value: 'stuff',
enumerable: true
})// enumerable and writable
// with stdprop:
prop(obj, 'one', 'stuff', 'ew')// previously:
Object.defineProperty(obj, 'stuff', {
value: 'stuff',
enumerable: true,
writable: true
})
```### getters & setters
Also included are two shorthand methods to create getters and setters on any given object.
```js
var { getter, setter } = require('stdprop')var interface = {}
var storage = {}
setter(interface, 'shout', function (str) { storage.shout = str.toUpperCase() })
getter(interface, 'shout', function () { return storage.shout + '!' })interface.shout = 'hello'
console.log(interface.shout) // => 'HELLO!'
console.log(storage.shout) // => 'HELLO'
```## Acknowledgement
The idea of using shorthand flags to create property descriptors was taken from the existing module [prr](https://npmjs.org/package/prr).
## License
Apache-2.0