@pro6pp/infer-core 0.0.2-beta.0 → 0.0.2-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs DELETED
@@ -1,202 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
-
5
- // src/core.ts
6
- var DEFAULTS = {
7
- API_URL: "https://api.pro6pp.nl/v2",
8
- LIMIT: 1e3,
9
- DEBOUNCE_MS: 300
10
- };
11
- var PATTERNS = {
12
- DIGITS_1_3: /^[0-9]{1,3}$/
13
- };
14
- var INITIAL_STATE = {
15
- query: "",
16
- stage: null,
17
- cities: [],
18
- streets: [],
19
- suggestions: [],
20
- isValid: false,
21
- isError: false,
22
- isLoading: false
23
- };
24
- var InferCore = class {
25
- constructor(config) {
26
- __publicField(this, "country");
27
- __publicField(this, "authKey");
28
- __publicField(this, "apiUrl");
29
- __publicField(this, "limit");
30
- __publicField(this, "fetcher");
31
- __publicField(this, "onStateChange");
32
- __publicField(this, "onSelect");
33
- __publicField(this, "state");
34
- __publicField(this, "abortController", null);
35
- __publicField(this, "debouncedFetch");
36
- this.country = config.country;
37
- this.authKey = config.authKey;
38
- this.apiUrl = config.apiUrl || DEFAULTS.API_URL;
39
- this.limit = config.limit || DEFAULTS.LIMIT;
40
- this.fetcher = config.fetcher || ((url, init) => fetch(url, init));
41
- this.onStateChange = config.onStateChange || (() => {
42
- });
43
- this.onSelect = config.onSelect || (() => {
44
- });
45
- this.state = { ...INITIAL_STATE };
46
- this.debouncedFetch = this.debounce(
47
- (val) => this.executeFetch(val),
48
- DEFAULTS.DEBOUNCE_MS
49
- );
50
- }
51
- handleInput(value) {
52
- this.updateState({
53
- query: value,
54
- isValid: false,
55
- isLoading: !!value.trim()
56
- });
57
- if (this.state.stage === "final") {
58
- this.onSelect(null);
59
- }
60
- this.debouncedFetch(value);
61
- }
62
- handleKeyDown(event) {
63
- const target = event.target;
64
- const val = target.value;
65
- if (event.key === " " && this.shouldAutoInsertComma(val)) {
66
- event.preventDefault();
67
- const next = `${val.trim()}, `;
68
- this.updateQueryAndFetch(next);
69
- }
70
- }
71
- selectItem(item) {
72
- const label = typeof item === "string" ? item : item.label;
73
- const value = typeof item !== "string" ? item.value : void 0;
74
- const subtitle = typeof item !== "string" ? item.subtitle : null;
75
- if (this.state.stage === "final") {
76
- this.finishSelection(label, value);
77
- return;
78
- }
79
- this.processSelection(label, subtitle);
80
- }
81
- shouldAutoInsertComma(currentVal) {
82
- const isStartOfSegmentAndNumeric = !currentVal.includes(",") && PATTERNS.DIGITS_1_3.test(currentVal.trim());
83
- if (isStartOfSegmentAndNumeric) return true;
84
- if (this.state.stage === "house_number") {
85
- const currentFragment = this.getCurrentFragment(currentVal);
86
- return PATTERNS.DIGITS_1_3.test(currentFragment);
87
- }
88
- return false;
89
- }
90
- finishSelection(label, value) {
91
- this.updateState({ query: label, suggestions: [], cities: [], streets: [], isValid: true });
92
- this.onSelect(value || label);
93
- }
94
- processSelection(label, subtitle) {
95
- const { stage, query } = this.state;
96
- let nextQuery = query;
97
- const isContextualSelection = subtitle && (stage === "city" || stage === "street" || stage === "mixed");
98
- if (isContextualSelection) {
99
- if (stage === "city") {
100
- nextQuery = `${subtitle}, ${label}, `;
101
- } else {
102
- const prefix = this.getQueryPrefix(query);
103
- nextQuery = prefix ? `${prefix} ${label}, ${subtitle}, ` : `${label}, ${subtitle}, `;
104
- }
105
- this.updateQueryAndFetch(nextQuery);
106
- return;
107
- }
108
- if (stage === "direct" || stage === "addition") {
109
- this.finishSelection(label);
110
- this.handleInput(label);
111
- return;
112
- }
113
- const hasComma = query.includes(",");
114
- const isFirstSegment = !hasComma && (stage === "city" || stage === "street" || stage === "house_number_first");
115
- if (isFirstSegment) {
116
- nextQuery = `${label}, `;
117
- } else {
118
- nextQuery = this.replaceLastSegment(query, label);
119
- if (stage !== "house_number") {
120
- nextQuery += ", ";
121
- }
122
- }
123
- this.updateQueryAndFetch(nextQuery);
124
- }
125
- executeFetch(val) {
126
- const text = (val || "").toString();
127
- if (!text.trim()) {
128
- this.abortController?.abort();
129
- this.resetState();
130
- return;
131
- }
132
- this.updateState({ isError: false });
133
- if (this.abortController) this.abortController.abort();
134
- this.abortController = new AbortController();
135
- const url = new URL(`${this.apiUrl}/infer/${this.country.toLowerCase()}`);
136
- const params = {
137
- authKey: this.authKey,
138
- query: text,
139
- limit: this.limit.toString()
140
- };
141
- url.search = new URLSearchParams(params).toString();
142
- this.fetcher(url.toString(), { signal: this.abortController.signal }).then((res) => {
143
- if (!res.ok) throw new Error("Network error");
144
- return res.json();
145
- }).then((data) => this.mapResponseToState(data)).catch((e) => {
146
- if (e.name !== "AbortError") {
147
- this.updateState({ isError: true, isLoading: false });
148
- }
149
- });
150
- }
151
- mapResponseToState(data) {
152
- const newState = {
153
- stage: data.stage,
154
- isLoading: false
155
- };
156
- if (data.stage === "mixed") {
157
- newState.cities = data.cities || [];
158
- newState.streets = data.streets || [];
159
- newState.suggestions = [];
160
- } else {
161
- newState.suggestions = data.suggestions || [];
162
- newState.cities = [];
163
- newState.streets = [];
164
- }
165
- newState.isValid = data.stage === "final";
166
- this.updateState(newState);
167
- }
168
- updateQueryAndFetch(nextQuery) {
169
- this.updateState({ query: nextQuery, suggestions: [], cities: [], streets: [] });
170
- this.handleInput(nextQuery);
171
- }
172
- replaceLastSegment(fullText, newSegment) {
173
- const lastCommaIndex = fullText.lastIndexOf(",");
174
- if (lastCommaIndex === -1) return newSegment;
175
- return `${fullText.slice(0, lastCommaIndex + 1)} ${newSegment}`.trim();
176
- }
177
- getQueryPrefix(q) {
178
- const lastComma = q.lastIndexOf(",");
179
- return lastComma === -1 ? "" : q.slice(0, lastComma + 1).trimEnd();
180
- }
181
- getCurrentFragment(q) {
182
- return (q.split(",").slice(-1)[0] ?? "").trim();
183
- }
184
- resetState() {
185
- this.updateState({ ...INITIAL_STATE, query: this.state.query });
186
- }
187
- updateState(updates) {
188
- this.state = { ...this.state, ...updates };
189
- this.onStateChange(this.state);
190
- }
191
- debounce(func, wait) {
192
- let timeout;
193
- return (...args) => {
194
- if (timeout) clearTimeout(timeout);
195
- timeout = setTimeout(() => func.apply(this, args), wait);
196
- };
197
- }
198
- };
199
- export {
200
- INITIAL_STATE,
201
- InferCore
202
- };