postal-code-scraper 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/.mocharc.json +4 -0
  2. package/LICENSE +21 -0
  3. package/README.md +194 -0
  4. package/build/test/src/index.js +26 -0
  5. package/build/test/src/index.js.map +1 -0
  6. package/build/test/src/scraper/fetchers.js +49 -0
  7. package/build/test/src/scraper/fetchers.js.map +1 -0
  8. package/build/test/src/scraper/parsers.js +63 -0
  9. package/build/test/src/scraper/parsers.js.map +1 -0
  10. package/build/test/src/scraper/queue.js +69 -0
  11. package/build/test/src/scraper/queue.js.map +1 -0
  12. package/build/test/src/scraper/scrapers.js +148 -0
  13. package/build/test/src/scraper/scrapers.js.map +1 -0
  14. package/build/test/src/types.js +3 -0
  15. package/build/test/src/types.js.map +1 -0
  16. package/build/test/src/utils/id-generator.js +33 -0
  17. package/build/test/src/utils/id-generator.js.map +1 -0
  18. package/build/test/src/utils/logger.js +87 -0
  19. package/build/test/src/utils/logger.js.map +1 -0
  20. package/build/test/tests/postal-code-scraper.test.js +14 -0
  21. package/build/test/tests/postal-code-scraper.test.js.map +1 -0
  22. package/dist/index.d.ts +3 -0
  23. package/dist/index.js +25 -0
  24. package/dist/scraper/fetchers.d.ts +9 -0
  25. package/dist/scraper/fetchers.js +48 -0
  26. package/dist/scraper/parsers.d.ts +7 -0
  27. package/dist/scraper/parsers.js +62 -0
  28. package/dist/scraper/queue.d.ts +12 -0
  29. package/dist/scraper/queue.js +67 -0
  30. package/dist/scraper/scrapers.d.ts +19 -0
  31. package/dist/scraper/scrapers.js +149 -0
  32. package/dist/types.d.ts +32 -0
  33. package/dist/types.js +2 -0
  34. package/dist/utils/env-config.d.ts +1 -0
  35. package/dist/utils/env-config.js +7 -0
  36. package/dist/utils/id-generator.d.ts +4 -0
  37. package/dist/utils/id-generator.js +26 -0
  38. package/dist/utils/logger.d.ts +33 -0
  39. package/dist/utils/logger.js +86 -0
  40. package/dist/utils/string-utils.d.ts +1 -0
  41. package/dist/utils/string-utils.js +13 -0
  42. package/package.json +61 -0
  43. package/src/index.ts +3 -0
  44. package/src/scraper/fetchers.ts +30 -0
  45. package/src/scraper/parsers.ts +67 -0
  46. package/src/scraper/queue.ts +55 -0
  47. package/src/scraper/scrapers.ts +143 -0
  48. package/src/types.ts +37 -0
  49. package/src/utils/env-config.ts +3 -0
  50. package/src/utils/id-generator.ts +35 -0
  51. package/src/utils/logger.ts +105 -0
  52. package/src/utils/string-utils.ts +9 -0
  53. package/tests/postal-code-scraper.test.ts +100 -0
  54. package/tests/tsconfig.json +13 -0
  55. package/tsconfig.json +15 -0
@@ -0,0 +1,100 @@
1
+ import { expect } from "chai";
2
+ import sinon from "sinon";
3
+ import { PostalCodeScraper, ScraperConfig } from "../src";
4
+ import path from "path";
5
+ import fs from "fs";
6
+
7
+ const TEST_DATA_DIR = "./tests/test-data";
8
+
9
+ describe("PostalCodeScraper", () => {
10
+ let scraper: PostalCodeScraper;
11
+ let config: ScraperConfig;
12
+
13
+ before(async function () {
14
+ this.timeout(20000);
15
+ config = {
16
+ headless: true,
17
+ concurrency: 10,
18
+ logger: console,
19
+ directory: TEST_DATA_DIR,
20
+ };
21
+
22
+ scraper = new PostalCodeScraper(config);
23
+ await scraper.scrapeCountry("romania");
24
+ });
25
+
26
+ afterEach(async () => {
27
+ sinon.restore();
28
+ });
29
+
30
+ after(() => {
31
+ fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
32
+ });
33
+
34
+ describe("Country Scraping", function () {
35
+ it("should handle country found", async () => {
36
+ const postalCodesFile = path.join(TEST_DATA_DIR, "romania-postal-codes.json");
37
+ const lookupFile = path.join(TEST_DATA_DIR, "romania-lookup.json");
38
+
39
+ expect(fs.existsSync(postalCodesFile), "Postal codes file should exist").to.be.true;
40
+ expect(fs.existsSync(lookupFile), "Lookup file should exist").to.be.true;
41
+ });
42
+
43
+ it("should handle country not found", async () => {
44
+ const loggerSpy = sinon.spy(console, "warn");
45
+ await scraper.scrapeCountry("NonExistentCountry");
46
+ expect(loggerSpy.calledOnce).to.be.true;
47
+ });
48
+ });
49
+
50
+ describe("Data Processing", () => {
51
+ it("should generate valid postal code data", async () => {
52
+ const postalCodesFile = path.join(TEST_DATA_DIR, "romania-postal-codes.json");
53
+ const postalCodesContent = JSON.parse(fs.readFileSync(postalCodesFile, "utf8"));
54
+
55
+ const expectedPostalCodes = {
56
+ alba: {
57
+ abrud: ["515100"],
58
+ },
59
+ };
60
+
61
+ expect(postalCodesContent).to.have.property("alba");
62
+ expect(postalCodesContent.alba).to.have.property("abrud");
63
+ expect(postalCodesContent.alba.abrud).to.deep.equal(expectedPostalCodes.alba.abrud);
64
+ });
65
+
66
+ it("should generate valid postal code lookup", () => {
67
+ const lookupFile = path.join(TEST_DATA_DIR, "romania-lookup.json");
68
+ const lookupContent = JSON.parse(fs.readFileSync(lookupFile, "utf8"));
69
+
70
+ const expectedLookup = {
71
+ postalCodeMap: {
72
+ "450145": "zalau_1",
73
+ },
74
+ regions: {
75
+ zalau_1: ["salaj", "zalau"],
76
+ },
77
+ };
78
+
79
+ expect(lookupContent).to.have.property("postalCodeMap");
80
+ expect(lookupContent.postalCodeMap).to.have.property("450145");
81
+ expect(lookupContent.regions.zalau_1).to.deep.equal(expectedLookup.regions.zalau_1);
82
+ expect(lookupContent).to.have.property("regions");
83
+ expect(lookupContent.regions).to.have.property("zalau_1");
84
+ expect(lookupContent.regions.zalau_1).to.deep.equal(expectedLookup.regions.zalau_1);
85
+ });
86
+ });
87
+
88
+ describe("Error Handling", () => {
89
+ it("should retry failed fetches", async () => {
90
+ const fetcherStub = sinon.stub((scraper as any).fetcher, "fetchWithRetry").rejects(new Error("Network error"));
91
+
92
+ try {
93
+ await (scraper as any).getCountriesDetails();
94
+ } catch (err) {
95
+ expect(err).to.be.an("error");
96
+ expect(fetcherStub.callCount).to.equal(config.maxRetries);
97
+ }
98
+ });
99
+ });
100
+ });
@@ -0,0 +1,13 @@
1
+ {
2
+ "extends": "../tsconfig.json",
3
+
4
+ "compilerOptions": {
5
+ "outDir": "../build/test",
6
+ "rootDir": "../",
7
+ "rootDirs": ["../src", "./"],
8
+ "noEmitOnError": true,
9
+ "declaration": false,
10
+ "sourceMap": true
11
+ },
12
+ "include": ["./**/*", "../src/**/*"]
13
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "compilerOptions": {
3
+ "rootDir": "./src",
4
+ "outDir": "./dist",
5
+ "forceConsistentCasingInFileNames": true,
6
+ "strict": true,
7
+ "skipLibCheck": true,
8
+ "declaration": true,
9
+ "target": "ES6",
10
+ "module": "CommonJS",
11
+ "esModuleInterop": true,
12
+ "moduleResolution": "node"
13
+ },
14
+ "include": ["src/**/*"]
15
+ }