@sparted/shared-library 1.0.6 → 1.0.8-alpha.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 (336) hide show
  1. package/.circleci/config.yml +40 -0
  2. package/.eslintignore +1 -0
  3. package/.eslintrc.js +17 -0
  4. package/.gitignore +37 -0
  5. package/CHANGELOG.md +34 -0
  6. package/README.md +26 -0
  7. package/{cdn → dist/cdn}/cdn.factory.js +1 -1
  8. package/dist/cdn/cdn.factory.js.map +1 -0
  9. package/dist/github/commits/create-git-blob.d.ts +26 -0
  10. package/dist/github/commits/create-git-blob.js +42 -0
  11. package/dist/github/commits/create-git-blob.js.map +1 -0
  12. package/dist/github/commits/create-git-commit.d.ts +23 -0
  13. package/dist/github/commits/create-git-commit.js +39 -0
  14. package/dist/github/commits/create-git-commit.js.map +1 -0
  15. package/dist/github/commits/create-git-tree.d.ts +29 -0
  16. package/dist/github/commits/create-git-tree.js +47 -0
  17. package/dist/github/commits/create-git-tree.js.map +1 -0
  18. package/dist/github/commits/get-sha-last-commit-of-branch.d.ts +21 -0
  19. package/dist/github/commits/get-sha-last-commit-of-branch.js +35 -0
  20. package/dist/github/commits/get-sha-last-commit-of-branch.js.map +1 -0
  21. package/dist/github/commits/get-tree-by-commit-sha.d.ts +23 -0
  22. package/dist/github/commits/get-tree-by-commit-sha.js +37 -0
  23. package/dist/github/commits/get-tree-by-commit-sha.js.map +1 -0
  24. package/dist/github/commits/index.d.ts +6 -0
  25. package/dist/github/commits/index.js +20 -0
  26. package/dist/github/commits/run-new-commit-process.d.ts +27 -0
  27. package/dist/github/commits/run-new-commit-process.js +49 -0
  28. package/dist/github/commits/run-new-commit-process.js.map +1 -0
  29. package/dist/github/commits/utils.d.ts +1 -0
  30. package/dist/github/commits/utils.js +29 -0
  31. package/dist/github/commits/utils.js.map +1 -0
  32. package/dist/github/github.d.ts +22 -0
  33. package/dist/github/github.js +50 -0
  34. package/dist/github/github.js.map +1 -0
  35. package/dist/github/index.d.ts +1 -0
  36. package/dist/github/index.js +10 -0
  37. package/dist/index.d.ts +15 -0
  38. package/{index.js → dist/index.js} +2 -0
  39. package/{index.js.map → dist/index.js.map} +1 -1
  40. package/dist/package.json +65 -0
  41. package/dist/string/index.js.map +1 -0
  42. package/dist/time/index.js.map +1 -0
  43. package/global.d.ts +2 -0
  44. package/package.json +5 -1
  45. package/rollup.config.ts +15 -0
  46. package/src/array/__tests__/array-to-map.test.ts +50 -0
  47. package/src/array/__tests__/get-values-from-keys.test.ts +14 -0
  48. package/src/array/array-to-map.ts +32 -0
  49. package/src/array/get-values-from-keys.ts +3 -0
  50. package/src/array/index.ts +2 -0
  51. package/src/cdn/__tests__/cdn.factory.test.ts +120 -0
  52. package/src/cdn/__tests__/utils.test.ts +456 -0
  53. package/src/cdn/cdn.factory.ts +63 -0
  54. package/src/cdn/index.ts +2 -0
  55. package/src/cdn/types/cdn-factory.type.ts +35 -0
  56. package/src/cdn/types/index.ts +1 -0
  57. package/src/cdn/utils/extract-info-from-url.util.ts +21 -0
  58. package/src/cdn/utils/get-cdn-from-s3-url.util.ts +20 -0
  59. package/src/cdn/utils/get-cdn-url.util.ts +26 -0
  60. package/src/formatting/__tests__/format-number.test.ts +11 -0
  61. package/src/formatting/format-number.ts +14 -0
  62. package/src/formatting/index.ts +1 -0
  63. package/src/fp/__tests__/run-while.test.ts +29 -0
  64. package/src/fp/index.ts +1 -0
  65. package/src/fp/run-while.ts +54 -0
  66. package/src/github/commits/create-git-blob.ts +57 -0
  67. package/src/github/commits/create-git-commit.ts +57 -0
  68. package/src/github/commits/create-git-tree.ts +68 -0
  69. package/src/github/commits/get-sha-last-commit-of-branch.ts +50 -0
  70. package/src/github/commits/get-tree-by-commit-sha.ts +52 -0
  71. package/src/github/commits/index.ts +6 -0
  72. package/src/github/commits/run-new-commit-process.ts +88 -0
  73. package/src/github/commits/utils.ts +31 -0
  74. package/src/github/github.ts +69 -0
  75. package/src/github/index.ts +1 -0
  76. package/src/http/__tests__/encode-search-object.test.ts +34 -0
  77. package/src/http/__tests__/fetch-with-timeout.test.ts +50 -0
  78. package/src/http/encode-search-object.ts +39 -0
  79. package/src/http/fetch-with-timeout.ts +24 -0
  80. package/src/http/index.ts +2 -0
  81. package/{index.d.ts → src/index.ts} +15 -14
  82. package/src/linked-list/__tests__/apply-diff-linked-list.test.ts +103 -0
  83. package/src/linked-list/__tests__/create-linked-list.test.ts +204 -0
  84. package/src/linked-list/__tests__/find-linked-list.test.ts +40 -0
  85. package/src/linked-list/__tests__/get-linked-list.test.ts +28 -0
  86. package/src/linked-list/__tests__/insert-linked-list.test.ts +186 -0
  87. package/src/linked-list/__tests__/linked-list-utils.test.ts +252 -0
  88. package/src/linked-list/__tests__/reduce-linked-list.test.ts +31 -0
  89. package/src/linked-list/__tests__/remove-linked-list.test.ts +129 -0
  90. package/src/linked-list/__tests__/update-linked-list.test.ts +53 -0
  91. package/src/linked-list/__tests__/while-linked-list.test.ts +58 -0
  92. package/src/linked-list/apply-diff-linked-list.ts +35 -0
  93. package/src/linked-list/create-linked-list.ts +79 -0
  94. package/src/linked-list/find-linked-list.ts +26 -0
  95. package/src/linked-list/get-linked-list.ts +9 -0
  96. package/src/linked-list/index.ts +2 -0
  97. package/src/linked-list/insert-linked-list.ts +50 -0
  98. package/src/linked-list/linked-list-utils.ts +143 -0
  99. package/src/linked-list/reduce-linked-list.ts +18 -0
  100. package/src/linked-list/remove-linked-list.ts +36 -0
  101. package/src/linked-list/type.ts +38 -0
  102. package/src/linked-list/update-linked-list.ts +29 -0
  103. package/src/linked-list/while-linked-list.ts +33 -0
  104. package/src/math/__tests__/clamp-number.test.ts +30 -0
  105. package/src/math/__tests__/interpolate.test.ts +28 -0
  106. package/src/math/__tests__/number-range.test.ts +59 -0
  107. package/src/math/__tests__/random-number.test.ts +11 -0
  108. package/src/math/clamp-number.ts +8 -0
  109. package/src/math/index.ts +4 -0
  110. package/src/math/interpolate.ts +18 -0
  111. package/src/math/number-range.ts +16 -0
  112. package/src/math/random-number.ts +10 -0
  113. package/src/object/__tests__/merge-deep.test.ts +56 -0
  114. package/src/object/index.ts +1 -0
  115. package/src/object/merge-deep.ts +27 -0
  116. package/src/parsing/__tests__/parse-boolean.test.ts +15 -0
  117. package/src/parsing/__tests__/parse-enum.test.ts +19 -0
  118. package/src/parsing/__tests__/parse-number.test.ts +20 -0
  119. package/src/parsing/index.ts +3 -0
  120. package/src/parsing/parse-boolean.ts +10 -0
  121. package/src/parsing/parse-enum.ts +16 -0
  122. package/src/parsing/parse-number.ts +12 -0
  123. package/src/predicates/__tests__/is-function.test.ts +11 -0
  124. package/src/predicates/__tests__/is-null.test.ts +11 -0
  125. package/src/predicates/__tests__/is-object.test.ts +11 -0
  126. package/src/predicates/__tests__/is-primitive.test.ts +42 -0
  127. package/src/predicates/__tests__/is-undefined.test.ts +11 -0
  128. package/src/predicates/__tests__/is-uuid.test.ts +13 -0
  129. package/src/predicates/index.ts +5 -0
  130. package/src/predicates/is-function.ts +3 -0
  131. package/src/predicates/is-null.ts +3 -0
  132. package/src/predicates/is-object.ts +3 -0
  133. package/src/predicates/is-primitive.ts +19 -0
  134. package/src/predicates/is-undefined.ts +3 -0
  135. package/src/predicates/is-uuid.ts +3 -0
  136. package/src/promise/__tests__/delay.test.ts +13 -0
  137. package/src/promise/__tests__/execute-or-resolve-default.test.ts +25 -0
  138. package/src/promise/__tests__/execute-with-delay.test.ts +24 -0
  139. package/src/promise/delay.ts +3 -0
  140. package/src/promise/execute-or-resolve-default.ts +14 -0
  141. package/src/promise/execute-with-delay.ts +6 -0
  142. package/src/promise/index.ts +3 -0
  143. package/src/string/__tests__/replace-in-template.test.ts +52 -0
  144. package/src/string/index.ts +1 -0
  145. package/src/string/replace-in-template.ts +45 -0
  146. package/src/time/__tests__/convert.test.ts +14 -0
  147. package/src/time/__tests__/format-minutes.test.ts +10 -0
  148. package/src/time/__tests__/get-time-diff-as-string.test.ts +128 -0
  149. package/src/time/constants.ts +4 -0
  150. package/src/time/convert.ts +5 -0
  151. package/src/time/format-minutes.ts +9 -0
  152. package/src/time/get-time-diff-as-string.ts +196 -0
  153. package/src/time/index.ts +4 -0
  154. package/src/types/function.ts +1 -0
  155. package/src/types/general.ts +8 -0
  156. package/src/types/index.ts +4 -0
  157. package/src/types/object.ts +33 -0
  158. package/src/types/search.ts +29 -0
  159. package/tsconfig.eslint.json +8 -0
  160. package/tsconfig.json +25 -0
  161. package/cdn/cdn.factory.js.map +0 -1
  162. /package/{_virtual → dist/_virtual}/_tslib.js +0 -0
  163. /package/{_virtual → dist/_virtual}/_tslib.js.map +0 -0
  164. /package/{array → dist/array}/array-to-map.d.ts +0 -0
  165. /package/{array → dist/array}/array-to-map.js +0 -0
  166. /package/{array → dist/array}/array-to-map.js.map +0 -0
  167. /package/{array → dist/array}/get-values-from-keys.d.ts +0 -0
  168. /package/{array → dist/array}/get-values-from-keys.js +0 -0
  169. /package/{array → dist/array}/get-values-from-keys.js.map +0 -0
  170. /package/{array → dist/array}/index.d.ts +0 -0
  171. /package/{array → dist/array}/index.js +0 -0
  172. /package/{array → dist/array}/index.js.map +0 -0
  173. /package/{cdn → dist/cdn}/cdn.factory.d.ts +0 -0
  174. /package/{cdn → dist/cdn}/index.d.ts +0 -0
  175. /package/{cdn → dist/cdn}/index.js +0 -0
  176. /package/{cdn → dist/cdn}/index.js.map +0 -0
  177. /package/{cdn → dist/cdn}/types/cdn-factory.type.d.ts +0 -0
  178. /package/{cdn → dist/cdn}/types/index.d.ts +0 -0
  179. /package/{cdn → dist/cdn}/types/index.js +0 -0
  180. /package/{cdn → dist/cdn}/types/index.js.map +0 -0
  181. /package/{cdn → dist/cdn}/utils/extract-info-from-url.util.d.ts +0 -0
  182. /package/{cdn → dist/cdn}/utils/extract-info-from-url.util.js +0 -0
  183. /package/{cdn → dist/cdn}/utils/extract-info-from-url.util.js.map +0 -0
  184. /package/{cdn → dist/cdn}/utils/get-cdn-from-s3-url.util.d.ts +0 -0
  185. /package/{cdn → dist/cdn}/utils/get-cdn-from-s3-url.util.js +0 -0
  186. /package/{cdn → dist/cdn}/utils/get-cdn-from-s3-url.util.js.map +0 -0
  187. /package/{cdn → dist/cdn}/utils/get-cdn-url.util.d.ts +0 -0
  188. /package/{cdn → dist/cdn}/utils/get-cdn-url.util.js +0 -0
  189. /package/{cdn → dist/cdn}/utils/get-cdn-url.util.js.map +0 -0
  190. /package/{formatting → dist/formatting}/format-number.d.ts +0 -0
  191. /package/{formatting → dist/formatting}/format-number.js +0 -0
  192. /package/{formatting → dist/formatting}/format-number.js.map +0 -0
  193. /package/{formatting → dist/formatting}/index.d.ts +0 -0
  194. /package/{formatting → dist/formatting}/index.js +0 -0
  195. /package/{formatting → dist/formatting}/index.js.map +0 -0
  196. /package/{fp → dist/fp}/index.d.ts +0 -0
  197. /package/{fp → dist/fp}/index.js +0 -0
  198. /package/{fp → dist/fp}/index.js.map +0 -0
  199. /package/{fp → dist/fp}/run-while.d.ts +0 -0
  200. /package/{fp → dist/fp}/run-while.js +0 -0
  201. /package/{fp → dist/fp}/run-while.js.map +0 -0
  202. /package/{time → dist/github/commits}/index.js.map +0 -0
  203. /package/{linked-list → dist/github}/index.js.map +0 -0
  204. /package/{http → dist/http}/encode-search-object.d.ts +0 -0
  205. /package/{http → dist/http}/encode-search-object.js +0 -0
  206. /package/{http → dist/http}/encode-search-object.js.map +0 -0
  207. /package/{http → dist/http}/fetch-with-timeout.d.ts +0 -0
  208. /package/{http → dist/http}/fetch-with-timeout.js +0 -0
  209. /package/{http → dist/http}/fetch-with-timeout.js.map +0 -0
  210. /package/{http → dist/http}/index.d.ts +0 -0
  211. /package/{http → dist/http}/index.js +0 -0
  212. /package/{http → dist/http}/index.js.map +0 -0
  213. /package/{linked-list → dist/linked-list}/apply-diff-linked-list.d.ts +0 -0
  214. /package/{linked-list → dist/linked-list}/apply-diff-linked-list.js +0 -0
  215. /package/{linked-list → dist/linked-list}/apply-diff-linked-list.js.map +0 -0
  216. /package/{linked-list → dist/linked-list}/create-linked-list.d.ts +0 -0
  217. /package/{linked-list → dist/linked-list}/create-linked-list.js +0 -0
  218. /package/{linked-list → dist/linked-list}/create-linked-list.js.map +0 -0
  219. /package/{linked-list → dist/linked-list}/find-linked-list.d.ts +0 -0
  220. /package/{linked-list → dist/linked-list}/find-linked-list.js +0 -0
  221. /package/{linked-list → dist/linked-list}/find-linked-list.js.map +0 -0
  222. /package/{linked-list → dist/linked-list}/get-linked-list.d.ts +0 -0
  223. /package/{linked-list → dist/linked-list}/get-linked-list.js +0 -0
  224. /package/{linked-list → dist/linked-list}/get-linked-list.js.map +0 -0
  225. /package/{linked-list → dist/linked-list}/index.d.ts +0 -0
  226. /package/{linked-list → dist/linked-list}/index.js +0 -0
  227. /package/{object → dist/linked-list}/index.js.map +0 -0
  228. /package/{linked-list → dist/linked-list}/insert-linked-list.d.ts +0 -0
  229. /package/{linked-list → dist/linked-list}/insert-linked-list.js +0 -0
  230. /package/{linked-list → dist/linked-list}/insert-linked-list.js.map +0 -0
  231. /package/{linked-list → dist/linked-list}/linked-list-utils.d.ts +0 -0
  232. /package/{linked-list → dist/linked-list}/linked-list-utils.js +0 -0
  233. /package/{linked-list → dist/linked-list}/linked-list-utils.js.map +0 -0
  234. /package/{linked-list → dist/linked-list}/reduce-linked-list.d.ts +0 -0
  235. /package/{linked-list → dist/linked-list}/reduce-linked-list.js +0 -0
  236. /package/{linked-list → dist/linked-list}/reduce-linked-list.js.map +0 -0
  237. /package/{linked-list → dist/linked-list}/remove-linked-list.d.ts +0 -0
  238. /package/{linked-list → dist/linked-list}/remove-linked-list.js +0 -0
  239. /package/{linked-list → dist/linked-list}/remove-linked-list.js.map +0 -0
  240. /package/{linked-list → dist/linked-list}/type.d.ts +0 -0
  241. /package/{linked-list → dist/linked-list}/update-linked-list.d.ts +0 -0
  242. /package/{linked-list → dist/linked-list}/update-linked-list.js +0 -0
  243. /package/{linked-list → dist/linked-list}/update-linked-list.js.map +0 -0
  244. /package/{linked-list → dist/linked-list}/while-linked-list.d.ts +0 -0
  245. /package/{linked-list → dist/linked-list}/while-linked-list.js +0 -0
  246. /package/{linked-list → dist/linked-list}/while-linked-list.js.map +0 -0
  247. /package/{math → dist/math}/clamp-number.d.ts +0 -0
  248. /package/{math → dist/math}/clamp-number.js +0 -0
  249. /package/{math → dist/math}/clamp-number.js.map +0 -0
  250. /package/{math → dist/math}/index.d.ts +0 -0
  251. /package/{math → dist/math}/index.js +0 -0
  252. /package/{math → dist/math}/index.js.map +0 -0
  253. /package/{math → dist/math}/interpolate.d.ts +0 -0
  254. /package/{math → dist/math}/interpolate.js +0 -0
  255. /package/{math → dist/math}/interpolate.js.map +0 -0
  256. /package/{math → dist/math}/number-range.d.ts +0 -0
  257. /package/{math → dist/math}/number-range.js +0 -0
  258. /package/{math → dist/math}/number-range.js.map +0 -0
  259. /package/{math → dist/math}/random-number.d.ts +0 -0
  260. /package/{math → dist/math}/random-number.js +0 -0
  261. /package/{math → dist/math}/random-number.js.map +0 -0
  262. /package/{object → dist/object}/index.d.ts +0 -0
  263. /package/{object → dist/object}/index.js +0 -0
  264. /package/{string → dist/object}/index.js.map +0 -0
  265. /package/{object → dist/object}/merge-deep.d.ts +0 -0
  266. /package/{object → dist/object}/merge-deep.js +0 -0
  267. /package/{object → dist/object}/merge-deep.js.map +0 -0
  268. /package/{parsing → dist/parsing}/index.d.ts +0 -0
  269. /package/{parsing → dist/parsing}/index.js +0 -0
  270. /package/{parsing → dist/parsing}/index.js.map +0 -0
  271. /package/{parsing → dist/parsing}/parse-boolean.d.ts +0 -0
  272. /package/{parsing → dist/parsing}/parse-boolean.js +0 -0
  273. /package/{parsing → dist/parsing}/parse-boolean.js.map +0 -0
  274. /package/{parsing → dist/parsing}/parse-enum.d.ts +0 -0
  275. /package/{parsing → dist/parsing}/parse-enum.js +0 -0
  276. /package/{parsing → dist/parsing}/parse-enum.js.map +0 -0
  277. /package/{parsing → dist/parsing}/parse-number.d.ts +0 -0
  278. /package/{parsing → dist/parsing}/parse-number.js +0 -0
  279. /package/{parsing → dist/parsing}/parse-number.js.map +0 -0
  280. /package/{predicates → dist/predicates}/index.d.ts +0 -0
  281. /package/{predicates → dist/predicates}/index.js +0 -0
  282. /package/{predicates → dist/predicates}/index.js.map +0 -0
  283. /package/{predicates → dist/predicates}/is-function.d.ts +0 -0
  284. /package/{predicates → dist/predicates}/is-function.js +0 -0
  285. /package/{predicates → dist/predicates}/is-function.js.map +0 -0
  286. /package/{predicates → dist/predicates}/is-null.d.ts +0 -0
  287. /package/{predicates → dist/predicates}/is-null.js +0 -0
  288. /package/{predicates → dist/predicates}/is-null.js.map +0 -0
  289. /package/{predicates → dist/predicates}/is-object.d.ts +0 -0
  290. /package/{predicates → dist/predicates}/is-object.js +0 -0
  291. /package/{predicates → dist/predicates}/is-object.js.map +0 -0
  292. /package/{predicates → dist/predicates}/is-primitive.d.ts +0 -0
  293. /package/{predicates → dist/predicates}/is-primitive.js +0 -0
  294. /package/{predicates → dist/predicates}/is-primitive.js.map +0 -0
  295. /package/{predicates → dist/predicates}/is-undefined.d.ts +0 -0
  296. /package/{predicates → dist/predicates}/is-undefined.js +0 -0
  297. /package/{predicates → dist/predicates}/is-undefined.js.map +0 -0
  298. /package/{predicates → dist/predicates}/is-uuid.d.ts +0 -0
  299. /package/{promise → dist/promise}/delay.d.ts +0 -0
  300. /package/{promise → dist/promise}/delay.js +0 -0
  301. /package/{promise → dist/promise}/delay.js.map +0 -0
  302. /package/{promise → dist/promise}/execute-or-resolve-default.d.ts +0 -0
  303. /package/{promise → dist/promise}/execute-or-resolve-default.js +0 -0
  304. /package/{promise → dist/promise}/execute-or-resolve-default.js.map +0 -0
  305. /package/{promise → dist/promise}/execute-with-delay.d.ts +0 -0
  306. /package/{promise → dist/promise}/execute-with-delay.js +0 -0
  307. /package/{promise → dist/promise}/execute-with-delay.js.map +0 -0
  308. /package/{promise → dist/promise}/index.d.ts +0 -0
  309. /package/{promise → dist/promise}/index.js +0 -0
  310. /package/{promise → dist/promise}/index.js.map +0 -0
  311. /package/{string → dist/string}/index.d.ts +0 -0
  312. /package/{string → dist/string}/index.js +0 -0
  313. /package/{string → dist/string}/replace-in-template.d.ts +0 -0
  314. /package/{string → dist/string}/replace-in-template.js +0 -0
  315. /package/{string → dist/string}/replace-in-template.js.map +0 -0
  316. /package/{time → dist/time}/constants.d.ts +0 -0
  317. /package/{time → dist/time}/constants.js +0 -0
  318. /package/{time → dist/time}/constants.js.map +0 -0
  319. /package/{time → dist/time}/convert.d.ts +0 -0
  320. /package/{time → dist/time}/convert.js +0 -0
  321. /package/{time → dist/time}/convert.js.map +0 -0
  322. /package/{time → dist/time}/format-minutes.d.ts +0 -0
  323. /package/{time → dist/time}/format-minutes.js +0 -0
  324. /package/{time → dist/time}/format-minutes.js.map +0 -0
  325. /package/{time → dist/time}/get-time-diff-as-string.d.ts +0 -0
  326. /package/{time → dist/time}/get-time-diff-as-string.js +0 -0
  327. /package/{time → dist/time}/get-time-diff-as-string.js.map +0 -0
  328. /package/{time → dist/time}/index.d.ts +0 -0
  329. /package/{time → dist/time}/index.js +0 -0
  330. /package/{types → dist/types}/function.d.ts +0 -0
  331. /package/{types → dist/types}/general.d.ts +0 -0
  332. /package/{types → dist/types}/index.d.ts +0 -0
  333. /package/{types → dist/types}/index.js +0 -0
  334. /package/{types → dist/types}/index.js.map +0 -0
  335. /package/{types → dist/types}/object.d.ts +0 -0
  336. /package/{types → dist/types}/search.d.ts +0 -0
@@ -0,0 +1,456 @@
1
+ import {
2
+ imgixTransformationQueries,
3
+ twicpicsTransformationQueries,
4
+ } from '../cdn.factory';
5
+ import { extractInfoFromUrl } from '../utils/extract-info-from-url.util';
6
+ import { getCdnUrlFactory } from '../utils/get-cdn-url.util';
7
+ import { getCdnFromS3UrlFactory } from '../utils/get-cdn-from-s3-url.util';
8
+ import type { InstanceMapper } from '../types';
9
+
10
+ describe('extract-info-from-url', () => {
11
+ it('should handle non-aws s3 url', () => {
12
+ const info = extractInfoFromUrl('https://www.google.com');
13
+
14
+ expect(info).toBeUndefined();
15
+ });
16
+
17
+ it('should handle all aws s3 url', () => {
18
+ const { instanceId: instanceIdEW1, path: pathEW1 } = extractInfoFromUrl(
19
+ 'https://sparted-prod-check.s3.eu-west-1/path/to/something',
20
+ ) ?? {};
21
+
22
+ expect(instanceIdEW1).toBe('check');
23
+ expect(pathEW1).toBe('/path/to/something');
24
+
25
+ const { instanceId: instanceIdAmazonaws, path: pathAmazonaws } = extractInfoFromUrl(
26
+ 'https://sparted-prod-check.s3.amazonaws.com/path/to/something',
27
+ ) ?? {};
28
+
29
+ expect(instanceIdAmazonaws).toBe('check');
30
+ expect(pathAmazonaws).toBe('/path/to/something');
31
+
32
+ const { instanceId: instanceIdCloud, path: pathCloud } = extractInfoFromUrl(
33
+ 'https://sparted-prod-check.s3.fr-par.scw.cloud/path/to/something',
34
+ ) ?? {};
35
+
36
+ expect(instanceIdCloud).toBe('check');
37
+ expect(pathCloud).toBe('/path/to/something');
38
+ });
39
+ });
40
+
41
+ describe('getCdnUrlFactory', () => {
42
+ it('Imgix - should generate url without params', () => {
43
+ const domain = 'https://sparted.imgix.fr';
44
+ const instanceMapper: InstanceMapper = (instanceId) => `/prod/${instanceId}`;
45
+ const getCdnUrl = getCdnUrlFactory({
46
+ domain,
47
+ instanceMapper,
48
+ transformationQueries: imgixTransformationQueries,
49
+ });
50
+
51
+ expect(getCdnUrl('test', '/path/to/something')).toBe(
52
+ 'https://sparted.imgix.fr/prod/test/path/to/something?auto=compress,format',
53
+ );
54
+ });
55
+
56
+ it('Twicpics - should generate url without params', () => {
57
+ const domain = 'https://sparted.twicpics.fr';
58
+ const instanceMapper: InstanceMapper = (instanceId) => `/prod/${instanceId}`;
59
+ const getCdnUrl = getCdnUrlFactory({
60
+ domain,
61
+ instanceMapper,
62
+ transformationQueries: twicpicsTransformationQueries,
63
+ });
64
+
65
+ expect(getCdnUrl('test', '/path/to/something')).toBe(
66
+ 'https://sparted.twicpics.fr/prod/test/path/to/something',
67
+ );
68
+ });
69
+
70
+ it('Imgix - should generate url of image as thumbnail', () => {
71
+ const domain = 'https://sparted.imgix.fr';
72
+ const instanceMapper: InstanceMapper = (instanceId) => `/prod/${instanceId}`;
73
+ const getCdnUrl = getCdnUrlFactory({
74
+ domain,
75
+ instanceMapper,
76
+ transformationQueries: imgixTransformationQueries,
77
+ });
78
+
79
+ expect(getCdnUrl('test', '/path/to/something', { asThumbnail: true })).toBe(
80
+ 'https://sparted.imgix.fr/prod/test/path/to/something?auto=compress,format&h=300&w=300',
81
+ );
82
+ });
83
+
84
+ it('Twicpics - should generate url of image as thumbnail', () => {
85
+ const domain = 'https://sparted.twicpics.fr';
86
+ const instanceMapper: InstanceMapper = (instanceId) => `/prod/${instanceId}`;
87
+ const getCdnUrl = getCdnUrlFactory({
88
+ domain,
89
+ instanceMapper,
90
+ transformationQueries: twicpicsTransformationQueries,
91
+ });
92
+
93
+ expect(getCdnUrl('test', '/path/to/something', { asThumbnail: true })).toBe(
94
+ 'https://sparted.twicpics.fr/prod/test/path/to/something?twic=v1/cover=300x300',
95
+ );
96
+ });
97
+
98
+ it('Imgix - should generate url with params', () => {
99
+ const domain = 'https://sparted.imgix.fr';
100
+ const instanceMapper: InstanceMapper = (instanceId) => `/prod/${instanceId}`;
101
+ const getCdnUrl = getCdnUrlFactory({
102
+ domain,
103
+ instanceMapper,
104
+ transformationQueries: imgixTransformationQueries,
105
+ });
106
+
107
+ expect(
108
+ getCdnUrl('test', '/path/to/something', {
109
+ transformation: { fit: 'crop', height: 1, width: 1 },
110
+ }),
111
+ ).toBe(
112
+ 'https://sparted.imgix.fr/prod/test/path/to/something?auto=compress,format&fit=crop&h=1&w=1',
113
+ );
114
+
115
+ expect(
116
+ getCdnUrl('test', '/path/to/something', {
117
+ transformation: { fit: 'crop', width: 1 },
118
+ }),
119
+ ).toBe(
120
+ 'https://sparted.imgix.fr/prod/test/path/to/something?auto=compress,format&fit=crop&w=1',
121
+ );
122
+
123
+ expect(
124
+ getCdnUrl('test', '/path/to/something', {
125
+ transformation: { fit: 'crop', height: 1 },
126
+ }),
127
+ ).toBe(
128
+ 'https://sparted.imgix.fr/prod/test/path/to/something?auto=compress,format&fit=crop&h=1',
129
+ );
130
+
131
+ expect(
132
+ getCdnUrl('test', '/path/to/something', { transformation: { width: 1 } }),
133
+ ).toBe(
134
+ 'https://sparted.imgix.fr/prod/test/path/to/something?auto=compress,format&w=1',
135
+ );
136
+
137
+ expect(
138
+ getCdnUrl('test', '/path/to/something', { transformation: { height: 1 } }),
139
+ ).toBe(
140
+ 'https://sparted.imgix.fr/prod/test/path/to/something?auto=compress,format&h=1',
141
+ );
142
+
143
+ expect(
144
+ getCdnUrl('test', '/path/to/something', {
145
+ transformation: { height: 1, width: 1 },
146
+ }),
147
+ ).toBe(
148
+ 'https://sparted.imgix.fr/prod/test/path/to/something?auto=compress,format&h=1&w=1',
149
+ );
150
+
151
+ expect(
152
+ getCdnUrl('test', '/path/to/something', {
153
+ transformation: { devicePixelRatio: 2 },
154
+ }),
155
+ ).toBe(
156
+ 'https://sparted.imgix.fr/prod/test/path/to/something?auto=compress,format&dpr=2',
157
+ );
158
+
159
+ expect(
160
+ getCdnUrl('test', '/path/to/something', {
161
+ transformation: { quality: 75 },
162
+ }),
163
+ ).toBe(
164
+ 'https://sparted.imgix.fr/prod/test/path/to/something?auto=compress,format&q=75',
165
+ );
166
+ });
167
+
168
+ it('Twicpics - should generate url of image as thumbnail', () => {
169
+ const domain = 'https://sparted.twicpics.fr';
170
+ const instanceMapper: InstanceMapper = (instanceId) => `/prod/${instanceId}`;
171
+ const getCdnUrl = getCdnUrlFactory({
172
+ domain,
173
+ instanceMapper,
174
+ transformationQueries: twicpicsTransformationQueries,
175
+ });
176
+
177
+ expect(
178
+ getCdnUrl('test', '/path/to/something', {
179
+ transformation: { fit: 'crop', height: 1, width: 1 },
180
+ }),
181
+ ).toBe(
182
+ 'https://sparted.twicpics.fr/prod/test/path/to/something?twic=v1/crop=1x1',
183
+ );
184
+
185
+ expect(
186
+ getCdnUrl('test', '/path/to/something', {
187
+ transformation: { fit: 'contain', height: 1, width: 1 },
188
+ }),
189
+ ).toBe(
190
+ 'https://sparted.twicpics.fr/prod/test/path/to/something?twic=v1/contain=1x1',
191
+ );
192
+
193
+ expect(
194
+ getCdnUrl('test', '/path/to/something', { transformation: { width: 1 } }),
195
+ ).toBe(
196
+ 'https://sparted.twicpics.fr/prod/test/path/to/something?twic=v1/cover=1',
197
+ );
198
+ });
199
+ });
200
+
201
+ describe('getCdnFromS3UrlFactory', () => {
202
+ it('Imgix - should handle non-aws s3 url', () => {
203
+ const domain = 'https://sparted.imgix.fr';
204
+ const instanceMapper: InstanceMapper = (instanceId) => `/prod/${instanceId}`;
205
+ const getCdnUrl = getCdnUrlFactory({
206
+ domain,
207
+ instanceMapper,
208
+ transformationQueries: imgixTransformationQueries,
209
+ });
210
+
211
+ const getCdnFromS3Url = getCdnFromS3UrlFactory({
212
+ getCdnUrl,
213
+ });
214
+
215
+ expect(getCdnFromS3Url('https://www.google.com')).toBe(
216
+ 'https://www.google.com',
217
+ );
218
+ });
219
+
220
+ it('Twicpics - should handle non-aws s3 url', () => {
221
+ const domain = 'https://sparted.twicpics.fr';
222
+ const instanceMapper: InstanceMapper = (instanceId) => `/prod/${instanceId}`;
223
+ const getCdnUrl = getCdnUrlFactory({
224
+ domain,
225
+ instanceMapper,
226
+ transformationQueries: twicpicsTransformationQueries,
227
+ });
228
+
229
+ const getCdnFromS3Url = getCdnFromS3UrlFactory({
230
+ getCdnUrl,
231
+ });
232
+
233
+ expect(getCdnFromS3Url('https://www.google.com')).toBe(
234
+ 'https://www.google.com',
235
+ );
236
+ });
237
+
238
+ it('Imgix - should handle all aws s3 url with no params', () => {
239
+ const domain = 'https://sparted.imgix.fr';
240
+ const instanceMapper: InstanceMapper = (instanceId) => `/prod/${instanceId}`;
241
+ const getCdnUrl = getCdnUrlFactory({
242
+ domain,
243
+ instanceMapper,
244
+ transformationQueries: imgixTransformationQueries,
245
+ });
246
+
247
+ const getCdnFromS3Url = getCdnFromS3UrlFactory({
248
+ getCdnUrl,
249
+ });
250
+
251
+ expect(
252
+ getCdnFromS3Url(
253
+ 'https://sparted-prod-check.s3.eu-west-1/path/to/something',
254
+ ),
255
+ ).toBe(
256
+ 'https://sparted.imgix.fr/prod/check/path/to/something?auto=compress,format',
257
+ );
258
+
259
+ expect(
260
+ getCdnFromS3Url(
261
+ 'https://sparted-prod-check.s3.amazonaws.com/path/to/something',
262
+ ),
263
+ ).toBe(
264
+ 'https://sparted.imgix.fr/prod/check/path/to/something?auto=compress,format',
265
+ );
266
+
267
+ expect(
268
+ getCdnFromS3Url(
269
+ 'https://sparted-prod-check.s3.fr-par.scw.cloud/path/to/something',
270
+ ),
271
+ ).toBe(
272
+ 'https://sparted.imgix.fr/prod/check/path/to/something?auto=compress,format',
273
+ );
274
+ });
275
+
276
+ it('Twicpics - should handle all aws s3 url with no params', () => {
277
+ const domain = 'https://sparted.twicpics.fr';
278
+ const instanceMapper: InstanceMapper = (instanceId) => `/prod/${instanceId}`;
279
+ const getCdnUrl = getCdnUrlFactory({
280
+ domain,
281
+ instanceMapper,
282
+ transformationQueries: twicpicsTransformationQueries,
283
+ });
284
+
285
+ const getCdnFromS3Url = getCdnFromS3UrlFactory({
286
+ getCdnUrl,
287
+ });
288
+
289
+ expect(
290
+ getCdnFromS3Url(
291
+ 'https://sparted-prod-check.s3.eu-west-1/path/to/something',
292
+ ),
293
+ ).toBe('https://sparted.twicpics.fr/prod/check/path/to/something');
294
+
295
+ expect(
296
+ getCdnFromS3Url(
297
+ 'https://sparted-prod-check.s3.amazonaws.com/path/to/something',
298
+ ),
299
+ ).toBe('https://sparted.twicpics.fr/prod/check/path/to/something');
300
+
301
+ expect(
302
+ getCdnFromS3Url(
303
+ 'https://sparted-prod-check.s3.fr-par.scw.cloud/path/to/something',
304
+ ),
305
+ ).toBe('https://sparted.twicpics.fr/prod/check/path/to/something');
306
+ });
307
+
308
+ it('Imgix - should handle params', () => {
309
+ const domain = 'https://sparted.imgix.fr';
310
+ const instanceMapper: InstanceMapper = (instanceId) => `/prod/${instanceId}`;
311
+ const getCdnUrl = getCdnUrlFactory({
312
+ domain,
313
+ instanceMapper,
314
+ transformationQueries: imgixTransformationQueries,
315
+ });
316
+
317
+ const getCdnFromS3Url = getCdnFromS3UrlFactory({
318
+ getCdnUrl,
319
+ });
320
+
321
+ expect(
322
+ getCdnFromS3Url(
323
+ 'https://sparted-prod-check.s3.eu-west-1/path/to/something',
324
+ {
325
+ transformation: { fit: 'crop', height: 1, width: 1 },
326
+ },
327
+ ),
328
+ ).toBe(
329
+ 'https://sparted.imgix.fr/prod/check/path/to/something?auto=compress,format&fit=crop&h=1&w=1',
330
+ );
331
+
332
+ expect(
333
+ getCdnFromS3Url(
334
+ 'https://sparted-prod-check.s3.eu-west-1/path/to/something',
335
+ {
336
+ transformation: { fit: 'crop', width: 1 },
337
+ },
338
+ ),
339
+ ).toBe(
340
+ 'https://sparted.imgix.fr/prod/check/path/to/something?auto=compress,format&fit=crop&w=1',
341
+ );
342
+
343
+ expect(
344
+ getCdnFromS3Url(
345
+ 'https://sparted-prod-check.s3.eu-west-1/path/to/something',
346
+ {
347
+ transformation: { fit: 'crop', height: 1 },
348
+ },
349
+ ),
350
+ ).toBe(
351
+ 'https://sparted.imgix.fr/prod/check/path/to/something?auto=compress,format&fit=crop&h=1',
352
+ );
353
+
354
+ expect(
355
+ getCdnFromS3Url(
356
+ 'https://sparted-prod-check.s3.eu-west-1/path/to/something',
357
+ {
358
+ transformation: { width: 1 },
359
+ },
360
+ ),
361
+ ).toBe(
362
+ 'https://sparted.imgix.fr/prod/check/path/to/something?auto=compress,format&w=1',
363
+ );
364
+
365
+ expect(
366
+ getCdnFromS3Url(
367
+ 'https://sparted-prod-check.s3.eu-west-1/path/to/something',
368
+ {
369
+ transformation: { height: 1 },
370
+ },
371
+ ),
372
+ ).toBe(
373
+ 'https://sparted.imgix.fr/prod/check/path/to/something?auto=compress,format&h=1',
374
+ );
375
+
376
+ expect(
377
+ getCdnFromS3Url(
378
+ 'https://sparted-prod-check.s3.eu-west-1/path/to/something',
379
+ {
380
+ transformation: { height: 1, width: 1 },
381
+ },
382
+ ),
383
+ ).toBe(
384
+ 'https://sparted.imgix.fr/prod/check/path/to/something?auto=compress,format&h=1&w=1',
385
+ );
386
+
387
+ expect(
388
+ getCdnFromS3Url(
389
+ 'https://sparted-prod-check.s3.eu-west-1/path/to/something',
390
+ {
391
+ transformation: { devicePixelRatio: 2 },
392
+ },
393
+ ),
394
+ ).toBe(
395
+ 'https://sparted.imgix.fr/prod/check/path/to/something?auto=compress,format&dpr=2',
396
+ );
397
+
398
+ expect(
399
+ getCdnFromS3Url(
400
+ 'https://sparted-prod-check.s3.eu-west-1/path/to/something',
401
+ {
402
+ transformation: { quality: 75 },
403
+ },
404
+ ),
405
+ ).toBe(
406
+ 'https://sparted.imgix.fr/prod/check/path/to/something?auto=compress,format&q=75',
407
+ );
408
+ });
409
+
410
+ it('Twicpics - should handle params', () => {
411
+ const domain = 'https://sparted.twicpics.fr';
412
+ const instanceMapper: InstanceMapper = (instanceId) => `/prod/${instanceId}`;
413
+ const getCdnUrl = getCdnUrlFactory({
414
+ domain,
415
+ instanceMapper,
416
+ transformationQueries: twicpicsTransformationQueries,
417
+ });
418
+
419
+ const getCdnFromS3Url = getCdnFromS3UrlFactory({
420
+ getCdnUrl,
421
+ });
422
+
423
+ expect(
424
+ getCdnFromS3Url(
425
+ 'https://sparted-prod-check.s3.eu-west-1/path/to/something',
426
+ {
427
+ transformation: { fit: 'crop', height: 1, width: 1 },
428
+ },
429
+ ),
430
+ ).toBe(
431
+ 'https://sparted.twicpics.fr/prod/check/path/to/something?twic=v1/crop=1x1',
432
+ );
433
+
434
+ expect(
435
+ getCdnFromS3Url(
436
+ 'https://sparted-prod-check.s3.eu-west-1/path/to/something',
437
+ {
438
+ transformation: { fit: 'contain', height: 1, width: 1 },
439
+ },
440
+ ),
441
+ ).toBe(
442
+ 'https://sparted.twicpics.fr/prod/check/path/to/something?twic=v1/contain=1x1',
443
+ );
444
+
445
+ expect(
446
+ getCdnFromS3Url(
447
+ 'https://sparted-prod-check.s3.eu-west-1/path/to/something',
448
+ {
449
+ transformation: { width: 1 },
450
+ },
451
+ ),
452
+ ).toBe(
453
+ 'https://sparted.twicpics.fr/prod/check/path/to/something?twic=v1/cover=1',
454
+ );
455
+ });
456
+ });
@@ -0,0 +1,63 @@
1
+ import type { CreateCdn, TransformationQueries } from './types';
2
+ import { getCdnFromS3UrlFactory } from './utils/get-cdn-from-s3-url.util';
3
+ import { getCdnUrlFactory } from './utils/get-cdn-url.util';
4
+
5
+ export const imgixTransformationQueries: TransformationQueries = ({
6
+ fit,
7
+ devicePixelRatio,
8
+ height,
9
+ width,
10
+ quality,
11
+ }) => [
12
+ '?auto=compress,format',
13
+ fit === 'crop' ? '&fit=crop' : '',
14
+ Number.isFinite(devicePixelRatio) ? `&dpr=${devicePixelRatio}` : '',
15
+ Number.isFinite(height) ? `&h=${height}` : '',
16
+ Number.isFinite(width) ? `&w=${width}` : '',
17
+ quality !== undefined && quality >= 0 && quality <= 100
18
+ ? `&q=${quality}`
19
+ : '',
20
+ ].join('');
21
+
22
+ export const twicpicsTransformationQueries: TransformationQueries = (
23
+ params,
24
+ ) => {
25
+ if (params.fit !== undefined) {
26
+ if (params.width === undefined || params.height === undefined) {
27
+ // eslint-disable-next-line no-console
28
+ console.error('⚠️ The Twicpics api needs a width and a height.');
29
+ return '';
30
+ }
31
+ return `?twic=v1/${params.fit}=${params.width}x${params.height}`;
32
+ }
33
+ if (params.width !== undefined) {
34
+ const resizeHeight = params.height !== undefined ? `x${params.height}` : '';
35
+ return `?twic=v1/cover=${params.width}${resizeHeight}`;
36
+ }
37
+ return '';
38
+ };
39
+
40
+ /**
41
+ *
42
+ * @param type cdn provider: 'twicpics' | 'imgix'
43
+ * @param domain provider domain
44
+ * @param instanceMapper domain + the result of the instanceMapper will form the base path
45
+ * @returns \{ getCdnUrl, transformationQueries \}
46
+ */
47
+ export const createCdn: CreateCdn = ({ type, domain, instanceMapper }) => {
48
+ const getCdnUrl = getCdnUrlFactory({
49
+ domain,
50
+ instanceMapper,
51
+ transformationQueries:
52
+ type === 'imgix'
53
+ ? imgixTransformationQueries
54
+ : twicpicsTransformationQueries,
55
+ });
56
+
57
+ return {
58
+ getCdnUrl,
59
+ getCdnFromS3Url: getCdnFromS3UrlFactory({
60
+ getCdnUrl,
61
+ }),
62
+ };
63
+ };
@@ -0,0 +1,2 @@
1
+ export { createCdn } from './cdn.factory';
2
+ export * from './types';
@@ -0,0 +1,35 @@
1
+ export type BaseUrlMapperFunc = (instanceId: string) => string;
2
+
3
+ export type Transformation = {
4
+ fit?: 'crop' | 'contain';
5
+ width?: number;
6
+ height?: number;
7
+ devicePixelRatio?: number;
8
+ quality?: number;
9
+ };
10
+
11
+ export type CdnParams = {
12
+ asThumbnail?: boolean;
13
+ transformation?: Transformation;
14
+ };
15
+
16
+ export type TransformationQueries = (params: Transformation) => string;
17
+
18
+ export type GetCdnUrl = (instanceId: string, path: `/${string}`, params?: CdnParams) => string;
19
+
20
+ export type GetCdnFromS3Url = (url: string, params?: CdnParams) => string;
21
+
22
+ export type InstanceMapper = (instanceId: string) => string;
23
+
24
+ export type CdnConfig = {
25
+ type: 'twicpics' | 'imgix';
26
+ domain: `https://${string}`;
27
+ instanceMapper: InstanceMapper;
28
+ };
29
+
30
+ export type Cdn = {
31
+ getCdnUrl: GetCdnUrl;
32
+ getCdnFromS3Url: GetCdnFromS3Url;
33
+ };
34
+
35
+ export type CreateCdn = (config: CdnConfig) => Cdn;
@@ -0,0 +1 @@
1
+ export * from './cdn-factory.type';
@@ -0,0 +1,21 @@
1
+ type ExtractInfoFromUrl = (url: string) =>
2
+ | {
3
+ instanceId: string;
4
+ path: `/${string}`;
5
+ }
6
+ | undefined;
7
+
8
+ const S3_URL_RE = /https:\/\/sparted-prod-([^.]+)\.s3(\.eu-west-1)?(\.amazonaws\.com)?(\.fr-par\.scw\.cloud)?(\/[^?]+)/;
9
+
10
+ export const extractInfoFromUrl: ExtractInfoFromUrl = (url) => {
11
+ const matchS3Url = url.match(S3_URL_RE);
12
+
13
+ if (!matchS3Url) {
14
+ return undefined;
15
+ }
16
+
17
+ const instanceId = matchS3Url[1];
18
+ const path = matchS3Url[5] as `/${string}`;
19
+
20
+ return { instanceId, path };
21
+ };
@@ -0,0 +1,20 @@
1
+ import type { GetCdnFromS3Url, GetCdnUrl } from '../types';
2
+ import { extractInfoFromUrl } from './extract-info-from-url.util';
3
+
4
+ type FactoryConfig = {
5
+ getCdnUrl: GetCdnUrl;
6
+ };
7
+
8
+ export function getCdnFromS3UrlFactory(config: FactoryConfig): GetCdnFromS3Url {
9
+ return (url, params) => {
10
+ const { getCdnUrl } = config;
11
+
12
+ const info = extractInfoFromUrl(url);
13
+ if (info === undefined) {
14
+ return url;
15
+ }
16
+ const { instanceId, path } = info;
17
+
18
+ return getCdnUrl(instanceId, path, params);
19
+ };
20
+ }
@@ -0,0 +1,26 @@
1
+ import type {
2
+ GetCdnUrl,
3
+ InstanceMapper,
4
+ Transformation,
5
+ TransformationQueries,
6
+ } from '../types';
7
+
8
+ const DEFAULT_THUMBNAIL_CONFIG: Transformation = { width: 300, height: 300 };
9
+
10
+ type FactoryConfig = {
11
+ domain: `https://${string}`;
12
+ instanceMapper: InstanceMapper;
13
+ transformationQueries: TransformationQueries;
14
+ };
15
+
16
+ export const getCdnUrlFactory = (config: FactoryConfig): GetCdnUrl => (instanceId, path, params = {}) => {
17
+ const { domain, instanceMapper, transformationQueries } = config;
18
+
19
+ const {
20
+ transformation = params.asThumbnail ? DEFAULT_THUMBNAIL_CONFIG : {},
21
+ } = params;
22
+
23
+ return `${domain}${instanceMapper(
24
+ instanceId,
25
+ )}${path}${transformationQueries(transformation)}`;
26
+ };
@@ -0,0 +1,11 @@
1
+ import { formatNumber } from '../format-number';
2
+
3
+ describe('formatNumber', () => {
4
+ it('should format numbers correctly', () => {
5
+ expect(formatNumber(48)).toBe('48');
6
+ expect(formatNumber(999)).toBe('999');
7
+ expect(formatNumber(1000)).toBe('1.0k');
8
+ expect(formatNumber(42869)).toBe('42.8k');
9
+ expect(formatNumber(992169)).toBe('992.1k');
10
+ });
11
+ });
@@ -0,0 +1,14 @@
1
+ const DECIMAL_SEPARATOR = '.';
2
+ const THOUSAND_UNIT = 'k';
3
+
4
+ export const formatNumber = (n: number): string => {
5
+ const numString = String(n);
6
+ if (n < 1000) {
7
+ return numString;
8
+ }
9
+
10
+ const decimalsMinusLast = numString.slice(0, numString.length - 3);
11
+ const lastDecimal = numString.slice(numString.length - 3, numString.length - 2);
12
+
13
+ return `${decimalsMinusLast}${DECIMAL_SEPARATOR}${lastDecimal}${THOUSAND_UNIT}`;
14
+ };
@@ -0,0 +1 @@
1
+ export * from './format-number';
@@ -0,0 +1,29 @@
1
+ import { runWhile } from '../run-while';
2
+
3
+ describe('runWhile', () => {
4
+ it('should run while the predicate is true and return the right interation count', () => {
5
+ const { result, iterationCount } = runWhile({
6
+ predicate: (counter) => counter <= 9,
7
+ callback: (counter) => counter + 1,
8
+ initialValue: 0,
9
+ });
10
+
11
+ expect(result).toStrictEqual(10);
12
+ expect(iterationCount).toStrictEqual(10);
13
+ });
14
+
15
+ it('should throw an error when the iteration limit is reached', () => {
16
+ const predicateMock = jest.fn().mockReturnValue(true);
17
+
18
+ const { iterationCount } = runWhile({
19
+ predicate: predicateMock,
20
+ callback: (counter) => counter + 1,
21
+ initialValue: 0,
22
+ iterationLimit: 89,
23
+ });
24
+
25
+ // Called once outside the while loop
26
+ expect(predicateMock).toHaveBeenCalledTimes(90);
27
+ expect(iterationCount).toStrictEqual(89);
28
+ });
29
+ });
@@ -0,0 +1 @@
1
+ export * from './run-while';