Skip to content

Commit 2edc1ef

Browse files
ExamplegetCurrentThread
authored andcommitted
feat(npm): add package configuration and GitHub Actions workflow
1 parent 65fdc70 commit 2edc1ef

File tree

7 files changed

+131
-1
lines changed

7 files changed

+131
-1
lines changed

.github/workflows/npm-publish.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Publish to npm Registry
2+
3+
on:
4+
push:
5+
tags:
6+
- '*'
7+
8+
jobs:
9+
build-and-publish:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v3
13+
14+
- name: Setup Node.js
15+
uses: actions/setup-node@v3
16+
with:
17+
node-version: '18'
18+
registry-url: 'https://registry.npmjs.org'
19+
20+
- name: Install dependencies
21+
run: npm ci || npm install
22+
23+
- name: Build
24+
run: npm run build
25+
26+
- name: Publish to npm
27+
run: npm publish --access public
28+
env:
29+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.git
2+
.github
3+
test.js
4+
name-cache.json
5+
.npmrc

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
registry=https://registry.npmjs.org/

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
이 라이브러리는 안전하고 유연한 템플릿 문자열 파싱 및 평가 기능을 제공합니다. 복잡한 수학 표현식, 변수 참조, 함수 호출, 배열 인덱싱 등을 지원하며, `eval()` 또는 `new Function()`을 사용하지 않아 보안상 안전합니다.
44

5+
## 설치
6+
7+
### npm을 통한 설치
8+
9+
```bash
10+
npm install safe-template-parser
11+
```
12+
13+
### 직접 다운로드
14+
15+
최신 릴리스에서 `safe-template-parser.min.js` 파일을 다운로드하여 프로젝트에 포함할 수 있습니다.
16+
517
## 주요 기능
618

719
- 변수 참조 및 중첩 객체 속성 접근
@@ -16,6 +28,10 @@
1628
### 1. 라이브러리 가져오기
1729

1830
```javascript
31+
// ESM
32+
import { parseTemplateString } from 'safe-template-parser';
33+
34+
// CommonJS
1935
const { parseTemplateString } = require('safe-template-parser');
2036
```
2137

package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "safe-template-parser",
3+
"version": "1.0.0",
4+
"description": "A safe template string parser for JavaScript",
5+
"main": "safe-template-parser.js",
6+
"module": "safe-template-parser.js",
7+
"type": "module",
8+
"scripts": {
9+
"test": "echo \"No tests available\" && exit 0",
10+
"build": "npx terser safe-template-parser.js -o safe-template-parser.min.js"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git+https://github.com/getCurrentThread/SafeTemplateStringJS.git"
15+
},
16+
"keywords": [
17+
"template",
18+
"parser",
19+
"template-string",
20+
"safe"
21+
],
22+
"author": "getCurrentThread",
23+
"license": "MIT",
24+
"bugs": {
25+
"url": "https://github.com/getCurrentThread/SafeTemplateStringJS/issues"
26+
},
27+
"homepage": "https://github.com/getCurrentThread/SafeTemplateStringJS#readme",
28+
"devDependencies": {
29+
"terser": "^5.19.0"
30+
}
31+
}

safe-template-parser.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ class Interpreter {
365365
}
366366
}
367367

368-
export function parseTemplateString(templateString, data) {
368+
function parseTemplateString(templateString, data) {
369369
const regex = /\{\{(.+?)\}\}/g;
370370

371371
return templateString.replace(regex, (match, expression) => {
@@ -382,6 +382,26 @@ export function parseTemplateString(templateString, data) {
382382
});
383383
}
384384

385+
// Support for both CommonJS and ES modules
386+
if (typeof module !== 'undefined' && module.exports) {
387+
// CommonJS
388+
module.exports = { parseTemplateString };
389+
} else if (typeof exports !== 'undefined') {
390+
// CommonJS (alternative)
391+
exports.parseTemplateString = parseTemplateString;
392+
} else if (typeof define === 'function' && define.amd) {
393+
// AMD
394+
define([], function() {
395+
return { parseTemplateString };
396+
});
397+
} else if (typeof window !== 'undefined') {
398+
// Browser global
399+
window.parseTemplateString = parseTemplateString;
400+
}
401+
402+
// ES modules
403+
export { parseTemplateString };
404+
385405
// // 사용 예시
386406
// const template =
387407
// "안녕하세요, {{name}}님. 당신의 나이는 {{age}}세이고, {{address.city}}에 살고 계시네요. " +

test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Test script for safe-template-parser
2+
import { parseTemplateString } from './safe-template-parser.js';
3+
4+
// Test template
5+
const template = "안녕하세요, {{name}}님. 당신의 나이는 {{age}}세이고, {{address.city}}에 살고 계시네요. " +
6+
"5년 후의 나이는 {{age + 5}}세입니다. " +
7+
"나이의 제곱근은 {{round(abs(age) ^ 0.5)}}입니다. " +
8+
"첫 번째 친구의 이름은 {{friends[0].name}}이고, " +
9+
"가장 나이 많은 친구는 {{max(friends[0].age, friends[1].age)}}세입니다.";
10+
11+
// Test data
12+
const data = {
13+
name: "홍길동",
14+
age: 30,
15+
address: {
16+
city: "서울"
17+
},
18+
friends: [
19+
{ name: "김철수", age: 28 },
20+
{ name: "이영희", age: 32 }
21+
]
22+
};
23+
24+
// Parse template and output the result
25+
console.log("--- Test Result ---");
26+
const result = parseTemplateString(template, data);
27+
console.log(result);
28+
console.log("\nTest completed successfully!");

0 commit comments

Comments
 (0)