Skip to content

Commit

Permalink
Merge pull request #1 from JasonBoy/v2
Browse files Browse the repository at this point in the history
Migrate to TypeScript
  • Loading branch information
JasonBoy authored Jul 23, 2019
2 parents 624d3bf + 4daea0b commit 7e80623
Show file tree
Hide file tree
Showing 22 changed files with 10,925 additions and 2,456 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ coverage
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
scripts/Release

# Dependency directories
node_modules
Expand All @@ -39,4 +39,6 @@ jspm_packages
--no-cache
.idea
config.json
.Ds_Store
.Ds_Store
dist
!dist/location*.json
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
language: node_js
node_js:
- stable
script: npm test
- "8"
- "10"
- "stable"
script: npm test
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
[![npm](https://img.shields.io/npm/v/china-location.svg)](https://www.npmjs.com/package/china-location)
[![Building Status](https://travis-ci.org/JasonBoy/china-location.svg?branch=master)](https://travis-ci.org/JasonBoy/china-location)

NPM module for [中国行政区划信息](https://github.com/mumuy/data_location#中国行政区划信息)
Simplify the use of chinese administrative division data.
JS Library for [中国行政区划信息](https://github.com/mumuy/data_location#中国行政区划信息)
Simplify the usage of chinese administrative division data.

[An React Component](https://github.com/JasonBoy/react-china-location) For this

Expand All @@ -13,8 +13,9 @@ Simplify the use of chinese administrative division data.
`yarn add china-location`

```javascript
const list = require('china-location/dist/location.json');
const ChinaLocation = require('china-location');
import list from 'china-location/dist/location.json';
import ChinaLocation from 'china-location';
//const ChinaLocation = require('china-location');
const location = new ChinaLocation(list);

//get default location
Expand Down Expand Up @@ -58,8 +59,8 @@ npm run reformat -- /path/to/data_location/list.json
And in your project, you can:

```javascript
const yourNewLocation = require('path/to/location.json');
const ChinaLocation = require('china-location');
import yourNewLocation from 'path/to/location.json';
import ChinaLocation from 'china-location';
const location = new ChinaLocation(yourNewLocation);
//...
```
Expand Down
57 changes: 57 additions & 0 deletions __tests__/ChinaLocation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import list from "../dist/location.json";
import ChinaLocation from "../lib/ChinaLocation";

describe("ChinaLocation", function() {
let newProvince: string;
let newCity: string;
let newDistrict: string;

beforeAll(() => {
newProvince = "320000";
newCity = "320500";
newDistrict = "320509";
});

it("should get default location", function() {
const location = new ChinaLocation(list);
const defaultLocation = location.getCurrentAddress();
expect(defaultLocation.province.code).toEqual("110000");
expect(defaultLocation.city.code).toEqual("110000");
expect(defaultLocation.district.code).toEqual("110101");
});

it("should change location", function() {
const location = new ChinaLocation(list);
location.changeProvince(newProvince);
location.changeCity(newCity);
location.changeDistrict(newDistrict);
const newLocation = location.getCurrentAddress();
expect(newLocation.province.name).toEqual("江苏省");
expect(newLocation.city.name).toEqual("苏州市");
expect(newLocation.district.name).toEqual("吴江区");
});

it("should change location at one time", function() {
const location = new ChinaLocation(list);
location.changeLocation(newProvince, newCity, newDistrict);
const newLocation = location.getCurrentAddress();
expect(newLocation.province.name).toEqual("江苏省");
expect(newLocation.city.name).toEqual("苏州市");
expect(newLocation.district.name).toEqual("吴江区");
});

it("should have no district", function() {
const location = new ChinaLocation(list);
location.changeLocation("440000", "441900");
const newLocation = location.getCurrentAddress();
expect(newLocation.province.name).toEqual("广东省");
expect(newLocation.city.name).toEqual("东莞市");
expect(newLocation.district.name).toEqual("");
});

it("should get new added location", function() {
const location = new ChinaLocation(list);
const district = location.getDistrictByCode("370614", "370600", "370000");
expect(district).toEqual("蓬莱区");
});
});
3 changes: 0 additions & 3 deletions build/index.js

This file was deleted.

Loading

0 comments on commit 7e80623

Please sign in to comment.