-
Notifications
You must be signed in to change notification settings - Fork 78
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
1 parent
8534bee
commit 647ccb9
Showing
15 changed files
with
27,238 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 @@ | ||
node_modules/ |
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
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 @@ | ||
lib/ |
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,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` và `Level3` có thêm field `parent`. | ||
`Level1`, `Level2` có thêm field `children` và methods `findLevelXById`, `findLevelXByName`. |
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,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', | ||
Xã: '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)) | ||
} |
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,10 @@ | ||
{ | ||
"transform": { | ||
"^.+\\.ts$": "ts-jest" | ||
}, | ||
"testRegex": "/__tests__/.*\\.test\\.ts$", | ||
"moduleFileExtensions": [ | ||
"js", | ||
"ts" | ||
] | ||
} |
Oops, something went wrong.
647ccb9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: