Skip to content

Commit 8c2d180

Browse files
Joxitmissinglink
authored andcommitted
feat(resources): Add custom resources directory which is git ignored (#27)
1 parent 5872f3d commit 8c2d180

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,8 @@ resources/whosonfirst/dictionaries/*/*.txt
1616
!resources/whosonfirst/dictionaries/locality/name:eng_x_preferred.txt
1717
!resources/whosonfirst/dictionaries/locality/name:fra_x_preferred.txt
1818

19+
# ignore all custom dictionaries
20+
resources/custom/dictionaries
21+
1922
# ignore any openaddresses test cases
2023
test/oa

resources/custom/custom.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
const dictPath = path.join(__dirname, `./dictionaries`)
4+
5+
function load (filename, add, remove) {
6+
let filepath = path.join(dictPath, filename)
7+
if (!fs.existsSync(filepath)) { return }
8+
let dict = fs.readFileSync(filepath, 'utf8')
9+
dict.split('\n').forEach(row => {
10+
if (row.trim().startsWith('#')) {
11+
// Do nothing, this is a comment
12+
} else if (row.startsWith('!')) {
13+
row.substring(1).split('|').forEach(remove)
14+
} else {
15+
row.split('|').forEach(add)
16+
}
17+
})
18+
}
19+
20+
module.exports.load = load

resources/libpostal/libpostal.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const fs = require('fs')
22
const path = require('path')
33
const pelias = require('../pelias/pelias')
4+
const custom = require('../custom/custom')
45
const dictPath = path.join(__dirname, `./dictionaries`)
56
const allLanguages = fs.readdirSync(dictPath).filter(p => !p.includes('.'))
67

@@ -20,6 +21,10 @@ function load (index, langs, filename, options) {
2021
langs.forEach(lang => {
2122
pelias.load(path.join('libpostal', lang, filename), add, remove)
2223
})
24+
25+
langs.forEach(lang => {
26+
custom.load(path.join('libpostal', lang, filename), add, remove)
27+
})
2328
}
2429

2530
function _normalize (cell, options) {

resources/whosonfirst/whosonfirst.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const fs = require('fs')
22
const path = require('path')
33
const pelias = require('../pelias/pelias')
4+
const custom = require('../custom/custom')
45
const dictPath = path.join(__dirname, `./dictionaries`)
56
const allPlacetypes = fs.readdirSync(dictPath).filter(p => !p.includes('.'))
67

@@ -24,6 +25,12 @@ function load (set, placetypes, filenames, options) {
2425
pelias.load(path.join('whosonfirst', placetype, filename), add, remove)
2526
})
2627
})
28+
29+
placetypes.forEach(placetype => {
30+
filenames.forEach(filename => {
31+
custom.load(path.join('whosonfirst', placetype, filename), add, remove)
32+
})
33+
})
2734
}
2835

2936
function _normalize (cell, options) {

0 commit comments

Comments
 (0)