Skip to content

Commit

Permalink
MP20200108 - Added type definition build step to package lib, (SEE: s…
Browse files Browse the repository at this point in the history
  • Loading branch information
mpouncy-panda committed Jan 8, 2020
1 parent b2d82bf commit eea7b59
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 69 deletions.
31 changes: 31 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports = function(api) {
api.cache(true);

const config = {
"comments": false
}

const presets = [
["minify", {
"mangle": false,
"keepFnName": true
}],
["@babel/preset-env", {
"targets": {
"esmodules": true
},
"modules": "cjs"
}],
"@babel/preset-typescript"
];

const plugins = [
["@babel/plugin-proposal-class-properties", { "loose": false }]
];

return {
...config,
presets,
plugins
};
};
40 changes: 29 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,44 @@
{
"name": "react-geocode",
"version": "0.2.1",
"version": "0.2.2",
"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.",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"author": "Pir Shukarulalh Shah <[email protected]> (http://www.shukarullah.com)",
"contributors": [
{
"name": "Mike Pouncy",
"url": "https://github.com/pouncyisdead/"
}
],
"license": "MIT",
"homepage": "https://github.com/shukerullah/react-geocode#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/shukerullah/react-geocode.git"
},
"bugs": {
"url": "https://github.com/shukerullah/react-geocode/issues"
},
"keywords": [
"google",
"geocode",
"geocoder",
"geocoding"
],
"author": "Pir Shukarulalh Shah <[email protected]> (http://www.shukarullah.com)",
"license": "MIT",
"bugs": {
"url": "https://github.com/shukerullah/react-geocode/issues"
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"scripts": {
"build": "npm run build:types && npm run build:js",
"build:js": "babel src -d lib --extensions '.js'",
"build:types": "tsc --emitDeclarationOnly"
},
"homepage": "https://github.com/shukerullah/react-geocode#readme",
"dependencies": {
"regenerator-runtime": "^0.13.3"
}
"devDependencies": {
"@babel/cli": "^7.7.7",
"@babel/core": "^7.7.7",
"@babel/plugin-proposal-class-properties": "^7.7.4",
"@babel/preset-env": "^7.7.7",
"@babel/preset-typescript": "^7.7.7",
"babel-preset-minify": "^0.5.1",
"typescript": "^3.7.4"
},
"dependencies": {}
}
90 changes: 45 additions & 45 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@

/** @type {boolean | null} */
let DEBUG = false;

/** @type {string | null} */
let API_KEY = null;

/** @type {string | null} */
let LANGUAGE = "en";

/** @type {string | number | boolean | null} */
let REGION = null;

/** @type {string | null} */
const GOOGLE_API = "https://maps.google.com/maps/api/geocode/json";

/**
* @param {string} message
* @param {boolean} warn
* @returns {void}
*/
function log(message, warn = false) {
if (DEBUG) {
Expand Down Expand Up @@ -59,46 +64,71 @@ async function handleUrl(url) {
);
}

/**
* @param {string} [apiKey]
* @param {string} [language]
* @param {string | null} [region]
* @returns {string}
*/
function buildQueryString(apiKey, language, region) {
let queryString = "";

if (apiKey || API_KEY) {
API_KEY = apiKey || API_KEY;
queryString += `&key=${API_KEY}`;
}

if (language || LANGUAGE) {
LANGUAGE = language || LANGUAGE;
queryString += `&language=${LANGUAGE}`;
}

if (region || REGION) {
REGION = region || REGION;
// @ts-ignore
queryString += `&region=${encodeURIComponent(REGION)}`;
}

return queryString;
}

/**
* @namespace reactGeocode
*/
const reactGeocode = {
/**
*
*
* @param {string} apiKey
* @returns {void}
*/
setApiKey(apiKey) {
API_KEY = apiKey;
},

/**
*
*
* @param {string} language
* @returns {void}
*/
setLanguage(language) {
LANGUAGE = language;
},

/**
*
*
* @param {string} region
* @returns {void}
*/
setRegion(region) {
REGION = region;
},

/**
*
*
* @param {boolean} [flag=true]
* @returns {void}
*/
enableDebug(flag = true) {
DEBUG = flag;
},

/**
*
*
* @param {string} lat
* @param {string} lng
* @param {string} [apiKey]
Expand All @@ -113,30 +143,13 @@ const reactGeocode = {
}

const latLng = `${lat},${lng}`;
let url = `${GOOGLE_API}?latlng=${encodeURIComponent(latLng)}`;

if (apiKey || API_KEY) {
API_KEY = apiKey || API_KEY;
url += `&key=${API_KEY}`;
}

if (language || LANGUAGE) {
LANGUAGE = language || LANGUAGE;
url += `&language=${LANGUAGE}`;
}

if (region || REGION) {
REGION = region || REGION;
// @ts-ignore
url += `&region=${encodeURIComponent(REGION)}`;
}
const url = `${GOOGLE_API}?latlng=${encodeURIComponent(latLng)}${buildQueryString(apiKey, language, region)}`;

return handleUrl(url);
},

/**
*
*
* @param {string} address
* @param {string} [apiKey]
* @param {string} [language]
Expand All @@ -149,26 +162,13 @@ const reactGeocode = {
return Promise.reject(new Error("Provided address is invalid"));
}

let url = `${GOOGLE_API}?address=${encodeURIComponent(address)}`;

if (apiKey || API_KEY) {
API_KEY = apiKey || API_KEY;
url += `&key=${API_KEY}`;
}

if (language || LANGUAGE) {
LANGUAGE = language || LANGUAGE;
url += `&language=${LANGUAGE}`;
}

if (region || REGION) {
REGION = region || REGION;
// @ts-ignore
url += `&region=${encodeURIComponent(REGION)}`;
}
const url = `${GOOGLE_API}?address=${encodeURIComponent(address)}${buildQueryString(apiKey, language, region)}`;

return handleUrl(url);
}
};

/**
* @module 'react-geocode'
*/
export default reactGeocode;
41 changes: 28 additions & 13 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
{
"compilerOptions": { /* Enable incremental compilation */
"target": "ES5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"lib": [ "ES2015", "DOM" ] /* Specify library files to be included in the compilation. */,
"allowJs": true /* Allow javascript files to be compiled. */,
"checkJs": true /* Report errors in .js files. */,
"declaration": true /* Generates corresponding '.d.ts' file. */,
"noEmit": false /* Do not emit outputs. */,
"strict": true /* Enable all strict type-checking options. */,
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */,
"outDir": "lib/"
}
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"lib": ["ES2015", "DOM"],
"baseUrl": "./src",
"outDir": "./lib",
"declaration": true,
"noEmit": false,
"sourceMap": false,
"importHelpers": false,
"allowJs": true,
"checkJs": true,
"strict": true,
"alwaysStrict": true,
"strictNullChecks": true,
"esModuleInterop": true,
"removeComments": true,
"allowSyntheticDefaultImports": true,
"allowUnreachableCode": false,
"noImplicitAny": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true
},
"exclude": ["lib", "node_modules", "babel.config.js"],
}

0 comments on commit eea7b59

Please sign in to comment.