Skip to content

Commit

Permalink
Merge pull request #5 from ozgurg/dev
Browse files Browse the repository at this point in the history
release: v1.1.1
  • Loading branch information
ozgurg authored Aug 28, 2022
2 parents 768f65d + 51b53f9 commit 66d9d5f
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 40 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/update-dependencies.js
/.run/
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const currency = await googleCurrencyScraper({
// }
```

_This package is a [pure ESM package](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c)._

## API

### googleCurrencyScraper({ from, to })
Expand Down
52 changes: 32 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "google-currency-scraper",
"version": "1.1.0",
"version": "1.1.1",
"description": "'google-currency-scraper' goes Google '1 USD to TRY' (for example) search result and scrape exchange rate and last updated date for you by using Puppeteer under the hood.",
"license": "MIT",
"type": "module",
Expand Down Expand Up @@ -30,8 +30,8 @@
"currency api"
],
"dependencies": {
"dayjs": "^1.11.4",
"puppeteer": "^16.1.0"
"dayjs": "^1.11.5",
"puppeteer": "^16.2.0"
},
"devDependencies": {
"@jest/globals": "^28.1.3",
Expand Down
19 changes: 11 additions & 8 deletions src/google-currency-scraper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { getDate, parseAndNormalizeDateInSearchResult } from "./utils/date.js";

/**
* @param {object} params
* @param {CurrencyCode|string} params.from
* @param {CurrencyCode|string} params.to
* @returns {Promise<{from: CurrencyCode|string, to: CurrencyCode|string, rate: number, dateUpdated: string}>}
* @param {CurrencyCode | string} params.from
* @param {CurrencyCode | string} params.to
* @returns {Promise<{from: CurrencyCode | string, to: CurrencyCode | string, rate: number, dateUpdated: string}>}
*/
const googleCurrencyScraper = async ({ from, to }) => {
if (!isValidCurrencyCode(from)) {
Expand All @@ -32,10 +32,13 @@ const googleCurrencyScraper = async ({ from, to }) => {

const page = await openNewPage(browser);

// To lighter page and faster load times, I emulate a mobile device
// To lighter page and faster load times, I emulate a mobile device.
// So why iPhone 7?
// It doesn't matter which one as long as it's a mobile device.
// That's why I chose iPhone 7 as I use it in real life :)
await emulateDevice(page, "iPhone 7");

// To lighter page and faster load times, load only document
// To lighter page and faster load times, make sure load only document.
await ensurePageLoadOnlyDocument(page);

await goToGoogleCurrencySearchResult(page, { from, to });
Expand All @@ -55,9 +58,9 @@ const googleCurrencyScraper = async ({ from, to }) => {
/**
* @param {*} page
* @param {object} params
* @param {CurrencyCode|string} params.from
* @param {CurrencyCode|string} params.to
* @returns {Promise<*|null>}
* @param {CurrencyCode | string} params.from
* @param {CurrencyCode | string} params.to
* @returns {Promise<* | null>}
*/
async function goToGoogleCurrencySearchResult(page, { from, to }) {
const qs = objectToQueryString({
Expand Down
2 changes: 1 addition & 1 deletion src/google-currency-scraper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import googleCurrencyScraper from "./google-currency-scraper.js";
import { jest } from "@jest/globals";
import { getDate } from "./utils/date.js";

jest.setTimeout(15000);
jest.setTimeout(20_000);

describe("google-currency-scraper", () => {
const date = Date;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/currency-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ const CurrencyCode = {
};

/**
* @param {CurrencyCode|string} code
* @param {CurrencyCode | string} code
* @returns {boolean}
*/
const isValidCurrencyCode = code => code in CurrencyCode;
Expand Down
15 changes: 12 additions & 3 deletions src/utils/object-to-query-string.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import querystring from "querystring";

/**
* @param {object} object
* @returns {string}
*/
const objectToQueryString = object => {
return querystring.stringify(object);
const objectToArray = [];
for (const [name, values] of Object.entries(object)) {
if (Array.isArray(values)) {
values.forEach(value => {
objectToArray.push([name, value]);
});
} else {
objectToArray.push([name, values]);
}
}

return new URLSearchParams(objectToArray).toString();
};

export {
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"**/*.js"
],
"exclude": [
"**/*.test.js"
"**/*.test.js",
"update-dependencies.js"
],
"compilerOptions": {
"declaration": true,
Expand Down
6 changes: 3 additions & 3 deletions update-dependencies.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { exec } from "child_process";

import packageJson from "./package.json" assert { type: 'json' };
import packageJson from "./package.json" assert { type: "json" };

if(packageJson.dependencies !== undefined) {
if (packageJson.dependencies !== undefined) {
const dependencies = Object.keys(packageJson.dependencies);

(async () => {
await exec(`npm i ${dependencies.join(" ")} --save`);
})();
}

if(packageJson.devDependencies !== undefined) {
if (packageJson.devDependencies !== undefined) {
const devDependencies = Object.keys(packageJson.devDependencies);

(async () => {
Expand Down

0 comments on commit 66d9d5f

Please sign in to comment.