@pro6pp/infer-core 0.1.0-beta.17 → 0.1.0-beta.19
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.cjs +1 -1
- package/dist/index.d.cts +6 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.global.js +2 -2
- package/dist/index.js +2 -2
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var g=Object.defineProperty;var _=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var L=Object.prototype.hasOwnProperty;var w=(n,t,e)=>t in n?g(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var A=(n,t)=>{for(var e in t)g(n,e,{get:t[e],enumerable:!0})},E=(n,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of T(t))!L.call(n,r)&&r!==e&&g(n,r,{get:()=>t[r],enumerable:!(i=_(t,r))||i.enumerable});return n};var D=n=>E(g({},"__esModule",{value:!0}),n);var p=(n,t,e)=>w(n,typeof t!="symbol"?t+"":t,e);var U={};A(U,{DEFAULT_STYLES:()=>I,INITIAL_STATE:()=>y,InferCore:()=>S,formatLabelByInputOrder:()=>m,getHighlightSegments:()=>k});module.exports=D(U);function v(n){return n.toLowerCase().trim()}function F(n){return n.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function R(n,t){let e=v(n),i=v(t);if(i.includes(" "))return e.indexOf(i);let s=new RegExp(`(?:^|[,\\s])${F(i)}(?:$|[,\\s])`,"g").exec(e);if(s){let o=s.index,l=e[o];return l===","||l===" "?o+1:o}return-1}function $(n,t){let e=[],i=[];t.street&&i.push({value:t.street,type:"street"}),t.city&&i.push({value:t.city,type:"city"}),t.postcode&&i.push({value:t.postcode,type:"postcode"}),t.street_number!==void 0&&t.street_number!==null&&i.push({value:String(t.street_number),type:"street_number"}),t.addition&&i.push({value:t.addition,type:"addition"});for(let r of i){let s=R(n,r.value);s!==-1&&e.push({type:r.type,value:r.value,position:s})}return e.sort((r,s)=>r.position-s.position),e}function m(n,t){if(!t||!n)return"";let e=$(n,t),i=new Set(e.map(o=>o.type)),r=[];for(let o of e)r.push(o.value);let s=["street","street_number","addition","postcode","city"];for(let o of s){if(i.has(o))continue;let l;switch(o){case"street":l=t.street;break;case"city":l=t.city;break;case"street_number":l=t.street_number!==void 0?String(t.street_number):void 0;break;case"postcode":l=t.postcode;break;case"addition":l=t.addition;break}l&&r.push(l)}return r.join(", ")}var f={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},x={DIGITS_1_3:/^[0-9]{1,3}$/,STREET_NUMBER_PREFIX:/^(\d+)\s*,\s*$/},y={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,value:null,isError:!1,isLoading:!1,hasMore:!1,selectedSuggestionIndex:-1},S=class{constructor(t){p(this,"country");p(this,"authKey");p(this,"explicitApiUrl");p(this,"baseLimit");p(this,"currentLimit");p(this,"maxRetries");p(this,"language");p(this,"fetcher");p(this,"onStateChange");p(this,"onSelect");p(this,"state");p(this,"abortController",null);p(this,"debouncedFetch");p(this,"isDestroyed",!1);this.country=t.country,this.authKey=t.authKey,this.explicitApiUrl=t.apiUrl,this.baseLimit=t.limit||f.LIMIT,this.currentLimit=this.baseLimit,this.language=t.language;let e=t.maxRetries!==void 0?t.maxRetries:f.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(e,10)),this.fetcher=t.fetcher||((s,o)=>fetch(s,o)),this.onStateChange=t.onStateChange||(()=>{}),this.onSelect=t.onSelect||(()=>{}),this.state={...y};let i=t.debounceMs!==void 0?t.debounceMs:f.DEBOUNCE_MS,r=Math.max(i,f.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(s=>this.executeFetch(s),r)}handleInput(t){if(this.isDestroyed)return;this.currentLimit=this.baseLimit;let e=this.state.stage==="final"&&t!==this.state.query;this.updateState({query:t,isValid:!1,value:null,isLoading:!!t.trim(),selectedSuggestionIndex:-1,hasMore:!1,stage:e?null:this.state.stage}),e&&this.onSelect(null),this.debouncedFetch(t)}loadMore(){this.isDestroyed||this.state.isLoading||(this.currentLimit+=this.baseLimit,this.updateState({isLoading:!0}),this.executeFetch(this.state.query))}handleKeyDown(t){if(this.isDestroyed)return;let e=t.target;if(!e)return;let i=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(i>0){if(t.key==="ArrowDown"){t.preventDefault();let s=this.state.selectedSuggestionIndex+1;s>=i&&(s=0),this.updateState({selectedSuggestionIndex:s});return}if(t.key==="ArrowUp"){t.preventDefault();let s=this.state.selectedSuggestionIndex-1;s<0&&(s=i-1),this.updateState({selectedSuggestionIndex:s});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 s=`${r.trim()}, `;this.updateQueryAndFetch(s)}}selectItem(t){if(this.isDestroyed)return!1;this.debouncedFetch.cancel(),this.abortController&&this.abortController.abort();let e=typeof t=="string"?t:t.label,i=e;typeof t!="string"&&typeof t.value=="string"&&(i=t.value);let r=typeof t!="string"&&typeof t.value=="object"?t.value:void 0,s=!!r&&Object.keys(r).length>0;if(this.state.stage==="final"||s){let l=e;if(r&&Object.keys(r).length>0){let{street:u,street_number:a,postcode:d,city:c,addition:h}=r;if(u&&a&&c){let b=h?` ${h}`:"",C=d?`${d}, `:"";l=`${u}, ${a}${b}, ${C}${c}`}}return this.finishSelection(l,r),!0}let o=typeof t!="string"?t.subtitle:null;return this.processSelection(i,o),!1}destroy(){this.isDestroyed||(this.isDestroyed=!0,this.debouncedFetch.cancel(),this.abortController?.abort(),this.abortController=null)}shouldAutoInsertComma(t){if(!t.includes(",")&&x.DIGITS_1_3.test(t.trim()))return!0;if(this.state.stage==="street_number"){let i=this.getCurrentFragment(t);return x.DIGITS_1_3.test(i)}return!1}finishSelection(t,e){this.updateState({query:t,suggestions:[],cities:[],streets:[],isValid:!0,value:e||null,stage:"final",hasMore:!1}),this.onSelect(e||t)}processSelection(t,e){let{stage:i,query:r}=this.state,s=r;if(e&&(i==="city"||i==="street"||i==="mixed")){if(i==="city")s=`${e}, ${t}, `;else{let a=this.getQueryPrefix(r),d=!a||!a.includes(e),c=a;if(a&&e){let h=a.match(x.STREET_NUMBER_PREFIX);if(h){let b=h[1];e.startsWith(b)&&(c="")}}d?s=c?`${c} ${t}, ${e}, `:`${t}, ${e}, `:s=c?`${c} ${t}, `:`${t}, `}this.updateQueryAndFetch(s);return}if(i==="direct"||i==="addition"){this.finishSelection(t);return}!r.includes(",")&&(i==="city"||i==="street"||i==="street_number_first")?s=`${t}, `:(s=this.replaceLastSegment(r,t),i!=="street_number"&&(s+=", ")),this.updateQueryAndFetch(s)}executeFetch(t,e=0){if(this.isDestroyed)return;let i=(t||"").toString();if(!i.trim()){this.abortController?.abort(),this.resetState();return}e===0&&(this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController);let r=this.abortController?.signal,s=this.explicitApiUrl?this.explicitApiUrl:`${f.API_URL}/infer/${this.country.toLowerCase()}`,o=new URLSearchParams({query:i,limit:this.currentLimit.toString()});this.explicitApiUrl&&o.append("country",this.country.toLowerCase()),this.authKey&&o.set("authKey",this.authKey),this.language&&o.set("language",this.language);let l=s.includes("?")?"&":"?",u=`${s}${l}${o.toString()}`;this.fetcher(u,{signal:r}).then(a=>{if(!a.ok){if(e<this.maxRetries&&(a.status>=500||a.status===429))return this.retry(t,e,r);throw new Error("Network error")}return a.json()}).then(a=>{this.isDestroyed||r?.aborted||a&&this.mapResponseToState(a)}).catch(a=>{if(!this.isDestroyed&&a.name!=="AbortError"){if(e<this.maxRetries)return this.retry(t,e,r);this.updateState({isError:!0,isLoading:!1})}})}retry(t,e,i){if(this.isDestroyed||i?.aborted)return;let r=Math.pow(2,e)*200;setTimeout(()=>{!this.isDestroyed&&!i?.aborted&&this.executeFetch(t,e+1)},r)}mapResponseToState(t){let e={stage:t.stage,isLoading:!1},i=t.suggestions||[],r=[],s=new Set;for(let l of i){let u=`${l.label}|${l.subtitle||""}|${JSON.stringify(l.value||{})}`;if(!s.has(u)){s.add(u);let a=this.reformatSuggestionLabel(l);r.push(a)}}let o=r.length+(t.cities?.length||0)+(t.streets?.length||0);e.hasMore=o>=this.currentLimit,t.stage==="mixed"?(e.cities=t.cities||[],e.streets=t.streets||[],e.cities?.length===0&&e.streets?.length===0?e.suggestions=r:e.suggestions=[]):(e.suggestions=r,e.cities=[],e.streets=[]),e.isValid=t.stage==="final",this.updateState(e),e.isValid&&r.length===1&&this.selectItem(r[0])}reformatSuggestionLabel(t){if(!t.value||typeof t.value=="string")return t;let e=t.value;if(!e.street||!e.city)return t;let i=m(this.state.query,e);return i?{...t,label:i}:t}updateQueryAndFetch(t){this.updateState({query:t,suggestions:[],cities:[],streets:[],isValid:!1,value:null,isLoading:!0,hasMore:!1}),this.debouncedFetch(t)}replaceLastSegment(t,e){let i=t.lastIndexOf(",");return i===-1?e:`${t.slice(0,i+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({...y,query:this.state.query})}updateState(t){this.isDestroyed||(this.state={...this.state,...t},this.onStateChange(this.state))}debounce(t,e){let i,r=(...s)=>{i&&clearTimeout(i),i=setTimeout(()=>t.apply(this,s),e)};return r.cancel=()=>{i&&(clearTimeout(i),i=void 0)},r}};function M(n){if(n.length===0)return n;let t=[];for(let e of n){let i=t[t.length-1];i&&i.match===e.match?i.text+=e.text:t.push({text:e.text,match:e.match})}return t}function k(n,t){if(!t||!n)return[{text:n,match:!1}];let e=[],i=n.toLowerCase(),r=t.toLowerCase(),s=0,o=0;for(let a=0;a<n.length;a++){if(!(s<t.length&&i[a]===r[s]))continue;a>o&&e.push({text:n.slice(o,a),match:!1}),e.push({text:n[a],match:!0}),s++,o=a+1}return o<n.length&&e.push({text:n.slice(o),match:!1}),s===t.length?M(e):[{text:n,match:!1}]}var I=`
|
|
2
2
|
.pro6pp-wrapper {
|
|
3
3
|
position: relative;
|
|
4
4
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Supported ISO 3166-1 alpha-2 country codes.
|
|
3
3
|
*/
|
|
4
|
-
type CountryCode = 'NL' | 'DE' | 'BE' | 'AT' | 'DK' | (string & {});
|
|
4
|
+
type CountryCode = 'NL' | 'DE' | 'BE' | 'AT' | 'DK' | 'CH' | 'LU' | 'FR' | (string & {});
|
|
5
5
|
/**
|
|
6
6
|
* Supported language codes for address labels.
|
|
7
7
|
*/
|
|
@@ -179,6 +179,7 @@ declare class InferCore {
|
|
|
179
179
|
state: InferState;
|
|
180
180
|
private abortController;
|
|
181
181
|
private debouncedFetch;
|
|
182
|
+
private isDestroyed;
|
|
182
183
|
/**
|
|
183
184
|
* Initializes a new instance of the Infer engine.
|
|
184
185
|
* @param config The configuration object including API keys and callbacks.
|
|
@@ -214,6 +215,10 @@ declare class InferCore {
|
|
|
214
215
|
* @returns boolean True if the selection is a final address.
|
|
215
216
|
*/
|
|
216
217
|
selectItem(item: InferResult | string): boolean;
|
|
218
|
+
/**
|
|
219
|
+
* Disposes the engine and prevents pending async work from updating state.
|
|
220
|
+
*/
|
|
221
|
+
destroy(): void;
|
|
217
222
|
private shouldAutoInsertComma;
|
|
218
223
|
private finishSelection;
|
|
219
224
|
private processSelection;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Supported ISO 3166-1 alpha-2 country codes.
|
|
3
3
|
*/
|
|
4
|
-
type CountryCode = 'NL' | 'DE' | 'BE' | 'AT' | 'DK' | (string & {});
|
|
4
|
+
type CountryCode = 'NL' | 'DE' | 'BE' | 'AT' | 'DK' | 'CH' | 'LU' | 'FR' | (string & {});
|
|
5
5
|
/**
|
|
6
6
|
* Supported language codes for address labels.
|
|
7
7
|
*/
|
|
@@ -179,6 +179,7 @@ declare class InferCore {
|
|
|
179
179
|
state: InferState;
|
|
180
180
|
private abortController;
|
|
181
181
|
private debouncedFetch;
|
|
182
|
+
private isDestroyed;
|
|
182
183
|
/**
|
|
183
184
|
* Initializes a new instance of the Infer engine.
|
|
184
185
|
* @param config The configuration object including API keys and callbacks.
|
|
@@ -214,6 +215,10 @@ declare class InferCore {
|
|
|
214
215
|
* @returns boolean True if the selection is a final address.
|
|
215
216
|
*/
|
|
216
217
|
selectItem(item: InferResult | string): boolean;
|
|
218
|
+
/**
|
|
219
|
+
* Disposes the engine and prevents pending async work from updating state.
|
|
220
|
+
*/
|
|
221
|
+
destroy(): void;
|
|
217
222
|
private shouldAutoInsertComma;
|
|
218
223
|
private finishSelection;
|
|
219
224
|
private processSelection;
|
package/dist/index.global.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";var Pro6PPCore=(()=>{var
|
|
1
|
+
"use strict";var Pro6PPCore=(()=>{var g=Object.defineProperty;var _=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var L=Object.prototype.hasOwnProperty;var w=(n,t,e)=>t in n?g(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var A=(n,t)=>{for(var e in t)g(n,e,{get:t[e],enumerable:!0})},E=(n,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of T(t))!L.call(n,r)&&r!==e&&g(n,r,{get:()=>t[r],enumerable:!(i=_(t,r))||i.enumerable});return n};var D=n=>E(g({},"__esModule",{value:!0}),n);var p=(n,t,e)=>w(n,typeof t!="symbol"?t+"":t,e);var U={};A(U,{DEFAULT_STYLES:()=>I,INITIAL_STATE:()=>y,InferCore:()=>S,formatLabelByInputOrder:()=>m,getHighlightSegments:()=>k});function v(n){return n.toLowerCase().trim()}function F(n){return n.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function R(n,t){let e=v(n),i=v(t);if(i.includes(" "))return e.indexOf(i);let s=new RegExp(`(?:^|[,\\s])${F(i)}(?:$|[,\\s])`,"g").exec(e);if(s){let o=s.index,l=e[o];return l===","||l===" "?o+1:o}return-1}function $(n,t){let e=[],i=[];t.street&&i.push({value:t.street,type:"street"}),t.city&&i.push({value:t.city,type:"city"}),t.postcode&&i.push({value:t.postcode,type:"postcode"}),t.street_number!==void 0&&t.street_number!==null&&i.push({value:String(t.street_number),type:"street_number"}),t.addition&&i.push({value:t.addition,type:"addition"});for(let r of i){let s=R(n,r.value);s!==-1&&e.push({type:r.type,value:r.value,position:s})}return e.sort((r,s)=>r.position-s.position),e}function m(n,t){if(!t||!n)return"";let e=$(n,t),i=new Set(e.map(o=>o.type)),r=[];for(let o of e)r.push(o.value);let s=["street","street_number","addition","postcode","city"];for(let o of s){if(i.has(o))continue;let l;switch(o){case"street":l=t.street;break;case"city":l=t.city;break;case"street_number":l=t.street_number!==void 0?String(t.street_number):void 0;break;case"postcode":l=t.postcode;break;case"addition":l=t.addition;break}l&&r.push(l)}return r.join(", ")}var f={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},x={DIGITS_1_3:/^[0-9]{1,3}$/,STREET_NUMBER_PREFIX:/^(\d+)\s*,\s*$/},y={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,value:null,isError:!1,isLoading:!1,hasMore:!1,selectedSuggestionIndex:-1},S=class{constructor(t){p(this,"country");p(this,"authKey");p(this,"explicitApiUrl");p(this,"baseLimit");p(this,"currentLimit");p(this,"maxRetries");p(this,"language");p(this,"fetcher");p(this,"onStateChange");p(this,"onSelect");p(this,"state");p(this,"abortController",null);p(this,"debouncedFetch");p(this,"isDestroyed",!1);this.country=t.country,this.authKey=t.authKey,this.explicitApiUrl=t.apiUrl,this.baseLimit=t.limit||f.LIMIT,this.currentLimit=this.baseLimit,this.language=t.language;let e=t.maxRetries!==void 0?t.maxRetries:f.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(e,10)),this.fetcher=t.fetcher||((s,o)=>fetch(s,o)),this.onStateChange=t.onStateChange||(()=>{}),this.onSelect=t.onSelect||(()=>{}),this.state={...y};let i=t.debounceMs!==void 0?t.debounceMs:f.DEBOUNCE_MS,r=Math.max(i,f.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(s=>this.executeFetch(s),r)}handleInput(t){if(this.isDestroyed)return;this.currentLimit=this.baseLimit;let e=this.state.stage==="final"&&t!==this.state.query;this.updateState({query:t,isValid:!1,value:null,isLoading:!!t.trim(),selectedSuggestionIndex:-1,hasMore:!1,stage:e?null:this.state.stage}),e&&this.onSelect(null),this.debouncedFetch(t)}loadMore(){this.isDestroyed||this.state.isLoading||(this.currentLimit+=this.baseLimit,this.updateState({isLoading:!0}),this.executeFetch(this.state.query))}handleKeyDown(t){if(this.isDestroyed)return;let e=t.target;if(!e)return;let i=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(i>0){if(t.key==="ArrowDown"){t.preventDefault();let s=this.state.selectedSuggestionIndex+1;s>=i&&(s=0),this.updateState({selectedSuggestionIndex:s});return}if(t.key==="ArrowUp"){t.preventDefault();let s=this.state.selectedSuggestionIndex-1;s<0&&(s=i-1),this.updateState({selectedSuggestionIndex:s});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 s=`${r.trim()}, `;this.updateQueryAndFetch(s)}}selectItem(t){if(this.isDestroyed)return!1;this.debouncedFetch.cancel(),this.abortController&&this.abortController.abort();let e=typeof t=="string"?t:t.label,i=e;typeof t!="string"&&typeof t.value=="string"&&(i=t.value);let r=typeof t!="string"&&typeof t.value=="object"?t.value:void 0,s=!!r&&Object.keys(r).length>0;if(this.state.stage==="final"||s){let l=e;if(r&&Object.keys(r).length>0){let{street:u,street_number:a,postcode:d,city:c,addition:h}=r;if(u&&a&&c){let b=h?` ${h}`:"",C=d?`${d}, `:"";l=`${u}, ${a}${b}, ${C}${c}`}}return this.finishSelection(l,r),!0}let o=typeof t!="string"?t.subtitle:null;return this.processSelection(i,o),!1}destroy(){this.isDestroyed||(this.isDestroyed=!0,this.debouncedFetch.cancel(),this.abortController?.abort(),this.abortController=null)}shouldAutoInsertComma(t){if(!t.includes(",")&&x.DIGITS_1_3.test(t.trim()))return!0;if(this.state.stage==="street_number"){let i=this.getCurrentFragment(t);return x.DIGITS_1_3.test(i)}return!1}finishSelection(t,e){this.updateState({query:t,suggestions:[],cities:[],streets:[],isValid:!0,value:e||null,stage:"final",hasMore:!1}),this.onSelect(e||t)}processSelection(t,e){let{stage:i,query:r}=this.state,s=r;if(e&&(i==="city"||i==="street"||i==="mixed")){if(i==="city")s=`${e}, ${t}, `;else{let a=this.getQueryPrefix(r),d=!a||!a.includes(e),c=a;if(a&&e){let h=a.match(x.STREET_NUMBER_PREFIX);if(h){let b=h[1];e.startsWith(b)&&(c="")}}d?s=c?`${c} ${t}, ${e}, `:`${t}, ${e}, `:s=c?`${c} ${t}, `:`${t}, `}this.updateQueryAndFetch(s);return}if(i==="direct"||i==="addition"){this.finishSelection(t);return}!r.includes(",")&&(i==="city"||i==="street"||i==="street_number_first")?s=`${t}, `:(s=this.replaceLastSegment(r,t),i!=="street_number"&&(s+=", ")),this.updateQueryAndFetch(s)}executeFetch(t,e=0){if(this.isDestroyed)return;let i=(t||"").toString();if(!i.trim()){this.abortController?.abort(),this.resetState();return}e===0&&(this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController);let r=this.abortController?.signal,s=this.explicitApiUrl?this.explicitApiUrl:`${f.API_URL}/infer/${this.country.toLowerCase()}`,o=new URLSearchParams({query:i,limit:this.currentLimit.toString()});this.explicitApiUrl&&o.append("country",this.country.toLowerCase()),this.authKey&&o.set("authKey",this.authKey),this.language&&o.set("language",this.language);let l=s.includes("?")?"&":"?",u=`${s}${l}${o.toString()}`;this.fetcher(u,{signal:r}).then(a=>{if(!a.ok){if(e<this.maxRetries&&(a.status>=500||a.status===429))return this.retry(t,e,r);throw new Error("Network error")}return a.json()}).then(a=>{this.isDestroyed||r?.aborted||a&&this.mapResponseToState(a)}).catch(a=>{if(!this.isDestroyed&&a.name!=="AbortError"){if(e<this.maxRetries)return this.retry(t,e,r);this.updateState({isError:!0,isLoading:!1})}})}retry(t,e,i){if(this.isDestroyed||i?.aborted)return;let r=Math.pow(2,e)*200;setTimeout(()=>{!this.isDestroyed&&!i?.aborted&&this.executeFetch(t,e+1)},r)}mapResponseToState(t){let e={stage:t.stage,isLoading:!1},i=t.suggestions||[],r=[],s=new Set;for(let l of i){let u=`${l.label}|${l.subtitle||""}|${JSON.stringify(l.value||{})}`;if(!s.has(u)){s.add(u);let a=this.reformatSuggestionLabel(l);r.push(a)}}let o=r.length+(t.cities?.length||0)+(t.streets?.length||0);e.hasMore=o>=this.currentLimit,t.stage==="mixed"?(e.cities=t.cities||[],e.streets=t.streets||[],e.cities?.length===0&&e.streets?.length===0?e.suggestions=r:e.suggestions=[]):(e.suggestions=r,e.cities=[],e.streets=[]),e.isValid=t.stage==="final",this.updateState(e),e.isValid&&r.length===1&&this.selectItem(r[0])}reformatSuggestionLabel(t){if(!t.value||typeof t.value=="string")return t;let e=t.value;if(!e.street||!e.city)return t;let i=m(this.state.query,e);return i?{...t,label:i}:t}updateQueryAndFetch(t){this.updateState({query:t,suggestions:[],cities:[],streets:[],isValid:!1,value:null,isLoading:!0,hasMore:!1}),this.debouncedFetch(t)}replaceLastSegment(t,e){let i=t.lastIndexOf(",");return i===-1?e:`${t.slice(0,i+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({...y,query:this.state.query})}updateState(t){this.isDestroyed||(this.state={...this.state,...t},this.onStateChange(this.state))}debounce(t,e){let i,r=(...s)=>{i&&clearTimeout(i),i=setTimeout(()=>t.apply(this,s),e)};return r.cancel=()=>{i&&(clearTimeout(i),i=void 0)},r}};function M(n){if(n.length===0)return n;let t=[];for(let e of n){let i=t[t.length-1];i&&i.match===e.match?i.text+=e.text:t.push({text:e.text,match:e.match})}return t}function k(n,t){if(!t||!n)return[{text:n,match:!1}];let e=[],i=n.toLowerCase(),r=t.toLowerCase(),s=0,o=0;for(let a=0;a<n.length;a++){if(!(s<t.length&&i[a]===r[s]))continue;a>o&&e.push({text:n.slice(o,a),match:!1}),e.push({text:n[a],match:!0}),s++,o=a+1}return o<n.length&&e.push({text:n.slice(o),match:!1}),s===t.length?M(e):[{text:n,match:!1}]}var I=`
|
|
2
2
|
.pro6pp-wrapper {
|
|
3
3
|
position: relative;
|
|
4
4
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
|
@@ -201,4 +201,4 @@
|
|
|
201
201
|
@keyframes pro6pp-spin {
|
|
202
202
|
to { transform: rotate(360deg); }
|
|
203
203
|
}
|
|
204
|
-
`;return
|
|
204
|
+
`;return D(U);})();
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var I=Object.defineProperty;var C=(n,t,e)=>t in n?I(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var
|
|
1
|
+
var I=Object.defineProperty;var C=(n,t,e)=>t in n?I(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var p=(n,t,e)=>C(n,typeof t!="symbol"?t+"":t,e);function x(n){return n.toLowerCase().trim()}function _(n){return n.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function T(n,t){let e=x(n),i=x(t);if(i.includes(" "))return e.indexOf(i);let r=new RegExp(`(?:^|[,\\s])${_(i)}(?:$|[,\\s])`,"g").exec(e);if(r){let o=r.index,l=e[o];return l===","||l===" "?o+1:o}return-1}function L(n,t){let e=[],i=[];t.street&&i.push({value:t.street,type:"street"}),t.city&&i.push({value:t.city,type:"city"}),t.postcode&&i.push({value:t.postcode,type:"postcode"}),t.street_number!==void 0&&t.street_number!==null&&i.push({value:String(t.street_number),type:"street_number"}),t.addition&&i.push({value:t.addition,type:"addition"});for(let s of i){let r=T(n,s.value);r!==-1&&e.push({type:s.type,value:s.value,position:r})}return e.sort((s,r)=>s.position-r.position),e}function m(n,t){if(!t||!n)return"";let e=L(n,t),i=new Set(e.map(o=>o.type)),s=[];for(let o of e)s.push(o.value);let r=["street","street_number","addition","postcode","city"];for(let o of r){if(i.has(o))continue;let l;switch(o){case"street":l=t.street;break;case"city":l=t.city;break;case"street_number":l=t.street_number!==void 0?String(t.street_number):void 0;break;case"postcode":l=t.postcode;break;case"addition":l=t.addition;break}l&&s.push(l)}return s.join(", ")}var f={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},b={DIGITS_1_3:/^[0-9]{1,3}$/,STREET_NUMBER_PREFIX:/^(\d+)\s*,\s*$/},y={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,value:null,isError:!1,isLoading:!1,hasMore:!1,selectedSuggestionIndex:-1},S=class{constructor(t){p(this,"country");p(this,"authKey");p(this,"explicitApiUrl");p(this,"baseLimit");p(this,"currentLimit");p(this,"maxRetries");p(this,"language");p(this,"fetcher");p(this,"onStateChange");p(this,"onSelect");p(this,"state");p(this,"abortController",null);p(this,"debouncedFetch");p(this,"isDestroyed",!1);this.country=t.country,this.authKey=t.authKey,this.explicitApiUrl=t.apiUrl,this.baseLimit=t.limit||f.LIMIT,this.currentLimit=this.baseLimit,this.language=t.language;let e=t.maxRetries!==void 0?t.maxRetries:f.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(e,10)),this.fetcher=t.fetcher||((r,o)=>fetch(r,o)),this.onStateChange=t.onStateChange||(()=>{}),this.onSelect=t.onSelect||(()=>{}),this.state={...y};let i=t.debounceMs!==void 0?t.debounceMs:f.DEBOUNCE_MS,s=Math.max(i,f.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(r=>this.executeFetch(r),s)}handleInput(t){if(this.isDestroyed)return;this.currentLimit=this.baseLimit;let e=this.state.stage==="final"&&t!==this.state.query;this.updateState({query:t,isValid:!1,value:null,isLoading:!!t.trim(),selectedSuggestionIndex:-1,hasMore:!1,stage:e?null:this.state.stage}),e&&this.onSelect(null),this.debouncedFetch(t)}loadMore(){this.isDestroyed||this.state.isLoading||(this.currentLimit+=this.baseLimit,this.updateState({isLoading:!0}),this.executeFetch(this.state.query))}handleKeyDown(t){if(this.isDestroyed)return;let e=t.target;if(!e)return;let i=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(i>0){if(t.key==="ArrowDown"){t.preventDefault();let r=this.state.selectedSuggestionIndex+1;r>=i&&(r=0),this.updateState({selectedSuggestionIndex:r});return}if(t.key==="ArrowUp"){t.preventDefault();let r=this.state.selectedSuggestionIndex-1;r<0&&(r=i-1),this.updateState({selectedSuggestionIndex:r});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 s=e.value;if(t.key===" "&&this.shouldAutoInsertComma(s)){t.preventDefault();let r=`${s.trim()}, `;this.updateQueryAndFetch(r)}}selectItem(t){if(this.isDestroyed)return!1;this.debouncedFetch.cancel(),this.abortController&&this.abortController.abort();let e=typeof t=="string"?t:t.label,i=e;typeof t!="string"&&typeof t.value=="string"&&(i=t.value);let s=typeof t!="string"&&typeof t.value=="object"?t.value:void 0,r=!!s&&Object.keys(s).length>0;if(this.state.stage==="final"||r){let l=e;if(s&&Object.keys(s).length>0){let{street:u,street_number:a,postcode:d,city:c,addition:h}=s;if(u&&a&&c){let g=h?` ${h}`:"",v=d?`${d}, `:"";l=`${u}, ${a}${g}, ${v}${c}`}}return this.finishSelection(l,s),!0}let o=typeof t!="string"?t.subtitle:null;return this.processSelection(i,o),!1}destroy(){this.isDestroyed||(this.isDestroyed=!0,this.debouncedFetch.cancel(),this.abortController?.abort(),this.abortController=null)}shouldAutoInsertComma(t){if(!t.includes(",")&&b.DIGITS_1_3.test(t.trim()))return!0;if(this.state.stage==="street_number"){let i=this.getCurrentFragment(t);return b.DIGITS_1_3.test(i)}return!1}finishSelection(t,e){this.updateState({query:t,suggestions:[],cities:[],streets:[],isValid:!0,value:e||null,stage:"final",hasMore:!1}),this.onSelect(e||t)}processSelection(t,e){let{stage:i,query:s}=this.state,r=s;if(e&&(i==="city"||i==="street"||i==="mixed")){if(i==="city")r=`${e}, ${t}, `;else{let a=this.getQueryPrefix(s),d=!a||!a.includes(e),c=a;if(a&&e){let h=a.match(b.STREET_NUMBER_PREFIX);if(h){let g=h[1];e.startsWith(g)&&(c="")}}d?r=c?`${c} ${t}, ${e}, `:`${t}, ${e}, `:r=c?`${c} ${t}, `:`${t}, `}this.updateQueryAndFetch(r);return}if(i==="direct"||i==="addition"){this.finishSelection(t);return}!s.includes(",")&&(i==="city"||i==="street"||i==="street_number_first")?r=`${t}, `:(r=this.replaceLastSegment(s,t),i!=="street_number"&&(r+=", ")),this.updateQueryAndFetch(r)}executeFetch(t,e=0){if(this.isDestroyed)return;let i=(t||"").toString();if(!i.trim()){this.abortController?.abort(),this.resetState();return}e===0&&(this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController);let s=this.abortController?.signal,r=this.explicitApiUrl?this.explicitApiUrl:`${f.API_URL}/infer/${this.country.toLowerCase()}`,o=new URLSearchParams({query:i,limit:this.currentLimit.toString()});this.explicitApiUrl&&o.append("country",this.country.toLowerCase()),this.authKey&&o.set("authKey",this.authKey),this.language&&o.set("language",this.language);let l=r.includes("?")?"&":"?",u=`${r}${l}${o.toString()}`;this.fetcher(u,{signal:s}).then(a=>{if(!a.ok){if(e<this.maxRetries&&(a.status>=500||a.status===429))return this.retry(t,e,s);throw new Error("Network error")}return a.json()}).then(a=>{this.isDestroyed||s?.aborted||a&&this.mapResponseToState(a)}).catch(a=>{if(!this.isDestroyed&&a.name!=="AbortError"){if(e<this.maxRetries)return this.retry(t,e,s);this.updateState({isError:!0,isLoading:!1})}})}retry(t,e,i){if(this.isDestroyed||i?.aborted)return;let s=Math.pow(2,e)*200;setTimeout(()=>{!this.isDestroyed&&!i?.aborted&&this.executeFetch(t,e+1)},s)}mapResponseToState(t){let e={stage:t.stage,isLoading:!1},i=t.suggestions||[],s=[],r=new Set;for(let l of i){let u=`${l.label}|${l.subtitle||""}|${JSON.stringify(l.value||{})}`;if(!r.has(u)){r.add(u);let a=this.reformatSuggestionLabel(l);s.push(a)}}let o=s.length+(t.cities?.length||0)+(t.streets?.length||0);e.hasMore=o>=this.currentLimit,t.stage==="mixed"?(e.cities=t.cities||[],e.streets=t.streets||[],e.cities?.length===0&&e.streets?.length===0?e.suggestions=s:e.suggestions=[]):(e.suggestions=s,e.cities=[],e.streets=[]),e.isValid=t.stage==="final",this.updateState(e),e.isValid&&s.length===1&&this.selectItem(s[0])}reformatSuggestionLabel(t){if(!t.value||typeof t.value=="string")return t;let e=t.value;if(!e.street||!e.city)return t;let i=m(this.state.query,e);return i?{...t,label:i}:t}updateQueryAndFetch(t){this.updateState({query:t,suggestions:[],cities:[],streets:[],isValid:!1,value:null,isLoading:!0,hasMore:!1}),this.debouncedFetch(t)}replaceLastSegment(t,e){let i=t.lastIndexOf(",");return i===-1?e:`${t.slice(0,i+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({...y,query:this.state.query})}updateState(t){this.isDestroyed||(this.state={...this.state,...t},this.onStateChange(this.state))}debounce(t,e){let i,s=(...r)=>{i&&clearTimeout(i),i=setTimeout(()=>t.apply(this,r),e)};return s.cancel=()=>{i&&(clearTimeout(i),i=void 0)},s}};function w(n){if(n.length===0)return n;let t=[];for(let e of n){let i=t[t.length-1];i&&i.match===e.match?i.text+=e.text:t.push({text:e.text,match:e.match})}return t}function M(n,t){if(!t||!n)return[{text:n,match:!1}];let e=[],i=n.toLowerCase(),s=t.toLowerCase(),r=0,o=0;for(let a=0;a<n.length;a++){if(!(r<t.length&&i[a]===s[r]))continue;a>o&&e.push({text:n.slice(o,a),match:!1}),e.push({text:n[a],match:!0}),r++,o=a+1}return o<n.length&&e.push({text:n.slice(o),match:!1}),r===t.length?w(e):[{text:n,match:!1}]}var A=`
|
|
2
2
|
.pro6pp-wrapper {
|
|
3
3
|
position: relative;
|
|
4
4
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
|
@@ -201,4 +201,4 @@ var I=Object.defineProperty;var C=(n,t,e)=>t in n?I(n,t,{enumerable:!0,configura
|
|
|
201
201
|
@keyframes pro6pp-spin {
|
|
202
202
|
to { transform: rotate(360deg); }
|
|
203
203
|
}
|
|
204
|
-
`;export{A as DEFAULT_STYLES,y as INITIAL_STATE,S as InferCore,m as formatLabelByInputOrder,
|
|
204
|
+
`;export{A as DEFAULT_STYLES,y as INITIAL_STATE,S as InferCore,m as formatLabelByInputOrder,M as getHighlightSegments};
|