https://github.com/rickwong/trespass
Access and chain object properties and methods in a safe manner.
https://github.com/rickwong/trespass
access deep es6 methods node null object properties proxy safe trespass undefined
Last synced: 12 months ago
JSON representation
Access and chain object properties and methods in a safe manner.
- Host: GitHub
- URL: https://github.com/rickwong/trespass
- Owner: RickWong
- License: bsd-3-clause
- Created: 2017-11-09T16:32:54.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-08-24T04:35:33.000Z (over 5 years ago)
- Last Synced: 2024-02-25T09:46:52.958Z (about 2 years ago)
- Topics: access, deep, es6, methods, node, null, object, properties, proxy, safe, trespass, undefined
- Language: JavaScript
- Size: 93.8 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# trespass
Tired of `&&` or `?:` property guarding? Access undefined object properties and chain undefined methods in a safe manner.
## Installation
```bash
npm install --save trespass
```
## Usage
Simple:
```js
import t from 'trespass';
const fakeUser = undefined;
const realUser = { name: 'Rick' };
t(fakeUser).contact.$; // access undefined or null objects
t(realUser).contact.faxNumber.$; // access undefined properties
t(realUser).greet().fakeSlap().$; // chain defined and undefined methods
t(fakeUser).fakeSlap().greet().fakeName.$; // all together
```
Custom terminator:
```js
const $ = t.createWrapper('$val');
$(foo).bar.$val;
```
## Requirements
Node 6.4+. Native [Browser support](https://caniuse.com/#search=Proxy) for ES6 Proxies.
## Why
Because the official Optional Chaining `?.` proposal is still in stage-1 currently, see https://github.com/tc39/proposal-optional-chaining.
## Other packages
- https://github.com/facebookincubator/idx
- https://github.com/ktsn/safe-object-proxy
- https://github.com/erictrinh/safe-proxy
- https://github.com/forceuser/access-deep
- https://github.com/tonylukasavage/safeproxy
## Community
Let's start one together! After you ★ this project, follow me [@rygu](https://twitter.com/rygu) on Twitter.
## License
BSD 3-Clause license. Copyright © 2017, Rick Wong. All rights reserved.