china-address-search
Version:
141 lines • 5.15 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.takeResult = exports.queryAddress = exports.prefixQuery = void 0;
const address_data_json_1 = __importDefault(require("./address-data.json"));
const recursive_mjs_1 = require("taio/esm/libs/custom/algorithms/recursive.mjs");
const isAlpha = (char) => /\w/.test(char);
const isChinese = (char) => /[\u4e00-\u9fa5]/.test(char);
const flattenAddress = (0, recursive_mjs_1.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: Object.assign(Object.assign({}, 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;
}
}
};
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;
}
}
}
}
exports.prefixQuery = prefixQuery;
function* queryAddress(input, addressData = address_data_json_1.default) {
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, []));
}
}
exports.queryAddress = queryAddress;
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;
}
exports.takeResult = takeResult;
//# sourceMappingURL=index.js.map