Skip to content

Commit 61eff41

Browse files
committed
initial splitting from kmb-lwb-combined-eta
0 parents  commit 61eff41

34 files changed

+4377
-0
lines changed

.eslintignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules/
2+
/dist/

.eslintrc

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"root" : true,
3+
"parser" : "@typescript-eslint/parser",
4+
"plugins" : [
5+
"@typescript-eslint"
6+
],
7+
"extends" : [
8+
"eslint:recommended",
9+
"plugin:@typescript-eslint/eslint-recommended",
10+
"plugin:@typescript-eslint/recommended"
11+
],
12+
"overrides" : [
13+
{
14+
"rules" : {
15+
"no-debugger" : "off"
16+
},
17+
"files" : "**"
18+
},
19+
{
20+
"files" : "*.js",
21+
"rules": {
22+
"@typescript-eslint/no-var-requires": "off"
23+
}
24+
}
25+
]
26+
}

.gitignore

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
2+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
3+
4+
# User-specific stuff
5+
.idea/**/workspace.xml
6+
.idea/**/tasks.xml
7+
.idea/**/usage.statistics.xml
8+
.idea/**/dictionaries
9+
.idea/**/shelf
10+
11+
# Generated files
12+
.idea/**/contentModel.xml
13+
14+
# Sensitive or high-churn files
15+
.idea/**/dataSources/
16+
.idea/**/dataSources.ids
17+
.idea/**/dataSources.local.xml
18+
.idea/**/sqlDataSources.xml
19+
.idea/**/dynamic.xml
20+
.idea/**/uiDesigner.xml
21+
.idea/**/dbnavigator.xml
22+
23+
# Gradle
24+
.idea/**/gradle.xml
25+
.idea/**/libraries
26+
27+
# Gradle and Maven with auto-import
28+
# When using Gradle or Maven with auto-import, you should exclude module files,
29+
# since they will be recreated, and may cause churn. Uncomment if using
30+
# auto-import.
31+
# .idea/artifacts
32+
# .idea/compiler.xml
33+
# .idea/jarRepositories.xml
34+
# .idea/modules.xml
35+
# .idea/*.iml
36+
# .idea/modules
37+
# *.iml
38+
# *.ipr
39+
40+
# CMake
41+
cmake-build-*/
42+
43+
# Mongo Explorer plugin
44+
.idea/**/mongoSettings.xml
45+
46+
# File-based project format
47+
*.iws
48+
49+
# IntelliJ
50+
out/
51+
52+
# mpeltonen/sbt-idea plugin
53+
.idea_modules/
54+
55+
# JIRA plugin
56+
atlassian-ide-plugin.xml
57+
58+
# Cursive Clojure plugin
59+
.idea/replstate.xml
60+
61+
# Crashlytics plugin (for Android Studio and IntelliJ)
62+
com_crashlytics_export_strings.xml
63+
crashlytics.properties
64+
crashlytics-build.properties
65+
fabric.properties
66+
67+
# Editor-based Rest Client
68+
.idea/httpRequests
69+
70+
# Android studio 3.1+ serialized cache file
71+
.idea/caches/build_file_checksums.ser
72+
73+
/node_modules/
74+
/dist/
75+
/scratch/
76+
/yarn-error.log

.idea/codeStyles/codeStyleConfig.xml

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

+302
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/jsLibraryMappings.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/kmb-api.iml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations/test.xml

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.mocharc.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extension" : ["ts"],
3+
"spec" : "test",
4+
"require" : "register-mocha-requires.js"
5+
}

CHANGELOG.md

Whitespace-only changes.

LICENCE

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Copyright 2020 Michael Tsang
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8+
9+

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Javascript module for getting KMB data
2+
This is a library for getting KMB data. It can get routes, stops, variants, and ETAs.
3+
4+
## Classes
5+
### `Route`
6+
Represents a route with number and bound.
7+
### `Stop`
8+
Represents a stop with ID, name, direction in route and sequence in route.
9+
The name is stored in localStorage automatically.
10+
11+
Calling `Stop.get` with a variant will return the stop list of that variant.
12+
13+
### `IncompleteStop`
14+
Represents a stop with ID only. The name property is retrieved from localStorage by `Stop`
15+
16+
17+
### `StopRoute`
18+
Represents a stop in a variant.
19+
20+
Calling `StopRoute.get` with a stop will return all `StopRoute`s containing with all routes serving that stop.
21+
22+
### `Variant`
23+
Represents a variant in a route.
24+
25+
Calling `Variant.get` with a route will return all variants of that route.
26+
27+
### `Eta`
28+
Represents an ETA entry.
29+
30+
Calling `Eta.get` with a StopRoute will return the ETA for that stop for that route.
31+
32+
## Node.js
33+
This library is designed for use on the browser. Usage on node.js requires [global-jsdom](https://www.npmjs.com/package/global-jsdom) to be loaded.

package.json

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "js-kmb-api",
3+
"description": "Library for querying KMB data",
4+
"version": "2.0.0-beta.1",
5+
"type": "module",
6+
"main": "dist/src/index.js",
7+
"types": "dist/src/index.d.ts",
8+
"author": "Michael Tsang",
9+
"license": "MIT",
10+
"files": [
11+
"dist/src"
12+
],
13+
"devDependencies": {
14+
"@testdeck/mocha": "^0.1.2",
15+
"@types/aes-js": "^3.1.1",
16+
"@types/chai": "^4.2.13",
17+
"@types/jquery": "^3.5.3",
18+
"@types/mocha": "^8.0.3",
19+
"@types/node-localstorage": "^1.3.0",
20+
"@types/sinon": "^9.0.8",
21+
"@typescript-eslint/eslint-plugin": "^4.4.1",
22+
"@typescript-eslint/parser": "^4.4.1",
23+
"chai": "^4.2.0",
24+
"eslint": "^7.11.0",
25+
"mocha": "^8.1.3",
26+
"mock-local-storage": "^1.1.15",
27+
"sinon": "^9.2.0",
28+
"source-map-support": "^0.5.19",
29+
"ts-node": "^9.0.0",
30+
"typescript": "^4.0.3"
31+
},
32+
"dependencies": {
33+
"aes-js": "^3.1.2",
34+
"global-jsdom": "^7.0.0",
35+
"jquery": "^3.5.1",
36+
"jsdom": "^16.4.0"
37+
},
38+
"scripts": {
39+
"build": "tsc",
40+
"test": "mocha"
41+
}
42+
}

register-mocha-requires.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import TsNode from 'ts-node';
2+
import SourceMapSupport from 'source-map-support';
3+
import globalJsDom from 'global-jsdom';
4+
5+
SourceMapSupport.install();
6+
TsNode.register({compilerOptions : {"module":"commonjs"}});
7+
globalJsDom();
8+
9+
import 'mock-local-storage';

src/Common.ts

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import {Language} from "./Language";
2+
import $ from "jquery";
3+
4+
export default class Common {
5+
public static readonly PROXY_URL = 'https://miklcct.com/proxy/';
6+
public static readonly API_ENDPOINT = 'https://search.kmb.hk/KMBWebSite/Function/FunctionRequest.ashx';
7+
8+
public static async callApi(query : Record<string, string>) : Promise<Record<string, unknown>> {
9+
return $.get(Common.API_ENDPOINT, query);
10+
}
11+
12+
/**
13+
* Get the stop ID in the query string
14+
*/
15+
public static getQueryStopId() : string | null {
16+
return (new URLSearchParams(window.location.search)).get('stop');
17+
}
18+
19+
/**
20+
* Get the selected route IDs and stop positions in the query string
21+
*/
22+
public static getQuerySelections() : [string, number|null][]{
23+
return (new URLSearchParams(window.location.search)).getAll('selections').map(
24+
item => {
25+
const segments = item.split(':');
26+
return [segments[0], segments.length >= 2 ? Number(segments[1]) : null];
27+
}
28+
);
29+
}
30+
31+
/**
32+
* Get if "one departure" mode is selected
33+
*/
34+
public static getQueryOneDeparture() : boolean {
35+
return Boolean((new URLSearchParams(window.location.search)).get('one_departure'));
36+
}
37+
38+
/**
39+
* Get the language used in the document
40+
*/
41+
public static getLanguage() : Language {
42+
return ($('html').attr('lang') ?? 'en') as Language;
43+
}
44+
}
45+
46+
declare global {
47+
// noinspection JSUnusedGlobalSymbols
48+
interface String {
49+
toTitleCase(): string;
50+
}
51+
}
52+
53+
/**
54+
* Convert string to title case
55+
*
56+
* FIXME: this is English only
57+
*/
58+
String.prototype.toTitleCase = function () {
59+
return this.toLowerCase().replace(/((^|[^a-z0-9'])+)(.)/g, (match, p1, p2, p3) => p1 + p3.toUpperCase());
60+
};

0 commit comments

Comments
 (0)