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.
Files changed (401) hide show
  1. package/.eslintrc.js +93 -0
  2. package/LICENSE +21 -0
  3. package/README.md +2 -0
  4. package/lib/constants/DAY_IN_MS.d.ts +6 -0
  5. package/lib/constants/DAY_IN_MS.js +9 -0
  6. package/lib/constants/DAY_IN_MS.js.map +1 -0
  7. package/lib/constants/HOUR_IN_MS.d.ts +6 -0
  8. package/lib/constants/HOUR_IN_MS.js +9 -0
  9. package/lib/constants/HOUR_IN_MS.js.map +1 -0
  10. package/lib/constants/LOG_REVIEW_GET_LOGS_ROUTE.d.ts +6 -0
  11. package/lib/constants/LOG_REVIEW_GET_LOGS_ROUTE.js +13 -0
  12. package/lib/constants/LOG_REVIEW_GET_LOGS_ROUTE.js.map +1 -0
  13. package/lib/constants/LOG_REVIEW_ROUTE_PATH_PREFIX.d.ts +6 -0
  14. package/lib/constants/LOG_REVIEW_ROUTE_PATH_PREFIX.js +13 -0
  15. package/lib/constants/LOG_REVIEW_ROUTE_PATH_PREFIX.js.map +1 -0
  16. package/lib/constants/LOG_REVIEW_STATUS_ROUTE.d.ts +7 -0
  17. package/lib/constants/LOG_REVIEW_STATUS_ROUTE.js +14 -0
  18. package/lib/constants/LOG_REVIEW_STATUS_ROUTE.js.map +1 -0
  19. package/lib/constants/LOG_ROUTE_PATH.d.ts +6 -0
  20. package/lib/constants/LOG_ROUTE_PATH.js +13 -0
  21. package/lib/constants/LOG_ROUTE_PATH.js.map +1 -0
  22. package/lib/constants/MINUTE_IN_MS.d.ts +6 -0
  23. package/lib/constants/MINUTE_IN_MS.js +9 -0
  24. package/lib/constants/MINUTE_IN_MS.js.map +1 -0
  25. package/lib/constants/ROUTE_PATH_PREFIX.d.ts +6 -0
  26. package/lib/constants/ROUTE_PATH_PREFIX.js +9 -0
  27. package/lib/constants/ROUTE_PATH_PREFIX.js.map +1 -0
  28. package/lib/constants/SELECT_ADMIN_CHECK_ROUTE.d.ts +6 -0
  29. package/lib/constants/SELECT_ADMIN_CHECK_ROUTE.js +9 -0
  30. package/lib/constants/SELECT_ADMIN_CHECK_ROUTE.js.map +1 -0
  31. package/lib/errors/ErrorWithCode.d.ts +9 -0
  32. package/lib/errors/ErrorWithCode.js +33 -0
  33. package/lib/errors/ErrorWithCode.js.map +1 -0
  34. package/lib/helpers/abbreviate.d.ts +10 -0
  35. package/lib/helpers/abbreviate.js +24 -0
  36. package/lib/helpers/abbreviate.js.map +1 -0
  37. package/lib/helpers/abbreviate.test.d.ts +5 -0
  38. package/lib/helpers/abbreviate.test.js +28 -0
  39. package/lib/helpers/abbreviate.test.js.map +1 -0
  40. package/lib/helpers/asyncArrayFunctions/everyAsync.d.ts +13 -0
  41. package/lib/helpers/asyncArrayFunctions/everyAsync.js +81 -0
  42. package/lib/helpers/asyncArrayFunctions/everyAsync.js.map +1 -0
  43. package/lib/helpers/asyncArrayFunctions/everyAsync.test.d.ts +1 -0
  44. package/lib/helpers/asyncArrayFunctions/everyAsync.test.js +219 -0
  45. package/lib/helpers/asyncArrayFunctions/everyAsync.test.js.map +1 -0
  46. package/lib/helpers/asyncArrayFunctions/filterAsync.d.ts +13 -0
  47. package/lib/helpers/asyncArrayFunctions/filterAsync.js +81 -0
  48. package/lib/helpers/asyncArrayFunctions/filterAsync.js.map +1 -0
  49. package/lib/helpers/asyncArrayFunctions/filterAsync.test.d.ts +1 -0
  50. package/lib/helpers/asyncArrayFunctions/filterAsync.test.js +244 -0
  51. package/lib/helpers/asyncArrayFunctions/filterAsync.test.js.map +1 -0
  52. package/lib/helpers/asyncArrayFunctions/forEachAsync.d.ts +10 -0
  53. package/lib/helpers/asyncArrayFunctions/forEachAsync.js +72 -0
  54. package/lib/helpers/asyncArrayFunctions/forEachAsync.js.map +1 -0
  55. package/lib/helpers/asyncArrayFunctions/forEachAsync.test.d.ts +1 -0
  56. package/lib/helpers/asyncArrayFunctions/forEachAsync.test.js +223 -0
  57. package/lib/helpers/asyncArrayFunctions/forEachAsync.test.js.map +1 -0
  58. package/lib/helpers/asyncArrayFunctions/mapAsync.d.ts +11 -0
  59. package/lib/helpers/asyncArrayFunctions/mapAsync.js +77 -0
  60. package/lib/helpers/asyncArrayFunctions/mapAsync.js.map +1 -0
  61. package/lib/helpers/asyncArrayFunctions/mapAsync.test.d.ts +1 -0
  62. package/lib/helpers/asyncArrayFunctions/mapAsync.test.js +219 -0
  63. package/lib/helpers/asyncArrayFunctions/mapAsync.test.js.map +1 -0
  64. package/lib/helpers/asyncArrayFunctions/someAsync.d.ts +13 -0
  65. package/lib/helpers/asyncArrayFunctions/someAsync.js +80 -0
  66. package/lib/helpers/asyncArrayFunctions/someAsync.js.map +1 -0
  67. package/lib/helpers/asyncArrayFunctions/someAsync.test.d.ts +1 -0
  68. package/lib/helpers/asyncArrayFunctions/someAsync.test.js +227 -0
  69. package/lib/helpers/asyncArrayFunctions/someAsync.test.js.map +1 -0
  70. package/lib/helpers/avg.d.ts +8 -0
  71. package/lib/helpers/avg.js +24 -0
  72. package/lib/helpers/avg.js.map +1 -0
  73. package/lib/helpers/avg.test.d.ts +5 -0
  74. package/lib/helpers/avg.test.js +25 -0
  75. package/lib/helpers/avg.test.js.map +1 -0
  76. package/lib/helpers/capitalize.d.ts +7 -0
  77. package/lib/helpers/capitalize.js +20 -0
  78. package/lib/helpers/capitalize.js.map +1 -0
  79. package/lib/helpers/ceilToNumDecimals.d.ts +9 -0
  80. package/lib/helpers/ceilToNumDecimals.js +15 -0
  81. package/lib/helpers/ceilToNumDecimals.js.map +1 -0
  82. package/lib/helpers/ceilToNumDecimals.test.d.ts +5 -0
  83. package/lib/helpers/ceilToNumDecimals.test.js +38 -0
  84. package/lib/helpers/ceilToNumDecimals.test.js.map +1 -0
  85. package/lib/helpers/cloneDeep.d.ts +8 -0
  86. package/lib/helpers/cloneDeep.js +15 -0
  87. package/lib/helpers/cloneDeep.js.map +1 -0
  88. package/lib/helpers/compareArraysByProp.d.ts +13 -0
  89. package/lib/helpers/compareArraysByProp.js +62 -0
  90. package/lib/helpers/compareArraysByProp.js.map +1 -0
  91. package/lib/helpers/compareArraysByProp.test.d.ts +5 -0
  92. package/lib/helpers/compareArraysByProp.test.js +140 -0
  93. package/lib/helpers/compareArraysByProp.test.js.map +1 -0
  94. package/lib/helpers/extractProp.d.ts +12 -0
  95. package/lib/helpers/extractProp.js +19 -0
  96. package/lib/helpers/extractProp.js.map +1 -0
  97. package/lib/helpers/extractProp.test.d.ts +5 -0
  98. package/lib/helpers/extractProp.test.js +36 -0
  99. package/lib/helpers/extractProp.test.js.map +1 -0
  100. package/lib/helpers/floorToNumDecimals.d.ts +9 -0
  101. package/lib/helpers/floorToNumDecimals.js +15 -0
  102. package/lib/helpers/floorToNumDecimals.js.map +1 -0
  103. package/lib/helpers/floorToNumDecimals.test.d.ts +5 -0
  104. package/lib/helpers/floorToNumDecimals.test.js +38 -0
  105. package/lib/helpers/floorToNumDecimals.test.js.map +1 -0
  106. package/lib/helpers/forceNumIntoBounds.d.ts +10 -0
  107. package/lib/helpers/forceNumIntoBounds.js +15 -0
  108. package/lib/helpers/forceNumIntoBounds.js.map +1 -0
  109. package/lib/helpers/forceNumIntoBounds.test.d.ts +5 -0
  110. package/lib/helpers/forceNumIntoBounds.test.js +34 -0
  111. package/lib/helpers/forceNumIntoBounds.test.js.map +1 -0
  112. package/lib/helpers/genCSV.d.ts +18 -0
  113. package/lib/helpers/genCSV.js +69 -0
  114. package/lib/helpers/genCSV.js.map +1 -0
  115. package/lib/helpers/genCSV.test.d.ts +5 -0
  116. package/lib/helpers/genCSV.test.js +75 -0
  117. package/lib/helpers/genCSV.test.js.map +1 -0
  118. package/lib/helpers/genCommaList.d.ts +12 -0
  119. package/lib/helpers/genCommaList.js +20 -0
  120. package/lib/helpers/genCommaList.js.map +1 -0
  121. package/lib/helpers/genCommaList.test.d.ts +1 -0
  122. package/lib/helpers/genCommaList.test.js +94 -0
  123. package/lib/helpers/genCommaList.test.js.map +1 -0
  124. package/lib/helpers/getHumanReadableDate.d.ts +8 -0
  125. package/lib/helpers/getHumanReadableDate.js +32 -0
  126. package/lib/helpers/getHumanReadableDate.js.map +1 -0
  127. package/lib/helpers/getHumanReadableDate.test.d.ts +5 -0
  128. package/lib/helpers/getHumanReadableDate.test.js +31 -0
  129. package/lib/helpers/getHumanReadableDate.test.js.map +1 -0
  130. package/lib/helpers/getLocalTimeInfo.d.ts +19 -0
  131. package/lib/helpers/getLocalTimeInfo.js +51 -0
  132. package/lib/helpers/getLocalTimeInfo.js.map +1 -0
  133. package/lib/helpers/getLocalTimeInfo.test.d.ts +6 -0
  134. package/lib/helpers/getLocalTimeInfo.test.js +50 -0
  135. package/lib/helpers/getLocalTimeInfo.test.js.map +1 -0
  136. package/lib/helpers/getMonthName.d.ts +14 -0
  137. package/lib/helpers/getMonthName.js +31 -0
  138. package/lib/helpers/getMonthName.js.map +1 -0
  139. package/lib/helpers/getMonthName.test.d.ts +5 -0
  140. package/lib/helpers/getMonthName.test.js +70 -0
  141. package/lib/helpers/getMonthName.test.js.map +1 -0
  142. package/lib/helpers/getOrdinal.d.ts +8 -0
  143. package/lib/helpers/getOrdinal.js +16 -0
  144. package/lib/helpers/getOrdinal.js.map +1 -0
  145. package/lib/helpers/getOrdinal.test.d.ts +5 -0
  146. package/lib/helpers/getOrdinal.test.js +74 -0
  147. package/lib/helpers/getOrdinal.test.js.map +1 -0
  148. package/lib/helpers/getPartOfDay.d.ts +7 -0
  149. package/lib/helpers/getPartOfDay.js +24 -0
  150. package/lib/helpers/getPartOfDay.js.map +1 -0
  151. package/lib/helpers/getPartOfDay.test.d.ts +5 -0
  152. package/lib/helpers/getPartOfDay.test.js +49 -0
  153. package/lib/helpers/getPartOfDay.test.js.map +1 -0
  154. package/lib/helpers/getTimeInfoInET.d.ts +21 -0
  155. package/lib/helpers/getTimeInfoInET.js +67 -0
  156. package/lib/helpers/getTimeInfoInET.js.map +1 -0
  157. package/lib/helpers/getTimeInfoInET.test.d.ts +5 -0
  158. package/lib/helpers/getTimeInfoInET.test.js +74 -0
  159. package/lib/helpers/getTimeInfoInET.test.js.map +1 -0
  160. package/lib/helpers/getTimestampFromTimeInfoInET.d.ts +20 -0
  161. package/lib/helpers/getTimestampFromTimeInfoInET.js +136 -0
  162. package/lib/helpers/getTimestampFromTimeInfoInET.js.map +1 -0
  163. package/lib/helpers/getWordCount.d.ts +10 -0
  164. package/lib/helpers/getWordCount.js +24 -0
  165. package/lib/helpers/getWordCount.js.map +1 -0
  166. package/lib/helpers/idify.d.ts +9 -0
  167. package/lib/helpers/idify.js +24 -0
  168. package/lib/helpers/idify.js.map +1 -0
  169. package/lib/helpers/onlyKeepLetters.d.ts +8 -0
  170. package/lib/helpers/onlyKeepLetters.js +13 -0
  171. package/lib/helpers/onlyKeepLetters.js.map +1 -0
  172. package/lib/helpers/padDecimalZeros.d.ts +10 -0
  173. package/lib/helpers/padDecimalZeros.js +30 -0
  174. package/lib/helpers/padDecimalZeros.js.map +1 -0
  175. package/lib/helpers/padZerosLeft.d.ts +9 -0
  176. package/lib/helpers/padZerosLeft.js +21 -0
  177. package/lib/helpers/padZerosLeft.js.map +1 -0
  178. package/lib/helpers/parallelLimit.d.ts +12 -0
  179. package/lib/helpers/parallelLimit.js +104 -0
  180. package/lib/helpers/parallelLimit.js.map +1 -0
  181. package/lib/helpers/prefixWithAOrAn.d.ts +10 -0
  182. package/lib/helpers/prefixWithAOrAn.js +28 -0
  183. package/lib/helpers/prefixWithAOrAn.js.map +1 -0
  184. package/lib/helpers/roundToNumDecimals.d.ts +9 -0
  185. package/lib/helpers/roundToNumDecimals.js +15 -0
  186. package/lib/helpers/roundToNumDecimals.js.map +1 -0
  187. package/lib/helpers/shuffleArray.d.ts +8 -0
  188. package/lib/helpers/shuffleArray.js +29 -0
  189. package/lib/helpers/shuffleArray.js.map +1 -0
  190. package/lib/helpers/startMinWait.d.ts +9 -0
  191. package/lib/helpers/startMinWait.js +79 -0
  192. package/lib/helpers/startMinWait.js.map +1 -0
  193. package/lib/helpers/stringsToHumanReadableList.d.ts +10 -0
  194. package/lib/helpers/stringsToHumanReadableList.js +37 -0
  195. package/lib/helpers/stringsToHumanReadableList.js.map +1 -0
  196. package/lib/helpers/sum.d.ts +8 -0
  197. package/lib/helpers/sum.js +15 -0
  198. package/lib/helpers/sum.js.map +1 -0
  199. package/lib/helpers/validators/ChicagoTitleCase.d.ts +9 -0
  200. package/lib/helpers/validators/ChicagoTitleCase.js +31 -0
  201. package/lib/helpers/validators/ChicagoTitleCase.js.map +1 -0
  202. package/lib/helpers/validators/ChicagoTitleCase.test.d.ts +0 -0
  203. package/lib/helpers/validators/ChicagoTitleCase.test.js +79 -0
  204. package/lib/helpers/validators/ChicagoTitleCase.test.js.map +1 -0
  205. package/lib/helpers/validators/findURL.d.ts +0 -0
  206. package/lib/helpers/validators/findURL.js +39 -0
  207. package/lib/helpers/validators/findURL.js.map +1 -0
  208. package/lib/helpers/validators/findURL.test.d.ts +0 -0
  209. package/lib/helpers/validators/findURL.test.js +77 -0
  210. package/lib/helpers/validators/findURL.test.js.map +1 -0
  211. package/lib/helpers/validators/shared/constants/ERROR_MESSAGES.d.ts +10 -0
  212. package/lib/helpers/validators/shared/constants/ERROR_MESSAGES.js +21 -0
  213. package/lib/helpers/validators/shared/constants/ERROR_MESSAGES.js.map +1 -0
  214. package/lib/helpers/validators/shared/helpers/validateRegex.d.ts +19 -0
  215. package/lib/helpers/validators/shared/helpers/validateRegex.js +34 -0
  216. package/lib/helpers/validators/shared/helpers/validateRegex.js.map +1 -0
  217. package/lib/helpers/validators/shared/types/ValidationResult.d.ts +12 -0
  218. package/lib/helpers/validators/shared/types/ValidationResult.js +3 -0
  219. package/lib/helpers/validators/shared/types/ValidationResult.js.map +1 -0
  220. package/lib/helpers/validators/validURL.d.ts +8 -0
  221. package/lib/helpers/validators/validURL.js +20 -0
  222. package/lib/helpers/validators/validURL.js.map +1 -0
  223. package/lib/helpers/validators/validURL.test.d.ts +0 -0
  224. package/lib/helpers/validators/validURL.test.js +83 -0
  225. package/lib/helpers/validators/validURL.test.js.map +1 -0
  226. package/lib/helpers/validators/validateEmail.d.ts +11 -0
  227. package/lib/helpers/validators/validateEmail.js +39 -0
  228. package/lib/helpers/validators/validateEmail.js.map +1 -0
  229. package/lib/helpers/validators/validateEmail.test.d.ts +1 -0
  230. package/lib/helpers/validators/validateEmail.test.js +140 -0
  231. package/lib/helpers/validators/validateEmail.test.js.map +1 -0
  232. package/lib/helpers/validators/validatePhoneNumber.d.ts +11 -0
  233. package/lib/helpers/validators/validatePhoneNumber.js +40 -0
  234. package/lib/helpers/validators/validatePhoneNumber.js.map +1 -0
  235. package/lib/helpers/validators/validatePhoneNumber.test.d.ts +1 -0
  236. package/lib/helpers/validators/validatePhoneNumber.test.js +123 -0
  237. package/lib/helpers/validators/validatePhoneNumber.test.js.map +1 -0
  238. package/lib/helpers/validators/validateString.d.ts +22 -0
  239. package/lib/helpers/validators/validateString.js +124 -0
  240. package/lib/helpers/validators/validateString.js.map +1 -0
  241. package/lib/helpers/validators/validateString.test.d.ts +1 -0
  242. package/lib/helpers/validators/validateString.test.js +163 -0
  243. package/lib/helpers/validators/validateString.test.js.map +1 -0
  244. package/lib/helpers/waitMs.d.ts +7 -0
  245. package/lib/helpers/waitMs.js +68 -0
  246. package/lib/helpers/waitMs.js.map +1 -0
  247. package/lib/index.d.ts +63 -0
  248. package/lib/index.js +124 -0
  249. package/lib/index.js.map +1 -0
  250. package/lib/types/CommonKitErrorCode.d.ts +10 -0
  251. package/lib/types/CommonKitErrorCode.js +15 -0
  252. package/lib/types/CommonKitErrorCode.js.map +1 -0
  253. package/lib/types/DayOfWeek.d.ts +14 -0
  254. package/lib/types/DayOfWeek.js +18 -0
  255. package/lib/types/DayOfWeek.js.map +1 -0
  256. package/lib/types/Log/LogMainInfo.d.ts +39 -0
  257. package/lib/types/Log/LogMainInfo.js +3 -0
  258. package/lib/types/Log/LogMainInfo.js.map +1 -0
  259. package/lib/types/Log/LogSourceSpecificInfo.d.ts +13 -0
  260. package/lib/types/Log/LogSourceSpecificInfo.js +3 -0
  261. package/lib/types/Log/LogSourceSpecificInfo.js.map +1 -0
  262. package/lib/types/Log/LogTypeSpecificInfo.d.ts +17 -0
  263. package/lib/types/Log/LogTypeSpecificInfo.js +3 -0
  264. package/lib/types/Log/LogTypeSpecificInfo.js.map +1 -0
  265. package/lib/types/Log/index.d.ts +10 -0
  266. package/lib/types/Log/index.js +3 -0
  267. package/lib/types/Log/index.js.map +1 -0
  268. package/lib/types/LogAction.d.ts +29 -0
  269. package/lib/types/LogAction.js +55 -0
  270. package/lib/types/LogAction.js.map +1 -0
  271. package/lib/types/LogBuiltInMetadata.d.ts +16 -0
  272. package/lib/types/LogBuiltInMetadata.js +21 -0
  273. package/lib/types/LogBuiltInMetadata.js.map +1 -0
  274. package/lib/types/LogFunction.d.ts +24 -0
  275. package/lib/types/LogFunction.js +3 -0
  276. package/lib/types/LogFunction.js.map +1 -0
  277. package/lib/types/LogLevel.d.ts +10 -0
  278. package/lib/types/LogLevel.js +14 -0
  279. package/lib/types/LogLevel.js.map +1 -0
  280. package/lib/types/LogMetadataContextMap.d.ts +11 -0
  281. package/lib/types/LogMetadataContextMap.js +3 -0
  282. package/lib/types/LogMetadataContextMap.js.map +1 -0
  283. package/lib/types/LogMetadataTagMap.d.ts +8 -0
  284. package/lib/types/LogMetadataTagMap.js +3 -0
  285. package/lib/types/LogMetadataTagMap.js.map +1 -0
  286. package/lib/types/LogMetadataTargetMap.d.ts +8 -0
  287. package/lib/types/LogMetadataTargetMap.js +3 -0
  288. package/lib/types/LogMetadataTargetMap.js.map +1 -0
  289. package/lib/types/LogMetadataType.d.ts +13 -0
  290. package/lib/types/LogMetadataType.js +3 -0
  291. package/lib/types/LogMetadataType.js.map +1 -0
  292. package/lib/types/LogSource.d.ts +9 -0
  293. package/lib/types/LogSource.js +15 -0
  294. package/lib/types/LogSource.js.map +1 -0
  295. package/lib/types/LogType.d.ts +9 -0
  296. package/lib/types/LogType.js +15 -0
  297. package/lib/types/LogType.js.map +1 -0
  298. package/lib/types/ParamType.d.ts +17 -0
  299. package/lib/types/ParamType.js +21 -0
  300. package/lib/types/ParamType.js.map +1 -0
  301. package/package.json +47 -0
  302. package/src/constants/DAY_IN_MS.ts +7 -0
  303. package/src/constants/HOUR_IN_MS.ts +7 -0
  304. package/src/constants/LOG_REVIEW_GET_LOGS_ROUTE.ts +9 -0
  305. package/src/constants/LOG_REVIEW_ROUTE_PATH_PREFIX.ts +9 -0
  306. package/src/constants/LOG_REVIEW_STATUS_ROUTE.ts +10 -0
  307. package/src/constants/LOG_ROUTE_PATH.ts +9 -0
  308. package/src/constants/MINUTE_IN_MS.ts +7 -0
  309. package/src/constants/ROUTE_PATH_PREFIX.ts +7 -0
  310. package/src/constants/SELECT_ADMIN_CHECK_ROUTE.ts +7 -0
  311. package/src/errors/ErrorWithCode.ts +15 -0
  312. package/src/helpers/abbreviate.test.ts +26 -0
  313. package/src/helpers/abbreviate.ts +25 -0
  314. package/src/helpers/asyncArrayFunctions/everyAsync.test.ts +114 -0
  315. package/src/helpers/asyncArrayFunctions/everyAsync.ts +51 -0
  316. package/src/helpers/asyncArrayFunctions/filterAsync.test.ts +126 -0
  317. package/src/helpers/asyncArrayFunctions/filterAsync.ts +50 -0
  318. package/src/helpers/asyncArrayFunctions/forEachAsync.test.ts +136 -0
  319. package/src/helpers/asyncArrayFunctions/forEachAsync.ts +40 -0
  320. package/src/helpers/asyncArrayFunctions/mapAsync.test.ts +125 -0
  321. package/src/helpers/asyncArrayFunctions/mapAsync.ts +46 -0
  322. package/src/helpers/asyncArrayFunctions/someAsync.test.ts +92 -0
  323. package/src/helpers/asyncArrayFunctions/someAsync.ts +49 -0
  324. package/src/helpers/avg.test.ts +24 -0
  325. package/src/helpers/avg.ts +22 -0
  326. package/src/helpers/capitalize.ts +20 -0
  327. package/src/helpers/ceilToNumDecimals.test.ts +39 -0
  328. package/src/helpers/ceilToNumDecimals.ts +13 -0
  329. package/src/helpers/cloneDeep.ts +11 -0
  330. package/src/helpers/compareArraysByProp.test.ts +147 -0
  331. package/src/helpers/compareArraysByProp.ts +56 -0
  332. package/src/helpers/extractProp.test.ts +34 -0
  333. package/src/helpers/extractProp.ts +17 -0
  334. package/src/helpers/floorToNumDecimals.test.ts +39 -0
  335. package/src/helpers/floorToNumDecimals.ts +13 -0
  336. package/src/helpers/forceNumIntoBounds.test.ts +34 -0
  337. package/src/helpers/forceNumIntoBounds.ts +19 -0
  338. package/src/helpers/genCSV.test.ts +77 -0
  339. package/src/helpers/genCSV.ts +87 -0
  340. package/src/helpers/genCommaList.test.ts +57 -0
  341. package/src/helpers/genCommaList.ts +21 -0
  342. package/src/helpers/getHumanReadableDate.test.ts +29 -0
  343. package/src/helpers/getHumanReadableDate.ts +36 -0
  344. package/src/helpers/getLocalTimeInfo.test.ts +48 -0
  345. package/src/helpers/getLocalTimeInfo.ts +58 -0
  346. package/src/helpers/getMonthName.test.ts +79 -0
  347. package/src/helpers/getMonthName.ts +29 -0
  348. package/src/helpers/getOrdinal.test.ts +84 -0
  349. package/src/helpers/getOrdinal.ts +14 -0
  350. package/src/helpers/getPartOfDay.test.ts +56 -0
  351. package/src/helpers/getPartOfDay.ts +20 -0
  352. package/src/helpers/getTimeInfoInET.test.ts +72 -0
  353. package/src/helpers/getTimeInfoInET.ts +79 -0
  354. package/src/helpers/getTimestampFromTimeInfoInET.ts +179 -0
  355. package/src/helpers/getWordCount.ts +26 -0
  356. package/src/helpers/idify.ts +24 -0
  357. package/src/helpers/onlyKeepLetters.ts +11 -0
  358. package/src/helpers/padDecimalZeros.ts +32 -0
  359. package/src/helpers/padZerosLeft.ts +21 -0
  360. package/src/helpers/parallelLimit.ts +63 -0
  361. package/src/helpers/prefixWithAOrAn.ts +29 -0
  362. package/src/helpers/roundToNumDecimals.ts +13 -0
  363. package/src/helpers/shuffleArray.ts +19 -0
  364. package/src/helpers/startMinWait.ts +33 -0
  365. package/src/helpers/stringsToHumanReadableList.ts +39 -0
  366. package/src/helpers/sum.ts +16 -0
  367. package/src/helpers/validators/ChicagoTitleCase.test.ts +85 -0
  368. package/src/helpers/validators/ChicagoTitleCase.ts +32 -0
  369. package/src/helpers/validators/findURL.test.ts +83 -0
  370. package/src/helpers/validators/findURL.ts +43 -0
  371. package/src/helpers/validators/shared/constants/ERROR_MESSAGES.ts +18 -0
  372. package/src/helpers/validators/shared/helpers/validateRegex.ts +47 -0
  373. package/src/helpers/validators/shared/types/ValidationResult.ts +20 -0
  374. package/src/helpers/validators/validURL.test.ts +90 -0
  375. package/src/helpers/validators/validURL.ts +18 -0
  376. package/src/helpers/validators/validateEmail.test.ts +110 -0
  377. package/src/helpers/validators/validateEmail.ts +47 -0
  378. package/src/helpers/validators/validatePhoneNumber.test.ts +94 -0
  379. package/src/helpers/validators/validatePhoneNumber.ts +49 -0
  380. package/src/helpers/validators/validateString.test.ts +173 -0
  381. package/src/helpers/validators/validateString.ts +171 -0
  382. package/src/helpers/waitMs.ts +12 -0
  383. package/src/index.ts +139 -0
  384. package/src/types/CommonKitErrorCode.ts +13 -0
  385. package/src/types/DayOfWeek.ts +15 -0
  386. package/src/types/Log/LogMainInfo.ts +69 -0
  387. package/src/types/Log/LogSourceSpecificInfo.ts +25 -0
  388. package/src/types/Log/LogTypeSpecificInfo.ts +33 -0
  389. package/src/types/Log/index.ts +17 -0
  390. package/src/types/LogAction.ts +52 -0
  391. package/src/types/LogBuiltInMetadata.ts +19 -0
  392. package/src/types/LogFunction.ts +46 -0
  393. package/src/types/LogLevel.ts +11 -0
  394. package/src/types/LogMetadataContextMap.ts +15 -0
  395. package/src/types/LogMetadataTagMap.ts +9 -0
  396. package/src/types/LogMetadataTargetMap.ts +9 -0
  397. package/src/types/LogMetadataType.ts +16 -0
  398. package/src/types/LogSource.ts +12 -0
  399. package/src/types/LogType.ts +12 -0
  400. package/src/types/ParamType.ts +18 -0
  401. package/tsconfig.json +19 -0
@@ -0,0 +1,83 @@
1
+ // Import the function to be tested
2
+ // import findURLs from './findURL';
3
+
4
+ // // Import constants
5
+ // import { INVALID_STRING_ERRORS, INVALID_REGEX_ERROR } from './shared/constants/ERROR_MESSAGES';
6
+
7
+ // /*------------------------------------------------------------------------*/
8
+ // /* ---------------------------- Valid Tests --------------------------- */
9
+ // /*------------------------------------------------------------------------*/
10
+
11
+ // const validTests: { block: string, expected: { start: number, end: number }[] }[] = [
12
+ // {
13
+ // block: 'Visit http://example.com for more information.',
14
+ // expected: [{ start: 6, end: 23 }],
15
+ // },
16
+ // {
17
+ // block: 'Multiple URLs: http://one.com and http://two.com.',
18
+ // expected: [
19
+ // { start: 15, end: 29 },
20
+ // { start: 34, end: 48 },
21
+ // ],
22
+ // },
23
+ // {
24
+ // block: 'Localhost URLs: http://localhost and http://127.0.0.1.',
25
+ // expected: [
26
+ // { start: 16, end: 31 },
27
+ // { start: 36, end: 52 },
28
+ // ],
29
+ // },
30
+ // {
31
+ // block: 'Embedded URL: text https://embedded-url.com?query=1&value=2.',
32
+ // expected: [{ start: 13, end: 51 }],
33
+ // },
34
+ // {
35
+ // block: 'Secure site: https://secure-site.com/path?query=string#fragment',
36
+ // expected: [{ start: 13, end: 58 }],
37
+ // },
38
+ // ];
39
+
40
+ // test(
41
+ // 'Returns correct start and end indices for valid URLs within a string.',
42
+ // async () => {
43
+ // validTests.forEach(({ block, expected }) => {
44
+ // expect(findURLs(block)).toStrictEqual(expected);
45
+ // });
46
+ // },
47
+ // );
48
+
49
+ // /*------------------------------------------------------------------------*/
50
+ // /* ---------------------------- Invalid Tests --------------------------- */
51
+ // /*------------------------------------------------------------------------*/
52
+
53
+ // const invalidTests: { block: string, expected: { start: number, end: number }[] }[] = [
54
+ // {
55
+ // block: 'No URL.',
56
+ // expected: [],
57
+ // },
58
+ // {
59
+ // block: 'URL format is wrong: http:/example.com and htt://example.com.',
60
+ // expected: [],
61
+ // },
62
+ // {
63
+ // block: 'Any punctuation after the URL http://example.com, should not count.',
64
+ // expected: [{ start: 20, end: 37 }],
65
+ // },
66
+ // {
67
+ // block: 'Starting punctuation ,http://example.com should not count.',
68
+ // expected: [{ start: 22, end: 39 }],
69
+ // },
70
+ // {
71
+ // block: 'Spaces in the middle http://exa mple.com is invalid.',
72
+ // expected: [],
73
+ // },
74
+ // ];
75
+
76
+ // test(
77
+ // 'Returns empty array for strings without valid URLs.',
78
+ // async () => {
79
+ // invalidTests.forEach(({ block, expected }) => {
80
+ // expect(findURLs(block)).toStrictEqual(expected);
81
+ // });
82
+ // },
83
+ // );
@@ -0,0 +1,43 @@
1
+ // /**
2
+ // * This function finds URLs within a given string and returns an array of
3
+ // * their locations.
4
+ // * @author Leisha Bhandari
5
+ // * @param block: The block of text to search for URLs
6
+ // * @returns Arrays where each of them contain the start and end index of the URL
7
+ // */
8
+ // function findURLs(block: string): { start: number, end: number }[] {
9
+ // // Expression representing the skeleton of a URL to help match and find the
10
+ // // URLs within the block of text (Also takes care of Unicode characters)
11
+ // const urlSkeleton = /(https?:\/\/[^\s\u0000-\u001F.,]+[^\s\u0000-\u001F.,]?(?:\?[^\s\u0000-\u001F]+)?(?:#[^\s\u0000-\u001F]+)?)/g;
12
+ // const found: { start: number, end: number }[] = [];
13
+ // let match: RegExpExecArray | null;
14
+
15
+ // // While loop to find the URLs in the given block of texts using the URL
16
+ // // skeleton
17
+ // while ((match === urlSkeleton.exec(block))
18
+ // && match !== null) {
19
+ // // To find the beginning index of the URL
20
+ // let start = match.index;
21
+ // // To find the last index of the URL
22
+ // let end = match.index + match[0].length;
23
+
24
+ // // If statements to check whether the URL is followed by some kind of
25
+ // // punctuation mark
26
+ // if (block.charAt(end) === '.' || /[,;:!?"]/.test(block.charAt(end))) {
27
+ // end--;
28
+ // }
29
+
30
+ // // While statement to make sure that the URL does not start with a
31
+ // // punctuation mark
32
+ // while (start > 0 && /[.,:;!?]/.test(block.charAt(start - 1))) {
33
+ // start--;
34
+ // }
35
+
36
+ // // The object with start and end index of the URL is found and pushed to
37
+ // // the found array
38
+ // found.push({ start, end });
39
+ // }
40
+
41
+ // // Returns the array of the found URLs location
42
+ // return found;
43
+ // }
@@ -0,0 +1,18 @@
1
+ /*------------------------------------------------------------------------*/
2
+ /* ------------------------------ Constants ----------------------------- */
3
+ /*------------------------------------------------------------------------*/
4
+
5
+ export const INVALID_REGEX_ERROR = 'input does not follow the requested format';
6
+ export const INVALID_EMAIL_ERROR = 'Please provide a valid email address.';
7
+ export const INVALID_PHONE_ERROR = 'Please provide a valid phone number.';
8
+ export const INVALID_STRING_ERRORS = {
9
+ MIN_LEN: (minLen: number) => {
10
+ return `input must not be under ${minLen} character(s)`;
11
+ },
12
+ MAX_LEN: (maxLen: number) => {
13
+ return `input must not be over ${maxLen} character(s)`;
14
+ },
15
+ LETTERS_ONLY: 'input must only contain letters',
16
+ NUMBERS_ONLY: 'input must only contain numbers',
17
+ MESSAGE_INTRO: 'The following error(s) occurred: ',
18
+ };
@@ -0,0 +1,47 @@
1
+ // Import types
2
+ import ValidationResult from '../types/ValidationResult';
3
+
4
+ // Import constants
5
+ import { INVALID_REGEX_ERROR } from '../constants/ERROR_MESSAGES';
6
+
7
+ /**
8
+ * Determines whether a given input string is considered valid based on
9
+ * the provided regex.
10
+ * @author Austen Money
11
+ * @param opts object containing all args
12
+ * @param opts.input user-provided input to validate
13
+ * @param opts.regex regular expression to check against input
14
+ * @param [opts.regexDescription] description of what regexString is checking
15
+ * for, used to customize error message
16
+ * @returns the unchanged input if valid, or a customized error message if
17
+ * invalid
18
+ */
19
+ const validateRegex = (
20
+ opts: {
21
+ input: string,
22
+ regex: RegExp,
23
+ regexDescription?: string,
24
+ },
25
+ ): ValidationResult<string> => {
26
+ // customize error message in case of invalid input
27
+ const errorMessage = `${INVALID_REGEX_ERROR}${
28
+ opts.regexDescription
29
+ ? ': '
30
+ : ''
31
+ }${opts.regexDescription}`;
32
+
33
+ // return error message if test is invalid, or input string if valid
34
+ return (
35
+ opts.regex.test(opts.input)
36
+ ? {
37
+ isValid: true,
38
+ cleanedValue: opts.input,
39
+ }
40
+ : {
41
+ isValid: false,
42
+ errorMessage,
43
+ }
44
+ );
45
+ };
46
+
47
+ export default validateRegex;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Result of a validation function.
3
+ * @author Austen Money
4
+ */
5
+ type ValidationResult<CleanedValueType> = (
6
+ | {
7
+ // if true, the validation was successful
8
+ isValid: true,
9
+ // the cleaned version of the value
10
+ cleanedValue: CleanedValueType,
11
+ }
12
+ | {
13
+ // if false, the validation was not successful
14
+ isValid: false,
15
+ // describes why the validation failed
16
+ errorMessage: string,
17
+ }
18
+ );
19
+
20
+ export default ValidationResult;
@@ -0,0 +1,90 @@
1
+
2
+ // Import the function to be tested
3
+ // import isValid from './validURL';
4
+
5
+ // // Import constants
6
+ // import { INVALID_STRING_ERRORS, INVALID_REGEX_ERROR } from './shared/constants/ERROR_MESSAGES';
7
+
8
+ // /*------------------------------------------------------------------------*/
9
+ // /* ---------------------------- Valid Tests --------------------------- */
10
+ // /*------------------------------------------------------------------------*/
11
+
12
+ // const validUrls: string[] = [
13
+ // 'http://example.com',
14
+ // 'https://example.com',
15
+ // 'http://www.example.com',
16
+ // 'https://www.example.com',
17
+ // 'https://example.com/path',
18
+ // 'http://example.com/path',
19
+ // 'https://example.com?query=string',
20
+ // 'http://example.com?query=string',
21
+ // 'https://example.com#fragment',
22
+ // 'http://example.com#fragment',
23
+ // 'http://localhost',
24
+ // 'https://localhost:3000',
25
+ // 'http://localhost:3000',
26
+ // 'http://127.0.0.1',
27
+ // 'http://127.0.0.1:3000',
28
+ // 'ftp://example.com/path',
29
+ // 'ftp://example.com',
30
+ // 'https://127.0.0.1:3000',
31
+ // 'file:///C:/path/to/file'
32
+ // ];
33
+
34
+ // test(
35
+ // 'Returns true for a given valid URL.',
36
+ // async () => {
37
+ // validUrls.forEach((url) => {
38
+ // expect(isValid(url)).toBe(true);
39
+ // });
40
+ // },
41
+ // );
42
+
43
+ // /*------------------------------------------------------------------------*/
44
+ // /* ---------------------------- Invalid Tests --------------------------- */
45
+ // /*------------------------------------------------------------------------*/
46
+
47
+ // const invalidUrls: string[] = [
48
+ // '',
49
+ // ' ',
50
+ // 'example',
51
+ // 'http://',
52
+ // 'http://.',
53
+ // 'http://..',
54
+ // 'http://##/',
55
+ // 'http://../',
56
+ // 'http://??/',
57
+ // 'http://#',
58
+ // 'http://##',
59
+ // '//',
60
+ // '//a',
61
+ // '///a',
62
+ // '///',
63
+ // 'http:///a',
64
+ // 'http://www.foo.bar./',
65
+ // 'http://foo.bar/foo(bar)baz quux',
66
+ // 'http://3628126748',
67
+ // 'http://.www.foo.bar/',
68
+ // 'http://.www.foo.bar./',
69
+ // 'http://123.123.123',
70
+ // 'http://1.1.1.1.1',
71
+ // 'http://example.com:99999',
72
+ // 'http://example.com:80:80',
73
+ // 'http://example.com::80',
74
+ // 'http://example.com:-80',
75
+ // 'http://-example.com',
76
+ // 'http://example.com-',
77
+ // 'http://example.com_',
78
+ // 'http://example..com',
79
+ // 'http://example.com.',
80
+ // 'http://.example.com'
81
+ // ];
82
+
83
+ // test(
84
+ // 'Returns false for a given invalid URL.',
85
+ // async () => {
86
+ // invalidUrls.forEach((url) => {
87
+ // expect(isValid(url)).toBe(false);
88
+ // });
89
+ // },
90
+ // );
@@ -0,0 +1,18 @@
1
+ /**
2
+ * This function checks if a given input string is a valid URL.
3
+ * @author Leisha Bhandari
4
+ * @param URL: The input string that needs checked as a URL or not.
5
+ * @returns A true boolean value if the input string is a valid URL, and a false
6
+ * boolean value if the input string is a invalid URL
7
+ */
8
+ function isValid(url: string): boolean {
9
+ // Uses the input URL to create a URL object
10
+ try {
11
+ new URL(url);
12
+ // URL constructor does not throw an error, so input URL is valid
13
+ return true;
14
+ } catch (err) {
15
+ // URL constructor throws an error, so input URL is invalid
16
+ return false;
17
+ }
18
+ }
@@ -0,0 +1,110 @@
1
+ // Import types
2
+ import ValidationResult from './shared/types/ValidationResult';
3
+
4
+ // Import constants
5
+ import { INVALID_EMAIL_ERROR } from './shared/constants/ERROR_MESSAGES';
6
+
7
+ // Import function
8
+ import validateEmail from './validateEmail';
9
+
10
+ /*------------------------------------------------------------------------*/
11
+ /* ---------------------------- Valid Tests --------------------------- */
12
+ /*------------------------------------------------------------------------*/
13
+
14
+ const validEmailPairs: { input: string, cleaned: string }[] = [
15
+ {
16
+ input: 'username@gmail.com',
17
+ cleaned: 'username@gmail.com',
18
+ },
19
+ {
20
+ input: 'u@hotmail.org',
21
+ cleaned: 'u@hotmail.org',
22
+ },
23
+ {
24
+ input: ' username@gmail.com',
25
+ cleaned: 'username@gmail.com',
26
+ },
27
+ {
28
+ input: 'username@gmail.com ',
29
+ cleaned: 'username@gmail.com',
30
+ },
31
+ {
32
+ input: ' username@gmail.com ',
33
+ cleaned: 'username@gmail.com',
34
+ },
35
+ {
36
+ input: 'username@harvard.edu',
37
+ cleaned: 'username@harvard.edu',
38
+ },
39
+ {
40
+ input: 'username@harvard.subdomain.edu',
41
+ cleaned: 'username@harvard.subdomain.edu',
42
+ },
43
+ {
44
+ input: '\nusername@harvard.subdomain.edu',
45
+ cleaned: 'username@harvard.subdomain.edu',
46
+ },
47
+ {
48
+ input: '\n\tusername@harvard.subdomain.edu',
49
+ cleaned: 'username@harvard.subdomain.edu',
50
+ },
51
+ {
52
+ input: 'username@harvard.subdomain.edu\n',
53
+ cleaned: 'username@harvard.subdomain.edu',
54
+ },
55
+ {
56
+ input: 'username@harvard.subdomain.edu\n\t',
57
+ cleaned: 'username@harvard.subdomain.edu',
58
+ },
59
+ ];
60
+
61
+ test(
62
+ 'Returns true for a given valid email and removes any leading or trailing whitespace.',
63
+ async () => {
64
+ validEmailPairs.forEach((pair) => {
65
+ const validResponse: ValidationResult<string> = {
66
+ isValid: true,
67
+ cleanedValue: pair.cleaned,
68
+ };
69
+
70
+ expect(validateEmail(pair.input)).toStrictEqual(validResponse);
71
+ });
72
+ },
73
+ );
74
+
75
+ /*------------------------------------------------------------------------*/
76
+ /* ---------------------------- Invalid Tests --------------------------- */
77
+ /*------------------------------------------------------------------------*/
78
+
79
+ const invalidEmails: string[] = [
80
+ '',
81
+ ' ',
82
+ 'gmail.com',
83
+ '@gmail.com ',
84
+ 'username',
85
+ 'username@.com',
86
+ 'user name@gmail.com',
87
+ 'username @gmail.com',
88
+ 'username@ gmail.com',
89
+ 'username@gmail .com',
90
+ 'username\t@gmail.com',
91
+ 'username\n@gmail.com',
92
+ 'username@gmail\n.com',
93
+ 'username@gmail.\tcom',
94
+ 'user\nname@gmail.com',
95
+ 'user\tname@gmail.com',
96
+ ];
97
+
98
+ const invalidResponse: ValidationResult<string> = {
99
+ isValid: false,
100
+ errorMessage: INVALID_EMAIL_ERROR,
101
+ };
102
+
103
+ test(
104
+ 'Returns false for a given invalid email.',
105
+ async () => {
106
+ invalidEmails.forEach((email) => {
107
+ expect(validateEmail(email)).toStrictEqual(invalidResponse);
108
+ });
109
+ },
110
+ );
@@ -0,0 +1,47 @@
1
+ // Import helpers
2
+ import validateRegex from './shared/helpers/validateRegex';
3
+
4
+ // Import constants
5
+ import { INVALID_EMAIL_ERROR } from './shared/constants/ERROR_MESSAGES';
6
+
7
+ // Import types
8
+ import ValidationResult from './shared/types/ValidationResult';
9
+
10
+ /**
11
+ * Determines whether a given email address is valid.
12
+ * @author Austen Money
13
+ * @param email email address to validate
14
+ * @returns whether email fulfills proper formatting requirements, includes a
15
+ * cleaned version of the address without leading or trailing
16
+ * whitespace if valid or an error message if invalid.
17
+ */
18
+ const validateEmail = (
19
+ email: string,
20
+ ): ValidationResult<string> => {
21
+ // validation regex, sourced from HTML living standard: http://www.whatwg.org/specs/web-apps/current-work/multipage/forms.html#e-mail-state-(type=email)
22
+ // eslint-disable-next-line max-len
23
+ 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])?)*$/;
24
+
25
+ // remove leading and trailing whitespace
26
+ const cleanedValue: string = email.replace(/^\s+|\s+$/g, '');
27
+
28
+ // validate email with regex, and return error if not valid
29
+ return (
30
+ validateRegex(
31
+ {
32
+ input: cleanedValue,
33
+ regex: emailRegex,
34
+ },
35
+ ).isValid
36
+ ? {
37
+ isValid: true,
38
+ cleanedValue,
39
+ }
40
+ : {
41
+ isValid: false,
42
+ errorMessage: INVALID_EMAIL_ERROR,
43
+ }
44
+ );
45
+ };
46
+
47
+ export default validateEmail;
@@ -0,0 +1,94 @@
1
+ // Import types
2
+ import ValidationResult from './shared/types/ValidationResult';
3
+
4
+ // Import constants
5
+ import { INVALID_PHONE_ERROR } from './shared/constants/ERROR_MESSAGES';
6
+
7
+ // Import function
8
+ import validatePhoneNumber from './validatePhoneNumber';
9
+
10
+ /*------------------------------------------------------------------------*/
11
+ /* ---------------------------- Valid Tests --------------------------- */
12
+ /*------------------------------------------------------------------------*/
13
+
14
+ const cleanedNumber = '1234567890';
15
+ const cleanedNumberExt = '11234567890';
16
+
17
+ const validPhoneNumbers: { input: string, cleaned: string }[] = [
18
+ {
19
+ input: '1234567890',
20
+ cleaned: cleanedNumber,
21
+ },
22
+ {
23
+ input: '(123)-456-7890',
24
+ cleaned: cleanedNumber,
25
+ },
26
+ {
27
+ input: '123-456-7890',
28
+ cleaned: cleanedNumber,
29
+ },
30
+ {
31
+ input: '(123)456-7890',
32
+ cleaned: cleanedNumber,
33
+ },
34
+ {
35
+ input: '(123)4567890',
36
+ cleaned: cleanedNumber,
37
+ },
38
+ {
39
+ input: ' 123456-7890 ',
40
+ cleaned: cleanedNumber,
41
+ },
42
+ {
43
+ input: '+1 123 456 7890',
44
+ cleaned: cleanedNumberExt,
45
+ },
46
+ {
47
+ input: '+1123456 7890',
48
+ cleaned: cleanedNumberExt,
49
+ },
50
+ {
51
+ input: '+1(123)456-7890',
52
+ cleaned: cleanedNumberExt,
53
+ },
54
+ ];
55
+
56
+ test(
57
+ 'Returns true for a given valid phone number and removes any non-digit characters.',
58
+ async () => {
59
+ validPhoneNumbers.forEach((pair) => {
60
+ const validResponse: ValidationResult<string> = {
61
+ isValid: true,
62
+ cleanedValue: pair.cleaned,
63
+ };
64
+
65
+ expect(validatePhoneNumber(pair.input)).toStrictEqual(validResponse);
66
+ });
67
+ },
68
+ );
69
+
70
+ /*------------------------------------------------------------------------*/
71
+ /* ---------------------------- Invalid Tests --------------------------- */
72
+ /*------------------------------------------------------------------------*/
73
+
74
+ const invalidPhoneNumbers: string[] = [
75
+ '123456789',
76
+ '1234567894567890',
77
+ '1234-567-890',
78
+ '123456m78',
79
+ '123 4567 890',
80
+ ];
81
+
82
+ const invalidResponse: ValidationResult<string> = {
83
+ isValid: false,
84
+ errorMessage: INVALID_PHONE_ERROR,
85
+ };
86
+
87
+ test(
88
+ 'Returns false for a given valid phone number.',
89
+ async () => {
90
+ invalidPhoneNumbers.forEach((phoneNumber) => {
91
+ expect(validatePhoneNumber(phoneNumber)).toStrictEqual(invalidResponse);
92
+ });
93
+ },
94
+ );
@@ -0,0 +1,49 @@
1
+ // Import helpers
2
+ import validateRegex from './shared/helpers/validateRegex';
3
+
4
+ // Import constants
5
+ import { INVALID_PHONE_ERROR } from './shared/constants/ERROR_MESSAGES';
6
+
7
+ // Import types
8
+ import ValidationResult from './shared/types/ValidationResult';
9
+
10
+ /**
11
+ * Determines whether a given phone number is valid.
12
+ * @author Austen Money
13
+ * @param phoneNumber phone number to validate
14
+ * @returns whether phone number is considered valid - if valid, also returns
15
+ * a cleaned version of the number without any formatting. If invalid,
16
+ * returns an error message.
17
+ */
18
+ const validatePhoneNumber = (
19
+ phoneNumber: string,
20
+ ): ValidationResult<string> => {
21
+ // regex to validate phone number
22
+ const validationRegex = /^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$/;
23
+
24
+ // validate phone number with regex
25
+ const validationResponse = validateRegex(
26
+ {
27
+ input: phoneNumber,
28
+ regex: validationRegex,
29
+ },
30
+ );
31
+
32
+ // remove all non-digits from number
33
+ const cleanedValue: string = phoneNumber.replace(/\D/g, '');
34
+
35
+ // return cleaned value if valid, or error message if invalid
36
+ return (
37
+ validationResponse.isValid
38
+ ? {
39
+ isValid: true,
40
+ cleanedValue,
41
+ }
42
+ : {
43
+ isValid: false,
44
+ errorMessage: INVALID_PHONE_ERROR,
45
+ }
46
+ );
47
+ };
48
+
49
+ export default validatePhoneNumber;