load-google-maps-api
Advanced tools
Comparing version 1.3.3 to 2.0.0
# Changelog | ||
## 2.0.0 | ||
- Bump dependencies | ||
- Use modern syntax, namely let/const and default parameters | ||
## 1.3.1 | ||
@@ -4,0 +9,0 @@ |
33
index.js
@@ -1,14 +0,14 @@ | ||
var CALLBACK_NAME = '__googleMapsApiOnLoadCallback' | ||
const API_URL = 'https://maps.googleapis.com/maps/api/js' | ||
const CALLBACK_NAME = '__googleMapsApiOnLoadCallback' | ||
var OPTIONS_KEYS = ['channel', 'client', 'key', 'language', 'region', 'v'] | ||
const optionsKeys = ['channel', 'client', 'key', 'language', 'region', 'v'] | ||
var promise = null | ||
let promise = null | ||
module.exports = function (options) { | ||
options = options || {} | ||
if (!promise) { | ||
promise = new Promise(function (resolve, reject) { | ||
module.exports = function (options = {}) { | ||
promise = | ||
promise || | ||
new Promise(function (resolve, reject) { | ||
// Reject the promise after a timeout | ||
var timeoutId = setTimeout(function () { | ||
const timeoutId = setTimeout(function () { | ||
window[CALLBACK_NAME] = function () {} // Set the on load callback to a no-op | ||
@@ -28,14 +28,13 @@ reject(new Error('Could not load the Google Maps API')) | ||
// Prepare the `script` tag to be inserted into the page | ||
var scriptElement = document.createElement('script') | ||
var params = ['callback=' + CALLBACK_NAME] | ||
OPTIONS_KEYS.forEach(function (key) { | ||
const scriptElement = document.createElement('script') | ||
const params = [`callback=${CALLBACK_NAME}`] | ||
optionsKeys.forEach(function (key) { | ||
if (options[key]) { | ||
params.push(key + '=' + options[key]) | ||
params.push(`${key}=${options[key]}`) | ||
} | ||
}) | ||
if (options.libraries && options.libraries.length) { | ||
params.push('libraries=' + options.libraries.join(',')) | ||
params.push(`libraries=${options.libraries.join(',')}`) | ||
} | ||
scriptElement.src = | ||
'https://maps.googleapis.com/maps/api/js?' + params.join('&') | ||
scriptElement.src = `${options.apiUrl || API_URL}?${params.join('&')}` | ||
@@ -45,5 +44,3 @@ // Insert the `script` tag | ||
}) | ||
} | ||
return promise | ||
} |
{ | ||
"name": "load-google-maps-api", | ||
"version": "1.3.3", | ||
"description": "A thin, Promise-returning helper for loading the Google Maps JavaScript API.", | ||
"version": "2.0.0", | ||
"description": "A lightweight Promise-returning helper for loading the Google Maps JavaScript API", | ||
"author": "Lim Yuan Qing", | ||
@@ -14,10 +14,10 @@ "license": "MIT", | ||
"gzip-size-cli": "^3.0.0", | ||
"husky": "^1.3.1", | ||
"lint-staged": "^8.1.5", | ||
"husky": "^2.7.0", | ||
"lint-staged": "^8.2.1", | ||
"prettier-standard": "^9.1.1", | ||
"rimraf": "^2.6.3", | ||
"standard": "^12.0.1", | ||
"tape": "^4.10.1", | ||
"tape-run": "^5.0.0", | ||
"uglify-js": "^3.5.3" | ||
"tape": "^4.10.2", | ||
"tape-run": "^6.0.0", | ||
"terser": "^4.0.0" | ||
}, | ||
@@ -29,3 +29,3 @@ "scripts": { | ||
"test": "browserify test.js | tape-run", | ||
"weight": "uglifyjs index.js --compress --mangle --toplevel | gzip-size" | ||
"weight": "terser index.js --compress --mangle --toplevel | gzip-size" | ||
}, | ||
@@ -32,0 +32,0 @@ "lint-staged": { |
# load-google-maps-api [![npm Version](https://badgen.net/npm/v/load-google-maps-api)](https://www.npmjs.org/package/load-google-maps-api) [![Build Status](https://badgen.net/travis/yuanqing/load-google-maps-api?label=build)](https://travis-ci.org/yuanqing/load-google-maps-api) [![Bundle Size](https://badgen.net/bundlephobia/minzip/load-google-maps-api)](https://bundlephobia.com/result?p=load-google-maps-api) | ||
> 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/). | ||
> A lightweight Promise-returning helper for loading the [Google Maps JavaScript API](https://developers.google.com/maps/documentation/javascript/) | ||
@@ -30,3 +30,3 @@ - The Promise’s fulfilled callback is passed the `google.maps` object | ||
## Why | ||
## Motivation | ||
@@ -65,2 +65,3 @@ [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: | ||
:--|:--|:-- | ||
`apiUrl` | The Google Maps API `script` tag URL | `'https://maps.googleapis.com/maps/api/js'` | ||
`channel` | [Client usage reporting channel](https://developers.google.com/maps/premium/reports/usage-reports#channels) | `undefined` | ||
@@ -67,0 +68,0 @@ `client` | [Client ID](https://developers.google.com/maps/documentation/javascript/get-api-key#specifying-a-client-id-when-loading-the-api) | `undefined` |
7942
83
38