Skip to content

Commit dd2fae4

Browse files
authored
Merge pull request #22 from alfg/feature/npm-module
Add module export to support importing as an npm module.
2 parents 3901354 + 5d01b40 commit dd2fae4

File tree

8 files changed

+110
-3
lines changed

8 files changed

+110
-3
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Example in jQuery: http://jsfiddle.net/alfg/xjqbvt2o/
1111

1212
## Usage
1313

14+
* Download the distribution files in `dist` to your project.
15+
1416
```javascript
1517
var p = new Ping();
1618

@@ -24,6 +26,18 @@ p.ping("https://github.com", function(err, data) {
2426
});
2527
```
2628

29+
or import as a module:
30+
31+
```bash
32+
$ npm install ping.js
33+
```
34+
35+
```javascript
36+
import Ping from 'ping.js';
37+
```
38+
39+
See [demo/react-example](demo/react-example) for an example in React.
40+
2741
## API
2842

2943
```javascript

demo/react-example/ping.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<body>
3+
<div id="root"></div>
4+
<script src="./ping.js"></script>
5+
</body>
6+
</html>
7+
8+

demo/react-example/ping.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
4+
import Ping from '../ping.js';
5+
6+
class App extends React.Component {
7+
constructor() {
8+
super();
9+
10+
this.p = new Ping();
11+
12+
this.state = {
13+
google: 0,
14+
apple: 0,
15+
microsoft: 0
16+
};
17+
18+
this.handleClick = this.handleClick.bind(this);
19+
}
20+
21+
handleClick() {
22+
this.p.ping("http://google.com", (err, data) => {
23+
if (err) {
24+
console.log("error loading resource", err);
25+
}
26+
this.setState({ google: data });
27+
});
28+
29+
this.p.ping("http://apple.com", (err, data) => {
30+
if (err) {
31+
console.log("error loading resource", err);
32+
}
33+
this.setState({ apple: data });
34+
});
35+
36+
this.p.ping("http://microsoft.com", (err, data) => {
37+
if (err) {
38+
console.log("error loading resource", err);
39+
}
40+
this.setState({ microsoft: data });
41+
});
42+
}
43+
44+
render() {
45+
return (
46+
<div>
47+
<button onClick={this.handleClick}>Get Pings</button>
48+
<ul>
49+
<li>google.com <span>{this.state.google}</span></li>
50+
<li>apple.com <span>{this.state.apple}</span></li>
51+
<li>microsoft.com <span>{this.state.microsoft}</span></li>
52+
</ul>
53+
54+
</div>
55+
)
56+
}
57+
}
58+
59+
ReactDOM.render(
60+
<App />,
61+
document.getElementById('root')
62+
);

dist/ping.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,11 @@ Ping.prototype.ping = function(source, callback) {
5050

5151
this.img.src = source + this.favicon + "?" + (+new Date()); // Trigger image load with cache buster
5252
};
53+
54+
if (typeof exports !== "undefined") {
55+
if (typeof module !== "undefined" && module.exports) {
56+
module.exports = Ping;
57+
}
58+
} else {
59+
window.Ping = Ping;
60+
}

dist/ping.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const Ping = require('./src/ping');
2+
module.exports = Ping;

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"name": "ping.js",
33
"description": "Ping Utilities with Javascript",
4-
"version": "0.1.3",
4+
"version": "0.2.0",
55
"author": "Alf <[email protected]>",
6+
"main": "index.js",
67
"devDependencies": {
78
"grunt": "~0.4.5",
89
"grunt-cli": "~0.1.13",
@@ -13,6 +14,10 @@
1314
"scripts": {
1415
"start": "grunt"
1516
},
16-
"keywords": ["javascript", "ping", "utilities"],
17+
"keywords": [
18+
"javascript",
19+
"ping",
20+
"utilities"
21+
],
1722
"repository": "git://github.com/alfg/ping.js"
1823
}

src/ping.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,11 @@ Ping.prototype.ping = function(source, callback) {
4242

4343
this.img.src = source + this.favicon + "?" + (+new Date()); // Trigger image load with cache buster
4444
};
45+
46+
if (typeof exports !== "undefined") {
47+
if (typeof module !== "undefined" && module.exports) {
48+
module.exports = Ping;
49+
}
50+
} else {
51+
window.Ping = Ping;
52+
}

0 commit comments

Comments
 (0)