Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
koba-ninkigumi committed May 20, 2015
0 parents commit e28827a
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
node_modules
npm-debug.log
tsconfig.json
yubinbango-core-src.*
15 changes: 15 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re
import subprocess
import glob

# tsc yubinbango-core.ts --out yubinbango-core-src.js --removeComments --sourcemap
# uglifyjs yubinbango-core-src.js -o yubinbango-core.js --source-map yubinbango-core.js.map --in-source-map yubinbango-core-src.js.map -m 'sort,eval' --mangle-props --reserved-file reserve.json -c

for name in glob.glob('./*.ts'):
if re.search('(.*).ts$', name) is not None:
a = re.search('(.*)(.ts$)', name).group(1)
b = '''tsc {filename}.ts --out {filename}-src.js --removeComments --sourcemap; uglifyjs {filename}-src.js -o {filename}.js --source-map {filename}.js.map --in-source-map {filename}-src.js.map -m 'sort,eval' --mangle-props --reserved-file reserve.json -c'''
subprocess.call(b.format(filename=a), shell=True)
40 changes: 40 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

var gulp = require('gulp');
var runSequence = require('run-sequence');
var webserver = require('gulp-webserver');
var gulpProtractorAngular = require('gulp-angular-protractor');
var exit = require('gulp-exit');

gulp.task('webserver', function() {
return gulp
.src('')
.pipe(webserver({
hot: 'localhost',
port: 8888
}));
});

gulp.task('protractor', function(callback) {
gulp.src(['test/spec.js'])
.pipe(gulpProtractorAngular({
'configFile': 'test/protractor.conf.js',
'args': ['--baseUrl', 'http://localhost:8888'],
'debug': true,
'autoStartStopServer': true
}))
.on('error', function(e) { console.log(e); })
.on('end', function() {
callback();
});
});

gulp.task('test', function(callback) {
runSequence(
'webserver',
'protractor',
function() {
gulp.src("").pipe(exit());
callback();
});
});
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "yubinbango-core",
"version": "0.6.0",
"description": "",
"main": "yubinbango-core.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "gulp test"
},
"repository": {
"type": "git",
"url": "https://github.com/yubinbango/yubinbango-core.git"
},
"author": "Teruyuki Kobayashi [email protected]",
"license": "MIT",
"bugs": {
"url": "https://github.com/yubinbango/yubinbango-core/issues"
},
"homepage": "https://github.com/yubinbango/yubinbango-core",
"devDependencies": {
"gulp": "^3.8.11",
"gulp-angular-protractor": "0.0.2",
"gulp-exit": "0.0.2",
"gulp-webserver": "^0.9.0",
"run-sequence": "^1.1.0",
"uglifyjs": "^2.4.10"
}
}
4 changes: 4 additions & 0 deletions reserve.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"vars": [ "define", "require"],
"props": [ "length", "prototype", "value","p-region","p-locality","p-street-address","p-extended-address" ]
}
10 changes: 10 additions & 0 deletions test/htdocs/yubinbango-core.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="../../yubinbango-core.js" charset="UTF-8"></script>
</head>
<body>
</body>
</html>
9 changes: 9 additions & 0 deletions test/protractor.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
framework: 'jasmine',

jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000
}
};
18 changes: 18 additions & 0 deletions test/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
browser.ignoreSynchronization = true;
browser.get(browser.baseUrl+'/test/htdocs/yubinbango-core.html');

describe('yubinbango-core', function() {
it('7桁の数字を与えると住所を返す', function() {
browser.executeAsyncScript(function() {
var callback = arguments[arguments.length - 1];
var yubin7 = '1008950';
var a = new YubinBango.Core(yubin7, function(addr){callback(addr)});
}).then(function(addr) {
expect(addr.region_id).toEqual(13);
expect(addr.region).toEqual('東京都');
expect(addr.locality).toEqual('千代田区');
expect(addr.street).toEqual('霞が関');
expect(addr.extended).toEqual('1丁目2-1');
});
});
});
2 changes: 2 additions & 0 deletions yubinbango-core.js

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

1 change: 1 addition & 0 deletions yubinbango-core.js.map

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

68 changes: 68 additions & 0 deletions yubinbango-core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
let CACHE = [];
module YubinBango {
export class Core {
URL = 'https://yubinbango.github.io/yubinbango-data/data';
REGION: string[] = [
null, '北海道', '青森県', '岩手県', '宮城県',
'秋田県', '山形県', '福島県', '茨城県', '栃木県',
'群馬県', '埼玉県', '千葉県', '東京都', '神奈川県',
'新潟県', '富山県', '石川県', '福井県', '山梨県',
'長野県', '岐阜県', '静岡県', '愛知県', '三重県',
'滋賀県', '京都府', '大阪府', '兵庫県', '奈良県',
'和歌山県', '鳥取県', '島根県', '岡山県', '広島県',
'山口県', '徳島県', '香川県', '愛媛県', '高知県',
'福岡県', '佐賀県', '長崎県', '熊本県', '大分県',
'宮崎県', '鹿児島県', '沖縄県'
];
constructor(inputVal: string = '', callback?) {
if(inputVal){
// 全角の数字を半角に変換 ハイフンが入っていても数字のみの抽出
let a:string = inputVal.replace(/[-]/g, (s: string) => String.fromCharCode(s.charCodeAt(0) - 65248));
let b:RegExpMatchArray = a.match(/\d/g);
let c:string = b.join('');
let yubin7: string = this.chk7(c);
// 7桁の数字の時のみ作動
if (yubin7) { this.getAddr(yubin7, callback); }
}
}
chk7(val: string) {
if (val.length === 7) {
return val;
}
}
selectAddr(youbin7: string, addr: string[]) {
if (addr[0] && addr[1]) {
return {
'region_id': addr[0],
'region': this.REGION[addr[0]],
'locality': addr[1],
'street': addr[2],
'extended': addr[3]
};
}
}
jsonp(url: string, fn) {
window['$yubin'] = (data) => fn(data);
let scriptTag = document.createElement("script");
scriptTag.setAttribute("type", "text/javascript");
scriptTag.setAttribute("charset", "UTF-8");
scriptTag.setAttribute("src", url);
document.head.appendChild(scriptTag);
}
getAddr(yubin7: string, fn) {
let yubin3 = yubin7.substr(0, 3);
if (this.cachecheck(yubin7, yubin3)) {
fn(this.selectAddr(yubin7, CACHE[yubin3][yubin7]));
} else {
this.jsonp(`${this.URL}/${yubin3}.js`, (data) => {
CACHE[yubin3] = data;
fn(this.selectAddr(yubin7, data[yubin7]));
});
}
}
// 郵便番号上位3桁でキャッシュデータを確認
cachecheck(yubin7: string, yubin3: string) {
if (CACHE[yubin3]) return true;
}
}
}

0 comments on commit e28827a

Please sign in to comment.