@pro6pp/infer-core 0.0.2-beta.0 → 0.0.2-beta.1
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/README.md +3 -3
- package/dist/index.global.js +1 -0
- package/dist/index.js +1 -228
- package/dist/index.mjs +1 -202
- package/package.json +8 -4
package/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Pro6PP Infer Core
|
|
2
2
|
|
|
3
3
|
The headless logic engine behind the Pro6PP Infer SDKs.
|
|
4
4
|
Use this package if you are building a custom integration for a framework, or if you need to run Infer in a non-standard environment.
|
|
5
5
|
|
|
6
|
-
> **Note:** If you are using React, use [`@pro6pp/infer-react`](
|
|
7
|
-
> If you are using plain HTML/JS, use [`@pro6pp/infer-js`](
|
|
6
|
+
> **Note:** If you are using React, use [`@pro6pp/infer-react`](https://www.npmjs.com/package/@pro6pp/infer-react) instead.
|
|
7
|
+
> If you are using plain HTML/JS, use [`@pro6pp/infer-js`](https://www.npmjs.com/package/@pro6pp/infer-js).
|
|
8
8
|
|
|
9
9
|
## Installation
|
|
10
10
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var Pro6PPCore=(()=>{var o=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var g=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var S=(r,t,e)=>t in r?o(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e;var m=(r,t)=>{for(var e in t)o(r,e,{get:t[e],enumerable:!0})},v=(r,t,e,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of g(t))!f.call(r,i)&&i!==e&&o(r,i,{get:()=>t[i],enumerable:!(s=p(t,i))||s.enumerable});return r};var y=r=>v(o({},"__esModule",{value:!0}),r);var a=(r,t,e)=>S(r,typeof t!="symbol"?t+"":t,e);var I={};m(I,{INITIAL_STATE:()=>l,InferCore:()=>h});var u={API_URL:"https://api.pro6pp.nl/v2",LIMIT:1e3,DEBOUNCE_MS:300},d={DIGITS_1_3:/^[0-9]{1,3}$/},l={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,isError:!1,isLoading:!1},h=class{constructor(t){a(this,"country");a(this,"authKey");a(this,"apiUrl");a(this,"limit");a(this,"fetcher");a(this,"onStateChange");a(this,"onSelect");a(this,"state");a(this,"abortController",null);a(this,"debouncedFetch");this.country=t.country,this.authKey=t.authKey,this.apiUrl=t.apiUrl||u.API_URL,this.limit=t.limit||u.LIMIT,this.fetcher=t.fetcher||((e,s)=>fetch(e,s)),this.onStateChange=t.onStateChange||(()=>{}),this.onSelect=t.onSelect||(()=>{}),this.state={...l},this.debouncedFetch=this.debounce(e=>this.executeFetch(e),u.DEBOUNCE_MS)}handleInput(t){this.updateState({query:t,isValid:!1,isLoading:!!t.trim()}),this.state.stage==="final"&&this.onSelect(null),this.debouncedFetch(t)}handleKeyDown(t){let s=t.target.value;if(t.key===" "&&this.shouldAutoInsertComma(s)){t.preventDefault();let i=`${s.trim()}, `;this.updateQueryAndFetch(i)}}selectItem(t){let e=typeof t=="string"?t:t.label,s=typeof t!="string"?t.value:void 0,i=typeof t!="string"?t.subtitle:null;if(this.state.stage==="final"){this.finishSelection(e,s);return}this.processSelection(e,i)}shouldAutoInsertComma(t){if(!t.includes(",")&&d.DIGITS_1_3.test(t.trim()))return!0;if(this.state.stage==="house_number"){let s=this.getCurrentFragment(t);return d.DIGITS_1_3.test(s)}return!1}finishSelection(t,e){this.updateState({query:t,suggestions:[],cities:[],streets:[],isValid:!0}),this.onSelect(e||t)}processSelection(t,e){let{stage:s,query:i}=this.state,n=i;if(e&&(s==="city"||s==="street"||s==="mixed")){if(s==="city")n=`${e}, ${t}, `;else{let c=this.getQueryPrefix(i);n=c?`${c} ${t}, ${e}, `:`${t}, ${e}, `}this.updateQueryAndFetch(n);return}if(s==="direct"||s==="addition"){this.finishSelection(t),this.handleInput(t);return}!i.includes(",")&&(s==="city"||s==="street"||s==="house_number_first")?n=`${t}, `:(n=this.replaceLastSegment(i,t),s!=="house_number"&&(n+=", ")),this.updateQueryAndFetch(n)}executeFetch(t){let e=(t||"").toString();if(!e.trim()){this.abortController?.abort(),this.resetState();return}this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController;let s=new URL(`${this.apiUrl}/infer/${this.country.toLowerCase()}`),i={authKey:this.authKey,query:e,limit:this.limit.toString()};s.search=new URLSearchParams(i).toString(),this.fetcher(s.toString(),{signal:this.abortController.signal}).then(n=>{if(!n.ok)throw new Error("Network error");return n.json()}).then(n=>this.mapResponseToState(n)).catch(n=>{n.name!=="AbortError"&&this.updateState({isError:!0,isLoading:!1})})}mapResponseToState(t){let e={stage:t.stage,isLoading:!1};t.stage==="mixed"?(e.cities=t.cities||[],e.streets=t.streets||[],e.suggestions=[]):(e.suggestions=t.suggestions||[],e.cities=[],e.streets=[]),e.isValid=t.stage==="final",this.updateState(e)}updateQueryAndFetch(t){this.updateState({query:t,suggestions:[],cities:[],streets:[]}),this.handleInput(t)}replaceLastSegment(t,e){let s=t.lastIndexOf(",");return s===-1?e:`${t.slice(0,s+1)} ${e}`.trim()}getQueryPrefix(t){let e=t.lastIndexOf(",");return e===-1?"":t.slice(0,e+1).trimEnd()}getCurrentFragment(t){return(t.split(",").slice(-1)[0]??"").trim()}resetState(){this.updateState({...l,query:this.state.query})}updateState(t){this.state={...this.state,...t},this.onStateChange(this.state)}debounce(t,e){let s;return(...i)=>{s&&clearTimeout(s),s=setTimeout(()=>t.apply(this,i),e)}}};return y(I);})();
|
package/dist/index.js
CHANGED
|
@@ -1,228 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
21
|
-
|
|
22
|
-
// src/index.ts
|
|
23
|
-
var index_exports = {};
|
|
24
|
-
__export(index_exports, {
|
|
25
|
-
INITIAL_STATE: () => INITIAL_STATE,
|
|
26
|
-
InferCore: () => InferCore
|
|
27
|
-
});
|
|
28
|
-
module.exports = __toCommonJS(index_exports);
|
|
29
|
-
|
|
30
|
-
// src/core.ts
|
|
31
|
-
var DEFAULTS = {
|
|
32
|
-
API_URL: "https://api.pro6pp.nl/v2",
|
|
33
|
-
LIMIT: 1e3,
|
|
34
|
-
DEBOUNCE_MS: 300
|
|
35
|
-
};
|
|
36
|
-
var PATTERNS = {
|
|
37
|
-
DIGITS_1_3: /^[0-9]{1,3}$/
|
|
38
|
-
};
|
|
39
|
-
var INITIAL_STATE = {
|
|
40
|
-
query: "",
|
|
41
|
-
stage: null,
|
|
42
|
-
cities: [],
|
|
43
|
-
streets: [],
|
|
44
|
-
suggestions: [],
|
|
45
|
-
isValid: false,
|
|
46
|
-
isError: false,
|
|
47
|
-
isLoading: false
|
|
48
|
-
};
|
|
49
|
-
var InferCore = class {
|
|
50
|
-
constructor(config) {
|
|
51
|
-
__publicField(this, "country");
|
|
52
|
-
__publicField(this, "authKey");
|
|
53
|
-
__publicField(this, "apiUrl");
|
|
54
|
-
__publicField(this, "limit");
|
|
55
|
-
__publicField(this, "fetcher");
|
|
56
|
-
__publicField(this, "onStateChange");
|
|
57
|
-
__publicField(this, "onSelect");
|
|
58
|
-
__publicField(this, "state");
|
|
59
|
-
__publicField(this, "abortController", null);
|
|
60
|
-
__publicField(this, "debouncedFetch");
|
|
61
|
-
this.country = config.country;
|
|
62
|
-
this.authKey = config.authKey;
|
|
63
|
-
this.apiUrl = config.apiUrl || DEFAULTS.API_URL;
|
|
64
|
-
this.limit = config.limit || DEFAULTS.LIMIT;
|
|
65
|
-
this.fetcher = config.fetcher || ((url, init) => fetch(url, init));
|
|
66
|
-
this.onStateChange = config.onStateChange || (() => {
|
|
67
|
-
});
|
|
68
|
-
this.onSelect = config.onSelect || (() => {
|
|
69
|
-
});
|
|
70
|
-
this.state = { ...INITIAL_STATE };
|
|
71
|
-
this.debouncedFetch = this.debounce(
|
|
72
|
-
(val) => this.executeFetch(val),
|
|
73
|
-
DEFAULTS.DEBOUNCE_MS
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
handleInput(value) {
|
|
77
|
-
this.updateState({
|
|
78
|
-
query: value,
|
|
79
|
-
isValid: false,
|
|
80
|
-
isLoading: !!value.trim()
|
|
81
|
-
});
|
|
82
|
-
if (this.state.stage === "final") {
|
|
83
|
-
this.onSelect(null);
|
|
84
|
-
}
|
|
85
|
-
this.debouncedFetch(value);
|
|
86
|
-
}
|
|
87
|
-
handleKeyDown(event) {
|
|
88
|
-
const target = event.target;
|
|
89
|
-
const val = target.value;
|
|
90
|
-
if (event.key === " " && this.shouldAutoInsertComma(val)) {
|
|
91
|
-
event.preventDefault();
|
|
92
|
-
const next = `${val.trim()}, `;
|
|
93
|
-
this.updateQueryAndFetch(next);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
selectItem(item) {
|
|
97
|
-
const label = typeof item === "string" ? item : item.label;
|
|
98
|
-
const value = typeof item !== "string" ? item.value : void 0;
|
|
99
|
-
const subtitle = typeof item !== "string" ? item.subtitle : null;
|
|
100
|
-
if (this.state.stage === "final") {
|
|
101
|
-
this.finishSelection(label, value);
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
this.processSelection(label, subtitle);
|
|
105
|
-
}
|
|
106
|
-
shouldAutoInsertComma(currentVal) {
|
|
107
|
-
const isStartOfSegmentAndNumeric = !currentVal.includes(",") && PATTERNS.DIGITS_1_3.test(currentVal.trim());
|
|
108
|
-
if (isStartOfSegmentAndNumeric) return true;
|
|
109
|
-
if (this.state.stage === "house_number") {
|
|
110
|
-
const currentFragment = this.getCurrentFragment(currentVal);
|
|
111
|
-
return PATTERNS.DIGITS_1_3.test(currentFragment);
|
|
112
|
-
}
|
|
113
|
-
return false;
|
|
114
|
-
}
|
|
115
|
-
finishSelection(label, value) {
|
|
116
|
-
this.updateState({ query: label, suggestions: [], cities: [], streets: [], isValid: true });
|
|
117
|
-
this.onSelect(value || label);
|
|
118
|
-
}
|
|
119
|
-
processSelection(label, subtitle) {
|
|
120
|
-
const { stage, query } = this.state;
|
|
121
|
-
let nextQuery = query;
|
|
122
|
-
const isContextualSelection = subtitle && (stage === "city" || stage === "street" || stage === "mixed");
|
|
123
|
-
if (isContextualSelection) {
|
|
124
|
-
if (stage === "city") {
|
|
125
|
-
nextQuery = `${subtitle}, ${label}, `;
|
|
126
|
-
} else {
|
|
127
|
-
const prefix = this.getQueryPrefix(query);
|
|
128
|
-
nextQuery = prefix ? `${prefix} ${label}, ${subtitle}, ` : `${label}, ${subtitle}, `;
|
|
129
|
-
}
|
|
130
|
-
this.updateQueryAndFetch(nextQuery);
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
if (stage === "direct" || stage === "addition") {
|
|
134
|
-
this.finishSelection(label);
|
|
135
|
-
this.handleInput(label);
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
const hasComma = query.includes(",");
|
|
139
|
-
const isFirstSegment = !hasComma && (stage === "city" || stage === "street" || stage === "house_number_first");
|
|
140
|
-
if (isFirstSegment) {
|
|
141
|
-
nextQuery = `${label}, `;
|
|
142
|
-
} else {
|
|
143
|
-
nextQuery = this.replaceLastSegment(query, label);
|
|
144
|
-
if (stage !== "house_number") {
|
|
145
|
-
nextQuery += ", ";
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
this.updateQueryAndFetch(nextQuery);
|
|
149
|
-
}
|
|
150
|
-
executeFetch(val) {
|
|
151
|
-
const text = (val || "").toString();
|
|
152
|
-
if (!text.trim()) {
|
|
153
|
-
this.abortController?.abort();
|
|
154
|
-
this.resetState();
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
this.updateState({ isError: false });
|
|
158
|
-
if (this.abortController) this.abortController.abort();
|
|
159
|
-
this.abortController = new AbortController();
|
|
160
|
-
const url = new URL(`${this.apiUrl}/infer/${this.country.toLowerCase()}`);
|
|
161
|
-
const params = {
|
|
162
|
-
authKey: this.authKey,
|
|
163
|
-
query: text,
|
|
164
|
-
limit: this.limit.toString()
|
|
165
|
-
};
|
|
166
|
-
url.search = new URLSearchParams(params).toString();
|
|
167
|
-
this.fetcher(url.toString(), { signal: this.abortController.signal }).then((res) => {
|
|
168
|
-
if (!res.ok) throw new Error("Network error");
|
|
169
|
-
return res.json();
|
|
170
|
-
}).then((data) => this.mapResponseToState(data)).catch((e) => {
|
|
171
|
-
if (e.name !== "AbortError") {
|
|
172
|
-
this.updateState({ isError: true, isLoading: false });
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
mapResponseToState(data) {
|
|
177
|
-
const newState = {
|
|
178
|
-
stage: data.stage,
|
|
179
|
-
isLoading: false
|
|
180
|
-
};
|
|
181
|
-
if (data.stage === "mixed") {
|
|
182
|
-
newState.cities = data.cities || [];
|
|
183
|
-
newState.streets = data.streets || [];
|
|
184
|
-
newState.suggestions = [];
|
|
185
|
-
} else {
|
|
186
|
-
newState.suggestions = data.suggestions || [];
|
|
187
|
-
newState.cities = [];
|
|
188
|
-
newState.streets = [];
|
|
189
|
-
}
|
|
190
|
-
newState.isValid = data.stage === "final";
|
|
191
|
-
this.updateState(newState);
|
|
192
|
-
}
|
|
193
|
-
updateQueryAndFetch(nextQuery) {
|
|
194
|
-
this.updateState({ query: nextQuery, suggestions: [], cities: [], streets: [] });
|
|
195
|
-
this.handleInput(nextQuery);
|
|
196
|
-
}
|
|
197
|
-
replaceLastSegment(fullText, newSegment) {
|
|
198
|
-
const lastCommaIndex = fullText.lastIndexOf(",");
|
|
199
|
-
if (lastCommaIndex === -1) return newSegment;
|
|
200
|
-
return `${fullText.slice(0, lastCommaIndex + 1)} ${newSegment}`.trim();
|
|
201
|
-
}
|
|
202
|
-
getQueryPrefix(q) {
|
|
203
|
-
const lastComma = q.lastIndexOf(",");
|
|
204
|
-
return lastComma === -1 ? "" : q.slice(0, lastComma + 1).trimEnd();
|
|
205
|
-
}
|
|
206
|
-
getCurrentFragment(q) {
|
|
207
|
-
return (q.split(",").slice(-1)[0] ?? "").trim();
|
|
208
|
-
}
|
|
209
|
-
resetState() {
|
|
210
|
-
this.updateState({ ...INITIAL_STATE, query: this.state.query });
|
|
211
|
-
}
|
|
212
|
-
updateState(updates) {
|
|
213
|
-
this.state = { ...this.state, ...updates };
|
|
214
|
-
this.onStateChange(this.state);
|
|
215
|
-
}
|
|
216
|
-
debounce(func, wait) {
|
|
217
|
-
let timeout;
|
|
218
|
-
return (...args) => {
|
|
219
|
-
if (timeout) clearTimeout(timeout);
|
|
220
|
-
timeout = setTimeout(() => func.apply(this, args), wait);
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
};
|
|
224
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
225
|
-
0 && (module.exports = {
|
|
226
|
-
INITIAL_STATE,
|
|
227
|
-
InferCore
|
|
228
|
-
});
|
|
1
|
+
"use strict";var o=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var g=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var S=(r,t,e)=>t in r?o(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e;var m=(r,t)=>{for(var e in t)o(r,e,{get:t[e],enumerable:!0})},v=(r,t,e,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of g(t))!f.call(r,i)&&i!==e&&o(r,i,{get:()=>t[i],enumerable:!(s=p(t,i))||s.enumerable});return r};var y=r=>v(o({},"__esModule",{value:!0}),r);var a=(r,t,e)=>S(r,typeof t!="symbol"?t+"":t,e);var I={};m(I,{INITIAL_STATE:()=>l,InferCore:()=>h});module.exports=y(I);var u={API_URL:"https://api.pro6pp.nl/v2",LIMIT:1e3,DEBOUNCE_MS:300},d={DIGITS_1_3:/^[0-9]{1,3}$/},l={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,isError:!1,isLoading:!1},h=class{constructor(t){a(this,"country");a(this,"authKey");a(this,"apiUrl");a(this,"limit");a(this,"fetcher");a(this,"onStateChange");a(this,"onSelect");a(this,"state");a(this,"abortController",null);a(this,"debouncedFetch");this.country=t.country,this.authKey=t.authKey,this.apiUrl=t.apiUrl||u.API_URL,this.limit=t.limit||u.LIMIT,this.fetcher=t.fetcher||((e,s)=>fetch(e,s)),this.onStateChange=t.onStateChange||(()=>{}),this.onSelect=t.onSelect||(()=>{}),this.state={...l},this.debouncedFetch=this.debounce(e=>this.executeFetch(e),u.DEBOUNCE_MS)}handleInput(t){this.updateState({query:t,isValid:!1,isLoading:!!t.trim()}),this.state.stage==="final"&&this.onSelect(null),this.debouncedFetch(t)}handleKeyDown(t){let s=t.target.value;if(t.key===" "&&this.shouldAutoInsertComma(s)){t.preventDefault();let i=`${s.trim()}, `;this.updateQueryAndFetch(i)}}selectItem(t){let e=typeof t=="string"?t:t.label,s=typeof t!="string"?t.value:void 0,i=typeof t!="string"?t.subtitle:null;if(this.state.stage==="final"){this.finishSelection(e,s);return}this.processSelection(e,i)}shouldAutoInsertComma(t){if(!t.includes(",")&&d.DIGITS_1_3.test(t.trim()))return!0;if(this.state.stage==="house_number"){let s=this.getCurrentFragment(t);return d.DIGITS_1_3.test(s)}return!1}finishSelection(t,e){this.updateState({query:t,suggestions:[],cities:[],streets:[],isValid:!0}),this.onSelect(e||t)}processSelection(t,e){let{stage:s,query:i}=this.state,n=i;if(e&&(s==="city"||s==="street"||s==="mixed")){if(s==="city")n=`${e}, ${t}, `;else{let c=this.getQueryPrefix(i);n=c?`${c} ${t}, ${e}, `:`${t}, ${e}, `}this.updateQueryAndFetch(n);return}if(s==="direct"||s==="addition"){this.finishSelection(t),this.handleInput(t);return}!i.includes(",")&&(s==="city"||s==="street"||s==="house_number_first")?n=`${t}, `:(n=this.replaceLastSegment(i,t),s!=="house_number"&&(n+=", ")),this.updateQueryAndFetch(n)}executeFetch(t){let e=(t||"").toString();if(!e.trim()){this.abortController?.abort(),this.resetState();return}this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController;let s=new URL(`${this.apiUrl}/infer/${this.country.toLowerCase()}`),i={authKey:this.authKey,query:e,limit:this.limit.toString()};s.search=new URLSearchParams(i).toString(),this.fetcher(s.toString(),{signal:this.abortController.signal}).then(n=>{if(!n.ok)throw new Error("Network error");return n.json()}).then(n=>this.mapResponseToState(n)).catch(n=>{n.name!=="AbortError"&&this.updateState({isError:!0,isLoading:!1})})}mapResponseToState(t){let e={stage:t.stage,isLoading:!1};t.stage==="mixed"?(e.cities=t.cities||[],e.streets=t.streets||[],e.suggestions=[]):(e.suggestions=t.suggestions||[],e.cities=[],e.streets=[]),e.isValid=t.stage==="final",this.updateState(e)}updateQueryAndFetch(t){this.updateState({query:t,suggestions:[],cities:[],streets:[]}),this.handleInput(t)}replaceLastSegment(t,e){let s=t.lastIndexOf(",");return s===-1?e:`${t.slice(0,s+1)} ${e}`.trim()}getQueryPrefix(t){let e=t.lastIndexOf(",");return e===-1?"":t.slice(0,e+1).trimEnd()}getCurrentFragment(t){return(t.split(",").slice(-1)[0]??"").trim()}resetState(){this.updateState({...l,query:this.state.query})}updateState(t){this.state={...this.state,...t},this.onStateChange(this.state)}debounce(t,e){let s;return(...i)=>{s&&clearTimeout(s),s=setTimeout(()=>t.apply(this,i),e)}}};0&&(module.exports={INITIAL_STATE,InferCore});
|
package/dist/index.mjs
CHANGED
|
@@ -1,202 +1 @@
|
|
|
1
|
-
var
|
|
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
|
-
};
|
|
1
|
+
var d=Object.defineProperty;var p=(a,t,e)=>t in a?d(a,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[t]=e;var n=(a,t,e)=>p(a,typeof t!="symbol"?t+"":t,e);var o={API_URL:"https://api.pro6pp.nl/v2",LIMIT:1e3,DEBOUNCE_MS:300},l={DIGITS_1_3:/^[0-9]{1,3}$/},h={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,isError:!1,isLoading:!1},c=class{constructor(t){n(this,"country");n(this,"authKey");n(this,"apiUrl");n(this,"limit");n(this,"fetcher");n(this,"onStateChange");n(this,"onSelect");n(this,"state");n(this,"abortController",null);n(this,"debouncedFetch");this.country=t.country,this.authKey=t.authKey,this.apiUrl=t.apiUrl||o.API_URL,this.limit=t.limit||o.LIMIT,this.fetcher=t.fetcher||((e,s)=>fetch(e,s)),this.onStateChange=t.onStateChange||(()=>{}),this.onSelect=t.onSelect||(()=>{}),this.state={...h},this.debouncedFetch=this.debounce(e=>this.executeFetch(e),o.DEBOUNCE_MS)}handleInput(t){this.updateState({query:t,isValid:!1,isLoading:!!t.trim()}),this.state.stage==="final"&&this.onSelect(null),this.debouncedFetch(t)}handleKeyDown(t){let s=t.target.value;if(t.key===" "&&this.shouldAutoInsertComma(s)){t.preventDefault();let r=`${s.trim()}, `;this.updateQueryAndFetch(r)}}selectItem(t){let e=typeof t=="string"?t:t.label,s=typeof t!="string"?t.value:void 0,r=typeof t!="string"?t.subtitle:null;if(this.state.stage==="final"){this.finishSelection(e,s);return}this.processSelection(e,r)}shouldAutoInsertComma(t){if(!t.includes(",")&&l.DIGITS_1_3.test(t.trim()))return!0;if(this.state.stage==="house_number"){let s=this.getCurrentFragment(t);return l.DIGITS_1_3.test(s)}return!1}finishSelection(t,e){this.updateState({query:t,suggestions:[],cities:[],streets:[],isValid:!0}),this.onSelect(e||t)}processSelection(t,e){let{stage:s,query:r}=this.state,i=r;if(e&&(s==="city"||s==="street"||s==="mixed")){if(s==="city")i=`${e}, ${t}, `;else{let u=this.getQueryPrefix(r);i=u?`${u} ${t}, ${e}, `:`${t}, ${e}, `}this.updateQueryAndFetch(i);return}if(s==="direct"||s==="addition"){this.finishSelection(t),this.handleInput(t);return}!r.includes(",")&&(s==="city"||s==="street"||s==="house_number_first")?i=`${t}, `:(i=this.replaceLastSegment(r,t),s!=="house_number"&&(i+=", ")),this.updateQueryAndFetch(i)}executeFetch(t){let e=(t||"").toString();if(!e.trim()){this.abortController?.abort(),this.resetState();return}this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController;let s=new URL(`${this.apiUrl}/infer/${this.country.toLowerCase()}`),r={authKey:this.authKey,query:e,limit:this.limit.toString()};s.search=new URLSearchParams(r).toString(),this.fetcher(s.toString(),{signal:this.abortController.signal}).then(i=>{if(!i.ok)throw new Error("Network error");return i.json()}).then(i=>this.mapResponseToState(i)).catch(i=>{i.name!=="AbortError"&&this.updateState({isError:!0,isLoading:!1})})}mapResponseToState(t){let e={stage:t.stage,isLoading:!1};t.stage==="mixed"?(e.cities=t.cities||[],e.streets=t.streets||[],e.suggestions=[]):(e.suggestions=t.suggestions||[],e.cities=[],e.streets=[]),e.isValid=t.stage==="final",this.updateState(e)}updateQueryAndFetch(t){this.updateState({query:t,suggestions:[],cities:[],streets:[]}),this.handleInput(t)}replaceLastSegment(t,e){let s=t.lastIndexOf(",");return s===-1?e:`${t.slice(0,s+1)} ${e}`.trim()}getQueryPrefix(t){let e=t.lastIndexOf(",");return e===-1?"":t.slice(0,e+1).trimEnd()}getCurrentFragment(t){return(t.split(",").slice(-1)[0]??"").trim()}resetState(){this.updateState({...h,query:this.state.query})}updateState(t){this.state={...this.state,...t},this.onStateChange(this.state)}debounce(t,e){let s;return(...r)=>{s&&clearTimeout(s),s=setTimeout(()=>t.apply(this,r),e)}}};export{h as INITIAL_STATE,c as InferCore};
|
package/package.json
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pro6pp/infer-core",
|
|
3
|
-
"
|
|
3
|
+
"description": "Core logic and client for the Pro6PP Infer API.",
|
|
4
|
+
"version": "0.0.2-beta.1",
|
|
4
5
|
"main": "./dist/index.js",
|
|
5
6
|
"module": "./dist/index.mjs",
|
|
6
7
|
"types": "./dist/index.d.ts",
|
|
8
|
+
"unpkg": "./dist/index.global.js",
|
|
9
|
+
"jsdelivr": "./dist/index.global.js",
|
|
7
10
|
"exports": {
|
|
8
11
|
".": {
|
|
9
12
|
"types": "./dist/index.d.ts",
|
|
@@ -16,8 +19,8 @@
|
|
|
16
19
|
"README.md"
|
|
17
20
|
],
|
|
18
21
|
"scripts": {
|
|
19
|
-
"build": "tsup
|
|
20
|
-
"dev": "tsup
|
|
22
|
+
"build": "tsup",
|
|
23
|
+
"dev": "tsup --watch",
|
|
21
24
|
"type-check": "tsc --noEmit"
|
|
22
25
|
},
|
|
23
26
|
"devDependencies": {
|
|
@@ -30,6 +33,7 @@
|
|
|
30
33
|
"license": "MIT",
|
|
31
34
|
"repository": {
|
|
32
35
|
"type": "git",
|
|
33
|
-
"url": "https://
|
|
36
|
+
"url": "https://github.com/pro6pp/infer-sdk",
|
|
37
|
+
"directory": "packages/core"
|
|
34
38
|
}
|
|
35
39
|
}
|