Giter Club home page Giter Club logo

js-try's Introduction

JS-Try

NPM Version CI Status NPM Downloads Buy Me a Coffee

JS-Try is a Javascript implementation of the try method from Rails for safe navigation.

Install

Yarn, NPM, or Bower

yarn add js-try

npm install js-try

bower install js-try

Rails / Bundler

# Gemfile
source 'https://rails-assets.org' do
  gem 'rails-assets-js-try'
end


# app/assets/javascripts/application.js
/*
 *= require js-try
*/

Usage

Require/Import

var Try = require('js-try');
// or
import { Try } from 'js-try';

Basic Examples

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'); == false

var 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

For any consulting or contract work please contact me via my company website: Solid Foundation Web Development

Similar Libraries Created By Me

js-try's People

Contributors

westonganger avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.