-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 26ad07c
Showing
7 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
name: Node.js CI | ||
|
||
on: push | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: 16 | ||
registry-url: 'https://registry.npmjs.org' | ||
|
||
- run: npm install | ||
- run: npm run build | ||
|
||
|
||
- name: Generate Contributors Images | ||
uses: jaywcjlove/github-action-contributors@main | ||
with: | ||
filter-author: (renovate\[bot\]|renovate-bot|dependabot\[bot\]) | ||
output: dist/CONTRIBUTORS.svg | ||
avatarSize: 42 | ||
|
||
- run: npm install [email protected] -g | ||
- run: idoc -s "path-templater {{version}}" | ||
- run: npm run coverage | ||
- run: tree -I 'node_modules|.git|.github|src' -L 3 -a | ||
|
||
- name: Deploy Website | ||
uses: peaceiris/actions-gh-pages@v3 | ||
with: | ||
user_name: 'github-actions[bot]' | ||
user_email: 'github-actions[bot]@users.noreply.github.com' | ||
github_token: ${{ secrets.GITHUB_TOKEN }} | ||
publish_dir: ./dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
dist | ||
build | ||
lib | ||
node_modules | ||
|
||
npm-debug.log* | ||
lerna-debug.log | ||
yarn-error.log | ||
package-lock.json | ||
|
||
.DS_Store | ||
.cache | ||
.vscode | ||
.idea | ||
.env | ||
|
||
*.mpassword | ||
*.bak | ||
*.tem | ||
*.temp | ||
#.swp | ||
*.*~ | ||
~*.* | ||
|
||
# IDEA | ||
*.iml | ||
*.ipr | ||
*.iws | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
path-templater | ||
=== | ||
|
||
This is a simple path template replacement tool for nodejs. | ||
|
||
## Installation | ||
|
||
```shell | ||
$ npm install typenexus | ||
``` | ||
|
||
## Quick start | ||
|
||
```typescript | ||
import pathTemplater from 'path-templater'; | ||
|
||
pathTemplater('/:name', { name: 'wcj' }) // => /wcj | ||
pathTemplater('/:name/:name', { name: 'wcj' }) // => /wcj/wcj | ||
pathTemplater('http://localhost/:name/:name', { name: 'wcj' }) // => http://localhost/wcj/wcj | ||
|
||
pathTemplater('http://github.com/:owner/:repo', { owner: 'jaywcjlove', repo: 'path-templater' }) | ||
// => http://github.com/jaywcjlove/path-templater | ||
|
||
pathTemplater(':apiBaseUrl/:owner/:repo', { owner: 'jaywcjlove', repo: 'path-templater', apiBaseUrl: 'http://github.com' }) | ||
// => http://github.com/jaywcjlove/path-templater | ||
|
||
pathTemplater('http://localhost:3001/:name/:name', { name: 'wcj' }) | ||
// => http://localhost:3001/wcj/wcj | ||
|
||
pathTemplater('http://localhost:3001/:name/:name?id=:user', { name: 'wcj', user: 'jaywcjlove' }) | ||
// => http://localhost:3001/wcj/wcj?id=jaywcjlove | ||
|
||
pathTemplater(':apiBaseUrl/:owner/:repo', { owner: 'jaywcjlove', repo: 'path-templater' }) | ||
// => Could not find url parameter apiBaseUrl in passed options object; | ||
``` | ||
|
||
## License | ||
|
||
This package is licensed under the MIT License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "path-templater", | ||
"version": "0.0.1", | ||
"description": "This is a simple path template replacement tool for nodejs.", | ||
"author": "kenny wong <[email protected]>", | ||
"license": "MIT", | ||
"type": "module", | ||
"main": "./lib/index.js", | ||
"types": "./lib/index.d.ts", | ||
"homepage": "https://jaywcjlove.github.io/path-templater", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/jaywcjlove/path-templater.git" | ||
}, | ||
"keywords": [ | ||
"path", | ||
"templater", | ||
"nodejs", | ||
"TypeScript" | ||
], | ||
"files": [ | ||
"lib", | ||
"src" | ||
], | ||
"devDependencies": { | ||
"tsbb": "^3.7.9" | ||
}, | ||
"scripts": { | ||
"watch": "tsbb watch --disable-babel", | ||
"build": "tsbb build --disable-babel", | ||
"test": "tsbb test", | ||
"coverage": "tsbb test --coverage --bail" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// @ts-nocheck | ||
import pathTemplater from './index'; | ||
|
||
it('Path Templater test case', async () => { | ||
expect(pathTemplater('/:name', { name: 'wcj' })).toEqual('/wcj'); | ||
expect(pathTemplater('/:name/:name', { name: 'wcj' })).toEqual('/wcj/wcj'); | ||
expect(pathTemplater('http://localhost/:name/:name', { name: 'wcj' })).toEqual('http://localhost/wcj/wcj'); | ||
expect(pathTemplater('http://github.com/:owner/:repo', { owner: 'jaywcjlove', repo: 'path-templater' })).toEqual('http://github.com/jaywcjlove/path-templater'); | ||
expect(() => pathTemplater(':apiBaseUrl/:owner/:repo', { owner: 'jaywcjlove', repo: 'path-templater' })).toThrow('Could not find url parameter apiBaseUrl in passed options object'); | ||
expect(pathTemplater(':apiBaseUrl/:owner/:repo', { owner: 'jaywcjlove', repo: 'path-templater', apiBaseUrl: 'http://github.com' })).toEqual('http://github.com/jaywcjlove/path-templater'); | ||
expect(pathTemplater('http://localhost:3001/:name/:name', { name: 'wcj' })).toEqual('http://localhost:3001/wcj/wcj'); | ||
expect(pathTemplater('http://localhost:3001/:name/:name?id=:user', { name: 'wcj', user: 'jaywcjlove' })).toEqual('http://localhost:3001/wcj/wcj?id=jaywcjlove'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import url from 'node:url'; | ||
|
||
export default function pathTemplater(str: string, options: Record<string, string> = {}) { | ||
const parsedUrl = url.parse(str); | ||
parsedUrl.pathname = replace(parsedUrl.pathname, options); | ||
parsedUrl.search = replace(parsedUrl.search, options); | ||
return url.format(parsedUrl); | ||
} | ||
|
||
function replace(str: string, options: Record<string, string>) { | ||
if (!str) { | ||
return str; | ||
} | ||
|
||
return str.replace(/:(\w+)/gi, (match, p1) => { | ||
const replacement = options[p1]; | ||
if (!replacement) { | ||
throw new Error('Could not find url parameter ' + p1 + ' in passed options object'); | ||
} | ||
return replacement; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/tsconfig", | ||
"include": ["src"], | ||
"compilerOptions": { | ||
"module": "Node16", | ||
"target": "ESNext", | ||
"esModuleInterop": true, | ||
"declaration": true, | ||
"noImplicitAny": true, | ||
"resolveJsonModule": true, | ||
"strict": false, | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"sourceMap": false, | ||
"skipLibCheck": true, | ||
"outDir": "lib", | ||
"baseUrl": "." | ||
} | ||
} |