Skip to content

Commit

Permalink
feat: 当没有提供插值变量时返回保留原始字符串,如:flexvars.replace("I am {}") => "I am {}"
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangfisher committed Feb 5, 2025
1 parent 74a7492 commit 2a1d229
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .changeset/honest-books-confess.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
"flexvars": patch
---

feat: reserve {} when vars is empty
feat: 当没有提供插值变量时返回保留原始字符串,如:`flexvars.replace("I am {}") => "I am {}"`
```
4 changes: 2 additions & 2 deletions .github/workflows/website.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
pnpm docs:build # or npm run docs:build / yarn docs:build / bun run docs:build
touch docs/.vitepress/dist/.nojekyll
- name: Upload artifact
uses: actions/upload-pages-artifact@v2
uses: actions/upload-pages-artifact@v3
with:
path: docs/.vitepress/dist

Expand All @@ -69,4 +69,4 @@ jobs:
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v2
uses: actions/deploy-pages@v4
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
{
"name": "flexvars",
"version": "1.0.5",
"description": "",
"description": "Powerful string interpolation tool library",
"homepage": "https://zhangfisher.github.io/flexvars/",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"types": "dist/index.d.ts",
"repository": {
"type": "git",
"url": "git+https://gitee.com/zhangfisher/flexvars.git"
},
"scripts": {
"test": "vitest --",
"test:coverage": "vitest --coverage",
Expand All @@ -29,6 +34,6 @@
"vitest": "^0.34.4"
},
"dependencies": {
"flex-tools": "^1.4.14"
"flex-tools": "^1.4.27"
}
}
20 changes: 14 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ describe("FlexVars",()=>{
beforeEach(() => {
flexvars = new FlexVars()
})
test("无变量时保留原始值", () => {
expect(flexvars.replace("I am {}")).toBe("I am {}")
expect(flexvars.replace("{}{}{}")).toBe("{}{}{}")
expect(flexvars.replace("{x}{y}{z}")).toBe("{x}{y}{z}")
})

test("位置变量插值", () => {
expect(flexvars.replace("I am {}","tom")).toBe("I am tom")
Expand Down
1 change: 1 addition & 0 deletions src/flexvars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export class FlexVars<FilterContext extends Dict = Dict>{
* flexvars.replace("I am { value | upper }",{value:"hello"}) --> "I am HELLO"
*/
replace(template: string, ...args: any[]) {
if(args.length===0) return template
if(args.length === 1 && typeof(args[0])=='function') args[0] = args[0].call(this)
// ****************************变量插值****************************
if (args.length === 1 && isPlainObject(args[0])) {
Expand Down

0 comments on commit 2a1d229

Please sign in to comment.