Skip to content

Commit

Permalink
Change method names. Add README, CONTRIBUTING, LICENSE, and doc comme…
Browse files Browse the repository at this point in the history
…nts.
  • Loading branch information
albrow committed Jul 12, 2015
1 parent cb1dd55 commit 778b844
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 11 deletions.
26 changes: 26 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Feedback, bug reports, and pull requests are greatly appreciated :)

### Issues

The following are all great reasons to submit an issue:

1. You found a bug in the code.
2. Something is missing from the documentation or the existing documentation is unclear.
3. You have an idea for a new feature.

If you are thinking about submitting an issue please remember to:

1. Describe the issue in detail.
2. If applicable, describe the steps to reproduce the error, which probably should include some example code.
3. Mention details about your platform: OS, version of Go, etc.

### Pull Requests

Detect uses semantic versioning and the [git branching model described here](http://nvie.com/posts/a-successful-git-branching-model/).
If you plan on submitting a pull request, you should:

1. Fork the repository.
2. Create a new "feature branch" off of **develop** (not master) with a descriptive name (e.g. fix-database-error).
3. Make your changes in the feature branch.
4. Run the tests to make sure that they still pass. Updated the tests if needed.
5. Submit a pull request to merge your feature branch into the **develop** branch. Please do not request to merge directly into master.
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(The MIT License)

Copyright (C) 2015, Alex Browne

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
108 changes: 108 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
Humble/detect
=============

[![GoDoc](https://godoc.org/github.com/go-humble/detect?status.svg)](https://godoc.org/github.com/go-humble/detect)

Version X.X.X

Detect is a tiny go package for detecting whether code is running on the server
or browser. It is intended to be used with hybrid go applications and
[gopherjs](https://github.com/gopherjs/gopherjs). Detect works great as a
stand-alone package or in combination with other packages in the
[Humble Toolkit](https://github.com/go-humble).

Detect is written in pure go. It feels like go, follows go idioms when possible, and
compiles with the go tools. Detect runs on the server and can also be compiled
to javascript and run in the browser.


Browser Support
---------------

Detect works with IE9+ (with a
[polyfill for typed arrays](https://github.com/inexorabletash/polyfill/blob/master/typedarray.js))
and all other modern browsers. Detect compiles to javascript via [gopherjs](https://github.com/gopherjs/gopherjs)
and this is a gopherjs limitation.


Installation
------------

Install detect like you would any other go package:

```bash
go get github.com/go-humble/detect
```

You will also need to install gopherjs if you don't already have it. The latest version is
recommended. Install gopherjs with:

```
go get -u github.com/gopherjs/gopherjs
```


Example Usage
-------------

Detect is intended to be used in hybrid go applications which use gopherjs to
compile to javascript. When you are sharing code between the server and browser,
it is often useful to be able to quickly detect which platform the code is
currently running on. For example, if you are sharing models between the browser
and server, you probably want the server-side code to communicate with the
database, whereas the brwoser-side code should not.

```go
// The Todo type and its fields can be shared between the server and browser.
type Todo struct {
Title string
IsCompleted bool
}

// The Toggle method can be called server-side or browser-side because it does
// not touch the database.
func (t *Todo) Toggle() {
t.IsCompleted = !t.IsCompleted
}

// The Save method however, should have different behavior depending on whether
// we are running on the server or browser.
func (t *Todo) Save() {
switch {
case detect.IsBrowser():
// In this case, we might want to save to local storage first, and then
// try to sync with the server using WebSockets or http.
case detect.IsServer():
// In this case, we want to save directly to the database.
}
}
```

How it Works
------------

To see if code is running as javascript, detect checks whether `js.Global`
is nil. `js.Global` is a special gopherjs property that is only defined when
code is compiled to javascript.

To see if code is running in the browser, detect checks for the existence of a
global javascript "document" property. If it is defined, detect assumes the
code is running in the browser.

We encourage you to check out the
[source code](https://github.com/go-humble/detect/blob/master/detect.go) if
you're curious about how detect works under the hood. It's really tiny
(only a single file) and pretty straightforward.


Contributing
------------

See [CONTRIBUTING.md](https://github.com/go-humble/detect/blob/master/CONTRIBUTING.md)


License
-------

Detect is licensed under the MIT License. See the [LICENSE](https://github.com/go-humble/detect/blob/master/LICENSE)
file for more information.
41 changes: 30 additions & 11 deletions detect.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,44 @@
// Copyright 2015 Alex Browne. All rights reserved.
// Use of this source code is governed by the MIT
// license, which can be found in the LICENSE file.

// Package detect is a tiny package for detecting whether code is running on
// the server or browser. It is intended to be used with hybrid go applications
// and gopherjs.
package detect

import (
"github.com/gopherjs/gopherjs/js"
)

// IsClient returns true iff the code that is currently running is
// compiled javascript code running in a browser with a global document
// property.
func IsClient() bool {
// IsBrowser returns true iff the code that is currently running is compiled
// javascript code running in a browser. It works by checking if the global
// "document" property exists.
func IsBrowser() bool {
return IsJavascript() && js.Global.Get("document") != js.Undefined
}

// IsJavascript return true iff the code that is currently running
// is compiled javascript code.
// IsServer returns true iff the code is running on a server. It returns true
// if the code is pure go, or if it has been compiled to javascript and is
// being run with node. It will only return false if the global "document"
// property exists.
func IsServer() bool {
if IsJavascript() {
return !IsBrowser()
}
return true
}

// IsJavascript return true iff the code that is currently running is
// transpiled javascript code. It works by checking if the js.Global property
// is non-nil.
func IsJavascript() bool {
return js.Global != nil
}

// IsServer returns true iff the code that is currently running is
// pure go code. That is, if the code that is currently running has
// not been compiled to javascript and is not running inside a browser.
func IsServer() bool {
return !IsClient()
// IsGo returns true iff the code that is currently running is pure go code.
// That is, it has not been compiled to javascript. It works by checking if the
// js.Global property is nil.
func IsGo() bool {
return !IsJavascript()
}

0 comments on commit 778b844

Please sign in to comment.