Structural query keys and text common prefixes
Small pure functions for stable comparison and usage analysis, not an authorization, hashing, or semantic deduplication system.
commonPrefixLength compares the JS UTF-16 prefix length of strings for stable prefix analysis; it is not a token count, character count, or cache hit promise.
retrievalQueryKey sorts the keys of a JSON-serializable object and then serializes while preserving array order, making it suitable for comparing controlled query structures. It is not a cryptographic hash, identity signature, or a unique key for arbitrary JS objects; Date/Map/functions/cyclic objects are not among the controlled JSON query inputs shown here.
Progressive examples for the same business case
Compare the stable prefix of two real inputs
Beginner · Node.js
ts
import { commonPrefixLength } from '@jangts/agent-core';
console.log(commonPrefixLength('服务:正常', '服务:繁忙')); // 3js
import { commonPrefixLength } from '@jangts/agent-core';
console.log(commonPrefixLength('服务:正常', '服务:繁忙')); // 3Different object key order keeps the same structure semantics
Configuration · Node.js
ts
import { retrievalQueryKey } from '@jangts/agent-core';
const first = retrievalQueryKey({ query: '服务状态', filters: { region: 'cn', active: true } });
const second = retrievalQueryKey({ filters: { active: true, region: 'cn' }, query: '服务状态' });
console.log(first === second); // true
console.log(retrievalQueryKey({ ids: ['a', 'b'] }) === retrievalQueryKey({ ids: ['b', 'a'] })); // falsejs
import { retrievalQueryKey } from '@jangts/agent-core';
const first = retrievalQueryKey({ query: '服务状态', filters: { region: 'cn', active: true } });
const second = retrievalQueryKey({ filters: { active: true, region: 'cn' }, query: '服务状态' });
console.log(first === second); // true
console.log(retrievalQueryKey({ ids: ['a', 'b'] }) === retrievalQueryKey({ ids: ['b', 'a'] })); // false