Skip to content

Commit

Permalink
Publish npm package (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
daohoangson authored Aug 21, 2021
1 parent 8534bee commit 647ccb9
Show file tree
Hide file tree
Showing 15 changed files with 27,238 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ Ví dụ:
## Thư viện hỗ trợ

- Dart [pub.dev](https://pub.dev/packages/dvhcvn)
- JavaScript [npm](https://www.npmjs.com/package/dvhcvn)

## Demo

Expand Down
1 change: 1 addition & 0 deletions transformers/js-dvhcvn/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib/
50 changes: 50 additions & 0 deletions transformers/js-dvhcvn/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Các đơn vị hành chính Việt Nam

Dữ liệu được cập nhật từ [daohoangson/dvhcvn](https://github.com/daohoangson/dvhcvn).

## Ví dụ sử dụng

### JavaScript

```js
import { findLevel1ById } from 'dvhcvn'

// ...

const haNoi = findLevel1ById('01')
const baDinh = haNoi?.findLevel2ById('001')
const phucXa = baDinh?.findLevel3ById('00001')

console.log(phucXa); // Thành phố Hà Nội > Quận Ba Đình > Phường Phúc Xá
```
### Next.js
https://github.com/dvhcvn/nextjs-demo
## API
### const level1s
Đây là `array` chứa tất cả các đơn vị hành chính cấp 1 (thành phố trực thuộc Trung ương / tỉnh).
### findLevel1ById(string)
Tìm đơn vị hành chính cấp 1 theo ID.
### findLevel1ByName(string)
Tìm đơn vị hành chính cấp 1 theo tên.
### class Level1, Level2, Level3
Mỗi class này tương ứng với một cấp đơn vị hành chính.
Fields:
- `id: string`
- `name: string`
- `type: Type`
`Level2``Level3` có thêm field `parent`.
`Level1`, `Level2` có thêm field `children` và methods `findLevelXById`, `findLevelXByName`.
84 changes: 84 additions & 0 deletions transformers/js-dvhcvn/bin/generate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { readFileSync } from 'fs'

const stdout = {
write: (msg: string) => process.stdout.write(msg),
writeln: (msg: string = '') => process.stdout.write(`${msg}\n`)
}

const types: { [key: string]: string } = {
'Thành phố Trung ương': 'tptw',
Tỉnh: 'tinh',
'Thành phố': 'tp',
Quận: 'quan',
Huyện: 'huyen',
Phường: 'phuong',
: 'xa',
'Thị trấn': 'thi_tran',
'Thị xã': 'thi_xa'
}

function main (args: string[]): void {
stdout.writeln("import { Level1, Level2, Level3, Type } from './model';")
stdout.writeln()

stdout.write('export const level1s = [')

const txt = readFileSync(args[0], 'utf8')
const json = JSON.parse(txt)
const data = json.data as any[]
for (var i = 0; i < data.length; i++) {
_processLevel1(i, data[i])
}

stdout.write('];')
}

function _getString (str: string): string {
if (!str.includes("'")) return `'${str}'`
if (!str.includes('"')) return `"${str}"`
return "'" + str.replace("'", "\\'") + "'"
}

function _getType (str: string): string {
if (str in types) return `Type.${types[str]}`
throw new Error(`Type not found: ${str}`)
}

function _processLevel1 (level1Index: number, level1: any): void {
const id = _getString(level1.level1_id)
const name = _getString(level1.name)
const type = _getType(level1.type)
stdout.write(`new Level1(${id}, ${name}, ${type}, [\n`)

const level2s = level1.level2s as any[]
for (var i = 0; i < level2s.length; i++) {
_processLevel2(level1Index, i, level2s[i])
}

stdout.writeln(']),')
}

function _processLevel2 (level1Index: number, level2Index: number, level2: any): void {
const id = _getString(level2.level2_id)
const name = _getString(level2.name)
const type = _getType(level2.type)
stdout.write(`new Level2(${level1Index}, ${id}, ${name}, ${type}, [\n`)

const level3s = level2.level3s as any[]
for (const level3 of level3s) {
_processLevel3(level1Index, level2Index, level3)
}

stdout.writeln(']),')
}

function _processLevel3 (level1Index: number, level2Index: number, level3: any): void {
const id = _getString(level3.level3_id)
const name = _getString(level3.name)
const type = _getType(level3.type)
stdout.writeln(`new Level3(${level1Index}, ${level2Index}, ${id}, ${name}, ${type}),`)
}

if (require.main === module) {
main(process.argv.slice(process.argv.indexOf(__filename) + 1))
}
10 changes: 10 additions & 0 deletions transformers/js-dvhcvn/jestconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"transform": {
"^.+\\.ts$": "ts-jest"
},
"testRegex": "/__tests__/.*\\.test\\.ts$",
"moduleFileExtensions": [
"js",
"ts"
]
}
Loading

1 comment on commit 647ccb9

@vercel
Copy link

@vercel vercel bot commented on 647ccb9 Aug 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.