Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/westonganger/js-try
JS-Try is a Javascript implementation of the try method from Rails for safe navigation
https://github.com/westonganger/js-try
javascript js node-js rails ruby safe-navigation try
Last synced: 21 days ago
JSON representation
JS-Try is a Javascript implementation of the try method from Rails for safe navigation
- Host: GitHub
- URL: https://github.com/westonganger/js-try
- Owner: westonganger
- License: mit
- Created: 2017-07-14T03:07:20.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-09-06T19:56:36.000Z (5 months ago)
- Last Synced: 2024-12-25T00:50:27.232Z (30 days ago)
- Topics: javascript, js, node-js, rails, ruby, safe-navigation, try
- Language: JavaScript
- Homepage:
- Size: 10.7 KB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
JS-Try is a Javascript implementation of the try method from Rails for safe navigation.
# Install
#### Yarn, NPM, or Bower
```javascript
yarn add js-trynpm install js-try
bower install js-try
```#### Rails / Bundler
```ruby
# Gemfile
source 'https://rails-assets.org' do
gem 'rails-assets-js-try'
end# app/assets/javascripts/application.js
/*
*= require js-try
*/
```# Usage
#### Require/Import
```javascript
var Try = require('js-try');
// or
import { Try } from 'js-try';
```#### Basic Examples
```javascript
Try(undefined) == false;
Try(null) == false;
Try(false) == false;
Try(true) == true;
Try(0) == 0;
Try('') == '';
Try('foobar') == 'foobar';
Try([]) == [];
Try({}) == {};false.try('length') == false;
true.try('length') == false;''.try('length') == 0;
''.try('foobar') == false;
'foobar'.try('charAt', 3) == 'b';
''.try('badMethod').try('anotherBadMethod'); == falsevar x = 0;
x.try('toString') == '0';
x.try('foobar') == false;[].try('sort') == [];
[].try('foobar') == false;
[1,2,3].try(2) == 3;
[1,2,3].try(3) == false;{}.try('toString') == "[object Object]";
{}.try('foobar') == false;
{foo: 'bar'}.try('foo') == 'bar';
{foo: 'bar'}.try('bar') == false;
```# Credits
Created by Weston Ganger - [@westonganger](https://github.com/westonganger)