dce-reactkit 3.4.0 → 3.4.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.
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * Given an array of strings, create a single comma-separated string that includes
3
3
  * 'and' as well as an oxford comma.
4
- * Ex: ['apples'] => 'apples'
5
- * Ex: ['apples', 'bananas'] => 'apples and bananas'
6
- * Ex: ['apples', 'bananas', 'grapes'] => 'apples, bananas, and grapes'
4
+ * Ex: ['apples'] => 'apples'
5
+ * Ex: ['apples', 'bananas'] => 'apples and bananas'
6
+ * Ex: ['apples', 'bananas', 'grapes'] => 'apples, bananas, and grapes'
7
7
  * @author Austen Money
8
8
  * @param list an array of elements to be made into a single comma-separated string.
9
9
  * @returns a comma-separated string.
@@ -2,7 +2,7 @@
2
2
  * Result of a validation function.
3
3
  * @author Austen Money
4
4
  */
5
- type ValidationResult<CleanedValueType> = ({
5
+ declare type ValidationResult<CleanedValueType> = ({
6
6
  isValid: true;
7
7
  cleanedValue: CleanedValueType;
8
8
  } | {
@@ -54,6 +54,10 @@ import canReviewLogs from './helpers/canReviewLogs';
54
54
  import isMobileOrTablet from './helpers/isMobileOrTablet';
55
55
  import extractProp from './helpers/extractProp';
56
56
  import compareArraysByProp from './helpers/compareArraysByProp';
57
+ import genCommaList from './helpers/genCommaList';
58
+ import validateEmail from './helpers/validators/validateEmail';
59
+ import validatePhoneNumber from './helpers/validators/validatePhoneNumber';
60
+ import validateString from './helpers/validators/validateString';
57
61
  import getLocalTimeInfo from './helpers/getLocalTimeInfo';
58
62
  import ModalButtonType from './types/ModalButtonType';
59
63
  import ModalSize from './types/ModalSize';
@@ -70,4 +74,4 @@ import LogBuiltInMetadata from './types/LogBuiltInMetadata';
70
74
  import LogMetadataType from './types/LogMetadataType';
71
75
  import IntelliTableColumn from './types/IntelliTableColumn';
72
76
  import PickableItem from './components/ItemPicker/types/PickableItem';
73
- export { AppWrapper, LoadingSpinner, ErrorBox, Modal, TabBox, RadioButton, CheckboxButton, ButtonInputGroup, SimpleDateChooser, Drawer, PopSuccessMark, PopFailureMark, PopPendingMark, CopiableBox, ItemPicker, LogReviewer, IntelliTable, CSVDownloadButton, alert, confirm, showFatalError, ErrorWithCode, MINUTE_IN_MS, HOUR_IN_MS, DAY_IN_MS, DynamicWord, abbreviate, avg, ceilToNumDecimals, floorToNumDecimals, forceNumIntoBounds, padDecimalZeros, padZerosLeft, roundToNumDecimals, sum, waitMs, getOrdinal, getTimeInfoInET, stubServerEndpoint, startMinWait, getHumanReadableDate, getPartOfDay, stringsToHumanReadableList, onlyKeepLetters, parallelLimit, getMonthName, genCSV, canReviewLogs, isMobileOrTablet, extractProp, compareArraysByProp, getLocalTimeInfo, initClient, visitServerEndpoint, logClientEvent, initServer, genRouteHandler, handleError, handleSuccess, initLogCollection, ModalButtonType, ModalSize, ModalType, ReactKitErrorCode, Variant, DayOfWeek, Log, LogType, LogSource, LogAction, LogBuiltInMetadata, LogMetadataType, IntelliTableColumn, PickableItem, ParamType, };
77
+ export { AppWrapper, LoadingSpinner, ErrorBox, Modal, TabBox, RadioButton, CheckboxButton, ButtonInputGroup, SimpleDateChooser, Drawer, PopSuccessMark, PopFailureMark, PopPendingMark, CopiableBox, ItemPicker, LogReviewer, IntelliTable, CSVDownloadButton, alert, confirm, showFatalError, ErrorWithCode, MINUTE_IN_MS, HOUR_IN_MS, DAY_IN_MS, DynamicWord, abbreviate, avg, ceilToNumDecimals, floorToNumDecimals, forceNumIntoBounds, padDecimalZeros, padZerosLeft, roundToNumDecimals, sum, waitMs, getOrdinal, getTimeInfoInET, stubServerEndpoint, startMinWait, getHumanReadableDate, getPartOfDay, stringsToHumanReadableList, onlyKeepLetters, parallelLimit, getMonthName, genCSV, canReviewLogs, isMobileOrTablet, extractProp, compareArraysByProp, genCommaList, validateEmail, validatePhoneNumber, validateString, getLocalTimeInfo, initClient, visitServerEndpoint, logClientEvent, initServer, genRouteHandler, handleError, handleSuccess, initLogCollection, ModalButtonType, ModalSize, ModalType, ReactKitErrorCode, Variant, DayOfWeek, Log, LogType, LogSource, LogAction, LogBuiltInMetadata, LogMetadataType, IntelliTableColumn, PickableItem, ParamType, };
package/dist/esm/index.js CHANGED
@@ -5632,6 +5632,246 @@ const compareArraysByProp = (a, b, prop) => {
5632
5632
  });
5633
5633
  };
5634
5634
 
5635
+ /**
5636
+ * Given an array of strings, create a single comma-separated string that includes
5637
+ * 'and' as well as an oxford comma.
5638
+ * Ex: ['apples'] => 'apples'
5639
+ * Ex: ['apples', 'bananas'] => 'apples and bananas'
5640
+ * Ex: ['apples', 'bananas', 'grapes'] => 'apples, bananas, and grapes'
5641
+ * @author Austen Money
5642
+ * @param list an array of elements to be made into a single comma-separated string.
5643
+ * @returns a comma-separated string.
5644
+ */
5645
+ const genCommaList = (list) => {
5646
+ const { length } = list;
5647
+ return (length < 2
5648
+ ? list.join('')
5649
+ : `${list.slice(0, length - 1).join(', ')}${length < 3 ? ' and ' : ', and '}${list[length - 1]}`);
5650
+ };
5651
+
5652
+ /*------------------------------------------------------------------------*/
5653
+ /* ------------------------------ Constants ----------------------------- */
5654
+ /*------------------------------------------------------------------------*/
5655
+ const INVALID_REGEX_ERROR = 'input does not follow the requested format';
5656
+ const INVALID_EMAIL_ERROR = 'Please provide a valid email address.';
5657
+ const INVALID_PHONE_ERROR = 'Please provide a valid phone number.';
5658
+ const INVALID_STRING_ERRORS = {
5659
+ MIN_LEN: (minLen) => {
5660
+ return `input must not be under ${minLen} character(s)`;
5661
+ },
5662
+ MAX_LEN: (maxLen) => {
5663
+ return `input must not be over ${maxLen} character(s)`;
5664
+ },
5665
+ LETTERS_ONLY: 'input must only contain letters',
5666
+ NUMBERS_ONLY: 'input must only contain numbers',
5667
+ MESSAGE_INTRO: 'The following error(s) occurred: ',
5668
+ };
5669
+
5670
+ // Import constants
5671
+ /**
5672
+ * Determines whether a given input string is considered valid based on
5673
+ * the provided regex.
5674
+ * @author Austen Money
5675
+ * @param opts object containing all args
5676
+ * @param opts.input user-provided input to validate
5677
+ * @param opts.regex regular expression to check against input
5678
+ * @param [opts.regexDescription] description of what regexString is checking
5679
+ * for, used to customize error message
5680
+ * @returns the unchanged input if valid, or a customized error message if
5681
+ * invalid
5682
+ */
5683
+ const validateRegex = (opts) => {
5684
+ // customize error message in case of invalid input
5685
+ const errorMessage = `${INVALID_REGEX_ERROR}${opts.regexDescription
5686
+ ? ': '
5687
+ : ''}${opts.regexDescription}`;
5688
+ // return error message if test is invalid, or input string if valid
5689
+ return (opts.regex.test(opts.input)
5690
+ ? {
5691
+ isValid: true,
5692
+ cleanedValue: opts.input,
5693
+ }
5694
+ : {
5695
+ isValid: false,
5696
+ errorMessage,
5697
+ });
5698
+ };
5699
+
5700
+ // Import helpers
5701
+ /**
5702
+ * Determines whether a given email address is valid.
5703
+ * @author Austen Money
5704
+ * @param email email address to validate
5705
+ * @returns whether email fulfills proper formatting requirements, includes a
5706
+ * cleaned version of the address without leading or trailing
5707
+ * whitespace if valid or an error message if invalid.
5708
+ */
5709
+ const validateEmail = (email) => {
5710
+ // validation regex, sourced from HTML living standard: http://www.whatwg.org/specs/web-apps/current-work/multipage/forms.html#e-mail-state-(type=email)
5711
+ // eslint-disable-next-line max-len
5712
+ const emailRegex = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
5713
+ // remove leading and trailing whitespace
5714
+ const cleanedValue = email.replace(/^\s+|\s+$/g, '');
5715
+ // validate email with regex, and return error if not valid
5716
+ return (validateRegex({
5717
+ input: cleanedValue,
5718
+ regex: emailRegex,
5719
+ }).isValid
5720
+ ? {
5721
+ isValid: true,
5722
+ cleanedValue,
5723
+ }
5724
+ : {
5725
+ isValid: false,
5726
+ errorMessage: INVALID_EMAIL_ERROR,
5727
+ });
5728
+ };
5729
+
5730
+ // Import helpers
5731
+ /**
5732
+ * Determines whether a given phone number is valid.
5733
+ * @author Austen Money
5734
+ * @param phoneNumber phone number to validate
5735
+ * @returns whether phone number is considered valid - if valid, also returns
5736
+ * a cleaned version of the number without any formatting. If invalid,
5737
+ * returns an error message.
5738
+ */
5739
+ const validatePhoneNumber = (phoneNumber) => {
5740
+ // regex to validate phone number
5741
+ const validationRegex = /^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$/;
5742
+ // validate phone number with regex
5743
+ const validationResponse = validateRegex({
5744
+ input: phoneNumber,
5745
+ regex: validationRegex,
5746
+ });
5747
+ // remove all non-digits from number
5748
+ const cleanedValue = phoneNumber.replace(/\D/g, '');
5749
+ // return cleaned value if valid, or error message if invalid
5750
+ return (validationResponse.isValid
5751
+ ? {
5752
+ isValid: true,
5753
+ cleanedValue,
5754
+ }
5755
+ : {
5756
+ isValid: false,
5757
+ errorMessage: INVALID_PHONE_ERROR,
5758
+ });
5759
+ };
5760
+
5761
+ // Import helpers
5762
+ /*------------------------------------------------------------------------*/
5763
+ /* ------------------------------ Constants ----------------------------- */
5764
+ /*------------------------------------------------------------------------*/
5765
+ // define minimum and maximum range for lowercase ASCII chars
5766
+ const LOWERCASE_MIN = 65;
5767
+ const LOWERCASE_MAX = 90;
5768
+ // define minimum and maximum range for uppercase ASCII chars
5769
+ const UPPERCASE_MIN = 97;
5770
+ const UPPERCASE_MAX = 122;
5771
+ // define minimum and maximum range for ASCII digits
5772
+ const DIGIT_MIN = 48;
5773
+ const DIGIT_MAX = 57;
5774
+ /*------------------------------------------------------------------------*/
5775
+ /* ---------------------------- Main Function --------------------------- */
5776
+ /*------------------------------------------------------------------------*/
5777
+ /**
5778
+ * Determines whether a given input string is considered valid based on
5779
+ * the provided requirements.
5780
+ * @author Austen Money
5781
+ * @param input input string
5782
+ * @param opts options for validation
5783
+ * @returns whether input is considered valid according to reqs - if
5784
+ * valid, returns a cleaned version of input; if invalid, returns
5785
+ * a string containing error messages describing which requirements
5786
+ * were not met.
5787
+ */
5788
+ const validateString = (input, opts) => {
5789
+ // stores all invalid input errors
5790
+ const errorMessages = [];
5791
+ // contains version of input that will be returned
5792
+ let cleanedValue = input;
5793
+ // remove whitespace if required
5794
+ if (opts.ignoreWhitespace) {
5795
+ cleanedValue = input.replace(/\s+/g, '');
5796
+ }
5797
+ // apply max char requirement
5798
+ if (opts.minLen) {
5799
+ if (cleanedValue.length < opts.minLen) {
5800
+ errorMessages.push(INVALID_STRING_ERRORS.MIN_LEN(opts.minLen));
5801
+ }
5802
+ }
5803
+ // apply max char requirement
5804
+ if (opts.maxLen) {
5805
+ if (cleanedValue.length > opts.maxLen) {
5806
+ errorMessages.push(INVALID_STRING_ERRORS.MAX_LEN(opts.maxLen));
5807
+ }
5808
+ }
5809
+ // apply alphabetical requirement
5810
+ if (opts.lettersOnly) {
5811
+ // remove diacritics
5812
+ cleanedValue = cleanedValue.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
5813
+ const containsNonLetters = (cleanedValue
5814
+ // split into characters
5815
+ .split('')
5816
+ // convert into character codes
5817
+ .map((curr) => {
5818
+ return curr.charCodeAt(0);
5819
+ })
5820
+ // check for non-letters
5821
+ .some((currCode) => {
5822
+ return (!(currCode >= LOWERCASE_MIN && currCode <= LOWERCASE_MAX)
5823
+ && !(currCode >= UPPERCASE_MIN && currCode <= UPPERCASE_MAX));
5824
+ }));
5825
+ if (containsNonLetters) {
5826
+ errorMessages.push(INVALID_STRING_ERRORS.LETTERS_ONLY);
5827
+ }
5828
+ }
5829
+ // apply numerical requirement
5830
+ if (opts.numbersOnly) {
5831
+ const containsNonNumbers = (cleanedValue
5832
+ // split into characters
5833
+ .split('')
5834
+ // convert into character codes
5835
+ .map((curr) => {
5836
+ return curr.charCodeAt(0);
5837
+ })
5838
+ // check for non-numbers
5839
+ .some((currCode) => {
5840
+ return (!(currCode >= DIGIT_MIN && currCode <= DIGIT_MAX));
5841
+ }));
5842
+ if (containsNonNumbers) {
5843
+ errorMessages.push(INVALID_STRING_ERRORS.NUMBERS_ONLY);
5844
+ }
5845
+ }
5846
+ // apply regex requirement
5847
+ if (opts.regexTest) {
5848
+ const regex = opts.regexTest;
5849
+ // validate and create customized error message if description is provided
5850
+ const result = validateRegex({
5851
+ input: cleanedValue,
5852
+ regex,
5853
+ regexDescription: opts.regexDescription,
5854
+ });
5855
+ // if string did not pass regex validation, add error message
5856
+ if (result.isValid === false) {
5857
+ errorMessages.push(result.errorMessage);
5858
+ }
5859
+ }
5860
+ // combine all error messages into one string to return
5861
+ const errorMessage = `${INVALID_STRING_ERRORS.MESSAGE_INTRO}${genCommaList(errorMessages)}.`;
5862
+ return (
5863
+ // if no error messages, string is valid; if not, it is invalid
5864
+ errorMessages.length === 0
5865
+ ? {
5866
+ isValid: true,
5867
+ cleanedValue,
5868
+ }
5869
+ : {
5870
+ isValid: false,
5871
+ errorMessage,
5872
+ });
5873
+ };
5874
+
5635
5875
  /**
5636
5876
  * Get current time info in local time
5637
5877
  * @author Gabe Abrams
@@ -5696,5 +5936,5 @@ var DayOfWeek;
5696
5936
  })(DayOfWeek || (DayOfWeek = {}));
5697
5937
  var DayOfWeek$1 = DayOfWeek;
5698
5938
 
5699
- export { AppWrapper, ButtonInputGroup, CSVDownloadButton, CheckboxButton, CopiableBox, DAY_IN_MS, DayOfWeek$1 as DayOfWeek, Drawer, DynamicWord, ErrorBox, ErrorWithCode, HOUR_IN_MS, IntelliTable, ItemPicker, LoadingSpinner, LogAction$1 as LogAction, LogBuiltInMetadata, LogReviewer, LogSource$1 as LogSource, LogType$1 as LogType, MINUTE_IN_MS, Modal, ModalButtonType$1 as ModalButtonType, ModalSize$1 as ModalSize, ModalType$1 as ModalType, ParamType$1 as ParamType, PopFailureMark, PopPendingMark, PopSuccessMark, RadioButton, ReactKitErrorCode$1 as ReactKitErrorCode, SimpleDateChooser, TabBox, Variant$1 as Variant, abbreviate, alert$1 as alert, avg, canReviewLogs, ceilToNumDecimals, compareArraysByProp, confirm, extractProp, floorToNumDecimals, forceNumIntoBounds, genCSV, genRouteHandler, getHumanReadableDate, getLocalTimeInfo, getMonthName, getOrdinal, getPartOfDay, getTimeInfoInET, handleError, handleSuccess, initClient, initLogCollection, initServer, isMobileOrTablet, logClientEvent, onlyKeepLetters, padDecimalZeros, padZerosLeft, parallelLimit, roundToNumDecimals, showFatalError, startMinWait, stringsToHumanReadableList, stubServerEndpoint, sum, visitServerEndpoint, waitMs };
5939
+ export { AppWrapper, ButtonInputGroup, CSVDownloadButton, CheckboxButton, CopiableBox, DAY_IN_MS, DayOfWeek$1 as DayOfWeek, Drawer, DynamicWord, ErrorBox, ErrorWithCode, HOUR_IN_MS, IntelliTable, ItemPicker, LoadingSpinner, LogAction$1 as LogAction, LogBuiltInMetadata, LogReviewer, LogSource$1 as LogSource, LogType$1 as LogType, MINUTE_IN_MS, Modal, ModalButtonType$1 as ModalButtonType, ModalSize$1 as ModalSize, ModalType$1 as ModalType, ParamType$1 as ParamType, PopFailureMark, PopPendingMark, PopSuccessMark, RadioButton, ReactKitErrorCode$1 as ReactKitErrorCode, SimpleDateChooser, TabBox, Variant$1 as Variant, abbreviate, alert$1 as alert, avg, canReviewLogs, ceilToNumDecimals, compareArraysByProp, confirm, extractProp, floorToNumDecimals, forceNumIntoBounds, genCSV, genCommaList, genRouteHandler, getHumanReadableDate, getLocalTimeInfo, getMonthName, getOrdinal, getPartOfDay, getTimeInfoInET, handleError, handleSuccess, initClient, initLogCollection, initServer, isMobileOrTablet, logClientEvent, onlyKeepLetters, padDecimalZeros, padZerosLeft, parallelLimit, roundToNumDecimals, showFatalError, startMinWait, stringsToHumanReadableList, stubServerEndpoint, sum, validateEmail, validatePhoneNumber, validateString, visitServerEndpoint, waitMs };
5700
5940
  //# sourceMappingURL=index.js.map