dce-commonkit 4.4.0
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.
- package/.eslintrc.js +93 -0
- package/LICENSE +21 -0
- package/README.md +2 -0
- package/lib/constants/DAY_IN_MS.d.ts +6 -0
- package/lib/constants/DAY_IN_MS.js +9 -0
- package/lib/constants/DAY_IN_MS.js.map +1 -0
- package/lib/constants/HOUR_IN_MS.d.ts +6 -0
- package/lib/constants/HOUR_IN_MS.js +9 -0
- package/lib/constants/HOUR_IN_MS.js.map +1 -0
- package/lib/constants/LOG_REVIEW_GET_LOGS_ROUTE.d.ts +6 -0
- package/lib/constants/LOG_REVIEW_GET_LOGS_ROUTE.js +13 -0
- package/lib/constants/LOG_REVIEW_GET_LOGS_ROUTE.js.map +1 -0
- package/lib/constants/LOG_REVIEW_ROUTE_PATH_PREFIX.d.ts +6 -0
- package/lib/constants/LOG_REVIEW_ROUTE_PATH_PREFIX.js +13 -0
- package/lib/constants/LOG_REVIEW_ROUTE_PATH_PREFIX.js.map +1 -0
- package/lib/constants/LOG_REVIEW_STATUS_ROUTE.d.ts +7 -0
- package/lib/constants/LOG_REVIEW_STATUS_ROUTE.js +14 -0
- package/lib/constants/LOG_REVIEW_STATUS_ROUTE.js.map +1 -0
- package/lib/constants/LOG_ROUTE_PATH.d.ts +6 -0
- package/lib/constants/LOG_ROUTE_PATH.js +13 -0
- package/lib/constants/LOG_ROUTE_PATH.js.map +1 -0
- package/lib/constants/MINUTE_IN_MS.d.ts +6 -0
- package/lib/constants/MINUTE_IN_MS.js +9 -0
- package/lib/constants/MINUTE_IN_MS.js.map +1 -0
- package/lib/constants/ROUTE_PATH_PREFIX.d.ts +6 -0
- package/lib/constants/ROUTE_PATH_PREFIX.js +9 -0
- package/lib/constants/ROUTE_PATH_PREFIX.js.map +1 -0
- package/lib/constants/SELECT_ADMIN_CHECK_ROUTE.d.ts +6 -0
- package/lib/constants/SELECT_ADMIN_CHECK_ROUTE.js +9 -0
- package/lib/constants/SELECT_ADMIN_CHECK_ROUTE.js.map +1 -0
- package/lib/errors/ErrorWithCode.d.ts +9 -0
- package/lib/errors/ErrorWithCode.js +33 -0
- package/lib/errors/ErrorWithCode.js.map +1 -0
- package/lib/helpers/abbreviate.d.ts +10 -0
- package/lib/helpers/abbreviate.js +24 -0
- package/lib/helpers/abbreviate.js.map +1 -0
- package/lib/helpers/abbreviate.test.d.ts +5 -0
- package/lib/helpers/abbreviate.test.js +28 -0
- package/lib/helpers/abbreviate.test.js.map +1 -0
- package/lib/helpers/asyncArrayFunctions/everyAsync.d.ts +13 -0
- package/lib/helpers/asyncArrayFunctions/everyAsync.js +81 -0
- package/lib/helpers/asyncArrayFunctions/everyAsync.js.map +1 -0
- package/lib/helpers/asyncArrayFunctions/everyAsync.test.d.ts +1 -0
- package/lib/helpers/asyncArrayFunctions/everyAsync.test.js +219 -0
- package/lib/helpers/asyncArrayFunctions/everyAsync.test.js.map +1 -0
- package/lib/helpers/asyncArrayFunctions/filterAsync.d.ts +13 -0
- package/lib/helpers/asyncArrayFunctions/filterAsync.js +81 -0
- package/lib/helpers/asyncArrayFunctions/filterAsync.js.map +1 -0
- package/lib/helpers/asyncArrayFunctions/filterAsync.test.d.ts +1 -0
- package/lib/helpers/asyncArrayFunctions/filterAsync.test.js +244 -0
- package/lib/helpers/asyncArrayFunctions/filterAsync.test.js.map +1 -0
- package/lib/helpers/asyncArrayFunctions/forEachAsync.d.ts +10 -0
- package/lib/helpers/asyncArrayFunctions/forEachAsync.js +72 -0
- package/lib/helpers/asyncArrayFunctions/forEachAsync.js.map +1 -0
- package/lib/helpers/asyncArrayFunctions/forEachAsync.test.d.ts +1 -0
- package/lib/helpers/asyncArrayFunctions/forEachAsync.test.js +223 -0
- package/lib/helpers/asyncArrayFunctions/forEachAsync.test.js.map +1 -0
- package/lib/helpers/asyncArrayFunctions/mapAsync.d.ts +11 -0
- package/lib/helpers/asyncArrayFunctions/mapAsync.js +77 -0
- package/lib/helpers/asyncArrayFunctions/mapAsync.js.map +1 -0
- package/lib/helpers/asyncArrayFunctions/mapAsync.test.d.ts +1 -0
- package/lib/helpers/asyncArrayFunctions/mapAsync.test.js +219 -0
- package/lib/helpers/asyncArrayFunctions/mapAsync.test.js.map +1 -0
- package/lib/helpers/asyncArrayFunctions/someAsync.d.ts +13 -0
- package/lib/helpers/asyncArrayFunctions/someAsync.js +80 -0
- package/lib/helpers/asyncArrayFunctions/someAsync.js.map +1 -0
- package/lib/helpers/asyncArrayFunctions/someAsync.test.d.ts +1 -0
- package/lib/helpers/asyncArrayFunctions/someAsync.test.js +227 -0
- package/lib/helpers/asyncArrayFunctions/someAsync.test.js.map +1 -0
- package/lib/helpers/avg.d.ts +8 -0
- package/lib/helpers/avg.js +24 -0
- package/lib/helpers/avg.js.map +1 -0
- package/lib/helpers/avg.test.d.ts +5 -0
- package/lib/helpers/avg.test.js +25 -0
- package/lib/helpers/avg.test.js.map +1 -0
- package/lib/helpers/capitalize.d.ts +7 -0
- package/lib/helpers/capitalize.js +20 -0
- package/lib/helpers/capitalize.js.map +1 -0
- package/lib/helpers/ceilToNumDecimals.d.ts +9 -0
- package/lib/helpers/ceilToNumDecimals.js +15 -0
- package/lib/helpers/ceilToNumDecimals.js.map +1 -0
- package/lib/helpers/ceilToNumDecimals.test.d.ts +5 -0
- package/lib/helpers/ceilToNumDecimals.test.js +38 -0
- package/lib/helpers/ceilToNumDecimals.test.js.map +1 -0
- package/lib/helpers/cloneDeep.d.ts +8 -0
- package/lib/helpers/cloneDeep.js +15 -0
- package/lib/helpers/cloneDeep.js.map +1 -0
- package/lib/helpers/compareArraysByProp.d.ts +13 -0
- package/lib/helpers/compareArraysByProp.js +62 -0
- package/lib/helpers/compareArraysByProp.js.map +1 -0
- package/lib/helpers/compareArraysByProp.test.d.ts +5 -0
- package/lib/helpers/compareArraysByProp.test.js +140 -0
- package/lib/helpers/compareArraysByProp.test.js.map +1 -0
- package/lib/helpers/extractProp.d.ts +12 -0
- package/lib/helpers/extractProp.js +19 -0
- package/lib/helpers/extractProp.js.map +1 -0
- package/lib/helpers/extractProp.test.d.ts +5 -0
- package/lib/helpers/extractProp.test.js +36 -0
- package/lib/helpers/extractProp.test.js.map +1 -0
- package/lib/helpers/floorToNumDecimals.d.ts +9 -0
- package/lib/helpers/floorToNumDecimals.js +15 -0
- package/lib/helpers/floorToNumDecimals.js.map +1 -0
- package/lib/helpers/floorToNumDecimals.test.d.ts +5 -0
- package/lib/helpers/floorToNumDecimals.test.js +38 -0
- package/lib/helpers/floorToNumDecimals.test.js.map +1 -0
- package/lib/helpers/forceNumIntoBounds.d.ts +10 -0
- package/lib/helpers/forceNumIntoBounds.js +15 -0
- package/lib/helpers/forceNumIntoBounds.js.map +1 -0
- package/lib/helpers/forceNumIntoBounds.test.d.ts +5 -0
- package/lib/helpers/forceNumIntoBounds.test.js +34 -0
- package/lib/helpers/forceNumIntoBounds.test.js.map +1 -0
- package/lib/helpers/genCSV.d.ts +18 -0
- package/lib/helpers/genCSV.js +69 -0
- package/lib/helpers/genCSV.js.map +1 -0
- package/lib/helpers/genCSV.test.d.ts +5 -0
- package/lib/helpers/genCSV.test.js +75 -0
- package/lib/helpers/genCSV.test.js.map +1 -0
- package/lib/helpers/genCommaList.d.ts +12 -0
- package/lib/helpers/genCommaList.js +20 -0
- package/lib/helpers/genCommaList.js.map +1 -0
- package/lib/helpers/genCommaList.test.d.ts +1 -0
- package/lib/helpers/genCommaList.test.js +94 -0
- package/lib/helpers/genCommaList.test.js.map +1 -0
- package/lib/helpers/getHumanReadableDate.d.ts +8 -0
- package/lib/helpers/getHumanReadableDate.js +32 -0
- package/lib/helpers/getHumanReadableDate.js.map +1 -0
- package/lib/helpers/getHumanReadableDate.test.d.ts +5 -0
- package/lib/helpers/getHumanReadableDate.test.js +31 -0
- package/lib/helpers/getHumanReadableDate.test.js.map +1 -0
- package/lib/helpers/getLocalTimeInfo.d.ts +19 -0
- package/lib/helpers/getLocalTimeInfo.js +51 -0
- package/lib/helpers/getLocalTimeInfo.js.map +1 -0
- package/lib/helpers/getLocalTimeInfo.test.d.ts +6 -0
- package/lib/helpers/getLocalTimeInfo.test.js +50 -0
- package/lib/helpers/getLocalTimeInfo.test.js.map +1 -0
- package/lib/helpers/getMonthName.d.ts +14 -0
- package/lib/helpers/getMonthName.js +31 -0
- package/lib/helpers/getMonthName.js.map +1 -0
- package/lib/helpers/getMonthName.test.d.ts +5 -0
- package/lib/helpers/getMonthName.test.js +70 -0
- package/lib/helpers/getMonthName.test.js.map +1 -0
- package/lib/helpers/getOrdinal.d.ts +8 -0
- package/lib/helpers/getOrdinal.js +16 -0
- package/lib/helpers/getOrdinal.js.map +1 -0
- package/lib/helpers/getOrdinal.test.d.ts +5 -0
- package/lib/helpers/getOrdinal.test.js +74 -0
- package/lib/helpers/getOrdinal.test.js.map +1 -0
- package/lib/helpers/getPartOfDay.d.ts +7 -0
- package/lib/helpers/getPartOfDay.js +24 -0
- package/lib/helpers/getPartOfDay.js.map +1 -0
- package/lib/helpers/getPartOfDay.test.d.ts +5 -0
- package/lib/helpers/getPartOfDay.test.js +49 -0
- package/lib/helpers/getPartOfDay.test.js.map +1 -0
- package/lib/helpers/getTimeInfoInET.d.ts +21 -0
- package/lib/helpers/getTimeInfoInET.js +67 -0
- package/lib/helpers/getTimeInfoInET.js.map +1 -0
- package/lib/helpers/getTimeInfoInET.test.d.ts +5 -0
- package/lib/helpers/getTimeInfoInET.test.js +74 -0
- package/lib/helpers/getTimeInfoInET.test.js.map +1 -0
- package/lib/helpers/getTimestampFromTimeInfoInET.d.ts +20 -0
- package/lib/helpers/getTimestampFromTimeInfoInET.js +136 -0
- package/lib/helpers/getTimestampFromTimeInfoInET.js.map +1 -0
- package/lib/helpers/getWordCount.d.ts +10 -0
- package/lib/helpers/getWordCount.js +24 -0
- package/lib/helpers/getWordCount.js.map +1 -0
- package/lib/helpers/idify.d.ts +9 -0
- package/lib/helpers/idify.js +24 -0
- package/lib/helpers/idify.js.map +1 -0
- package/lib/helpers/onlyKeepLetters.d.ts +8 -0
- package/lib/helpers/onlyKeepLetters.js +13 -0
- package/lib/helpers/onlyKeepLetters.js.map +1 -0
- package/lib/helpers/padDecimalZeros.d.ts +10 -0
- package/lib/helpers/padDecimalZeros.js +30 -0
- package/lib/helpers/padDecimalZeros.js.map +1 -0
- package/lib/helpers/padZerosLeft.d.ts +9 -0
- package/lib/helpers/padZerosLeft.js +21 -0
- package/lib/helpers/padZerosLeft.js.map +1 -0
- package/lib/helpers/parallelLimit.d.ts +12 -0
- package/lib/helpers/parallelLimit.js +104 -0
- package/lib/helpers/parallelLimit.js.map +1 -0
- package/lib/helpers/prefixWithAOrAn.d.ts +10 -0
- package/lib/helpers/prefixWithAOrAn.js +28 -0
- package/lib/helpers/prefixWithAOrAn.js.map +1 -0
- package/lib/helpers/roundToNumDecimals.d.ts +9 -0
- package/lib/helpers/roundToNumDecimals.js +15 -0
- package/lib/helpers/roundToNumDecimals.js.map +1 -0
- package/lib/helpers/shuffleArray.d.ts +8 -0
- package/lib/helpers/shuffleArray.js +29 -0
- package/lib/helpers/shuffleArray.js.map +1 -0
- package/lib/helpers/startMinWait.d.ts +9 -0
- package/lib/helpers/startMinWait.js +79 -0
- package/lib/helpers/startMinWait.js.map +1 -0
- package/lib/helpers/stringsToHumanReadableList.d.ts +10 -0
- package/lib/helpers/stringsToHumanReadableList.js +37 -0
- package/lib/helpers/stringsToHumanReadableList.js.map +1 -0
- package/lib/helpers/sum.d.ts +8 -0
- package/lib/helpers/sum.js +15 -0
- package/lib/helpers/sum.js.map +1 -0
- package/lib/helpers/validators/ChicagoTitleCase.d.ts +9 -0
- package/lib/helpers/validators/ChicagoTitleCase.js +31 -0
- package/lib/helpers/validators/ChicagoTitleCase.js.map +1 -0
- package/lib/helpers/validators/ChicagoTitleCase.test.d.ts +0 -0
- package/lib/helpers/validators/ChicagoTitleCase.test.js +79 -0
- package/lib/helpers/validators/ChicagoTitleCase.test.js.map +1 -0
- package/lib/helpers/validators/findURL.d.ts +0 -0
- package/lib/helpers/validators/findURL.js +39 -0
- package/lib/helpers/validators/findURL.js.map +1 -0
- package/lib/helpers/validators/findURL.test.d.ts +0 -0
- package/lib/helpers/validators/findURL.test.js +77 -0
- package/lib/helpers/validators/findURL.test.js.map +1 -0
- package/lib/helpers/validators/shared/constants/ERROR_MESSAGES.d.ts +10 -0
- package/lib/helpers/validators/shared/constants/ERROR_MESSAGES.js +21 -0
- package/lib/helpers/validators/shared/constants/ERROR_MESSAGES.js.map +1 -0
- package/lib/helpers/validators/shared/helpers/validateRegex.d.ts +19 -0
- package/lib/helpers/validators/shared/helpers/validateRegex.js +34 -0
- package/lib/helpers/validators/shared/helpers/validateRegex.js.map +1 -0
- package/lib/helpers/validators/shared/types/ValidationResult.d.ts +12 -0
- package/lib/helpers/validators/shared/types/ValidationResult.js +3 -0
- package/lib/helpers/validators/shared/types/ValidationResult.js.map +1 -0
- package/lib/helpers/validators/validURL.d.ts +8 -0
- package/lib/helpers/validators/validURL.js +20 -0
- package/lib/helpers/validators/validURL.js.map +1 -0
- package/lib/helpers/validators/validURL.test.d.ts +0 -0
- package/lib/helpers/validators/validURL.test.js +83 -0
- package/lib/helpers/validators/validURL.test.js.map +1 -0
- package/lib/helpers/validators/validateEmail.d.ts +11 -0
- package/lib/helpers/validators/validateEmail.js +39 -0
- package/lib/helpers/validators/validateEmail.js.map +1 -0
- package/lib/helpers/validators/validateEmail.test.d.ts +1 -0
- package/lib/helpers/validators/validateEmail.test.js +140 -0
- package/lib/helpers/validators/validateEmail.test.js.map +1 -0
- package/lib/helpers/validators/validatePhoneNumber.d.ts +11 -0
- package/lib/helpers/validators/validatePhoneNumber.js +40 -0
- package/lib/helpers/validators/validatePhoneNumber.js.map +1 -0
- package/lib/helpers/validators/validatePhoneNumber.test.d.ts +1 -0
- package/lib/helpers/validators/validatePhoneNumber.test.js +123 -0
- package/lib/helpers/validators/validatePhoneNumber.test.js.map +1 -0
- package/lib/helpers/validators/validateString.d.ts +22 -0
- package/lib/helpers/validators/validateString.js +124 -0
- package/lib/helpers/validators/validateString.js.map +1 -0
- package/lib/helpers/validators/validateString.test.d.ts +1 -0
- package/lib/helpers/validators/validateString.test.js +163 -0
- package/lib/helpers/validators/validateString.test.js.map +1 -0
- package/lib/helpers/waitMs.d.ts +7 -0
- package/lib/helpers/waitMs.js +68 -0
- package/lib/helpers/waitMs.js.map +1 -0
- package/lib/index.d.ts +63 -0
- package/lib/index.js +124 -0
- package/lib/index.js.map +1 -0
- package/lib/types/CommonKitErrorCode.d.ts +10 -0
- package/lib/types/CommonKitErrorCode.js +15 -0
- package/lib/types/CommonKitErrorCode.js.map +1 -0
- package/lib/types/DayOfWeek.d.ts +14 -0
- package/lib/types/DayOfWeek.js +18 -0
- package/lib/types/DayOfWeek.js.map +1 -0
- package/lib/types/Log/LogMainInfo.d.ts +39 -0
- package/lib/types/Log/LogMainInfo.js +3 -0
- package/lib/types/Log/LogMainInfo.js.map +1 -0
- package/lib/types/Log/LogSourceSpecificInfo.d.ts +13 -0
- package/lib/types/Log/LogSourceSpecificInfo.js +3 -0
- package/lib/types/Log/LogSourceSpecificInfo.js.map +1 -0
- package/lib/types/Log/LogTypeSpecificInfo.d.ts +17 -0
- package/lib/types/Log/LogTypeSpecificInfo.js +3 -0
- package/lib/types/Log/LogTypeSpecificInfo.js.map +1 -0
- package/lib/types/Log/index.d.ts +10 -0
- package/lib/types/Log/index.js +3 -0
- package/lib/types/Log/index.js.map +1 -0
- package/lib/types/LogAction.d.ts +29 -0
- package/lib/types/LogAction.js +55 -0
- package/lib/types/LogAction.js.map +1 -0
- package/lib/types/LogBuiltInMetadata.d.ts +16 -0
- package/lib/types/LogBuiltInMetadata.js +21 -0
- package/lib/types/LogBuiltInMetadata.js.map +1 -0
- package/lib/types/LogFunction.d.ts +24 -0
- package/lib/types/LogFunction.js +3 -0
- package/lib/types/LogFunction.js.map +1 -0
- package/lib/types/LogLevel.d.ts +10 -0
- package/lib/types/LogLevel.js +14 -0
- package/lib/types/LogLevel.js.map +1 -0
- package/lib/types/LogMetadataContextMap.d.ts +11 -0
- package/lib/types/LogMetadataContextMap.js +3 -0
- package/lib/types/LogMetadataContextMap.js.map +1 -0
- package/lib/types/LogMetadataTagMap.d.ts +8 -0
- package/lib/types/LogMetadataTagMap.js +3 -0
- package/lib/types/LogMetadataTagMap.js.map +1 -0
- package/lib/types/LogMetadataTargetMap.d.ts +8 -0
- package/lib/types/LogMetadataTargetMap.js +3 -0
- package/lib/types/LogMetadataTargetMap.js.map +1 -0
- package/lib/types/LogMetadataType.d.ts +13 -0
- package/lib/types/LogMetadataType.js +3 -0
- package/lib/types/LogMetadataType.js.map +1 -0
- package/lib/types/LogSource.d.ts +9 -0
- package/lib/types/LogSource.js +15 -0
- package/lib/types/LogSource.js.map +1 -0
- package/lib/types/LogType.d.ts +9 -0
- package/lib/types/LogType.js +15 -0
- package/lib/types/LogType.js.map +1 -0
- package/lib/types/ParamType.d.ts +17 -0
- package/lib/types/ParamType.js +21 -0
- package/lib/types/ParamType.js.map +1 -0
- package/package.json +47 -0
- package/src/constants/DAY_IN_MS.ts +7 -0
- package/src/constants/HOUR_IN_MS.ts +7 -0
- package/src/constants/LOG_REVIEW_GET_LOGS_ROUTE.ts +9 -0
- package/src/constants/LOG_REVIEW_ROUTE_PATH_PREFIX.ts +9 -0
- package/src/constants/LOG_REVIEW_STATUS_ROUTE.ts +10 -0
- package/src/constants/LOG_ROUTE_PATH.ts +9 -0
- package/src/constants/MINUTE_IN_MS.ts +7 -0
- package/src/constants/ROUTE_PATH_PREFIX.ts +7 -0
- package/src/constants/SELECT_ADMIN_CHECK_ROUTE.ts +7 -0
- package/src/errors/ErrorWithCode.ts +15 -0
- package/src/helpers/abbreviate.test.ts +26 -0
- package/src/helpers/abbreviate.ts +25 -0
- package/src/helpers/asyncArrayFunctions/everyAsync.test.ts +114 -0
- package/src/helpers/asyncArrayFunctions/everyAsync.ts +51 -0
- package/src/helpers/asyncArrayFunctions/filterAsync.test.ts +126 -0
- package/src/helpers/asyncArrayFunctions/filterAsync.ts +50 -0
- package/src/helpers/asyncArrayFunctions/forEachAsync.test.ts +136 -0
- package/src/helpers/asyncArrayFunctions/forEachAsync.ts +40 -0
- package/src/helpers/asyncArrayFunctions/mapAsync.test.ts +125 -0
- package/src/helpers/asyncArrayFunctions/mapAsync.ts +46 -0
- package/src/helpers/asyncArrayFunctions/someAsync.test.ts +92 -0
- package/src/helpers/asyncArrayFunctions/someAsync.ts +49 -0
- package/src/helpers/avg.test.ts +24 -0
- package/src/helpers/avg.ts +22 -0
- package/src/helpers/capitalize.ts +20 -0
- package/src/helpers/ceilToNumDecimals.test.ts +39 -0
- package/src/helpers/ceilToNumDecimals.ts +13 -0
- package/src/helpers/cloneDeep.ts +11 -0
- package/src/helpers/compareArraysByProp.test.ts +147 -0
- package/src/helpers/compareArraysByProp.ts +56 -0
- package/src/helpers/extractProp.test.ts +34 -0
- package/src/helpers/extractProp.ts +17 -0
- package/src/helpers/floorToNumDecimals.test.ts +39 -0
- package/src/helpers/floorToNumDecimals.ts +13 -0
- package/src/helpers/forceNumIntoBounds.test.ts +34 -0
- package/src/helpers/forceNumIntoBounds.ts +19 -0
- package/src/helpers/genCSV.test.ts +77 -0
- package/src/helpers/genCSV.ts +87 -0
- package/src/helpers/genCommaList.test.ts +57 -0
- package/src/helpers/genCommaList.ts +21 -0
- package/src/helpers/getHumanReadableDate.test.ts +29 -0
- package/src/helpers/getHumanReadableDate.ts +36 -0
- package/src/helpers/getLocalTimeInfo.test.ts +48 -0
- package/src/helpers/getLocalTimeInfo.ts +58 -0
- package/src/helpers/getMonthName.test.ts +79 -0
- package/src/helpers/getMonthName.ts +29 -0
- package/src/helpers/getOrdinal.test.ts +84 -0
- package/src/helpers/getOrdinal.ts +14 -0
- package/src/helpers/getPartOfDay.test.ts +56 -0
- package/src/helpers/getPartOfDay.ts +20 -0
- package/src/helpers/getTimeInfoInET.test.ts +72 -0
- package/src/helpers/getTimeInfoInET.ts +79 -0
- package/src/helpers/getTimestampFromTimeInfoInET.ts +179 -0
- package/src/helpers/getWordCount.ts +26 -0
- package/src/helpers/idify.ts +24 -0
- package/src/helpers/onlyKeepLetters.ts +11 -0
- package/src/helpers/padDecimalZeros.ts +32 -0
- package/src/helpers/padZerosLeft.ts +21 -0
- package/src/helpers/parallelLimit.ts +63 -0
- package/src/helpers/prefixWithAOrAn.ts +29 -0
- package/src/helpers/roundToNumDecimals.ts +13 -0
- package/src/helpers/shuffleArray.ts +19 -0
- package/src/helpers/startMinWait.ts +33 -0
- package/src/helpers/stringsToHumanReadableList.ts +39 -0
- package/src/helpers/sum.ts +16 -0
- package/src/helpers/validators/ChicagoTitleCase.test.ts +85 -0
- package/src/helpers/validators/ChicagoTitleCase.ts +32 -0
- package/src/helpers/validators/findURL.test.ts +83 -0
- package/src/helpers/validators/findURL.ts +43 -0
- package/src/helpers/validators/shared/constants/ERROR_MESSAGES.ts +18 -0
- package/src/helpers/validators/shared/helpers/validateRegex.ts +47 -0
- package/src/helpers/validators/shared/types/ValidationResult.ts +20 -0
- package/src/helpers/validators/validURL.test.ts +90 -0
- package/src/helpers/validators/validURL.ts +18 -0
- package/src/helpers/validators/validateEmail.test.ts +110 -0
- package/src/helpers/validators/validateEmail.ts +47 -0
- package/src/helpers/validators/validatePhoneNumber.test.ts +94 -0
- package/src/helpers/validators/validatePhoneNumber.ts +49 -0
- package/src/helpers/validators/validateString.test.ts +173 -0
- package/src/helpers/validators/validateString.ts +171 -0
- package/src/helpers/waitMs.ts +12 -0
- package/src/index.ts +139 -0
- package/src/types/CommonKitErrorCode.ts +13 -0
- package/src/types/DayOfWeek.ts +15 -0
- package/src/types/Log/LogMainInfo.ts +69 -0
- package/src/types/Log/LogSourceSpecificInfo.ts +25 -0
- package/src/types/Log/LogTypeSpecificInfo.ts +33 -0
- package/src/types/Log/index.ts +17 -0
- package/src/types/LogAction.ts +52 -0
- package/src/types/LogBuiltInMetadata.ts +19 -0
- package/src/types/LogFunction.ts +46 -0
- package/src/types/LogLevel.ts +11 -0
- package/src/types/LogMetadataContextMap.ts +15 -0
- package/src/types/LogMetadataTagMap.ts +9 -0
- package/src/types/LogMetadataTargetMap.ts +9 -0
- package/src/types/LogMetadataType.ts +16 -0
- package/src/types/LogSource.ts +12 -0
- package/src/types/LogType.ts +12 -0
- package/src/types/ParamType.ts +18 -0
- package/tsconfig.json +19 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* Type of a log event
|
|
5
|
+
* @author Gabe Abrams
|
|
6
|
+
*/
|
|
7
|
+
var LogType;
|
|
8
|
+
(function (LogType) {
|
|
9
|
+
// User action
|
|
10
|
+
LogType["Action"] = "action";
|
|
11
|
+
// Error
|
|
12
|
+
LogType["Error"] = "error";
|
|
13
|
+
})(LogType || (LogType = {}));
|
|
14
|
+
exports.default = LogType;
|
|
15
|
+
//# sourceMappingURL=LogType.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LogType.js","sourceRoot":"","sources":["../../src/types/LogType.ts"],"names":[],"mappings":";;AAAA;;;GAGG;AACH,IAAK,OAKJ;AALD,WAAK,OAAO;IACV,cAAc;IACd,4BAAiB,CAAA;IACjB,QAAQ;IACR,0BAAe,CAAA;AACjB,CAAC,EALI,OAAO,KAAP,OAAO,QAKX;AAED,kBAAe,OAAO,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-side API param types
|
|
3
|
+
* @author Gabe Abrams
|
|
4
|
+
*/
|
|
5
|
+
declare enum ParamType {
|
|
6
|
+
Boolean = "Boolean",// Boolean
|
|
7
|
+
BooleanOptional = "BooleanOptional",// Optional boolean
|
|
8
|
+
Float = "Float",// Float Number
|
|
9
|
+
FloatOptional = "FloatOptional",// Optional Float Number
|
|
10
|
+
Int = "Int",// Integer Number
|
|
11
|
+
IntOptional = "IntOptional",// Optional Integer Number
|
|
12
|
+
JSON = "JSON",// JSONified object
|
|
13
|
+
JSONOptional = "JSONOptional",// Optional JSONified object
|
|
14
|
+
String = "String",// String
|
|
15
|
+
StringOptional = "StringOptional"
|
|
16
|
+
}
|
|
17
|
+
export default ParamType;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* Server-side API param types
|
|
5
|
+
* @author Gabe Abrams
|
|
6
|
+
*/
|
|
7
|
+
var ParamType;
|
|
8
|
+
(function (ParamType) {
|
|
9
|
+
ParamType["Boolean"] = "Boolean";
|
|
10
|
+
ParamType["BooleanOptional"] = "BooleanOptional";
|
|
11
|
+
ParamType["Float"] = "Float";
|
|
12
|
+
ParamType["FloatOptional"] = "FloatOptional";
|
|
13
|
+
ParamType["Int"] = "Int";
|
|
14
|
+
ParamType["IntOptional"] = "IntOptional";
|
|
15
|
+
ParamType["JSON"] = "JSON";
|
|
16
|
+
ParamType["JSONOptional"] = "JSONOptional";
|
|
17
|
+
ParamType["String"] = "String";
|
|
18
|
+
ParamType["StringOptional"] = "StringOptional";
|
|
19
|
+
})(ParamType || (ParamType = {}));
|
|
20
|
+
exports.default = ParamType;
|
|
21
|
+
//# sourceMappingURL=ParamType.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ParamType.js","sourceRoot":"","sources":["../../src/types/ParamType.ts"],"names":[],"mappings":";;AAAA;;;GAGG;AACH,IAAK,SAWJ;AAXD,WAAK,SAAS;IACZ,gCAAmB,CAAA;IACnB,gDAAmC,CAAA;IACnC,4BAAe,CAAA;IACf,4CAA+B,CAAA;IAC/B,wBAAW,CAAA;IACX,wCAA2B,CAAA;IAC3B,0BAAa,CAAA;IACb,0CAA6B,CAAA;IAC7B,8BAAiB,CAAA;IACjB,8CAAiC,CAAA;AACnC,CAAC,EAXI,SAAS,KAAT,SAAS,QAWb;AAED,kBAAe,SAAS,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "dce-commonkit",
|
|
3
|
+
"version": "4.4.0",
|
|
4
|
+
"description": "Common helpers, constants, and other resources that are shared between dce-reactkit and dce-expresskit",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"DCE",
|
|
7
|
+
"Harvard",
|
|
8
|
+
"React",
|
|
9
|
+
"Express",
|
|
10
|
+
"Helpers"
|
|
11
|
+
],
|
|
12
|
+
"main": "./lib/index.js",
|
|
13
|
+
"types": "./lib/index.d.ts",
|
|
14
|
+
"homepage": "https://github.com/harvard-edtech/dce-commonkit#readme",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/harvard-edtech/dce-commonkit/issues"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/harvard-edtech/dce-commonkit.git"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"author": "Gabe Abrams <gabeabrams@gmail.com>",
|
|
24
|
+
"type": "commonjs",
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsc --project ./tsconfig.json"
|
|
27
|
+
},
|
|
28
|
+
"jest": {
|
|
29
|
+
"preset": "ts-jest",
|
|
30
|
+
"testEnvironment": "jsdom"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/express": "^5.0.0",
|
|
34
|
+
"@types/express-session": "^1.18.1",
|
|
35
|
+
"@types/node": "^24.10.1",
|
|
36
|
+
"@typescript-eslint/eslint-plugin": "^5.59.9",
|
|
37
|
+
"@typescript-eslint/parser": "^5.59.9",
|
|
38
|
+
"eslint": "^8.42.0",
|
|
39
|
+
"typescript": "^5.8.2",
|
|
40
|
+
"@types/jest": "^28.1.7",
|
|
41
|
+
"jest": "^28.1.3",
|
|
42
|
+
"ts-jest": "^28.0.8"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"nanoclone": "^1.0.2"
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import ROUTE_PATH_PREFIX from './ROUTE_PATH_PREFIX';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Route for checking the status of the current user's
|
|
5
|
+
* access to log review
|
|
6
|
+
* @author Gabe Abrams
|
|
7
|
+
*/
|
|
8
|
+
const LOG_REVIEW_STATUS_ROUTE = `${ROUTE_PATH_PREFIX}/logs/access_allowed`;
|
|
9
|
+
|
|
10
|
+
export default LOG_REVIEW_STATUS_ROUTE;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An error with a code
|
|
3
|
+
* @author Gabe Abrams
|
|
4
|
+
*/
|
|
5
|
+
class ErrorWithCode extends Error {
|
|
6
|
+
code: string;
|
|
7
|
+
|
|
8
|
+
constructor(message: string, code: string) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = 'ErrorWithCode';
|
|
11
|
+
this.code = code;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export default ErrorWithCode;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for abbreviate helper
|
|
3
|
+
* @author Gabe Abrams
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import abbreviate from './abbreviate';
|
|
7
|
+
|
|
8
|
+
describe('helpers > abbreviate', () => {
|
|
9
|
+
it('should return the original text if it is shorter than the maxChars parameter', () => {
|
|
10
|
+
const text = 'Hello, world!';
|
|
11
|
+
const maxChars = 20;
|
|
12
|
+
expect(abbreviate(text, maxChars)).toEqual(text);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('should abbreviate the text if it is longer than the maxChars parameter', () => {
|
|
16
|
+
const text = 'This is a really long string that needs to be abbreviated';
|
|
17
|
+
const maxChars = 20;
|
|
18
|
+
expect(abbreviate(text, maxChars)).toEqual('This is a really...');
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should handle leading/trailing whitespace', () => {
|
|
22
|
+
const text = ' This is a string with leading/trailing whitespace ';
|
|
23
|
+
const maxChars = 20;
|
|
24
|
+
expect(abbreviate(text, maxChars)).toEqual('This is a string...');
|
|
25
|
+
});
|
|
26
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shorten text so it fits into a certain number of chars
|
|
3
|
+
* @author Gabe Abrams
|
|
4
|
+
* @param text the text to abbreviate
|
|
5
|
+
* @param maxChars the maximum number of chars to include
|
|
6
|
+
* @returns abbreviated text with length no greater than maxChars
|
|
7
|
+
* (including ellipses if applicable)
|
|
8
|
+
*/
|
|
9
|
+
const abbreviate = (text: string, maxChars: number): string => {
|
|
10
|
+
// Check if already short enough
|
|
11
|
+
if (text.trim().length < maxChars) {
|
|
12
|
+
return text.trim();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Abbreviate
|
|
16
|
+
const shortenedText = (
|
|
17
|
+
text
|
|
18
|
+
.trim()
|
|
19
|
+
.substring(0, maxChars - 3)
|
|
20
|
+
.trim()
|
|
21
|
+
);
|
|
22
|
+
return `${shortenedText}...`;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export default abbreviate;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// Import item to test
|
|
2
|
+
import everyAsync from './everyAsync';
|
|
3
|
+
|
|
4
|
+
describe('everyAsync', () => {
|
|
5
|
+
it('returns true for an empty array', async () => {
|
|
6
|
+
const result = await everyAsync(
|
|
7
|
+
[],
|
|
8
|
+
async () => {
|
|
9
|
+
return true;
|
|
10
|
+
},
|
|
11
|
+
);
|
|
12
|
+
expect(result).toBe(true);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('returns true if the operator function returns true for every item in the array', async () => {
|
|
16
|
+
const result = await everyAsync(
|
|
17
|
+
[1, 2, 3],
|
|
18
|
+
async (item) => {
|
|
19
|
+
return item > 0;
|
|
20
|
+
},
|
|
21
|
+
);
|
|
22
|
+
expect(result).toBe(true);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('returns false if the operator function returns false for any item in the array', async () => {
|
|
26
|
+
const result = await everyAsync(
|
|
27
|
+
[1, 2, 3],
|
|
28
|
+
async (item) => {
|
|
29
|
+
return item > 1;
|
|
30
|
+
},
|
|
31
|
+
);
|
|
32
|
+
expect(result).toBe(false);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('passes the index as the second argument to the operator function', async () => {
|
|
36
|
+
const result = await everyAsync(
|
|
37
|
+
[1, 2, 3],
|
|
38
|
+
async (item, index) => {
|
|
39
|
+
return index === item - 1;
|
|
40
|
+
},
|
|
41
|
+
);
|
|
42
|
+
expect(result).toBe(true);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('passes an object with a breakNow function and the array as the third argument to the operator function', async () => {
|
|
46
|
+
const result = await everyAsync(
|
|
47
|
+
[1, 2, 3],
|
|
48
|
+
async (item, index, { breakNow }) => {
|
|
49
|
+
if (item === 2) {
|
|
50
|
+
breakNow();
|
|
51
|
+
}
|
|
52
|
+
return true;
|
|
53
|
+
},
|
|
54
|
+
);
|
|
55
|
+
expect(result).toBe(false);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('stops iterating if the operator function calls the breakNow function', async () => {
|
|
59
|
+
const mockOperatorFunction = jest.fn(async (item, index, { breakNow }) => {
|
|
60
|
+
if (item === 2) {
|
|
61
|
+
breakNow();
|
|
62
|
+
}
|
|
63
|
+
return true;
|
|
64
|
+
});
|
|
65
|
+
const result = await everyAsync(
|
|
66
|
+
[1, 2, 3],
|
|
67
|
+
mockOperatorFunction,
|
|
68
|
+
);
|
|
69
|
+
expect(result).toBe(false);
|
|
70
|
+
expect(mockOperatorFunction).toHaveBeenCalledTimes(2);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('handles an operator function that returns a promise that resolves to true', async () => {
|
|
74
|
+
const result = await everyAsync(
|
|
75
|
+
[1, 2, 3],
|
|
76
|
+
async (item) => {
|
|
77
|
+
return Promise.resolve(item > 0);
|
|
78
|
+
},
|
|
79
|
+
);
|
|
80
|
+
expect(result).toBe(true);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('handles an operator function that returns a promise that resolves to false', async () => {
|
|
84
|
+
const result = await everyAsync(
|
|
85
|
+
[1, 2, 3],
|
|
86
|
+
async (item) => {
|
|
87
|
+
return Promise.resolve(item > 1);
|
|
88
|
+
},
|
|
89
|
+
);
|
|
90
|
+
expect(result).toBe(false);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('handles an operator function that throws an error', async () => {
|
|
94
|
+
const mockOperatorFunction = jest.fn(async (item) => {
|
|
95
|
+
if (item === 2) {
|
|
96
|
+
throw new Error('Test error');
|
|
97
|
+
}
|
|
98
|
+
return true;
|
|
99
|
+
});
|
|
100
|
+
let error: any = null;
|
|
101
|
+
let result: boolean | null = null;
|
|
102
|
+
try {
|
|
103
|
+
result = await everyAsync(
|
|
104
|
+
[1, 2, 3],
|
|
105
|
+
mockOperatorFunction,
|
|
106
|
+
);
|
|
107
|
+
} catch (err) {
|
|
108
|
+
error = err;
|
|
109
|
+
}
|
|
110
|
+
expect(result).toBe(null);
|
|
111
|
+
expect(error).not.toBe(null);
|
|
112
|
+
expect(mockOperatorFunction).toHaveBeenCalledTimes(2);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run the operator function on each item in the array, returning true if
|
|
3
|
+
* the operator function returns true for every item in the array
|
|
4
|
+
* @author Gabe Abrams
|
|
5
|
+
* @param operatorFunction the operator function to apply. If it returns true
|
|
6
|
+
* for every item, this function will return true
|
|
7
|
+
* @returns true if the operator function returns true for every item in the array
|
|
8
|
+
*/
|
|
9
|
+
const everyAsync = async <T>(
|
|
10
|
+
array: T[],
|
|
11
|
+
operatorFunction: (
|
|
12
|
+
item: T,
|
|
13
|
+
index: number,
|
|
14
|
+
opts: {
|
|
15
|
+
breakNow: () => void,
|
|
16
|
+
array: Array<T>,
|
|
17
|
+
},
|
|
18
|
+
) => Promise<any>,
|
|
19
|
+
): Promise<boolean> => {
|
|
20
|
+
// Create break logic
|
|
21
|
+
let done = false;
|
|
22
|
+
/**
|
|
23
|
+
* Break the loop (checking stops here)
|
|
24
|
+
* @author Gabe Abrams
|
|
25
|
+
*/
|
|
26
|
+
const breakNow = () => {
|
|
27
|
+
done = true;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// Loop through each item, checking
|
|
31
|
+
for (let i = 0; i < array.length && !done; i++) {
|
|
32
|
+
const passed = await operatorFunction(
|
|
33
|
+
array[i],
|
|
34
|
+
i,
|
|
35
|
+
{
|
|
36
|
+
breakNow,
|
|
37
|
+
array,
|
|
38
|
+
},
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
// Check if this one failed or if loop was broken
|
|
42
|
+
if (!passed || done) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Return true because none returned false
|
|
48
|
+
return true;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export default everyAsync;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import filterAsync from './filterAsync';
|
|
2
|
+
|
|
3
|
+
describe('filterAsync', () => {
|
|
4
|
+
it('should return an empty array when given an empty array', async () => {
|
|
5
|
+
const result = await filterAsync(
|
|
6
|
+
[],
|
|
7
|
+
async () => {
|
|
8
|
+
return true;
|
|
9
|
+
},
|
|
10
|
+
);
|
|
11
|
+
expect(result).toEqual([]);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('should return the original array when all items pass the filter', async () => {
|
|
15
|
+
const originalArray = [1, 2, 3];
|
|
16
|
+
const result = await filterAsync(
|
|
17
|
+
originalArray,
|
|
18
|
+
async () => {
|
|
19
|
+
return true;
|
|
20
|
+
},
|
|
21
|
+
);
|
|
22
|
+
expect(result).toEqual(originalArray);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should return an empty array when no items pass the filter', async () => {
|
|
26
|
+
const originalArray = [1, 2, 3];
|
|
27
|
+
const result = await filterAsync(
|
|
28
|
+
originalArray,
|
|
29
|
+
async () => {
|
|
30
|
+
return false;
|
|
31
|
+
},
|
|
32
|
+
);
|
|
33
|
+
expect(result).toEqual([]);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('should return only the items that pass the filter', async () => {
|
|
37
|
+
const originalArray = [1, 2, 3];
|
|
38
|
+
const result = await filterAsync(
|
|
39
|
+
originalArray,
|
|
40
|
+
async (item) => {
|
|
41
|
+
return item > 1;
|
|
42
|
+
},
|
|
43
|
+
);
|
|
44
|
+
expect(result).toEqual([2, 3]);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('should pass the correct index to the operator function', async () => {
|
|
48
|
+
const originalArray = [1, 2, 3];
|
|
49
|
+
const result = await filterAsync(
|
|
50
|
+
originalArray,
|
|
51
|
+
async (_, index) => {
|
|
52
|
+
return index === 1;
|
|
53
|
+
},
|
|
54
|
+
);
|
|
55
|
+
expect(result).toEqual([2]);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('should allow breaking out of the loop early', async () => {
|
|
59
|
+
const originalArray = [1, 2, 3];
|
|
60
|
+
const result = await filterAsync(
|
|
61
|
+
originalArray,
|
|
62
|
+
async (_, index, { breakNow }) => {
|
|
63
|
+
if (index === 1) {
|
|
64
|
+
breakNow();
|
|
65
|
+
}
|
|
66
|
+
return true;
|
|
67
|
+
},
|
|
68
|
+
);
|
|
69
|
+
expect(result).toEqual([1]);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('should pass the original array to the operator function', async () => {
|
|
73
|
+
const originalArray = [1, 2, 3];
|
|
74
|
+
const result = await filterAsync(
|
|
75
|
+
originalArray,
|
|
76
|
+
async (_, index, { array }) => {
|
|
77
|
+
return array.length === 3;
|
|
78
|
+
},
|
|
79
|
+
);
|
|
80
|
+
expect(result).toEqual(originalArray);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('should handle an operator function that returns a non-boolean value', async () => {
|
|
84
|
+
const originalArray = [1, 2, 3];
|
|
85
|
+
const result = await filterAsync(
|
|
86
|
+
originalArray,
|
|
87
|
+
async () => {
|
|
88
|
+
return 'hello';
|
|
89
|
+
},
|
|
90
|
+
);
|
|
91
|
+
expect(result).toEqual(originalArray);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('should handle an operator function that throws an error', async () => {
|
|
95
|
+
const originalArray = [1, 2, 3];
|
|
96
|
+
let error: any = null;
|
|
97
|
+
let result: number[] | null = null;
|
|
98
|
+
try {
|
|
99
|
+
result = await filterAsync(
|
|
100
|
+
originalArray,
|
|
101
|
+
async () => {
|
|
102
|
+
throw new Error('Something went wrong');
|
|
103
|
+
},
|
|
104
|
+
);
|
|
105
|
+
} catch (err) {
|
|
106
|
+
error = err;
|
|
107
|
+
}
|
|
108
|
+
expect(result).toEqual(null);
|
|
109
|
+
expect(error).not.toBe(null);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('should handle an array of objects', async () => {
|
|
113
|
+
const originalArray = [
|
|
114
|
+
{ name: 'Alice', age: 30 },
|
|
115
|
+
{ name: 'Bob', age: 25 },
|
|
116
|
+
{ name: 'Charlie', age: 35 },
|
|
117
|
+
];
|
|
118
|
+
const result = await filterAsync(
|
|
119
|
+
originalArray,
|
|
120
|
+
async (item) => {
|
|
121
|
+
return (item.age > 30);
|
|
122
|
+
},
|
|
123
|
+
);
|
|
124
|
+
expect(result).toEqual([{ name: 'Charlie', age: 35 }]);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run the operator function on each item in the array, returning a new array
|
|
3
|
+
* that only contains the items that pass the filter
|
|
4
|
+
* @author Gabe Abrams
|
|
5
|
+
* @param operatorFunction the operator function to apply. If it returns true
|
|
6
|
+
* for an item, the item will be included in the returned array
|
|
7
|
+
* @returns the filtered array
|
|
8
|
+
*/
|
|
9
|
+
const filterAsync = async <T>(
|
|
10
|
+
array: T[],
|
|
11
|
+
operatorFunction: (
|
|
12
|
+
item: T,
|
|
13
|
+
index: number,
|
|
14
|
+
opts: {
|
|
15
|
+
breakNow: () => void,
|
|
16
|
+
array: Array<T>,
|
|
17
|
+
},
|
|
18
|
+
) => Promise<any>,
|
|
19
|
+
): Promise<T[]> => {
|
|
20
|
+
// Create break logic
|
|
21
|
+
let done = false;
|
|
22
|
+
/**
|
|
23
|
+
* Break the loop (filtering stops here)
|
|
24
|
+
* @author Gabe Abrams
|
|
25
|
+
*/
|
|
26
|
+
const breakNow = () => {
|
|
27
|
+
done = true;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// Loop through each item, filtering
|
|
31
|
+
const output: T[] = [];
|
|
32
|
+
for (let i = 0; i < array.length && !done; i++) {
|
|
33
|
+
const included = await operatorFunction(
|
|
34
|
+
array[i],
|
|
35
|
+
i,
|
|
36
|
+
{
|
|
37
|
+
breakNow,
|
|
38
|
+
array,
|
|
39
|
+
},
|
|
40
|
+
);
|
|
41
|
+
if (included && !done) {
|
|
42
|
+
output.push(array[i]);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Return results
|
|
47
|
+
return output;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export default filterAsync;
|