From 33eaed2857fb4bd5b28dd504231af23f0b9ba547 Mon Sep 17 00:00:00 2001 From: Thomas Roch Date: Mon, 30 Dec 2019 22:01:34 +0100 Subject: [PATCH] feat: add generic type to `Path` (#45) * feat: add generic to Path * Update --- package.json | 8 ++++---- src/Path.ts | 25 +++++++++++++++---------- tsconfig.json | 5 +++-- yarn.lock | 8 ++++---- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index 63c41ec..0f7c17f 100644 --- a/package.json +++ b/package.json @@ -15,13 +15,13 @@ "start": "tsdx watch", "build": "tsdx build", "test": "tsdx test", - "lint": "tsdx lint", + "lint": "tsdx lint src test", "prepare": "tsdx build", "clog": "conventional-changelog -p angular -i CHANGELOG.md -s" }, "husky": { "hooks": { - "pre-commit": "tsdx lint" + "pre-commit": "tsdx lint src test" } }, "author": "Thomas Roch", @@ -34,14 +34,14 @@ }, "homepage": "https://github.com/troch/path-parser", "dependencies": { - "search-params": "2.1.3" + "search-params": "3.0.0", + "tslib": "^1.10.0" }, "devDependencies": { "conventional-changelog-cli": "2.0.31", "@types/jest": "^24.0.25", "husky": "^3.1.0", "tsdx": "^0.12.1", - "tslib": "^1.10.0", "typescript": "^3.7.4" } } diff --git a/src/Path.ts b/src/Path.ts index 99b2d48..86867f5 100644 --- a/src/Path.ts +++ b/src/Path.ts @@ -72,11 +72,15 @@ export interface IBuildOptions { queryParams?: IOptions } -export type TestMatch = Record | null - -export class Path { - public static createPath(path: string) { - return new Path(path) +export type TestMatch< + T extends Record = Record +> = T | null + +export class Path = Record> { + public static createPath = Record>( + path: string + ) { + return new Path(path) } public path: string @@ -129,7 +133,7 @@ export class Path { return this.spatParams.indexOf(name) !== -1 } - public test(path: string, opts?: ITestOptions): TestMatch { + public test(path: string, opts?: ITestOptions): TestMatch { const options = { strictTrailingSlash: false, queryParams: {}, ...opts } // trailingSlash: falsy => non optional, truthy => optional const source = optTrailingSlash(this.source, options.strictTrailingSlash) @@ -152,6 +156,7 @@ export class Path { if (unexpectedQueryParams.length === 0) { // Extend url match Object.keys(queryParams).forEach( + // @ts-ignore p => (match[p] = (queryParams as any)[p]) ) @@ -161,7 +166,7 @@ export class Path { return null } - public partialTest(path: string, opts?: IPartialTestOptions): TestMatch { + public partialTest(path: string, opts?: IPartialTestOptions): TestMatch { const options = { delimited: true, queryParams: {}, ...opts } // Check if partial match (start of given path matches regex) // trailingSlash: falsy => non optional, truthy => optional @@ -287,13 +292,13 @@ export class Path { path: string, source: string, caseSensitive = false - ): TestMatch { + ): TestMatch { const regex = new RegExp('^' + source, caseSensitive ? '' : 'i') const match = path.match(regex) if (!match) { return null } else if (!this.urlParams.length) { - return {} + return {} as T } // Reduce named params to key-value pairs return match @@ -301,7 +306,7 @@ export class Path { .reduce>((params, m, i) => { params[this.urlParams[i]] = decodeURIComponent(m) return params - }, {}) + }, {}) as T } } diff --git a/tsconfig.json b/tsconfig.json index c4d85e3..8a0838a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,7 +7,8 @@ "outDir": "./dist", "lib": ["es2015"], "strict": true, - "alwaysStrict": false + "alwaysStrict": false, + "importHelpers": false }, - "include": ["./src/index.ts"] + "include": ["src", "test"] } diff --git a/yarn.lock b/yarn.lock index e40394c..cd4cc7e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5663,10 +5663,10 @@ sax@^1.2.4: resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9" integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw== -search-params@2.1.3: - version "2.1.3" - resolved "https://registry.yarnpkg.com/search-params/-/search-params-2.1.3.tgz#90b98964eaf3d0edef0f73e6e8307d1480020413" - integrity sha512-hHxU9ZGWpZ/lrFBIHndSnQae2in7ra+m+tBSoeAahSWDDgOgpZqs4bfaTZpljgNgAgTbjiQoJtZW6FKSsfEcDA== +search-params@3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/search-params/-/search-params-3.0.0.tgz#dbc7c243058e5a33ae1e9870be91f5aced4100d8" + integrity sha512-8CYNl/bjkEhXWbDTU/K7c2jQtrnqEffIPyOLMqygW/7/b+ym8UtQumcAZjOfMLjZKR6AxK5tOr9fChbQZCzPqg== semver-compare@^1.0.0: version "1.0.0"