Skip to content

Commit eea7b59

Browse files
committed
MP20200108 - Added type definition build step to package lib, (SEE: shukerullah#24).
1 parent b2d82bf commit eea7b59

File tree

4 files changed

+133
-69
lines changed

4 files changed

+133
-69
lines changed

babel.config.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module.exports = function(api) {
2+
api.cache(true);
3+
4+
const config = {
5+
"comments": false
6+
}
7+
8+
const presets = [
9+
["minify", {
10+
"mangle": false,
11+
"keepFnName": true
12+
}],
13+
["@babel/preset-env", {
14+
"targets": {
15+
"esmodules": true
16+
},
17+
"modules": "cjs"
18+
}],
19+
"@babel/preset-typescript"
20+
];
21+
22+
const plugins = [
23+
["@babel/plugin-proposal-class-properties", { "loose": false }]
24+
];
25+
26+
return {
27+
...config,
28+
presets,
29+
plugins
30+
};
31+
};

package.json

+29-11
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,44 @@
11
{
22
"name": "react-geocode",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"description": "A module to transform a description of a location (i.e. street address, town name, etc.) into geographic coordinates (i.e. latitude and longitude) and vice versa.",
5-
"main": "./lib/index.js",
6-
"types": "./lib/index.d.ts",
5+
"author": "Pir Shukarulalh Shah <[email protected]> (http://www.shukarullah.com)",
6+
"contributors": [
7+
{
8+
"name": "Mike Pouncy",
9+
"url": "https://github.com/pouncyisdead/"
10+
}
11+
],
12+
"license": "MIT",
13+
"homepage": "https://github.com/shukerullah/react-geocode#readme",
714
"repository": {
815
"type": "git",
916
"url": "git+https://github.com/shukerullah/react-geocode.git"
1017
},
18+
"bugs": {
19+
"url": "https://github.com/shukerullah/react-geocode/issues"
20+
},
1121
"keywords": [
1222
"google",
1323
"geocode",
1424
"geocoder",
1525
"geocoding"
1626
],
17-
"author": "Pir Shukarulalh Shah <[email protected]> (http://www.shukarullah.com)",
18-
"license": "MIT",
19-
"bugs": {
20-
"url": "https://github.com/shukerullah/react-geocode/issues"
27+
"main": "./lib/index.js",
28+
"types": "./lib/index.d.ts",
29+
"scripts": {
30+
"build": "npm run build:types && npm run build:js",
31+
"build:js": "babel src -d lib --extensions '.js'",
32+
"build:types": "tsc --emitDeclarationOnly"
2133
},
22-
"homepage": "https://github.com/shukerullah/react-geocode#readme",
23-
"dependencies": {
24-
"regenerator-runtime": "^0.13.3"
25-
}
34+
"devDependencies": {
35+
"@babel/cli": "^7.7.7",
36+
"@babel/core": "^7.7.7",
37+
"@babel/plugin-proposal-class-properties": "^7.7.4",
38+
"@babel/preset-env": "^7.7.7",
39+
"@babel/preset-typescript": "^7.7.7",
40+
"babel-preset-minify": "^0.5.1",
41+
"typescript": "^3.7.4"
42+
},
43+
"dependencies": {}
2644
}

src/index.js

+45-45
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@
77

88
/** @type {boolean | null} */
99
let DEBUG = false;
10+
1011
/** @type {string | null} */
1112
let API_KEY = null;
13+
1214
/** @type {string | null} */
1315
let LANGUAGE = "en";
16+
1417
/** @type {string | number | boolean | null} */
1518
let REGION = null;
19+
1620
/** @type {string | null} */
1721
const GOOGLE_API = "https://maps.google.com/maps/api/geocode/json";
1822

1923
/**
2024
* @param {string} message
2125
* @param {boolean} warn
26+
* @returns {void}
2227
*/
2328
function log(message, warn = false) {
2429
if (DEBUG) {
@@ -59,46 +64,71 @@ async function handleUrl(url) {
5964
);
6065
}
6166

67+
/**
68+
* @param {string} [apiKey]
69+
* @param {string} [language]
70+
* @param {string | null} [region]
71+
* @returns {string}
72+
*/
73+
function buildQueryString(apiKey, language, region) {
74+
let queryString = "";
75+
76+
if (apiKey || API_KEY) {
77+
API_KEY = apiKey || API_KEY;
78+
queryString += `&key=${API_KEY}`;
79+
}
80+
81+
if (language || LANGUAGE) {
82+
LANGUAGE = language || LANGUAGE;
83+
queryString += `&language=${LANGUAGE}`;
84+
}
85+
86+
if (region || REGION) {
87+
REGION = region || REGION;
88+
// @ts-ignore
89+
queryString += `&region=${encodeURIComponent(REGION)}`;
90+
}
91+
92+
return queryString;
93+
}
94+
95+
/**
96+
* @namespace reactGeocode
97+
*/
6298
const reactGeocode = {
6399
/**
64-
*
65-
*
66100
* @param {string} apiKey
101+
* @returns {void}
67102
*/
68103
setApiKey(apiKey) {
69104
API_KEY = apiKey;
70105
},
71106

72107
/**
73-
*
74-
*
75108
* @param {string} language
109+
* @returns {void}
76110
*/
77111
setLanguage(language) {
78112
LANGUAGE = language;
79113
},
80114

81115
/**
82-
*
83-
*
84116
* @param {string} region
117+
* @returns {void}
85118
*/
86119
setRegion(region) {
87120
REGION = region;
88121
},
89122

90123
/**
91-
*
92-
*
93124
* @param {boolean} [flag=true]
125+
* @returns {void}
94126
*/
95127
enableDebug(flag = true) {
96128
DEBUG = flag;
97129
},
98130

99131
/**
100-
*
101-
*
102132
* @param {string} lat
103133
* @param {string} lng
104134
* @param {string} [apiKey]
@@ -113,30 +143,13 @@ const reactGeocode = {
113143
}
114144

115145
const latLng = `${lat},${lng}`;
116-
let url = `${GOOGLE_API}?latlng=${encodeURIComponent(latLng)}`;
117-
118-
if (apiKey || API_KEY) {
119-
API_KEY = apiKey || API_KEY;
120-
url += `&key=${API_KEY}`;
121-
}
122146

123-
if (language || LANGUAGE) {
124-
LANGUAGE = language || LANGUAGE;
125-
url += `&language=${LANGUAGE}`;
126-
}
127-
128-
if (region || REGION) {
129-
REGION = region || REGION;
130-
// @ts-ignore
131-
url += `&region=${encodeURIComponent(REGION)}`;
132-
}
147+
const url = `${GOOGLE_API}?latlng=${encodeURIComponent(latLng)}${buildQueryString(apiKey, language, region)}`;
133148

134149
return handleUrl(url);
135150
},
136151

137152
/**
138-
*
139-
*
140153
* @param {string} address
141154
* @param {string} [apiKey]
142155
* @param {string} [language]
@@ -149,26 +162,13 @@ const reactGeocode = {
149162
return Promise.reject(new Error("Provided address is invalid"));
150163
}
151164

152-
let url = `${GOOGLE_API}?address=${encodeURIComponent(address)}`;
153-
154-
if (apiKey || API_KEY) {
155-
API_KEY = apiKey || API_KEY;
156-
url += `&key=${API_KEY}`;
157-
}
158-
159-
if (language || LANGUAGE) {
160-
LANGUAGE = language || LANGUAGE;
161-
url += `&language=${LANGUAGE}`;
162-
}
163-
164-
if (region || REGION) {
165-
REGION = region || REGION;
166-
// @ts-ignore
167-
url += `&region=${encodeURIComponent(REGION)}`;
168-
}
165+
const url = `${GOOGLE_API}?address=${encodeURIComponent(address)}${buildQueryString(apiKey, language, region)}`;
169166

170167
return handleUrl(url);
171168
}
172169
};
173170

171+
/**
172+
* @module 'react-geocode'
173+
*/
174174
export default reactGeocode;

tsconfig.json

+28-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
11
{
2-
"compilerOptions": { /* Enable incremental compilation */
3-
"target": "ES5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
4-
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
5-
"lib": [ "ES2015", "DOM" ] /* Specify library files to be included in the compilation. */,
6-
"allowJs": true /* Allow javascript files to be compiled. */,
7-
"checkJs": true /* Report errors in .js files. */,
8-
"declaration": true /* Generates corresponding '.d.ts' file. */,
9-
"noEmit": false /* Do not emit outputs. */,
10-
"strict": true /* Enable all strict type-checking options. */,
11-
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
12-
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */,
13-
"outDir": "lib/"
14-
}
2+
"compilerOptions": {
3+
"target": "ES6",
4+
"module": "commonjs",
5+
"lib": ["ES2015", "DOM"],
6+
"baseUrl": "./src",
7+
"outDir": "./lib",
8+
"declaration": true,
9+
"noEmit": false,
10+
"sourceMap": false,
11+
"importHelpers": false,
12+
"allowJs": true,
13+
"checkJs": true,
14+
"strict": true,
15+
"alwaysStrict": true,
16+
"strictNullChecks": true,
17+
"esModuleInterop": true,
18+
"removeComments": true,
19+
"allowSyntheticDefaultImports": true,
20+
"allowUnreachableCode": false,
21+
"noImplicitAny": true,
22+
"noImplicitThis": true,
23+
"noUnusedLocals": true,
24+
"noUnusedParameters": true,
25+
"noImplicitReturns": true,
26+
"noFallthroughCasesInSwitch": true,
27+
"forceConsistentCasingInFileNames": true
28+
},
29+
"exclude": ["lib", "node_modules", "babel.config.js"],
1530
}

0 commit comments

Comments
 (0)