Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated readme examples to es6 #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 34 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@ the available tests, based on their own needs for providing you a normalized fut
**not stable**, so keep that in mind. This is a young project, and the decided naming conventions may be a moving target. The tests are nothing that haven't been done over and over in various places, so the intention is to come to some agreement on a basic naming convention and API based on real world use cases.

Currently, the testing convention is `has('somefeature')` returns _Boolean_, e.g.:

if(has("function-bind")){
// your enviroment has a native Function.prototype.bind
}else{
// you should get a new browser.
}
```js
if (has('function-bind')) {
// your enviroment has a native Function.prototype.bind
} else {
// you should get a new browser.
}
```

In the real world, this may translate into something like:

mylibrary.trim = has("string-trim") ? function(str){
return (str || "").trim();
} : function(str){
/* do the regexp based string trimming you feel like using */
}
```js
mylibrary.trim = has('string-trim')
? str => (str || '').trim()
: str => {
/* do the regexp based string trimming you feel like using */
}
```

By doing this, we can easily defer to browser-native versions of common functions, augment prototypes (which **has.js** will _not_ do) to
supplement the natives, or whatever we choose.
Expand All @@ -37,26 +39,29 @@ Running `has()` is a one-time cost, deferred until needed. After first run, subs
## Testing Registration

Each test is self-contained. Register a test with `has.add()`:

has.add("some-test-name", function(global, document, anElement){
// global is a reference to global scope, document is the same
// anElement only exists in browser enviroments, and can be used
// as a common element from which to do DOM working.
// ALWAYS CLEAN UP AFTER YOURSELF in a test. No leaks, thanks.
// return a Boolean from here.
return true;
});
```js
has.add('some-test-name', (global, document, anElement) => {
// global is a reference to global scope, document is the same
// anElement only exists in browser enviroments, and can be used
// as a common element from which to do DOM working.
// ALWAYS CLEAN UP AFTER YOURSELF in a test. No leaks, thanks.
// return a Boolean from here.
return true;
});
```

You can register and run a test immediately by passing a truthy value after the test function:

has.add("some-other-test", function(){
return false; // Boolean
}, true)
```js
has.add('some-other-test', () => {
return false; // Boolean
}, true)
```

This is preferred over what would seem a much more effective version:

// this is not wrapped in a function, and should be:
has.add("some-other-test", ("foo" in bar)); // or whatever
```js
// this is not wrapped in a function, and should be:
has.add('some-other-test', ('foo' in bar)); // or whatever
```

By forcing a function wrapper around the test logic we are able to defer execution until needed, as well as provide a normalized way for each test to have its own execution context. This way, we can remove some or all the tests we do not need in whatever upstream library should adopt _has_.

Expand Down Expand Up @@ -111,4 +116,4 @@ Tentatively, **has.js** is available under the Academic Free License, New BSD Li
+ moar tests. Again with the forking.
+ "compiler" code / frontend
+ ideally something that will use the list of tests, provide a clean interface to selecting tests needed and to download a single has.js file with tests embedded.
+ keeping in mind to remove additional closures and provide (only needed) `var CONTS = ""` style helpers in a single wrapping function.
+ keeping in mind to remove additional closures and provide (only needed) `var CONTS = ""` style helpers in a single wrapping function.