Skip to content

Access and chain object properties and methods in a safe manner.

License

Notifications You must be signed in to change notification settings

RickWong/trespass

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

trespass

Tired of && or ?: property guarding? Access undefined object properties and chain undefined methods in a safe manner.

Installation

npm install --save trespass

Usage

Simple:

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:

const $ = t.createWrapper('$val'); 

$(foo).bar.$val; 

Requirements

Node 6.4+. Native Browser support 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

Community

Let's start one together! After you ★ this project, follow me @rygu on Twitter.

License

BSD 3-Clause license. Copyright © 2017, Rick Wong. All rights reserved.

Releases

No releases published

Packages

No packages published