1/** 2 * @file Various utility functions 3 */ 4 5/** 6 * Create an element from a string 7 * @param {string} string - String to create an element from 8 * @returns {Element} 9 */ 10export function createElement(string) { 11 const div = document.createElement('div'); 12 div.innerHTML = string.trim(); 13 14 return div.firstChild; 15} 16 17/** 18 * Replace the handlebars in a string with data. 19 * @param {*} template - String to replace handlebars in 20 * @param {*} data - Data to replace handlebars with 21 * @returns {string} 22 */ 23 24export function updateTemplate(text, data) { 25 const matches = text.match(/\{\{(.*?)\}\}/g); 26 27 let copy = text; 28 29 matches.map((match) => { 30 const key = match.replace(/{|}/g, ''); 31 const value = data[key]; 32 33 copy = copy.replace(match, value); 34 35 return copy; 36 }); 37 38 return copy; 39} 40 41/** 42 * Fetch data from a URL and cache it in localStorage 43 * @param {string} url - URL to fetch 44 * @returns {Promise} 45 */ 46export function fetchWithCache(url) { 47 if (localStorage.getItem(url)) { 48 return new Promise((resolve, reject) => { 49 return resolve(JSON.parse(localStorage.getItem(url))); 50 }); 51 } 52 53 return fetch(url) 54 .then((r) => r.json()) 55 .then((data) => { 56 localStorage.setItem(url, JSON.stringify(data)); 57 58 return data; 59 }) 60 .then((data) => data); 61} 62 63/** 64 * Get the distance between two points in miles 65 * @param {Array} p1 - [lng, lat] 66 * @param {Array} p2 - [lng, lat] 67 */ 68export function getDistance(p1, p2) { 69 p1 = { 70 lng: p1[0], 71 lat: p1[1] 72 }; 73 74 p2 = { 75 lng: p2[0], 76 lat: p2[1] 77 }; 78 79 const R = 3958.8; 80 const Ï1 = (p1.lat * Math.PI) / 180; 81 const Ï2 = (p2.lat * Math.PI) / 180; 82 const ÎÏ = ((p2.lat - p1.lat) * Math.PI) / 180; 83 const Îλ = ((p2.lng - p1.lng) * Math.PI) / 180; 84 85 const a = 86 Math.sin(ÎÏ / 2) * Math.sin(ÎÏ / 2) + 87 Math.cos(Ï1) * Math.cos(Ï2) * Math.sin(Îλ / 2) * Math.sin(Îλ / 2); 88 89 const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); 90 91 const d = R * c; 92 93 return d; 94}
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.