@pro6pp/infer-core 0.0.2-beta.2 → 0.0.2-beta.3
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 +1 -2
- package/dist/index.cjs +1 -0
- package/dist/{index.d.mts → index.d.cts} +11 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.global.js +1 -1
- package/dist/index.js +1 -1
- package/package.json +7 -4
- package/dist/index.mjs +0 -1
package/README.md
CHANGED
|
@@ -3,8 +3,7 @@
|
|
|
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:**
|
|
7
|
-
> If you are using plain HTML/JS, use [`@pro6pp/infer-js`](https://www.npmjs.com/package/@pro6pp/infer-js).
|
|
6
|
+
> **Note:** Use **`@pro6pp/infer-react`** for React applications. For all other frameworks or Vanilla JS, use **`@pro6pp/infer-js`**.
|
|
8
7
|
|
|
9
8
|
## Installation
|
|
10
9
|
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var o=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var S=Object.prototype.hasOwnProperty;var m=(n,t,e)=>t in n?o(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var I=(n,t)=>{for(var e in t)o(n,e,{get:t[e],enumerable:!0})},y=(n,t,e,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of f(t))!S.call(n,r)&&r!==e&&o(n,r,{get:()=>t[r],enumerable:!(s=p(t,r))||s.enumerable});return n};var v=n=>y(o({},"__esModule",{value:!0}),n);var a=(n,t,e)=>m(n,typeof t!="symbol"?t+"":t,e);var x={};I(x,{INITIAL_STATE:()=>h,InferCore:()=>c});module.exports=v(x);var l={API_URL:"https://api.pro6pp.nl/v2",LIMIT:1e3,DEBOUNCE_MS:300},g={DIGITS_1_3:/^[0-9]{1,3}$/},h={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,isError:!1,isLoading:!1,selectedSuggestionIndex:-1},c=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||l.API_URL,this.limit=t.limit||l.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),l.DEBOUNCE_MS)}handleInput(t){this.updateState({query:t,isValid:!1,isLoading:!!t.trim(),selectedSuggestionIndex:-1}),this.state.stage==="final"&&this.onSelect(null),this.debouncedFetch(t)}handleKeyDown(t){let e=t.target;if(!e)return;let s=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(s>0){if(t.key==="ArrowDown"){t.preventDefault();let i=this.state.selectedSuggestionIndex+1;i>=s&&(i=0),this.updateState({selectedSuggestionIndex:i});return}if(t.key==="ArrowUp"){t.preventDefault();let i=this.state.selectedSuggestionIndex-1;i<0&&(i=s-1),this.updateState({selectedSuggestionIndex:i});return}if(t.key==="Enter"&&this.state.selectedSuggestionIndex>=0){t.preventDefault();let u=[...this.state.cities,...this.state.streets,...this.state.suggestions][this.state.selectedSuggestionIndex];u&&(this.selectItem(u),this.updateState({selectedSuggestionIndex:-1}));return}}let r=e.value;if(t.key===" "&&this.shouldAutoInsertComma(r)){t.preventDefault();let i=`${r.trim()}, `;this.updateQueryAndFetch(i)}}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(",")&&g.DIGITS_1_3.test(t.trim()))return!0;if(this.state.stage==="house_number"){let s=this.getCurrentFragment(t);return g.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 d=this.getQueryPrefix(r);i=d?`${d} ${t}, ${e}, `:`${t}, ${e}, `}this.updateQueryAndFetch(i);return}if(s==="direct"||s==="addition"){this.finishSelection(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)}}};0&&(module.exports={INITIAL_STATE,InferCore});
|
|
@@ -53,6 +53,12 @@ interface InferState {
|
|
|
53
53
|
isError: boolean;
|
|
54
54
|
/** True if a network request is currently active. */
|
|
55
55
|
isLoading: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* The index of the currently highlighted suggestion in the dropdown list.
|
|
58
|
+
* - Values `>= 0` indicate an active item (for keyboard navigation).
|
|
59
|
+
* - Value `-1` indicates no item is currently highlighted.
|
|
60
|
+
*/
|
|
61
|
+
selectedSuggestionIndex: number;
|
|
56
62
|
}
|
|
57
63
|
/**
|
|
58
64
|
* Custom fetch implementation, compatible with `window.fetch`.
|
|
@@ -111,7 +117,11 @@ declare class InferCore {
|
|
|
111
117
|
private debouncedFetch;
|
|
112
118
|
constructor(config: InferConfig);
|
|
113
119
|
handleInput(value: string): void;
|
|
114
|
-
handleKeyDown(event: KeyboardEvent |
|
|
120
|
+
handleKeyDown(event: KeyboardEvent | {
|
|
121
|
+
key: string;
|
|
122
|
+
target: EventTarget | null;
|
|
123
|
+
preventDefault: () => void;
|
|
124
|
+
}): void;
|
|
115
125
|
selectItem(item: InferResult | string): void;
|
|
116
126
|
private shouldAutoInsertComma;
|
|
117
127
|
private finishSelection;
|
package/dist/index.d.ts
CHANGED
|
@@ -53,6 +53,12 @@ interface InferState {
|
|
|
53
53
|
isError: boolean;
|
|
54
54
|
/** True if a network request is currently active. */
|
|
55
55
|
isLoading: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* The index of the currently highlighted suggestion in the dropdown list.
|
|
58
|
+
* - Values `>= 0` indicate an active item (for keyboard navigation).
|
|
59
|
+
* - Value `-1` indicates no item is currently highlighted.
|
|
60
|
+
*/
|
|
61
|
+
selectedSuggestionIndex: number;
|
|
56
62
|
}
|
|
57
63
|
/**
|
|
58
64
|
* Custom fetch implementation, compatible with `window.fetch`.
|
|
@@ -111,7 +117,11 @@ declare class InferCore {
|
|
|
111
117
|
private debouncedFetch;
|
|
112
118
|
constructor(config: InferConfig);
|
|
113
119
|
handleInput(value: string): void;
|
|
114
|
-
handleKeyDown(event: KeyboardEvent |
|
|
120
|
+
handleKeyDown(event: KeyboardEvent | {
|
|
121
|
+
key: string;
|
|
122
|
+
target: EventTarget | null;
|
|
123
|
+
preventDefault: () => void;
|
|
124
|
+
}): void;
|
|
115
125
|
selectItem(item: InferResult | string): void;
|
|
116
126
|
private shouldAutoInsertComma;
|
|
117
127
|
private finishSelection;
|
package/dist/index.global.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var Pro6PPCore=(()=>{var o=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var
|
|
1
|
+
"use strict";var Pro6PPCore=(()=>{var o=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var S=Object.prototype.hasOwnProperty;var m=(n,t,e)=>t in n?o(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var I=(n,t)=>{for(var e in t)o(n,e,{get:t[e],enumerable:!0})},y=(n,t,e,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of f(t))!S.call(n,r)&&r!==e&&o(n,r,{get:()=>t[r],enumerable:!(s=p(t,r))||s.enumerable});return n};var v=n=>y(o({},"__esModule",{value:!0}),n);var a=(n,t,e)=>m(n,typeof t!="symbol"?t+"":t,e);var x={};I(x,{INITIAL_STATE:()=>h,InferCore:()=>c});var l={API_URL:"https://api.pro6pp.nl/v2",LIMIT:1e3,DEBOUNCE_MS:300},g={DIGITS_1_3:/^[0-9]{1,3}$/},h={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,isError:!1,isLoading:!1,selectedSuggestionIndex:-1},c=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||l.API_URL,this.limit=t.limit||l.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),l.DEBOUNCE_MS)}handleInput(t){this.updateState({query:t,isValid:!1,isLoading:!!t.trim(),selectedSuggestionIndex:-1}),this.state.stage==="final"&&this.onSelect(null),this.debouncedFetch(t)}handleKeyDown(t){let e=t.target;if(!e)return;let s=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(s>0){if(t.key==="ArrowDown"){t.preventDefault();let i=this.state.selectedSuggestionIndex+1;i>=s&&(i=0),this.updateState({selectedSuggestionIndex:i});return}if(t.key==="ArrowUp"){t.preventDefault();let i=this.state.selectedSuggestionIndex-1;i<0&&(i=s-1),this.updateState({selectedSuggestionIndex:i});return}if(t.key==="Enter"&&this.state.selectedSuggestionIndex>=0){t.preventDefault();let u=[...this.state.cities,...this.state.streets,...this.state.suggestions][this.state.selectedSuggestionIndex];u&&(this.selectItem(u),this.updateState({selectedSuggestionIndex:-1}));return}}let r=e.value;if(t.key===" "&&this.shouldAutoInsertComma(r)){t.preventDefault();let i=`${r.trim()}, `;this.updateQueryAndFetch(i)}}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(",")&&g.DIGITS_1_3.test(t.trim()))return!0;if(this.state.stage==="house_number"){let s=this.getCurrentFragment(t);return g.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 d=this.getQueryPrefix(r);i=d?`${d} ${t}, ${e}, `:`${t}, ${e}, `}this.updateQueryAndFetch(i);return}if(s==="direct"||s==="addition"){this.finishSelection(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)}}};return v(x);})();
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
var g=Object.defineProperty;var p=(a,t,e)=>t in a?g(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 u={API_URL:"https://api.pro6pp.nl/v2",LIMIT:1e3,DEBOUNCE_MS:300},h={DIGITS_1_3:/^[0-9]{1,3}$/},c={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,isError:!1,isLoading:!1,selectedSuggestionIndex:-1},d=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||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={...c},this.debouncedFetch=this.debounce(e=>this.executeFetch(e),u.DEBOUNCE_MS)}handleInput(t){this.updateState({query:t,isValid:!1,isLoading:!!t.trim(),selectedSuggestionIndex:-1}),this.state.stage==="final"&&this.onSelect(null),this.debouncedFetch(t)}handleKeyDown(t){let e=t.target;if(!e)return;let s=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(s>0){if(t.key==="ArrowDown"){t.preventDefault();let i=this.state.selectedSuggestionIndex+1;i>=s&&(i=0),this.updateState({selectedSuggestionIndex:i});return}if(t.key==="ArrowUp"){t.preventDefault();let i=this.state.selectedSuggestionIndex-1;i<0&&(i=s-1),this.updateState({selectedSuggestionIndex:i});return}if(t.key==="Enter"&&this.state.selectedSuggestionIndex>=0){t.preventDefault();let o=[...this.state.cities,...this.state.streets,...this.state.suggestions][this.state.selectedSuggestionIndex];o&&(this.selectItem(o),this.updateState({selectedSuggestionIndex:-1}));return}}let r=e.value;if(t.key===" "&&this.shouldAutoInsertComma(r)){t.preventDefault();let i=`${r.trim()}, `;this.updateQueryAndFetch(i)}}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(",")&&h.DIGITS_1_3.test(t.trim()))return!0;if(this.state.stage==="house_number"){let s=this.getCurrentFragment(t);return h.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 l=this.getQueryPrefix(r);i=l?`${l} ${t}, ${e}, `:`${t}, ${e}, `}this.updateQueryAndFetch(i);return}if(s==="direct"||s==="addition"){this.finishSelection(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({...c,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{c as INITIAL_STATE,d as InferCore};
|
package/package.json
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pro6pp/infer-core",
|
|
3
|
+
"type": "module",
|
|
3
4
|
"description": "Core logic and client for the Pro6PP Infer API.",
|
|
4
5
|
"homepage": "https://github.com/pro6pp/infer-sdk/tree/main/packages/core",
|
|
5
6
|
"keywords": [
|
|
@@ -21,7 +22,7 @@
|
|
|
21
22
|
"url": "https://github.com/pro6pp/infer-sdk/issues"
|
|
22
23
|
},
|
|
23
24
|
"sideEffects": false,
|
|
24
|
-
"version": "0.0.2-beta.
|
|
25
|
+
"version": "0.0.2-beta.3",
|
|
25
26
|
"main": "./dist/index.js",
|
|
26
27
|
"module": "./dist/index.mjs",
|
|
27
28
|
"types": "./dist/index.d.ts",
|
|
@@ -30,9 +31,9 @@
|
|
|
30
31
|
"exports": {
|
|
31
32
|
".": {
|
|
32
33
|
"types": "./dist/index.d.ts",
|
|
34
|
+
"script": "./dist/index.global.js",
|
|
33
35
|
"import": "./dist/index.mjs",
|
|
34
|
-
"require": "./dist/index.js"
|
|
35
|
-
"script": "./dist/index.global.js"
|
|
36
|
+
"require": "./dist/index.js"
|
|
36
37
|
}
|
|
37
38
|
},
|
|
38
39
|
"files": [
|
|
@@ -42,7 +43,9 @@
|
|
|
42
43
|
"scripts": {
|
|
43
44
|
"build": "tsup",
|
|
44
45
|
"dev": "tsup --watch",
|
|
45
|
-
"type-check": "tsc --noEmit"
|
|
46
|
+
"type-check": "tsc --noEmit",
|
|
47
|
+
"test": "vitest",
|
|
48
|
+
"test:coverage": "vitest run --coverage"
|
|
46
49
|
},
|
|
47
50
|
"devDependencies": {
|
|
48
51
|
"tsup": "^8.0.0",
|
package/dist/index.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
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};
|