Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alexbykoff/sleeve
https://github.com/alexbykoff/sleeve
Last synced: 6 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/alexbykoff/sleeve
- Owner: alexbykoff
- Created: 2017-04-27T19:43:59.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-07-08T15:52:51.000Z (over 6 years ago)
- Last Synced: 2024-11-09T19:36:22.842Z (2 months ago)
- Language: JavaScript
- Size: 5.86 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# sleeve
#### always have an ace up your sleeve
----
explore, search for and read Javascript object nested properties without many a nasty error
[![npm version](https://badge.fury.io/js/sleeve.svg)](https://badge.fury.io/js/sleeve)##### Installation:
`npm install --save sleeve`##### Usage:
`const s = require('sleeve');`##### Syntax:
`s(object, "chain.of.properties.that.can.be.rather.long", fallbackValue)`##### Examples:
```
const user = {
name: "John",
login: "jjohanson",
contacts: {
phoneNumber: "(555)834-1337"
},
location:{},
data: {
a:{
b: ""
}
}
};
```##### not okay:
```
const email = user && user.contacts && user.contacts.email && user.contacts.email.primaryEmail ? user.contacts.email.primaryEmail : "no email provided";
```##### okay:
_fallbacks:_
```
const email = s(user, "contacts.email.primaryEmail", "no email provided");
console.log(email); // "no email provided"
```_property checking:_
```
let countryCode;
if (!s(user, "location.country.code")) {
countryCode = "US";
}
```_reading value:_
```
console.log(s(user, "data.a.b.c.going.too.deep")); // null
```_callback function:_
```
s(user, "i.do.not.know.where.to.go", () => console.log("this is a fail"));
```