@lowentry/utils 1.8.1 → 1.9.1

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 (3) hide show
  1. package/LeUtils.js +125 -1
  2. package/index.js +2 -4
  3. package/package.json +2 -1
package/LeUtils.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import FastDeepEqual from 'fast-deep-equal';
2
- import {ISSET, IS_OBJECT, STRING, INT_LAX, FLOAT_LAX, INT_LAX_ANY, FLOAT_LAX_ANY} from './LeTypes.js';
2
+ import CloneDeep from 'clone-deep';
3
+ import {ISSET, IS_OBJECT, IS_ARRAY, STRING, INT_LAX, FLOAT_LAX, INT_LAX_ANY, FLOAT_LAX_ANY} from './LeTypes.js';
3
4
 
4
5
 
5
6
  /**
@@ -2816,6 +2817,9 @@ export const LeUtils = {
2816
2817
  * }
2817
2818
  * })();
2818
2819
  * ```
2820
+ *
2821
+ * @param {string} name
2822
+ * @returns {{worker: Worker, sendMessage: function(Object, {timeout: number|undefined}|undefined): Promise<Object>}}
2819
2823
  */
2820
2824
  createWorkerThread:
2821
2825
  (name) =>
@@ -2877,6 +2881,11 @@ export const LeUtils = {
2877
2881
  * Sends a message to the given worker. Creates a worker thread for this worker if it doesn't exist yet.
2878
2882
  *
2879
2883
  * See {@link LeUtils#createWorkerThread} for more info on how to use workers.
2884
+ *
2885
+ * @param {string} workerName
2886
+ * @param {Object} data
2887
+ * @param {{timeout: number|undefined}} [options]
2888
+ * @returns {Promise<Object>}
2880
2889
  */
2881
2890
  sendWorkerMessage:
2882
2891
  (() =>
@@ -2891,4 +2900,119 @@ export const LeUtils = {
2891
2900
  return workers[workerName].sendMessage(data, options);
2892
2901
  };
2893
2902
  })(),
2903
+
2904
+ /**
2905
+ * Returns a deep copy of the given value.
2906
+ *
2907
+ * @param {*} value
2908
+ * @returns {*}
2909
+ */
2910
+ clone:
2911
+ (value) => CloneDeep(value, true),
2912
+
2913
+ /**
2914
+ * Purges the given email address, returning an empty string if it's invalid.
2915
+ *
2916
+ * @param {string} email
2917
+ * @returns {string}
2918
+ */
2919
+ purgeEmail:
2920
+ (email) =>
2921
+ {
2922
+ email = STRING(email).trim().toLowerCase().replace(/\s/g, '');
2923
+ if(!email.includes('@') || !email.includes('.'))
2924
+ {
2925
+ return '';
2926
+ }
2927
+ return email;
2928
+ },
2929
+
2930
+ /**
2931
+ * Returns true if the focus is effectively clear, meaning that the user is not typing in an input field.
2932
+ *
2933
+ * @returns {boolean}
2934
+ */
2935
+ isFocusClear:(() =>
2936
+ {
2937
+ const inputTypes = ['text', 'search', 'email', 'number', 'password', 'tel', 'time', 'url', 'week', 'month', 'date', 'datetime-local'];
2938
+ return () => !((document?.activeElement?.tagName?.toLowerCase() === 'input') && inputTypes.includes(document?.activeElement?.type?.toLowerCase()));
2939
+ })(),
2940
+
2941
+ /**
2942
+ * Returns the user's locale. Returns 'en-US' if it can't be determined.
2943
+ *
2944
+ * @returns {string}
2945
+ */
2946
+ getUserLocale:(() =>
2947
+ {
2948
+ let userLocale = null;
2949
+ return () =>
2950
+ {
2951
+ if(userLocale === null)
2952
+ {
2953
+ userLocale = (() =>
2954
+ {
2955
+ if(typeof window === 'undefined')
2956
+ {
2957
+ return 'en-US';
2958
+ }
2959
+ let locales = window.navigator.languages;
2960
+ if(!IS_ARRAY(locales) || (locales.length <= 0))
2961
+ {
2962
+ return 'en-US';
2963
+ }
2964
+ locales = locales.filter(locale => ((typeof locale === 'string') && locale.includes('-') && (locale.toLowerCase() !== 'en-us')));
2965
+ if(locales.length <= 0)
2966
+ {
2967
+ return 'en-US';
2968
+ }
2969
+ const localesNoEnglish = locales.filter(locale => !locale.toLowerCase().startsWith('en-'));
2970
+ if(localesNoEnglish.length <= 0)
2971
+ {
2972
+ return locales[0];
2973
+ }
2974
+ return localesNoEnglish[0];
2975
+ })();
2976
+ }
2977
+ return userLocale;
2978
+ };
2979
+ })(),
2980
+
2981
+ /**
2982
+ * Returns the user's locale date format. Always returns YYYY MM DD, with the character in between depending on the user's locale. Returns 'YYYY/MM/DD' if the user's locale can't be determined.
2983
+ *
2984
+ * @returns {string}
2985
+ */
2986
+ getUserLocaleDateFormat:(() =>
2987
+ {
2988
+ let userLocaleDateFormat = null;
2989
+ return () =>
2990
+ {
2991
+ if(userLocaleDateFormat === null)
2992
+ {
2993
+ userLocaleDateFormat = (() =>
2994
+ {
2995
+ let char = '/';
2996
+ if((typeof window !== 'undefined') && (typeof Intl !== 'undefined') && (typeof Intl.DateTimeFormat !== 'undefined'))
2997
+ {
2998
+ const formattedDate = new Intl.DateTimeFormat(LeUtils.getUserLocale()).format();
2999
+ if(formattedDate.includes('-'))
3000
+ {
3001
+ char = '-';
3002
+ }
3003
+ else if(formattedDate.includes('. '))
3004
+ {
3005
+ char = '.';
3006
+ }
3007
+ else if(formattedDate.includes('.'))
3008
+ {
3009
+ char = '.';
3010
+ }
3011
+ }
3012
+ return 'YYYY' + char + 'MM' + char + 'DD';
3013
+ })();
3014
+ }
3015
+ return userLocaleDateFormat;
3016
+ };
3017
+ })(),
2894
3018
  };
package/index.js CHANGED
@@ -1,4 +1,2 @@
1
- import {LeUtils} from './LeUtils.js';
2
- import {ISSET, IS_ARRAY, ARRAY, IS_OBJECT, OBJECT, STRING, STRING_ANY, INT, INT_ANY, FLOAT, FLOAT_ANY, INT_LAX, INT_LAX_ANY, FLOAT_LAX, FLOAT_LAX_ANY} from './LeTypes.js';
3
-
4
- export {LeUtils, ISSET, IS_ARRAY, ARRAY, IS_OBJECT, OBJECT, STRING, STRING_ANY, INT, INT_ANY, FLOAT, FLOAT_ANY, INT_LAX, INT_LAX_ANY, FLOAT_LAX, FLOAT_LAX_ANY};
1
+ export {LeUtils} from './LeUtils.js';
2
+ export {ISSET, IS_ARRAY, ARRAY, IS_OBJECT, OBJECT, STRING, STRING_ANY, INT, INT_ANY, FLOAT, FLOAT_ANY, INT_LAX, INT_LAX_ANY, FLOAT_LAX, FLOAT_LAX_ANY} from './LeTypes.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowentry/utils",
3
- "version": "1.8.1",
3
+ "version": "1.9.1",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Provides utilities for general JavaScript development.",
@@ -20,6 +20,7 @@
20
20
  "test": "node --check index.js"
21
21
  },
22
22
  "dependencies": {
23
+ "clone-deep": "^4",
23
24
  "fast-deep-equal": "^3"
24
25
  }
25
26
  }