Skip to content

Commit

Permalink
v2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
miklcct committed Oct 24, 2020
1 parent 0b4bbf2 commit b529263
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## [2.1.0] (2020-10-24)
* introduce storage versioning - it is now safe to reuse the same storage even the library version is changed

## [2.0.1] (2020-10-24)
* fixed a packaging issue causing missing cert
* fixed a packaging issue causing missing cert preventing running on node.js
* moved @types/node into dev dependencies

## [2.0.0] (2020-10-23)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "js-kmb-api",
"description": "Library for querying KMB data",
"version": "2.0.1",
"version": "2.1.0",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"author": "Michael Tsang",
Expand Down
18 changes: 18 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export default class Kmb {

private readonly apiEndpoint = 'https://search.kmb.hk/KMBWebSite/Function/FunctionRequest.ashx';

// change the below when the storage used is no longer compatible with the old version
public static readonly STORAGE_VERSION_KEY = '$version';
private static readonly stopStorageVersion = 2;
private static readonly stoppingStorageVersion = 2;

/**
* Construct an API instance
* @param language
Expand All @@ -62,6 +67,19 @@ export default class Kmb {
, stoppingStorage? : Storage
, public corsProxyUrl : string | null = null
) {
for (
const {storage, version} of
[
{storage : stopStorage, version : Kmb.stopStorageVersion},
{storage : stoppingStorage, version : Kmb.stoppingStorageVersion},
]
) {
if (storage !== undefined && Number(storage[Kmb.STORAGE_VERSION_KEY]) !== version) {
storage.clear();
storage[Kmb.STORAGE_VERSION_KEY] = version;
}
}

// eslint-disable-next-line @typescript-eslint/no-this-alias
const kmb = this;
this.Stop = class {
Expand Down
28 changes: 28 additions & 0 deletions test/KmbTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,37 @@ import {suite, test, params} from "@testdeck/mocha";
import Kmb from "../src";
import Sinon = require("sinon");
import {assert} from "chai";
import Storage = require('node-storage-shim');

@suite
export class KmbTest extends TestCase {
@test
storageIsClearedWhenConstructing() : void {
const storage = new Storage();
storage.setItem('foo', 'bar');
void new Kmb('en', storage);
assert.isNull(storage.getItem('foo'));
}

@test
storageIsClearedWhenReusingStorageAtPreviousVersion() : void {
const storage = new Storage();
void new Kmb('en', storage);
storage.setItem('foo', 'bar');
storage.setItem(Kmb.STORAGE_VERSION_KEY, String(Number(storage.getItem(Kmb.STORAGE_VERSION_KEY)) - 1));
void new Kmb('en', storage);
assert.isNull(storage.getItem('foo'));
}

@test
storageIsNotClearedWhenReusingStorageAtTheSameVersion() : void {
const storage = new Storage();
void new Kmb('en', storage);
storage.setItem('foo', 'bar');
void new Kmb('en', storage);
assert.strictEqual(storage.getItem('foo'), 'bar');
}

@test
async getRoutes(): Promise<void> {
const json = {
Expand Down
2 changes: 2 additions & 0 deletions test/StopTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export class StopTest extends TestCase {
@test
getNameFromStorage(): void {
const storage = new Storage;
// initialise the storage to the appropriate version
void new Kmb(undefined, storage);
storage.setItem('TS06-S-1000-0_zh-hans', '青雲站');
assert.strictEqual(new new Kmb('zh-hans', storage).Stop('TS06-S-1000-0').name, '青雲站');
}
Expand Down

0 comments on commit b529263

Please sign in to comment.