Skip to content

Commit

Permalink
allow non-string method names
Browse files Browse the repository at this point in the history
  • Loading branch information
westonganger committed Jul 18, 2017
1 parent 9f52dd7 commit 9bb3f63
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 16 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Changelog

- `v0.9.0 - 2016-07-13`
- `v0.9.1 - 2017-07-18`
* Allow non-string method names
- `v0.9.0 - 2017-07-13`
* Initial release
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ npm install js-try
bower install js-try
```

#### Rails
#### Rails / Bundler

```ruby
# Gemfile
Expand All @@ -28,8 +28,6 @@ end
*/
```
Rails 5.1+ should probably just install via yarn
# Usage
#### Require/Import
```javascript
Expand All @@ -50,19 +48,27 @@ 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
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-try",
"version": "0.9.0",
"version": "0.9.1",
"description": "JS-try is a Javascript implementation of the try method from Rails for safe navigation",
"main": "dist/js-try.min.js",
"directories": {
Expand Down
2 changes: 1 addition & 1 deletion dist/try.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
* @link http://github.com/westonganger/js-try
* @license MIT
*/
function Try(t){return!(!t&&0!==t&&""!==t)&&t}!function(){"use strict";if(!Object.prototype.try){var t=function(t){return!!(t&&t.constructor&&t.call&&t.apply)},r=function(t){return"string"==typeof t||t instanceof String};Object.prototype.try=function(e){if(arguments.length>1){var n=Array.prototype.slice.call(arguments);n.shift()}var o=this[e];return!(!r(e)||!o&&0!==o&&""!==o)&&(t(o)&&(o=o.apply(this,n)),Try(o))},Object.defineProperty(Object.prototype,"try",{enumerable:!1})}}(this),"undefined"!=typeof exports&&"undefined"!=typeof module&&module.exports?module.exports=Try:window&&(window.Try=Try);
function Try(t){return!(!t&&0!==t&&""!==t)&&t}!function(){"use strict";if(!Object.prototype.try){var t=function(t){return!!(t&&t.constructor&&t.call&&t.apply)};Object.prototype.try=function(r){var e=this[r];if(Try(e)){if(t(e)){if(arguments.length>1){var o=Array.prototype.slice.call(arguments);o.shift()}e=e.apply(this,o)}return Try(e)}return!1},Object.defineProperty(Object.prototype,"try",{enumerable:!1})}}(this),"undefined"!=typeof exports&&"undefined"!=typeof module&&module.exports?module.exports=Try:window&&(window.Try=Try);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-try",
"version": "0.9.0",
"version": "0.9.1",
"description": "JS-try is a Javascript implementation of the try method from Rails for safe navigation",
"main": "dist/try.min.js",
"directories": {
Expand Down
14 changes: 5 additions & 9 deletions src/try.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,14 @@
return !!(x && x.constructor && x.call && x.apply);
};

var isString = function(x){
return typeof x === 'string' || x instanceof String
};

Object.prototype.try = function(x){
if(arguments.length > 1){
var args = Array.prototype.slice.call(arguments);
args.shift();
}
var val = this[x];
if(isString(x) && (val || val === 0 || val === '')){
if(Try(val)){
if(isFunction(val)){
if(arguments.length > 1){
var args = Array.prototype.slice.call(arguments);
args.shift();
}
val = val.apply(this, args);
}
return Try(val);
Expand Down
4 changes: 4 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ describe('Try', function() {
it('array', function() {
assert.deepEqual([].try('sort'), []);
assert.equal([].try('foobar'), false);
assert.equal([].try(0), false);
assert.equal([1].try(0), 1);
});

it('object', function() {
assert.equal({}.try('toString'), "[object Object]");
assert.equal({}.try('foobar'), false);
assert.equal({foo: 'bar'}.try('foo'), 'bar');
assert.equal({foo: 'bar'}.try('bar'), false);
});
});
});

0 comments on commit 9bb3f63

Please sign in to comment.