vbb-find-stations
Advanced tools
Comparing version 1.7.0 to 3.0.0
79
index.js
'use strict' | ||
const fs = require('fs') | ||
const path = require('path') | ||
const filter = require('stream-filter') | ||
const through = require('through2') | ||
const leven = require('leven') | ||
const tokenize = require('vbb-tokenize-station') | ||
const ndjson = require('ndjson') | ||
const stream = require('stream') | ||
const autocomplete = require('vbb-stations-autocomplete') | ||
let rawData = require('vbb-stations/simple') | ||
const stations = Object.create(null) | ||
for (let s of rawData) stations[s.id] = s | ||
rawData = null | ||
const exact = (query) => through.obj(function (station, _, cb) { | ||
const tokens = Array.from(station.tokens) | ||
for (let fragment of query) { | ||
const i = tokens.indexOf(fragment) | ||
if (i < 0) { | ||
tokens.splice(i, 1) | ||
return cb() | ||
} | ||
} | ||
station = Object.assign({type: 'station'}, station) | ||
this.push(station) | ||
cb() | ||
}) | ||
const fuzzy = (query) => through.obj(function (station, _, cb) { | ||
const tokens = Array.from(station.tokens) | ||
let relevance = .5 / station.name.length | ||
for (let fragment of query) { | ||
let i = tokens.indexOf(fragment) // try exact matching | ||
if (i >= 0) {relevance += 1; tokens.splice(i, 1); continue} | ||
let distance = 3 | ||
for (let j = 0; j < tokens.length; j++) { | ||
const d = leven(tokens[j], fragment) | ||
if (d < distance) {distance = d; i = j} | ||
} | ||
if (i >= 0) { | ||
relevance += 1 / (distance + 1) | ||
tokens.splice(i, 1) | ||
continue | ||
} | ||
return cb() // fragment not found in tokens | ||
} | ||
station = Object.assign({type: 'station', relevance}, station) | ||
this.push(station) | ||
cb() | ||
}) | ||
const find = (query, filter = exact) => { | ||
query = tokenize(query) | ||
if (query.length === 0) return empty() | ||
return fs.createReadStream(path.join(__dirname, 'stations.ndjson')) | ||
.pipe(ndjson.parse()) | ||
.pipe(filter(query)) | ||
const findStations = (query, fuzzy = false) => { | ||
return autocomplete(query, 100, fuzzy, false) | ||
.filter(res => !!stations[res.id]) | ||
.map(res => ({...res, ...stations[res.id]})) | ||
} | ||
const empty = () => { | ||
const s = new stream.PassThrough({objectMode: true}) | ||
s.end() | ||
return s | ||
} | ||
module.exports = Object.assign(find, {exact, fuzzy}) | ||
module.exports = findStations |
{ | ||
"name": "vbb-find-stations", | ||
"description": "Search for stations of VBB.", | ||
"version": "1.7.0", | ||
"main": "index.js", | ||
"files": ["index.js", "stations.ndjson"], | ||
"keywords": ["vbb", "stations", "search", "berlin", "public transport", "open data"], | ||
"author": "Jannis R <mail@jannisr.de>", | ||
"homepage": "https://github.com/derhuerst/vbb-find-stations", | ||
"repository": "git://github.com/derhuerst/vbb-find-stations.git", | ||
"license": "ISC", | ||
"engines": {"node": ">=6"}, | ||
"name": "vbb-find-stations", | ||
"description": "Search for stations of VBB.", | ||
"version": "3.0.0", | ||
"main": "index.js", | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"vbb", | ||
"stations", | ||
"search", | ||
"berlin", | ||
"public transport", | ||
"open data" | ||
], | ||
"author": "Jannis R <mail@jannisr.de>", | ||
"homepage": "https://github.com/derhuerst/vbb-find-stations", | ||
"repository": "git://github.com/derhuerst/vbb-find-stations.git", | ||
"license": "ISC", | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"dependencies": { | ||
"stream-filter": "^2.1", | ||
"through2": "^2.0.1", | ||
"leven": "^2.1.0", | ||
"vbb-tokenize-station": "^0.3.0", | ||
"ndjson": "^1.4.3" | ||
"vbb-stations-autocomplete": "^4.0.0" | ||
}, | ||
"devDependencies": { | ||
"vbb-common-places": "^1.1.0", | ||
"vbb-stations": "^5.1.0", | ||
"blue-tape": "^1.0.0", | ||
"so": "^1.0.1", | ||
"tap-min": "^1.2.1" | ||
"tap-min": "^2.0.0", | ||
"tape": "^4.6.3", | ||
"vbb-stations": "^7.0.0" | ||
}, | ||
"scripts": { | ||
"build": "node build.js", | ||
"test": "tape test.js | tap-min", | ||
"prepublish": "npm run build; npm test" | ||
"test": "tape test.js | tap-min", | ||
"prepublish": "npm test" | ||
} | ||
} |
# vbb-find-stations | ||
*vbb-find-stations* provides a **stations search for the Berlin Brandenburg public transport service (VBB)**. It pulls its data from [`vbb-static`](https://github.com/derhuerst/vbb-static). | ||
**Find stations of Berlin Brandenburg public transport service (VBB).** This module is only a wrapper around [`vbb-stations-autocomplete`](https://github.com/derhuerst/vbb-stations-autocomplete). | ||
*Note*: [*vbb-stations-autocomplete*](https://github.com/derhuerst/vbb-stations-autocomplete) delivers the `n` most relevant stations for a query, this module returns all that match the query. | ||
[![npm version](https://img.shields.io/npm/v/vbb-find-stations.svg)](https://www.npmjs.com/package/vbb-find-stations) | ||
@@ -11,2 +9,3 @@ [![build status](https://img.shields.io/travis/derhuerst/vbb-find-stations.svg)](https://travis-ci.org/derhuerst/vbb-find-stations) | ||
[![gitter channel](https://badges.gitter.im/derhuerst/vbb-rest.svg)](https://gitter.im/derhuerst/vbb-rest) | ||
[![support me on Patreon](https://img.shields.io/badge/support%20me-on%20patreon-fa7664.svg)](https://patreon.com/derhuerst) | ||
@@ -25,44 +24,70 @@ | ||
const findStations = require('vbb-find-stations') | ||
``` | ||
This module accepts a filter function (that filters all stations by the first argument) as the second argument. `findStations.exact` and `findStations.fuzzy` are included, with `findStations.exact` being the default one. | ||
```javascript | ||
findStations('U Steglitz') | ||
.on('data', console.log) | ||
console.log(findStations('U Steglitz')) | ||
``` | ||
The stream emits stations in the [*Friendly Public Transport Format*](https://github.com/public-transport/friendly-public-transport-format). | ||
`findStations` returns [*Friendly Public Transport Format* 1.2.1](https://github.com/public-transport/friendly-public-transport-format/blob/1.2.1/spec/readme.md) `station`s. | ||
```js | ||
{ | ||
[ { | ||
type: 'station', | ||
id: '900000062202', | ||
name: 'S+U Rathaus Steglitz (Berlin)', | ||
tokens: ['sbahn', 'ubahn', 'rathaus', 'steglitz', 'berlin'] | ||
} | ||
{ | ||
location: { | ||
type: 'location', | ||
latitude: 52.455066, | ||
longitude: 13.322152 | ||
}, | ||
weight: 2736, | ||
relevance: 3.1185246962418125, | ||
score: 43.61687553989209 | ||
}, { | ||
type: 'station', | ||
id: '900000062282', | ||
name: 'S+U Rathaus Steglitz/Kreisel (Berlin)', | ||
tokens: ['sbahn', 'ubahn', 'rathaus', 'steglitz', 'kreisel', 'berlin'] | ||
} | ||
{ | ||
type: 'station', | ||
id: '900000062781', | ||
name: 'S+U Rathaus Steglitz (Berlin) [U9]', | ||
tokens: ['sbahn', 'ubahn', 'rathaus', 'steglitz', 'berlin', 'u9'] | ||
} | ||
{ | ||
location: { | ||
type: 'location', | ||
latitude: 52.456438, | ||
longitude: 13.319986 | ||
}, | ||
weight: 2831.5, | ||
relevance: 2.4948197569934503, | ||
score: 35.294851852317365 | ||
}, { | ||
type: 'station', | ||
id: '900000062782', | ||
name: 'S+U Rathaus Steglitz (Berlin) [Bus Schloßstr.]', | ||
tokens: ['sbahn', 'ubahn', 'rathaus', 'steglitz', 'berlin', 'bus', 'schloss', 'strasse'] | ||
} | ||
{ | ||
location: { | ||
type: 'location', | ||
latitude: 52.456755, | ||
longitude: 13.320584 | ||
}, | ||
weight: 4831.25, | ||
relevance: 2.0790164641612083, | ||
score: 35.14615060514675 | ||
}, { | ||
type: 'station', | ||
id: '900000062282', | ||
name: 'S+U Rathaus Steglitz/Kreisel (Berlin)', | ||
location: { | ||
type: 'location', | ||
latitude: 52.455889, | ||
longitude: 13.320852 | ||
}, | ||
weight: 2563.75, | ||
relevance: 2.4948197569934503, | ||
score: 34.14531045766747 | ||
}, { | ||
type: 'station', | ||
id: '900000062784', | ||
name: 'S+U Rathaus Steglitz (Berlin) [Bus Albrechtstr.]', | ||
tokens: ['sbahn', 'ubahn', 'rathaus', 'steglitz', 'berlin', 'bus', 'albrecht', 'strasse'] | ||
} | ||
location: { | ||
type: 'location', | ||
latitude: 52.45668, | ||
longitude: 13.321685 | ||
}, | ||
weight: 2834, | ||
relevance: 2.0790164641612083, | ||
score: 29.421030297346405 | ||
} ] | ||
``` | ||
@@ -69,0 +94,0 @@ |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
1
3
97
1
4470
4
12
+ Addedb4a@1.6.7(transitive)
+ Addedcommander@2.20.3(transitive)
+ Addeddiscontinuous-range@1.0.0(transitive)
+ Addedescape-string-regexp@4.0.0(transitive)
+ Addedgerman-license-plate-prefixes@2.18.0(transitive)
+ Addedhifo@1.0.0(transitive)
+ Addedleven@3.1.0(transitive)
+ Addedmoo@0.5.2(transitive)
+ Addednearley@2.20.1(transitive)
+ Addednormalize-vbb-station-name-for-search@1.0.1(transitive)
+ Addedparse-vbb-station-name@2.1.2(transitive)
+ Addedprotocol-buffers-encodings@1.2.0(transitive)
+ Addedrailroad-diagrams@1.0.0(transitive)
+ Addedrandexp@0.4.6(transitive)
+ Addedret@0.1.15(transitive)
+ Addedsigned-varint@2.0.1(transitive)
+ Addedsynchronous-autocomplete@2.3.1(transitive)
+ Addedvarint@5.0.0(transitive)
+ Addedvbb-stations-autocomplete@4.16.0(transitive)
- Removedleven@^2.1.0
- Removedndjson@^1.4.3
- Removedstream-filter@^2.1
- Removedthrough2@^2.0.1
- Removedvbb-tokenize-station@^0.3.0
- Removedcore-util-is@1.0.3(transitive)
- Removedinherits@2.0.4(transitive)
- Removedisarray@1.0.0(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedleven@2.1.0(transitive)
- Removedminimist@1.2.8(transitive)
- Removedndjson@1.5.0(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedsplit2@2.2.0(transitive)
- Removedstream-filter@2.1.0(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedthrough2@2.0.5(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removedxtend@4.0.2(transitive)