load-google-maps-api
Advanced tools
Comparing version 1.0.1 to 1.1.0
12
index.js
@@ -5,6 +5,6 @@ var CALLBACK_NAME = '__googleMapsApiOnLoadCallback' | ||
module.exports = function(options) { | ||
module.exports = function (options) { | ||
options = options || {} | ||
return new Promise(function(resolve, reject) { | ||
return new Promise(function (resolve, reject) { | ||
// Exit if not running inside a browser. | ||
@@ -18,4 +18,4 @@ if (typeof window === 'undefined') { | ||
// Reject the promise after a timeout. | ||
var timeoutId = setTimeout(function() { | ||
window[CALLBACK_NAME] = function() {} // Set the on load callback to a no-op. | ||
var timeoutId = setTimeout(function () { | ||
window[CALLBACK_NAME] = function () {} // Set the on load callback to a no-op. | ||
reject(new Error('Could not load the Google Maps API')) | ||
@@ -25,3 +25,3 @@ }, options.timeout || 10000) | ||
// Hook up the on load callback. | ||
window[CALLBACK_NAME] = function() { | ||
window[CALLBACK_NAME] = function () { | ||
if (timeoutId !== null) { | ||
@@ -37,3 +37,3 @@ clearTimeout(timeoutId) | ||
var params = ['callback=' + CALLBACK_NAME] | ||
OPTIONS_KEYS.forEach(function(key) { | ||
OPTIONS_KEYS.forEach(function (key) { | ||
if (options[key]) { | ||
@@ -40,0 +40,0 @@ params.push(key + '=' + options[key]) |
The MIT License (MIT) | ||
Copyright (c) 2017 Lim Yuan Qing | ||
Copyright (c) 2018 Lim Yuan Qing | ||
@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
{ | ||
"name": "load-google-maps-api", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "A thin, Promise-returning helper for loading the Google Maps JavaScript API.", | ||
@@ -12,11 +12,24 @@ "author": "Lim Yuan Qing", | ||
"devDependencies": { | ||
"browserify": "^14.3.0", | ||
"prettier": "^1.2.2", | ||
"tape": "^4.6.3", | ||
"tape-run": "^3.0.0" | ||
"browserify": "^16.1.0", | ||
"gzip-size-cli": "^2.1.0", | ||
"prettier-standard": "^8.0.0", | ||
"tape": "^4.8.0", | ||
"tape-run": "^3.0.4", | ||
"uglify-js": "^3.3.11" | ||
}, | ||
"scripts": { | ||
"lint": "prettier *.js --no-semi --single-quote --write", | ||
"test": "browserify test.js | tape-run" | ||
} | ||
"lint": "prettier-standard *.js --no-semi --single-quote --write", | ||
"test": "browserify test.js | tape-run", | ||
"weight": "uglifyjs index.js --compress --mangle --toplevel | gzip-size" | ||
}, | ||
"keywords": [ | ||
"api", | ||
"google", | ||
"google-maps", | ||
"google-maps-api", | ||
"loader", | ||
"maps", | ||
"promise", | ||
"typography" | ||
] | ||
} |
# load-google-maps-api [![npm Version](http://img.shields.io/npm/v/load-google-maps-api.svg?style=flat)](https://www.npmjs.com/package/load-google-maps-api) [![Build Status](https://img.shields.io/travis/yuanqing/load-google-maps-api.svg?branch=master&style=flat)](https://travis-ci.org/yuanqing/load-google-maps-api) | ||
> A thin, [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)-returning helper for loading the [Google Maps JavaScript API](https://developers.google.com/maps/documentation/javascript/). | ||
> A thin, [Promise](https://developers.google.com/web/fundamentals/primers/promises)-returning helper for loading the [Google Maps JavaScript API](https://developers.google.com/maps/documentation/javascript/). | ||
- The Promise’s fulfilled callback is passed the `google.maps` object | ||
- Optionally set a timeout, an API key, the language, [and more](#loadgooglemapsapioptions) | ||
- 456 bytes gzipped | ||
## Usage | ||
> [**Editable demo (CodePen)**](https://codepen.io/lyuanqing/pen/YeYBrN) | ||
```js | ||
const loadGoogleMapsAPI = require('load-google-maps-api') | ||
loadGoogleMapsAPI().then(function(googleMaps) { | ||
console.log(googleMaps) //=> Object { Animation: Object, ... | ||
}).catch((err) => { | ||
console.error(err) | ||
loadGoogleMapsApi().then(function (googleMaps) { | ||
new googleMaps.Map(document.querySelector('.map'), { | ||
center: { | ||
lat: 40.7484405, | ||
lng: -73.9944191 | ||
}, | ||
zoom: 12 | ||
}) | ||
}).catch(function (error) { | ||
console.error(error) | ||
}) | ||
``` | ||
Read [the source](index.js) to understand how this works. | ||
*N.B.* Just like the Google Maps API itself, this module is client-side only. | ||
@@ -23,8 +31,8 @@ | ||
Without this module, you would need to specify a named *global* callback, and pass said callback’s name as a parameter in the `script` tag’s `src`. For example: | ||
[Without this module](https://developers.google.com/maps/documentation/javascript/tutorial#Loading_the_Maps_API), you would need to specify a named *global* callback, and pass said callback’s name as a parameter in the `script` tag’s `src`. For example: | ||
```html | ||
<script> | ||
window.googleMapsOnLoad = () => { | ||
// `google.maps` available here | ||
window.googleMapsOnLoad = function () { | ||
// `window.google.maps` available here | ||
} | ||
@@ -35,3 +43,3 @@ </script> | ||
This module abstracts this ceremony away, and fits better with [Browserify](http://browserify.org/) or [Webpack](https://webpack.github.io/). | ||
This module abstracts this ceremony away, and fits better with modern bundlers like [Browserify](http://browserify.org/) or [Webpack](https://webpack.github.io/). | ||
@@ -46,7 +54,9 @@ ## API | ||
Returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise). (See [Usage](#usage).) | ||
Returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise). | ||
- **Fulfilled** if load was successful. The fulfilled callback is passed the `google.maps` object. | ||
- **Fulfilled** if loading was successful. The fulfilled callback is passed the `google.maps` object. | ||
- **Rejected** if we weren’t able to load the Google Maps API after `options.timeout`. | ||
See [Usage](#usage). | ||
`options` is an optional object literal: | ||
@@ -62,3 +72,3 @@ | ||
`region` | [Region](https://developers.google.com/maps/documentation/javascript/localization#Region) | `undefined` | ||
`timeout` | Time in milliseconds before rejecting the promise | `10000` | ||
`timeout` | Time in milliseconds before rejecting the Promise | `10000` | ||
`v` | [API version](https://developers.google.com/maps/documentation/javascript/versions) | `undefined` | ||
@@ -70,3 +80,3 @@ | ||
```bash | ||
```sh | ||
$ yarn add load-google-maps-api | ||
@@ -77,3 +87,3 @@ ``` | ||
```bash | ||
```sh | ||
$ npm install --save load-google-maps-api | ||
@@ -80,0 +90,0 @@ ``` |
const test = require('tape') | ||
const loadGoogleMapsAPI = require('./') | ||
test('loads the API', function(t) { | ||
test('loads the API', function (t) { | ||
t.plan(1) | ||
loadGoogleMapsAPI().then(t.ok, t.fail) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
114889
8
89
6