Skip to content

Commit

Permalink
init project.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Mar 21, 2023
0 parents commit 26ad07c
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/main.yml
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
29 changes: 29 additions & 0 deletions .gitignore
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/
39 changes: 39 additions & 0 deletions README.md
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.
34 changes: 34 additions & 0 deletions package.json
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"
}
}
13 changes: 13 additions & 0 deletions src/index.test.ts
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');
});
22 changes: 22 additions & 0 deletions src/index.ts
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;
});
}
19 changes: 19 additions & 0 deletions tsconfig.json
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": "."
}
}

0 comments on commit 26ad07c

Please sign in to comment.