Skip to content

Commit

Permalink
feat: improve typings (#62)
Browse files Browse the repository at this point in the history
* refactor type system
* remove enum and use strings instead
* add transform types
* fixing return types to this
* add better return type to query builder arrays
* remove double inline callback functions
* use keyof with specialization for nested properties
* update filter type in dynamic view
* improve typings of full-text query
* rename some properties of dynamic view
* add return and use ++ instead of += 1
* rename resultset to ResultSet
* remove ANY type
* rename Collection.data to Collection._data
* improve typings for adapter and storage package as well
* improve package export with typings
  • Loading branch information
Viatorus committed Dec 19, 2017
1 parent eb4fed5 commit b44f550
Show file tree
Hide file tree
Showing 66 changed files with 1,568 additions and 1,490 deletions.
4 changes: 2 additions & 2 deletions config/karma.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ module.exports = function (config) {
exclude: /node_modules/,
options: {
failOnHint: false,
configFile: path.join("config", "tslint.json"),
configFile: path.join(__dirname, "tslint.json"),
}
},
{
test: /\.ts$/,
loader: "ts-loader",
options: {
configFile: path.join("config", "tsconfig.webpack.json")
configFile: path.join(__dirname, "tsconfig.webpack.json")
}
},
{
Expand Down
17 changes: 5 additions & 12 deletions packages/common/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Loki} from "../loki/src/loki";

export type ANY = any;
/**
* @hidden
*/
import {Loki} from "../loki/src";

export interface StorageAdapter {
loadDatabase(dbname: string): Promise<any>;
Expand All @@ -11,7 +12,7 @@ export interface StorageAdapter {

mode?: string;

exportDatabase?(dbname: string, dbref: ANY): Promise<void>;
exportDatabase?(dbname: string, dbref: Loki): Promise<void>;
}

export type Doc<T extends object = object> = T & { $loki: number; meta: any; };
Expand All @@ -23,13 +24,5 @@ export interface Dict<T> {
}


export interface Query {

}

export interface Filter<E> {
type: string;
/*'find', 'where'*/
val: Query | ((obj: E, index: number, array: E[]) => boolean);
uid: number | string;
}
1 change: 1 addition & 0 deletions packages/fs-storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"url": "https://github.com/LokiJS-Forge/LokiJS2.git"
},
"main": "lokijs.fs-storage.js",
"types": "./types/fs-storage/src/index.d.ts",
"dependencies": {
"@lokijs/loki": "0"
}
Expand Down
10 changes: 5 additions & 5 deletions packages/fs-storage/spec/node/fs_storage.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global describe, it, expect */
import {Loki} from "../../../loki/src/loki";
import {LokiFSStorage} from "../../src/fs_storage";
import {FSStorage} from "../../src/fs_storage";

describe("testing fs storage", function () {

Expand All @@ -9,16 +9,16 @@ describe("testing fs storage", function () {
}

beforeAll(() => {
LokiFSStorage.register();
FSStorage.register();
});

afterAll(() => {
LokiFSStorage.deregister();
FSStorage.deregister();
});

it("LokiFSStorage", function (done) {
const db = new Loki("myTestApp");
const adapter = {adapter: new LokiFSStorage()};
const adapter = {adapter: new FSStorage()};
db.initializePersistence(adapter)
.then(() => {
db.addCollection<Name>("myColl").insert({name: "Hello World"});
Expand All @@ -35,7 +35,7 @@ describe("testing fs storage", function () {
})
.then(() => {
const db2 = new Loki("myTestApp");
return db2.initializePersistence({persistenceMethod: Loki.PersistenceMethod.FS_STORAGE})
return db2.initializePersistence({persistenceMethod: "fs-storage"})
.then(() => {
return db2.loadDatabase();
}).then(() => {
Expand Down
8 changes: 3 additions & 5 deletions packages/fs-storage/src/fs_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import * as fs from "fs";
/**
* A loki persistence adapter which persists using node fs module.
*/
export class LokiFSStorage implements StorageAdapter {
export class FSStorage implements StorageAdapter {
/**
* Registers the fs storage as plugin.
*/
static register(): void {
PLUGINS["LokiFSStorage"] = LokiFSStorage;
PLUGINS["FSStorage"] = FSStorage;
}

/**
* Deregisters the fs storage as plugin.
*/
static deregister(): void {
delete PLUGINS["LokiFSStorage"];
delete PLUGINS["FSStorage"];
}

/**
Expand Down Expand Up @@ -88,5 +88,3 @@ export class LokiFSStorage implements StorageAdapter {
});
}
}

export default LokiFSStorage;
4 changes: 4 additions & 0 deletions packages/fs-storage/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {FSStorage} from "./fs_storage";

export {FSStorage};
export default FSStorage;
1 change: 1 addition & 0 deletions packages/full-text-search-language-de/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"url": "https://github.com/LokiJS-Forge/LokiJS2.git"
},
"main": "lokijs.full-text-search-language-de.js",
"types": "./types/full-text-search-language-de/src/index.d.ts",
"dependencies": {
"@lokijs/full-text-search": "0",
"@lokijs/full-text-search-language": "0"
Expand Down
3 changes: 3 additions & 0 deletions packages/full-text-search-language-de/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {DE} from "./de";
export {DE};
export default DE;
2 changes: 1 addition & 1 deletion packages/full-text-search-language-de/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require("path");
const webpackConigCreator = require('../../config/webpack-config-creator.js');

module.exports = webpackConigCreator({
entry: path.join(__dirname, "src", "de.ts"),
entry: path.join(__dirname, "src", "index.ts"),
filename: "lokijs.full-text-search-language-de.js",
library: "@lokijs/full-text-search-language-de",
externals: {
Expand Down
1 change: 1 addition & 0 deletions packages/full-text-search-language-en/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"url": "https://github.com/LokiJS-Forge/LokiJS2.git"
},
"main": "lokijs.full-text-search-language-en.js",
"types": "./types/full-text-search-language-en/src/index.d.ts",
"dependencies": {
"@lokijs/full-text-search": "0",
"@lokijs/full-text-search-language": "0"
Expand Down
2 changes: 1 addition & 1 deletion packages/full-text-search-language-en/src/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class EnglishStemmer {
const re_3 = /^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$/;

const re_4 = /^(.+?)(al|ance|ence|er|ic|able|ible|ant|ement|ment|ent|ou|ism|ate|iti|ous|ive|ize)$/;
const re2_4 = /^(.+?)(s|t)(ion)$/;
const re2_4 = /^(.+?)([st])(ion)$/;

const re_5 = /^(.+?)e$/;
const re_5_1 = /ll$/;
Expand Down
3 changes: 3 additions & 0 deletions packages/full-text-search-language-en/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {EN} from "./en";
export {EN};
export default EN;
2 changes: 1 addition & 1 deletion packages/full-text-search-language-en/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require("path");
const webpackConigCreator = require('../../config/webpack-config-creator.js');

module.exports = webpackConigCreator({
entry: path.join(__dirname, "src", "en.ts"),
entry: path.join(__dirname, "src", "index.ts"),
filename: "lokijs.full-text-search-language-en.js",
library: "@lokijs/full-text-search-language-en",
externals: {
Expand Down
3 changes: 2 additions & 1 deletion packages/full-text-search-language/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"type": "git",
"url": "https://github.com/LokiJS-Forge/LokiJS2.git"
},
"main": "lokijs.full-text-search-language.js"
"main": "lokijs.full-text-search-language.js",
"types": "./types/full-text-search-language/src/index.d.ts"
}
1 change: 1 addition & 0 deletions packages/full-text-search-language/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {Among, SnowballProgram} from "./language";
2 changes: 1 addition & 1 deletion packages/full-text-search-language/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require("path");
const webpackConigCreator = require('../../config/webpack-config-creator.js');

module.exports = webpackConigCreator({
entry: path.join(__dirname, "src", "language.ts"),
entry: path.join(__dirname, "src", "index.ts"),
filename: "lokijs.full-text-search-language.js",
library: "@lokijs/full-text-search-language"
});
1 change: 1 addition & 0 deletions packages/full-text-search/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"url": "https://github.com/LokiJS-Forge/LokiJS2.git"
},
"main": "lokijs.full-text-search.js",
"types": "./types/full-text-search/src/index.d.ts",
"optionalDependencies": {
"@lokijs/loki": "0"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {LokiMemoryAdapter} from "../../../loki/src/memory_adapter";
import {Collection} from "../../../loki/src/collection";
import {FullTextSearch} from "../../src/full_text_search";
import {Tokenizer} from "../../src/tokenizer";
import {Doc} from "../../../common/types";

describe("full-text search", () => {
FullTextSearch.register();
Expand Down Expand Up @@ -61,7 +62,7 @@ describe("full-text search", () => {
it("update", () => {
coll.updateWhere((user: User) => {
return user.name === "quak";
}, (user: User) => {
}, (user: Doc<User>) => {
user.name = "quaaak";
return user;
});
Expand Down
6 changes: 3 additions & 3 deletions packages/full-text-search/spec/generic/query_builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ describe("query builder", () => {
expect(() => mqb.operator("and")).toThrowErrorOfType("SyntaxError");
expect(() => mqb.operator("not")).toThrowErrorOfType("SyntaxError");
mqb = new Query.MatchQueryBuilder("1", "1").operator("and");
expect(() => mqb.minimumShouldMatch(3)).toThrowErrorOfType("SyntaxError");
expect(() => mqb.minimumShouldMatch(3 as any)).toThrowErrorOfType("SyntaxError");
mqb = new Query.MatchQueryBuilder("1", "1");
expect(() => mqb.fuzziness(3)).toThrowErrorOfType("TypeError");
expect(() => mqb.fuzziness(3 as any)).toThrowErrorOfType("TypeError");
expect(() => mqb.fuzziness("3" as any)).toThrowErrorOfType("TypeError");
expect(() => mqb.fuzziness(-3)).toThrowErrorOfType("TypeError");
expect(() => mqb.fuzziness(-3 as any)).toThrowErrorOfType("TypeError");
expect(() => mqb.prefixLength(-1)).toThrowErrorOfType("TypeError");
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe("fuzzy query", () => {

let fb = new QB().fuzzy("a", "abc");
expect(() => fb.fuzziness("AUTO")).not.toThrowErrorOfType("TypeError");
expect(() => fb.fuzziness(-3)).toThrowErrorOfType("TypeError");
expect(() => fb.fuzziness(-3 as any)).toThrowErrorOfType("TypeError");
expect(() => fb.fuzziness("3" as any)).toThrowErrorOfType("TypeError");
expect(() => fb.prefixLength(-1)).toThrowErrorOfType("TypeError");
expect(() => fb.prefixLength("-1" as any)).toThrowErrorOfType("TypeError");
Expand Down
3 changes: 1 addition & 2 deletions packages/full-text-search/spec/node/elasticsearch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ function fieldLengthES6(fieldLength: number) {
if (i < NUM_FREE_VALUES) {
return i;
} else {
let decoded = NUM_FREE_VALUES + int4ToLong(i - NUM_FREE_VALUES);
return decoded;
return NUM_FREE_VALUES + int4ToLong(i - NUM_FREE_VALUES);
}
}

Expand Down
10 changes: 6 additions & 4 deletions packages/full-text-search/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {FullTextSearch} from "./full_text_search";
export {Tokenizer} from "./tokenizer";
export {QueryBuilder} from "./query_builder";
export {InvertedIndex} from "./inverted_index";
import {Tokenizer} from "./tokenizer";
import {QueryBuilder} from "./query_builder";

export {FullTextSearch};
FullTextSearch["Tokenizer"] = Tokenizer;
FullTextSearch["QueryBuilder"] = QueryBuilder;

export {FullTextSearch, Tokenizer, QueryBuilder};
export default FullTextSearch;
Loading

0 comments on commit b44f550

Please sign in to comment.