PageSourceSearch

https://www.belfor.com/us/wp-content/plugins/ds-locations/js/helpers.js?ver=0.1.0

js belfor.com collected 2026-10-03 22:02:35 UTC 3,953 bytes, 153 lines download raw bytes

1/**
2 *
3 * */
4const requestFunction = async ({url, type = 'get', data, typeError,}) => {
5	const isGetOrHead = ['get', 'head'].includes(type.toLowerCase());
6	const response = await fetch(url, {
7		method: type,
8		headers: {
9			'Content-Type': 'application/x-www-form-urlencoded',
10			'Cache-Control': 'no-cache',
11		},
12		...(isGetOrHead ? {} : {body: new URLSearchParams(data)}),
13	});
14	if (response.ok) {
15		return await response.json();
16	}
17	throw ({typeError, requestData: data, response});
18};
19
20/**
21 * Define query to get list of locations
22 * */
23const getMarkers = (data = {}) => requestFunction({
24	url: ds.ajax_url,
25	type: 'post',
26	data: {...data, action: 'get_markers'},
27	typeError: 'markers',
28});
29
30/**
31 * Define query to location popup for marker
32 * */
33const getInfoWindow = (data = {}) => requestFunction({
34	url: ds.ajax_url,
35	type: 'post',
36	data: {...data, action: 'info_window_content'},
37	typeError: 'infowindow',
38});
39
40// export { getMarkers, getInfoWindow };
41
42const getAddress = (address, geocoder) => new Promise((resolve) => {
43	geocoder.geocode({address, language: 'en'}, (results, status) => {
44		if (status === 'OK') {
45			resolve(results[0]);
46		} else {
47			// eslint-disable-next-line prefer-promise-reject-errors
48			resolve({typeError: 'geocoder', status, results});
49		}
50	});
51});
52
53// Get Markers by coordinate via geocoder (zip result) and filtering on BE
54const getMarkersByZip = async (props) => {
55	const {address, state} = props;
56	let response = {};
57	try {
58		const place = await getAddress(address, state.geocoder);
59		if (place && place['address_components'].find(item => item.types.includes("postal_code"))) {
60			if (place.geometry) {
61				response = await getMarkers({
62					type: 'by_coordinate',
63					radius: state.searchRadius,
64					lat: place.geometry.location.lat(),
65					lng: place.geometry.location.lng()
66				});
67			}
68		}
69		return response;
70	} catch (e) {
71		return response;
72	}
73};
74
75// Zip code validation
76const validationZipCode = (zip, countryCode) => {
77	if (!zip || !countryCode) {
78		return false;
79	}
80
81	const value = zip.trim();
82
83	const patterns = {
84		// USA: 12345 or 12345-6789
85		USA: /^\d{5}(-\d{4})?$/,
86
87		// Canada: K1A 0B1
88		CAN: /^[A-Z]\d[A-Z]\s?\d[A-Z]\d$/i,
89
90		// United Kingdom: SW1A 1AA
91		GBR: /^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$/i,
92
93		// Netherlands: 1012 AB
94		NLD: /^\d{4}\s?[A-Z]{2}$/i,
95
96		// Ireland: D02 X285
97		IRL: /^[A-Z]\d{2}\s?[A-Z0-9]{4}$/i,
98	};
99
100	// Special countries with alphanumeric ZIPs
101	if (patterns[countryCode]) {
102		return patterns[countryCode].test(value);
103	}
104
105	// Default numeric ZIP validation
106	// Supports:
107	// 1010
108	// 00-001
109	// 114 55
110	// 100-0001
111	return /^[\d\s-]+$/.test(value);
112};
113
114
115// Get Markers by zip and filtering in radius via google maps API only
116/*
117const getMarkersByZip = async (props) => {
118	const {address, state, radius = 100} = props;
119	let response = {};
120	let data = {markers: []}, markerList = document.createElement("div");
121	markerList.innerHTML = state.allData.html;
122	try {
123		const place = await getAddress(address, state.geocoder);
124		if (place && place['address_components'].find(item => item.types.includes("postal_code"))) {
125			if (place.geometry) {
126				let markers = [...state.allData.markers];
127				if (!markers[0]) {
128					const resp = await getMarkers({type: 'all'});
129					if (resp.success) {
130						markers = resp.data.markers;
131						markerList.innerHTML = resp.data.html;
132					}
133				}
134
135				markers.forEach(marker => {
136					let distance = (google.maps.geometry.spherical.computeDistanceBetween(new google.maps.LatLng(marker.lat, marker.lng), place.geometry.location)) / 1000;
137					if (distance < radius) {
138						data.markers.push(marker);
139					} else {
140						let markerElement = markerList.querySelector(`li[data-id="${marker.pid}"]`);
141						markerElement && markerElement.remove();
142					}
143				});
144			}
145			if (data.markers[0]) {
146				response = {data: {...data, html: markerList.innerHTML}, success: true}
147			}
148		}
149		return response;
150	} catch (e) {
151		return response;
152	}
153};*/

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.