china-address-search
Version:
135 lines • 4.74 kB
JavaScript
import data from "./address-data.json";
import { rawRecursiveGenerator } from "taio/esm/libs/custom/algorithms/recursive.mjs";
const isAlpha = (char) => /\w/.test(char);
const isChinese = (char) => /[\u4e00-\u9fa5]/.test(char);
const flattenAddress = rawRecursiveGenerator(function* (nodes, path) {
for (const node of nodes) {
if (node.children) {
yield this.sequence(node.children, [...path, node]);
}
else {
yield this.value({
path: [...path, node],
node,
});
}
}
});
function* prefixCombinations(subPath, baseLevel, getPrefixes) {
const firstNode = subPath[0];
if (!firstNode) {
return;
}
const currentLevelInfo = { [baseLevel]: true };
const currentLevelResults = [];
for (const prefix of getPrefixes(firstNode)) {
const matchItem = {
prefixes: [prefix],
levels: currentLevelInfo,
};
yield matchItem;
currentLevelResults.push(matchItem);
}
yield* currentLevelResults;
for (const childCombinations of prefixCombinations(subPath.slice(1), baseLevel + 1, getPrefixes)) {
yield {
levels: childCombinations.levels,
prefixes: ["", ...childCombinations.prefixes],
};
for (const currentLevel of currentLevelResults) {
yield {
prefixes: [...currentLevel.prefixes, ...childCombinations.prefixes],
levels: {
...currentLevel.levels,
...childCombinations.levels,
},
};
}
}
}
const suffixes = ["自治区", "特别行政区", ..."省市县区镇"];
const getNamePrefixes = function* ({ name }) {
yield name;
const totalLength = name.length;
for (const suffix of suffixes) {
if (name.endsWith(suffix)) {
const endIndex = totalLength - suffix.length;
yield name.slice(0, endIndex);
break;
}
}
};
const getSpellCodePrefixes = function* ({ name, spellCode }) {
yield spellCode;
const totalLength = name.length;
for (const suffix of suffixes) {
if (name.endsWith(suffix)) {
const endIndex = totalLength - suffix.length;
yield spellCode.slice(0, endIndex);
break;
}
}
};
export function* prefixQuery(input, getPrefixes, addressData) {
for (const addressInfo of addressData) {
const { path } = addressInfo;
const combinations = prefixCombinations(path, 0, getPrefixes);
for (const matchItem of combinations) {
const prefixes = matchItem.prefixes;
const totalPrefix = prefixes.join("");
if (totalPrefix.startsWith(input)) {
const levels = matchItem.levels;
const fragments = [];
for (let i = 0; i < path.length; i++) {
const node = path[i];
if (!levels[i]) {
fragments.push({
matched: false,
text: node.name,
});
continue;
}
const prefixLength = prefixes[i].length;
const maxLevel = Math.max(...Object.keys(levels).map((level) => +level));
const splitIndex = i === maxLevel
? prefixLength - totalPrefix.length + input.length
: prefixLength;
fragments.push({
matched: true,
text: node.name.slice(0, splitIndex),
}, {
matched: false,
text: node.name.slice(splitIndex),
});
}
yield {
addressInfo,
fragments,
};
break;
}
}
}
}
export function* queryAddress(input, addressData = data) {
const chars = [...input];
if (chars.every(isAlpha)) {
yield* prefixQuery(input.toUpperCase(), getSpellCodePrefixes, flattenAddress(addressData, []));
}
else if (chars.every(isChinese)) {
yield* prefixQuery(input, getNamePrefixes, flattenAddress(addressData, []));
}
}
export function takeResult(iterator, pageSize) {
const result = [];
for (let i = 0; i < pageSize; i++) {
const iteration = iterator.next();
if (iteration.done) {
return result;
}
const newOption = iteration.value;
result.push(newOption);
}
return result;
}
//# sourceMappingURL=index.mjs.map