@lowentry/utils 1.1.2 → 1.2.2

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.
Files changed (2) hide show
  1. package/LeUtils.js +57 -0
  2. package/package.json +1 -1
package/LeUtils.js CHANGED
@@ -2263,6 +2263,63 @@ export const LeUtils = {
2263
2263
  window.localStorage.removeItem('LeUtils_' + id);
2264
2264
  },
2265
2265
 
2266
+ /**
2267
+ * Returns whether the current hostname (window.location.hostname) is private (such as localhost, 192.168.1.1, etc).
2268
+ * This can be used to determine if the app is running in a development environment or not.
2269
+ *
2270
+ * Only "localhost" and IPv4 addresses are supported. IPv6 addresses will always return false.
2271
+ *
2272
+ * @returns {boolean}
2273
+ */
2274
+ isCurrentHostPrivate:
2275
+ (() =>
2276
+ {
2277
+ let lastHostname = null;
2278
+ let lastResult = false;
2279
+
2280
+ return () =>
2281
+ {
2282
+ if(typeof window === 'undefined')
2283
+ {
2284
+ return false; // server-side rendering, who knows to who it is being served to, assume it's public
2285
+ }
2286
+ const hostname = window.location.hostname;
2287
+ if(lastHostname === hostname)
2288
+ {
2289
+ return lastResult;
2290
+ }
2291
+ lastHostname = hostname;
2292
+ lastResult = LeUtils.isGivenHostPrivate(lastHostname);
2293
+ return lastResult;
2294
+ };
2295
+ })(),
2296
+
2297
+ /**
2298
+ * Returns true if the given hostname is private (such as localhost, 192.168.1.1, etc).
2299
+ *
2300
+ * Only "localhost" and IPv4 addresses are supported. IPv6 addresses will always return false.
2301
+ *
2302
+ * @param {string} host
2303
+ * @returns {boolean}
2304
+ */
2305
+ isGivenHostPrivate:
2306
+ (host) =>
2307
+ {
2308
+ host = STRING(host).trim().toLowerCase();
2309
+ if((host === 'localhost') || (host === '127.0.0.1'))
2310
+ {
2311
+ return true;
2312
+ }
2313
+ if(!/^(\d{1,3}\.){3}\d{1,3}$/.test(host))
2314
+ {
2315
+ return false;
2316
+ }
2317
+ const parts = host.split('.');
2318
+ return (parts[0] === '10') || // 10.0.0.0 - 10.255.255.255
2319
+ ((parts[0] === '172') && ((parseInt(parts[1], 10) >= 16) && (parseInt(parts[1], 10) <= 31))) || // 172.16.0.0 - 172.31.255.255
2320
+ ((parts[0] === '192') && (parts[1] === '168')); // 192.168.0.0 - 192.168.255.255
2321
+ },
2322
+
2266
2323
  /**
2267
2324
  * Creates and returns a new TreeSet.
2268
2325
  * A TreeSet is a set of elements, sorted by a comparator.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowentry/utils",
3
- "version": "1.1.2",
3
+ "version": "1.2.2",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Provides utilities for general JavaScript development.",