Skip to content

Commit

Permalink
feat: add generic type to Path (#45)
Browse files Browse the repository at this point in the history
* feat: add generic to Path

* Update
  • Loading branch information
troch authored Dec 30, 2019
1 parent 9237b79 commit 33eaed2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
}
}
25 changes: 15 additions & 10 deletions src/Path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,15 @@ export interface IBuildOptions {
queryParams?: IOptions
}

export type TestMatch = Record<string, any> | null

export class Path {
public static createPath(path: string) {
return new Path(path)
export type TestMatch<
T extends Record<string, any> = Record<string, any>
> = T | null

export class Path<T extends Record<string, any> = Record<string, any>> {
public static createPath<T extends Record<string, any> = Record<string, any>>(
path: string
) {
return new Path<T>(path)
}

public path: string
Expand Down Expand Up @@ -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<T> {
const options = { strictTrailingSlash: false, queryParams: {}, ...opts }
// trailingSlash: falsy => non optional, truthy => optional
const source = optTrailingSlash(this.source, options.strictTrailingSlash)
Expand All @@ -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])
)

Expand All @@ -161,7 +166,7 @@ export class Path {
return null
}

public partialTest(path: string, opts?: IPartialTestOptions): TestMatch {
public partialTest(path: string, opts?: IPartialTestOptions): TestMatch<T> {
const options = { delimited: true, queryParams: {}, ...opts }
// Check if partial match (start of given path matches regex)
// trailingSlash: falsy => non optional, truthy => optional
Expand Down Expand Up @@ -287,21 +292,21 @@ export class Path {
path: string,
source: string,
caseSensitive = false
): TestMatch {
): TestMatch<T> {
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
.slice(1, this.urlParams.length + 1)
.reduce<Record<string, any>>((params, m, i) => {
params[this.urlParams[i]] = decodeURIComponent(m)
return params
}, {})
}, {}) as T
}
}

Expand Down
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"outDir": "./dist",
"lib": ["es2015"],
"strict": true,
"alwaysStrict": false
"alwaysStrict": false,
"importHelpers": false
},
"include": ["./src/index.ts"]
"include": ["src", "test"]
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 33eaed2

Please sign in to comment.