1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 2 * Global Institute for Water Security | Global Water Futures 3 * Copyright (c) 2021 All Rights Reserved 4 * Author: Stephen O'Hearn 5 * 6 * This javascript contains simplified replacement functions for (I daresay, neo-experimental) functions (like includes() for arrays and 7 * contains() for strings) not for-sure supported in all browsers. 8 * 9 * Include this javascript file (at least nearly) before other javascript files! 10 * 11 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 12 13// Polyfill for older browser replaceAll() --> usage ziggle = ziggle.replaceAll("cat", "dog"); 14// causes "cat elephant cat aardvark" -> "dog elephant dog aardvark" 15// 16if (!String.prototype.replaceAll) { 17 String.prototype.replaceAll = function (toBeReplacedSubstring, replacementSubstring) { 18 if (this.indexOf(toBeReplacedSubstring) === -1) return this.toString(); 19 // Escape regex chars in toBeReplacedSubstring to match literal string 20 const toBeReplacedSubstringEscaped = toBeReplacedSubstring.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); //don't ask. 21 return this.replace(new RegExp(toBeReplacedSubstringEscaped, 'g'), replacementSubstring); 22 }; 23} 24 25 26// Reimplementation of includes() for arrays (confuser: contains()) 27// 28function ARRAY_INCLUDES(A, item) { // howto!! reliably true vs false in javascript 29 if (!A) { 30 return ""; 31 } 32 if (A.length <= 0) { 33 return ""; 34 } 35 for (i = 0; i < A.length; i++) { 36 if (A[i] == item) { 37 return "True"; 38 } 39 } 40 return ""; 41} 42 43// Same as ARRAY_INCLUDES() 44// 45function ARRAY_CONTAINS(A, item) { 46 return ARRAY_INCLUDES(A, item); 47} 48 49// Number.isNaN() ** is not supported * (in all browsers). However, Number.NaN yes indeed is. 50function IS_NOT_A_NUMBER(o) { return typeof (o) === 'number' && isNaN(o); } 51 52// log10() is ** not supported ** (in all browsers). However Math.log() yes indeed is. 53function LOG_10(n) { return Math.log(n) / Math.log(10); } 54 55// string.startsWith and endsWith not supported in Internet Explorer 11 56 57function STARTS_WITH(str, word) { 58 if (!str) return false; 59 return str.lastIndexOf(word, 0) === 0; 60} 61 62function ENDS_WITH(str, word) { 63 return str.indexOf(word, str.length - word.length) !== -1; 64} 65 66// returns pixel [width, height] - no nonsense--all browsers 67function WINDOW_SIZE() { 68 var win = window, 69 doc = document, 70 docElem = doc.documentElement, 71 body = doc.getElementsByTagName('body')[0], 72 x = win.innerWidth || docElem.clientWidth || body.clientWidth, 73 y = win.innerHeight || docElem.clientHeight || body.clientHeight; 74 //alert(x + ' à ' + y); 75 return [x, y]; 76} 77 78function REMOVE_ELEMENT(elementId) { 79 var el = document.getElementById(elementId); 80 if (!el || !el.parentNode) { 81 return; 82 } 83 el.parentNode.removeChild(el); 84} 85 86function TRIM_LAST_ONLY(chxr, str) { 87 // Calling TRIM_LAST_ONLY("|","a|b|c|||") here-how, e.g., will return "a|b|c||", removing only 88 // the very last character specified in chxr parameter (a string in chxr is allowed as well) 89 if (!str) { return ""; } 90 if (!chxr || chxr.length > str.length) { return str; } 91 if (str.charAt(str.length - chxr.length) === chxr) { 92 return str.substring(0, str.length - chxr.length); 93 } else { 94 return str; // BUGFIX 2026-03-11 95 } 96} 97 98// call herehow: var result = REMOVE_SUBSTRING_FROM_END("filename.txt", ".txt"); 99function REMOVE_SUBSTRING_FROM_END(str, sxbstring) { 100 var lastIndex = str.lastIndexOf(sxbstring); 101 if (lastIndex !== -1 && lastIndex === str.length - sxbstring.length) { 102 return str.substring(0, lastIndex); 103 } 104 return str; 105} 106 107// Functions reliably supported 108// ---------------------------- 109// 110// myString.indexOf() 111// myString.lastIndexOf() 112// trim(), e.g. var apple = ziggle.trim(); 113// replacement="zaggle"; s.replace(replacement,"ziggle"); only replaces first occurrance 114// replace(/herewhat/,"ziggle") - only using the slash form where to-be-replaced substring is represented between the /...../g part
115// e.g. replace first comma with nothing(i.e., trim first comma): s = s.replace(/^,/, '') 116// e.g. replace all red with green s = s.replace(/red/g, "green") 117// e.g. replace all plusses with an underscore s = s.replace(/\+/g, "_") 118// e.g. replace all pipes with tabs: vxlue = vxlue.replace(/\|/g, '\t'); 119// e.g. replace all whitespace with nothing: vxlue = vxlue.replace(/\s+/g, ''); 120// replaceAll is not universally supported (so polyfill has been provided at the top of this file) 121// split() 122// e.g. var myArray = text.split(","); 123// substring(begin,end) I think is universally supported, go ahead and use it 124//
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.