Skip to content

Instantly share code, notes, and snippets.

@PaulGuo
Created December 30, 2014 02:40
Show Gist options
  • Save PaulGuo/f3835a5533c4c3b1d8b6 to your computer and use it in GitHub Desktop.
Save PaulGuo/f3835a5533c4c3b1d8b6 to your computer and use it in GitHub Desktop.
var pinyin = require('pinyin');
var cities = require('./cityselect-open').data;
var pinyinHash = {
data: {},
addItem: function(name) {
if(this.data.hasOwnProperty(name) || !name) {
return;
}
var pinyinText = pinyin(name, {
style: pinyin.STYLE_NORMAL,
heteronym: false
}).join('');
var pinyinFirstLetter = pinyin(name, {
style: pinyin.STYLE_FIRST_LETTER,
heteronym: false
}).join('');
this.data[name] = {
pinyin: pinyinText,
acronym: pinyinFirstLetter
};
}
};
for(var i in cities.data) {
if(cities.data.hasOwnProperty(i)) {
pinyinHash.addItem(cities.data[i].name);
if(!cities.data[i].cities) {
continue;
}
for(var j in cities.data[i].cities) {
if(cities.data[i].cities.hasOwnProperty(j)) {
pinyinHash.addItem(cities.data[i].cities[j].name);
}
}
}
}
console.log(pinyinHash.data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment