Skip to content

Commit 1c4fc70

Browse files
committed
WIP Modernize
- switch to type: module - replace all CJS require/module.exports with ES6 import/expor
1 parent a0bfde2 commit 1c4fc70

20 files changed

+218
-185
lines changed

index.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export { default as matcher } from './lib/matcher.js';
2-
export { default as simplify } from './lib/simplify.js';
3-
export { default as stemmer } from './lib/stemmer.js';
1+
export { Matcher } from './lib/matcher.js';
2+
export { simplify } from './lib/simplify.js';
3+
export { stemmer } from './lib/stemmer.js';

lib/file_tree.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
const colors = require('colors/safe');
2-
const fs = require('fs-extra');
3-
const glob = require('glob');
4-
const idgen = require('./idgen.js');
5-
const JSON5 = require('json5');
6-
const sortObject = require('./sort_object.js');
7-
const stringify = require('@aitodotai/json-stringify-pretty-compact');
8-
const withLocale = require('locale-compare')('en-US');
1+
import colors from 'colors/safe.js';
2+
import fs from 'fs-extra';
3+
import glob from 'glob';
4+
import JSON5 from 'json5';
5+
import localeCompare from 'locale-compare';
6+
import stringify from '@aitodotai/json-stringify-pretty-compact';
7+
8+
import { idgen } from './idgen.js';
9+
import { sortObject } from './sort_object.js';
10+
import { validate } from './validate.js';
11+
12+
const withLocale = localeCompare('en-US');
913

1014
// metadata about the trees
11-
const trees = require('../config/trees.json').trees;
15+
const trees = JSON5.parse(fs.readFileSync('./config/trees.json', 'utf8')).trees;
1216

1317
// validate the files as we read them
14-
const validate = require('./validate.js');
15-
const categoriesSchema = require('../schema/categories.json');
18+
const categoriesSchema = JSON5.parse(fs.readFileSync('./schema/categories.json', 'utf8'));
1619

1720
// The code in here
1821
// - validates data on read, generating any missing data
@@ -32,7 +35,9 @@ const categoriesSchema = require('../schema/categories.json');
3235
// },
3336

3437

35-
exports.read = (cache, loco) => {
38+
export let fileTree = {
39+
40+
read: (cache, loco) => {
3641
cache = cache || {};
3742
cache.id = cache.id || new Map();
3843
cache.path = cache.path || {};
@@ -156,10 +161,10 @@ exports.read = (cache, loco) => {
156161
});
157162

158163
return cache;
159-
};
164+
},
160165

161166

162-
exports.write = (cache) => {
167+
write: (cache) => {
163168
cache = cache || {};
164169
cache.path = cache.path || {};
165170

@@ -305,10 +310,10 @@ exports.write = (cache) => {
305310
if (typeof val !== 'string') return val;
306311
return val.trim().toLowerCase();
307312
}
308-
};
313+
},
309314

310315

311-
exports.expandTemplates = (cache, loco) => {
316+
expandTemplates: (cache, loco) => {
312317
cache = cache || {};
313318
cache.id = cache.id || new Map();
314319
cache.path = cache.path || {};
@@ -406,4 +411,6 @@ exports.expandTemplates = (cache, loco) => {
406411
});
407412

408413
return cache;
414+
}
415+
409416
};

lib/idgen.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
const crypto = require('crypto');
2-
const simplify = require('./simplify.js');
1+
import crypto from 'node:crypto';
2+
import { simplify } from './simplify.js';
3+
34

45
// We want the identifiers to be useable in url strings and other places,
56
// and avoid any unicode or right-to-left surprises,
67
// so limit them to /^\w+$/ (only [A-Za-z0-9_] characters)
7-
module.exports = (item, tkv, locationID) => {
8+
export function idgen(item, tkv, locationID) {
89
let name;
910

1011
const parts = tkv.split('/', 3); // tkv = "tree/key/value"
@@ -53,4 +54,4 @@ module.exports = (item, tkv, locationID) => {
5354
} else {
5455
return null;
5556
}
56-
};
57+
}

lib/matcher.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
const simplify = require('./simplify.js');
2-
const matchGroups = require('../config/matchGroups.json').matchGroups;
3-
const genericWords = require('../config/genericWords.json').genericWords;
4-
const trees = require('../config/trees.json').trees;
5-
const whichPolygon = require('which-polygon');
1+
import whichPolygon from 'which-polygon';
2+
import { simplify } from './simplify.js';
63

4+
// This will not work in the browser :(
5+
// We may be able to switch to `import`, but:
6+
// - for node, with node with --experimental-json-modules https://stackoverflow.com/a/59758333/7620
7+
// - for browser, both esbuild and rollup should be able to bundle a .json import
8+
import fs from 'fs';
9+
const matchGroups = JSON.parse(fs.readFileSync('./config/matchGroups.json', 'utf8')).matchGroups;
10+
const genericWords = JSON.parse(fs.readFileSync('./config/genericWords.json', 'utf8')).genericWords;
11+
const trees = JSON.parse(fs.readFileSync('./config/trees.json', 'utf8')).trees;
712

8-
module.exports = () => {
13+
14+
export function Matcher() {
915

1016
// The `_matchIndex` is a specialized structure that allows us to quickly answer
1117
// _"Given a [key/value tagpair, name, location], what canonical items (brands etc) can match it?"_
@@ -521,4 +527,4 @@ module.exports = () => {
521527

522528

523529
return matcher;
524-
};
530+
}

lib/simplify.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const diacritics = require('diacritics');
1+
import diacritics from 'diacritics';
22

33
// remove spaces, punctuation, diacritics
44
// for punction see https://stackoverflow.com/a/21224179
5-
module.exports = (str) => {
5+
export function simplify(str) {
66
if (typeof str !== 'string') return '';
77

88
return diacritics.remove(
@@ -12,4 +12,4 @@ module.exports = (str) => {
1212
.replace(/[\s\-=_!"#%'*{},.\/:;?\(\)\[\]@\\$\^*+<>«»~`\u00a1\u00a7\u00b6\u00b7\u00bf\u037e\u0387\u055a-\u055f\u0589\u05c0\u05c3\u05c6\u05f3\u05f4\u0609\u060a\u060c\u060d\u061b\u061e\u061f\u066a-\u066d\u06d4\u0700-\u070d\u07f7-\u07f9\u0830-\u083e\u085e\u0964\u0965\u0970\u0af0\u0df4\u0e4f\u0e5a\u0e5b\u0f04-\u0f12\u0f14\u0f85\u0fd0-\u0fd4\u0fd9\u0fda\u104a-\u104f\u10fb\u1360-\u1368\u166d\u166e\u16eb-\u16ed\u1735\u1736\u17d4-\u17d6\u17d8-\u17da\u1800-\u1805\u1807-\u180a\u1944\u1945\u1a1e\u1a1f\u1aa0-\u1aa6\u1aa8-\u1aad\u1b5a-\u1b60\u1bfc-\u1bff\u1c3b-\u1c3f\u1c7e\u1c7f\u1cc0-\u1cc7\u1cd3\u2000-\u206f\u2cf9-\u2cfc\u2cfe\u2cff\u2d70\u2e00-\u2e7f\u3001-\u3003\u303d\u30fb\ua4fe\ua4ff\ua60d-\ua60f\ua673\ua67e\ua6f2-\ua6f7\ua874-\ua877\ua8ce\ua8cf\ua8f8-\ua8fa\ua92e\ua92f\ua95f\ua9c1-\ua9cd\ua9de\ua9df\uaa5c-\uaa5f\uaade\uaadf\uaaf0\uaaf1\uabeb\ufe10-\ufe16\ufe19\ufe30\ufe45\ufe46\ufe49-\ufe4c\ufe50-\ufe52\ufe54-\ufe57\ufe5f-\ufe61\ufe68\ufe6a\ufe6b\ufeff\uff01-\uff03\uff05-\uff07\uff0a\uff0c\uff0e\uff0f\uff1a\uff1b\uff1f\uff20\uff3c\uff61\uff64\uff65]+/g,'')
1313
.toLowerCase()
1414
);
15-
};
15+
}

lib/sort_object.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
const withLocale = require('locale-compare')('en-US');
1+
import localeCompare from 'locale-compare';
2+
const withLocale = localeCompare('en-US');
23

34
// Returns an object with sorted keys and sorted values.
45
// (This is useful for file diffing)
5-
module.exports = (obj) => {
6+
export function sortObject(obj) {
67
if (!obj) return null;
78

89
let sorted = {};
@@ -21,4 +22,4 @@ module.exports = (obj) => {
2122
return withLocale(a, b);
2223
}
2324
}
24-
};
25+
}

lib/stemmer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const simplify = require('./simplify.js');
1+
import { simplify } from './simplify.js';
22

33
// Removes noise from the name so that we can compare
44
// similar names for catching duplicates.
5-
module.exports = (str) => {
5+
export function stemmer(str) {
66
if (typeof str !== 'string') return '';
77

88
const noise = [
@@ -17,4 +17,4 @@ module.exports = (str) => {
1717

1818
str = noise.reduce((acc, regex) => acc.replace(regex, ''), str);
1919
return simplify(str);
20-
};
20+
}

lib/validate.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
const colors = require('colors/safe');
2-
const Validator = require('jsonschema').Validator;
1+
import colors from 'colors/safe.js';
2+
import jsonschema from 'jsonschema';
33

44
// Perform JSON Schema validation
5-
module.exports = (fileName, object, schema) => {
5+
export function validate(fileName, object, schema) {
6+
const Validator = jsonschema.Validator;
67
const v = new Validator();
78
const validationErrors = v.validate(object, schema, { nestedErrors: true }).errors;
89
if (validationErrors.length) {
@@ -25,4 +26,4 @@ module.exports = (fileName, object, schema) => {
2526
console.error();
2627
process.exit(1);
2728
}
28-
};
29+
}

lib/write_file_with_meta.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const crypto = require('crypto');
2-
const fs = require('fs');
3-
const JSON5 = require('json5');
4-
const packageJSON = require('../package.json');
1+
import crypto from 'node:crypto';
2+
import fs from 'node:fs';
3+
import JSON5 from 'json5';
54

5+
const packageJSON = JSON.parse(fs.readFileSync('./package.json', 'utf8'));
66
const URLRoot = 'https://raw.githubusercontent.com/osmlab/name-suggestion-index/main';
77

88
//
@@ -13,7 +13,7 @@ const URLRoot = 'https://raw.githubusercontent.com/osmlab/name-suggestion-index/
1313
// `file` = the path to the file
1414
// `contents` = should be stringified json containing an object {}
1515
//
16-
module.exports = (file, contents) => {
16+
export function writeFileWithMeta(file, contents) {
1717
// Load the previous file
1818
let previous = { _meta: { } };
1919
try {
@@ -39,4 +39,4 @@ module.exports = (file, contents) => {
3939
// Stick metadata at the beginning of the file in the most hacky way possible
4040
fs.writeFileSync(file, contents.replace(/^\{/, '{' + meta));
4141
}
42-
};
42+
}

package.json

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,13 @@
5252
"transit",
5353
"wikidata"
5454
],
55+
"type": "module",
5556
"main": "dist/index.js",
56-
"module": "index.mjs",
57+
"module": "./index.mjs",
58+
"exports": {
59+
"import": "./index.mjs",
60+
"require": "./dist/index.js"
61+
},
5762
"nsiguide": "docs/index.html",
5863
"targets": {
5964
"nsiguide": {
@@ -78,7 +83,7 @@
7883
"build": "run-s build:features build:index",
7984
"build:features": "node scripts/build_features.js",
8085
"build:index": "node scripts/build_index.js",
81-
"jest": "jest --coverage",
86+
"jest": "node --experimental-vm-modules node_modules/.bin/jest --no-cache --coverage",
8287
"lint": "eslint scripts/*.js lib/*.js",
8388
"test": "run-s lint build jest",
8489
"validate": "node scripts/validate.js",
@@ -125,20 +130,23 @@
125130
"rollup": "^2.36.2",
126131
"safe-regex": "^2.1.1",
127132
"shelljs": "^0.8.0",
128-
"whatwg-fetch": "^3.5.0",
129-
"xmlbuilder2": "^2.1.2"
130-
},
131-
"optionalDependencies": {
132133
"twitter": "^1.7.1",
134+
"whatwg-fetch": "^3.5.0",
133135
"wikibase-edit": "^4.7.3",
134-
"wikibase-sdk": "^7.7.1"
136+
"wikibase-sdk": "^7.7.1",
137+
"xmlbuilder2": "^2.1.2"
135138
},
136139
"engines": {
137-
"node": ">=10"
140+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
141+
},
142+
"jest": {
143+
"moduleFileExtensions": ["ts", "js", "json"],
144+
"transform": {},
145+
"verbose": true
138146
},
139147
"babel": {
140148
"presets": [
141149
"@babel/preset-react"
142150
]
143151
}
144-
}
152+
}

0 commit comments

Comments
 (0)