-
-
Save deathsoul00/0d61ea646fc3283e231cd48e7a756397 to your computer and use it in GitHub Desktop.
ES6 YubinBango
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
/** | |
* 郵便番号自動入力クラス | |
* YubinBango | |
* | |
* [SOURCE] | |
* https://github.com/kobabasu/micro-flux | |
* | |
* [LATEST VERSION] | |
* https://gist.github.com/hnq90/1c63e82377b5cac9ec0ed7a1d93269d2 | |
* | |
* [HOW TO USE] | |
* new YubinBango().fetch(zipCode, address => { | |
* if (address === false) { | |
* alert('Invalid Zipcode') | |
* } else { | |
* // var { region_id, region, locality, street, extended } = address | |
* console.log(address) | |
* } | |
* }) | |
* | |
* Modified by: Mike Alvarez | |
* | |
* changelog | |
* - added functionality to be able to acknowledge custom element for attachment | |
* - fixed compatibility issue regarding IE browser | |
*/ | |
var CACHE = []; | |
var DATA_URL = 'https://yubinbango.github.io/yubinbango-data/data'; | |
var ZIPCODE_LENGTH = 7; | |
var JSONP_CALLBACK = '$yubin'; | |
var REGION = [ | |
null, '北海道', '青森県', '岩手県', '宮城県', | |
'秋田県', '山形県', '福島県', '茨城県', '栃木県', | |
'群馬県', '埼玉県', '千葉県', '東京都', '神奈川県', | |
'新潟県', '富山県', '石川県', '福井県', '山梨県', | |
'長野県', '岐阜県', '静岡県', '愛知県', '三重県', | |
'滋賀県', '京都府', '大阪府', '兵庫県', '奈良県', | |
'和歌山県', '鳥取県', '島根県', '岡山県', '広島県', | |
'山口県', '徳島県', '香川県', '愛媛県', '高知県', | |
'福岡県', '佐賀県', '長崎県', '熊本県', '大分県', | |
'宮崎県', '鹿児島県', '沖縄県' | |
]; | |
/** | |
* polyfill for method `assign` causing an error on IE 'Object doesn't support property or method 'assign'' | |
* | |
* references: | |
* - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign | |
* - https://stackoverflow.com/questions/35215360/getting-error-object-doesnt-support-property-or-method-assign | |
* | |
*/ | |
if (typeof Object.assign != 'function') { | |
Object.assign = function(target, varArgs) { // .length of function is 2 | |
'use strict'; | |
if (target == null) { // TypeError if undefined or null | |
throw new TypeError('Cannot convert undefined or null to object'); | |
} | |
var to = Object(target); | |
for (var index = 1; index < arguments.length; index++) { | |
var nextSource = arguments[index]; | |
if (nextSource != null) { // Skip over if undefined or null | |
for (var nextKey in nextSource) { | |
// Avoid bugs when hasOwnProperty is shadowed | |
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) { | |
to[nextKey] = nextSource[nextKey]; | |
} | |
} | |
} | |
} | |
return to; | |
}; | |
} | |
var YubinBango = function() { | |
}; | |
YubinBango.prototype.cacheCheck = function (zip01) { | |
if (CACHE[zip01]) { | |
return true; | |
} | |
return false; | |
}; | |
YubinBango.prototype.validate = function (value) { | |
return value.length === ZIPCODE_LENGTH; | |
}; | |
YubinBango.prototype.jsonp = function (url, callback) { | |
window[JSONP_CALLBACK] = function(data) { | |
return callback(data); | |
} | |
var script_tag = document.createElement('script'); | |
script_tag.setAttribute('type', 'text/javascript'); | |
script_tag.setAttribute('charset', 'UTF-8'); | |
script_tag.setAttribute('src', url); | |
document.body.appendChild(script_tag); | |
}; | |
YubinBango.prototype.selectAddress = function (zipCode, address) { | |
var result = { | |
region_id: '', | |
region: '', | |
locality: '', | |
street: '', | |
extended: '' | |
}; | |
if (address) { | |
result = Object.assign( | |
{}, | |
result, | |
{ | |
region_id: address[0], | |
region: REGION[address[0]], | |
locality: address[1], | |
street: address[2], | |
extended: address[3] || '' | |
} | |
); | |
return result; | |
} | |
return false; | |
}; | |
YubinBango.prototype.getAddress = function (zipCode, callback) { | |
var ZIP01 = zipCode.substr(0, 3); | |
if (YubinBango.prototype.cacheCheck(ZIP01)) { | |
callback(YubinBango.prototype.selectAddress(zipCode, CACHE[ZIP01][zipCode])); | |
} else { | |
YubinBango.prototype.jsonp(DATA_URL + '/' + ZIP01 + '.js', function(data) { | |
if (data && Object.keys(data).length > 0) { | |
CACHE[ZIP01] = data; | |
callback(YubinBango.prototype.selectAddress(zipCode, data[zipCode])); | |
} else { | |
callback(false); | |
} | |
}); | |
} | |
}; | |
YubinBango.prototype.fetch = function (zipCode, callback) { | |
if (zipCode && typeof zipCode === 'string') { | |
var zipCode = zipCode.replace(/[0-9]/g, function(str) { | |
return String.fromCharCode(str.charCodeAt(0) - 65248); | |
}).match(/\d/g).join(''); | |
if (YubinBango.prototype.validate(zipCode)) { | |
YubinBango.prototype.getAddress(zipCode, callback); | |
} else { | |
callback(false); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample Usage: