@ssa-ui-kit/utils 0.0.1-alpha

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 (70) hide show
  1. package/LICENSE +21 -0
  2. package/dist/index.d.ts +8 -0
  3. package/dist/index.js +2 -0
  4. package/dist/index.js.map +1 -0
  5. package/dist/utils/CallAll.d.ts +1 -0
  6. package/dist/utils/dates/dateFormatters.d.ts +5 -0
  7. package/dist/utils/dates/dates.spec.d.ts +1 -0
  8. package/dist/utils/dates/index.d.ts +1 -0
  9. package/dist/utils/debounce/debounce.d.ts +3 -0
  10. package/dist/utils/debounce/debounce.spec.d.ts +1 -0
  11. package/dist/utils/debounce/index.d.ts +1 -0
  12. package/dist/utils/objects/assocPath.d.ts +1 -0
  13. package/dist/utils/objects/assocPath.spec.d.ts +1 -0
  14. package/dist/utils/objects/dissocPath.d.ts +1 -0
  15. package/dist/utils/objects/dissocPath.spec.d.ts +1 -0
  16. package/dist/utils/objects/index.d.ts +7 -0
  17. package/dist/utils/objects/mapObjIndexed.d.ts +3 -0
  18. package/dist/utils/objects/mapObjIndexed.spec.d.ts +1 -0
  19. package/dist/utils/objects/path.d.ts +1 -0
  20. package/dist/utils/objects/path.spec.d.ts +1 -0
  21. package/dist/utils/objects/pathOr.d.ts +1 -0
  22. package/dist/utils/objects/pathOr.spec.d.ts +1 -0
  23. package/dist/utils/objects/prop.d.ts +1 -0
  24. package/dist/utils/objects/prop.spec.d.ts +1 -0
  25. package/dist/utils/objects/propOr.d.ts +1 -0
  26. package/dist/utils/objects/propOr.spec.d.ts +1 -0
  27. package/dist/utils/pagination/generateRange.d.ts +3 -0
  28. package/dist/utils/pagination/generateRange.spec.d.ts +1 -0
  29. package/dist/utils/pagination/index.d.ts +1 -0
  30. package/dist/utils/pagination/types.d.ts +1 -0
  31. package/dist/utils/throttle/index.d.ts +1 -0
  32. package/dist/utils/throttle/throttle.d.ts +4 -0
  33. package/dist/utils/throttle/throttle.spec.d.ts +1 -0
  34. package/dist/utils/types.d.ts +4 -0
  35. package/package.json +40 -0
  36. package/src/index.ts +8 -0
  37. package/src/utils/CallAll.ts +5 -0
  38. package/src/utils/dates/dateFormatters.ts +61 -0
  39. package/src/utils/dates/dates.spec.tsx +127 -0
  40. package/src/utils/dates/index.ts +1 -0
  41. package/src/utils/debounce/debounce.spec.ts +85 -0
  42. package/src/utils/debounce/debounce.ts +24 -0
  43. package/src/utils/debounce/index.ts +1 -0
  44. package/src/utils/objects/assocPath.spec.tsx +53 -0
  45. package/src/utils/objects/assocPath.ts +11 -0
  46. package/src/utils/objects/dissocPath.spec.tsx +49 -0
  47. package/src/utils/objects/dissocPath.ts +14 -0
  48. package/src/utils/objects/index.ts +7 -0
  49. package/src/utils/objects/mapObjIndexed.spec.tsx +33 -0
  50. package/src/utils/objects/mapObjIndexed.ts +15 -0
  51. package/src/utils/objects/path.spec.tsx +24 -0
  52. package/src/utils/objects/path.ts +4 -0
  53. package/src/utils/objects/pathOr.spec.tsx +23 -0
  54. package/src/utils/objects/pathOr.ts +11 -0
  55. package/src/utils/objects/prop.spec.tsx +17 -0
  56. package/src/utils/objects/prop.ts +4 -0
  57. package/src/utils/objects/propOr.spec.tsx +17 -0
  58. package/src/utils/objects/propOr.ts +11 -0
  59. package/src/utils/pagination/generateRange.spec.ts +100 -0
  60. package/src/utils/pagination/generateRange.ts +91 -0
  61. package/src/utils/pagination/index.ts +1 -0
  62. package/src/utils/pagination/types.ts +4 -0
  63. package/src/utils/throttle/index.ts +1 -0
  64. package/src/utils/throttle/throttle.spec.ts +56 -0
  65. package/src/utils/throttle/throttle.ts +43 -0
  66. package/src/utils/types.ts +4 -0
  67. package/tsbuildcache +1 -0
  68. package/tsconfig.build.json +35 -0
  69. package/tsconfig.json +17 -0
  70. package/webpack.config.js +16 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 SSA Group
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,8 @@
1
+ export { callAll } from './utils/CallAll';
2
+ export { throttle } from './utils/throttle';
3
+ export * from './utils/debounce';
4
+ export * as dateFormatters from './utils/dates/dateFormatters';
5
+ export { mapObjIndexed } from './utils/objects/mapObjIndexed';
6
+ export { generateRange } from './utils/pagination';
7
+ export * from './utils/objects';
8
+ export * from './utils/types';
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.SSAUtils=t():e.SSAUtils=t()}(self,(function(){return function(){"use strict";var e={d:function(t,n){for(var r in n)e.o(n,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:n[r]})},o:function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r:function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{assocPath:function(){return m},callAll:function(){return r},dateFormatters:function(){return n},debounce:function(){return u},dissocPath:function(){return S},generateRange:function(){return g},mapObjIndexed:function(){return h},path:function(){return y},pathOr:function(){return b},prop:function(){return w},propOr:function(){return T},throttle:function(){return o}});var n={};e.r(n),e.d(n,{formatDate:function(){return l},formatDayOfWeek:function(){return f},formatTime:function(){return a},getTimeAgo:function(){return d},printDayOfTheWeek:function(){return s}});const r=(...e)=>(...t)=>e.forEach((e=>e?.(...t))),o=(e,t)=>{let n=!1,r=null,o=null;return[function u(...i){n?r=i:(n=!0,e(...i),o=setTimeout((()=>{n=!1,r&&(u(...r),r=null)}),t))},function(){o&&clearTimeout(o)}]},u=(e,t=200)=>{let n=null;return[(...r)=>{n&&clearTimeout(n),n=setTimeout((()=>{n&&clearTimeout(n),e(...r)}),t)},function(){n&&clearTimeout(n)}]},i=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],c={year:31536e3,month:2592e3,week:604800,day:86400,hour:3600,min:60},a=e=>new Date(e).toLocaleTimeString("en-US",{hour:"2-digit",minute:"2-digit"}),f=e=>i[new Date(e).getDay()],l=e=>new Date(e).toLocaleDateString("en-US",{day:"2-digit",month:"short"}),s=e=>{switch(e){case 0:return"Sun";case 1:return"Mon";case 2:return"Tue";case 3:return"Wed";case 4:return"Thu";case 5:return"Fri";case 6:return"Sat"}},d=e=>{const t=new Date(e).getTime();if(Number.isNaN(t))throw new Error("Invalid date");const n=Math.floor((Date.now()-t)/1e3);let r;for(const e in c)if(r=Math.floor(n/c[e]),r>=1){return`${r} ${e}${r>1?"s":""} ago`}return"Just Now"},h=(e,t)=>Object.keys(t).reduce(((n,r)=>(n[r]=e(t[r],r,t),n)),{}),p=(e,t,n)=>{for(let r=t;r<n;++r)e.push(r)};var g=(e,t)=>{if(null==e||!Number.isInteger(e))throw new Error("Pages count should be an integer");if(e<=0)return[];let n=[1];if(1===e)return n;if(null!=t&&!Number.isInteger(t))throw new Error("Selected page should be an integer");if(null!=t&&(t<1||t>e))throw new Error(`Selected page ${t} is out of range`);if(t&&t>2){const r=((e,t)=>{const n=[];return t!==e&&n.push(t),t>1&&n.unshift(t-1),t+1<e&&n.push(t+1),n})(e,t),[o,,u]=r;o-2>1?n.push(-1):p(n,2,o),n=n.concat(r),e-u>2?n.push(-1):p(n,u+1,e)}else e<=5?p(n,2,e):n.push(2,3,-1);return n.push(e),n};const m=([e,...t],n)=>r=>JSON.parse(JSON.stringify({...r,[e]:t.length?m(t,n)(r[e]):n})),S=e=>t=>{const n=JSON.parse(JSON.stringify(t));return e.reduce(((t,n,r)=>(r===e.length-1&&delete t[n],t[n])),n),n},y=e=>t=>e.reduce(((e,t)=>e?.[t]),t),b=(e,t)=>n=>{const r=y(t)(n);return null==r?e:r},w=e=>t=>t?.[e],T=(e,t)=>n=>{const r=w(t)(n);return null==r?e:r};return t}()}));
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","mappings":"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","sources":["webpack://SSAUtils/webpack/universalModuleDefinition","webpack://SSAUtils/webpack/bootstrap","webpack://SSAUtils/webpack/runtime/define property getters","webpack://SSAUtils/webpack/runtime/hasOwnProperty shorthand","webpack://SSAUtils/webpack/runtime/make namespace object","webpack://SSAUtils/./src/utils/CallAll.ts","webpack://SSAUtils/./src/utils/throttle/throttle.ts","webpack://SSAUtils/./src/utils/debounce/debounce.ts","webpack://SSAUtils/./src/utils/dates/dateFormatters.ts","webpack://SSAUtils/./src/utils/objects/mapObjIndexed.ts","webpack://SSAUtils/./src/utils/pagination/generateRange.ts","webpack://SSAUtils/./src/utils/objects/assocPath.ts","webpack://SSAUtils/./src/utils/objects/dissocPath.ts","webpack://SSAUtils/./src/utils/objects/path.ts","webpack://SSAUtils/./src/utils/objects/pathOr.ts","webpack://SSAUtils/./src/utils/objects/prop.ts","webpack://SSAUtils/./src/utils/objects/propOr.ts"],"sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"SSAUtils\"] = factory();\n\telse\n\t\troot[\"SSAUtils\"] = factory();\n})(self, function() {\nreturn ","// The require scope\nvar __webpack_require__ = {};\n\n","// define getter functions for harmony exports\n__webpack_require__.d = function(exports, definition) {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }","// define __esModule on exports\n__webpack_require__.r = function(exports) {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","/* eslint-disable @typescript-eslint/no-explicit-any */\nexport const callAll =\n (...fns: any[]) =>\n (...args: any) =>\n fns.forEach((fn) => fn?.(...args));\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype UnknownFn = (...args: any[]) => unknown;\ntype ThrottleFn = (\n fn: UnknownFn,\n delayMs: number,\n) => [(...args: unknown[]) => void, () => void];\n/* eslint-enable @typescript-eslint/no-explicit-any */\n\nexport const throttle: ThrottleFn = (fn, delayMs) => {\n let isThrottled = false;\n let savedArgs: unknown[] | null = null;\n let timeoutId: ReturnType<typeof setTimeout> | null = null;\n\n function throttledFn(...args: unknown[]) {\n if (isThrottled) {\n savedArgs = args;\n return;\n }\n\n isThrottled = true;\n\n fn(...args);\n\n timeoutId = setTimeout(() => {\n isThrottled = false;\n\n // istanbul ignore else\n if (savedArgs) {\n throttledFn(...savedArgs);\n savedArgs = null;\n }\n }, delayMs);\n }\n\n return [\n throttledFn,\n function cancel() {\n if (timeoutId) {\n clearTimeout(timeoutId);\n }\n },\n ];\n};\n","type UnknownFn = (...args: any[]) => unknown;\n\nexport const debounce = (func: UnknownFn, wait = 200) => {\n let timeoutId: NodeJS.Timeout | null = null;\n const executedFunction = (...args: any[]) => {\n const postponedFn = () => {\n if (timeoutId) {\n clearTimeout(timeoutId);\n }\n func(...args);\n };\n if (timeoutId) {\n clearTimeout(timeoutId);\n }\n timeoutId = setTimeout(postponedFn, wait);\n };\n\n const cancel = function () {\n if (timeoutId) {\n clearTimeout(timeoutId);\n }\n };\n return [executedFunction, cancel];\n};\n","const dayOfWeekNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];\nconst periodValues: Record<string, number> = {\n year: 31536000,\n month: 2592000,\n week: 604800,\n day: 86400,\n hour: 3600,\n min: 60,\n};\n\nexport const formatTime = (timestampMs: number) =>\n new Date(timestampMs).toLocaleTimeString('en-US', {\n hour: '2-digit',\n minute: '2-digit',\n });\n\nexport const formatDayOfWeek = (timestampMs: number) =>\n dayOfWeekNames[new Date(timestampMs).getDay()];\n\nexport const formatDate = (timestampMs: number) =>\n new Date(timestampMs).toLocaleDateString('en-US', {\n day: '2-digit',\n month: 'short',\n });\n\nexport const printDayOfTheWeek = (day: number) => {\n switch (day) {\n case 0:\n return 'Sun';\n case 1:\n return 'Mon';\n case 2:\n return 'Tue';\n case 3:\n return 'Wed';\n case 4:\n return 'Thu';\n case 5:\n return 'Fri';\n case 6:\n return 'Sat';\n }\n};\n\nexport const getTimeAgo = (timeValue: string | number) => {\n const date = new Date(timeValue).getTime();\n if (Number.isNaN(date)) {\n throw new Error('Invalid date');\n }\n const diff = Math.floor((Date.now() - date) / 1000);\n let interval;\n for (const key in periodValues) {\n interval = Math.floor(diff / periodValues[key]);\n if (interval >= 1) {\n const pluralValue = interval > 1 ? 's' : '';\n return `${interval} ${key}${pluralValue} ago`;\n }\n }\n\n return 'Just Now';\n};\n","type MapObjIndexedFn<T, U> = (\n value: T,\n key: string,\n obj: Record<string, T>,\n) => U;\n\nexport const mapObjIndexed = <T, U>(\n fn: MapObjIndexedFn<T, U>,\n obj: Record<string, T>,\n): Record<string, U> => {\n return Object.keys(obj).reduce((result: Record<string, U>, key: string) => {\n result[key] = fn(obj[key], key, obj);\n return result;\n }, {});\n};\n","import { GenerateRangeFn } from './types';\n\n/**\n * The function that returns an array of page numbers to show in the pagination\n * component.\n *\n * Rules:\n * - To always show the 1st and the last page.\n * - To show one item before and one item after the selected page.\n * - To return \"-1\" for the skipped items. This is to be able to display \"...\" in\n * the pagination component.\n * */\nconst SKIPPED_ITEMS_DELTA = 2;\n\nconst getSelectedRange = (pagesCount: number, selectedPage: number) => {\n const range: number[] = [];\n\n if (selectedPage !== pagesCount) {\n range.push(selectedPage);\n }\n\n if (selectedPage > 1) {\n range.unshift(selectedPage - 1);\n }\n\n if (selectedPage + 1 < pagesCount) {\n range.push(selectedPage + 1);\n }\n\n return range;\n};\n\nconst fill = (range: number[], minValue: number, maxValue: number) => {\n for (let i = minValue; i < maxValue; ++i) {\n range.push(i);\n }\n};\n\nconst generateRange: GenerateRangeFn = (pagesCount, selectedPage) => {\n if (pagesCount == null || !Number.isInteger(pagesCount)) {\n throw new Error('Pages count should be an integer');\n }\n\n if (pagesCount <= 0) {\n return [];\n }\n\n let range = [1];\n\n if (pagesCount === 1) {\n return range;\n }\n\n if (selectedPage != null && !Number.isInteger(selectedPage)) {\n throw new Error('Selected page should be an integer');\n }\n\n if (selectedPage != null && (selectedPage < 1 || selectedPage > pagesCount)) {\n throw new Error(`Selected page ${selectedPage} is out of range`);\n }\n\n if (selectedPage && selectedPage > 2) {\n const selectedRange = getSelectedRange(pagesCount, selectedPage);\n\n const [minSelectedRange, , maxSelectedRange] = selectedRange;\n\n if (minSelectedRange - SKIPPED_ITEMS_DELTA > 1) {\n range.push(-1);\n } else {\n fill(range, 2, minSelectedRange);\n }\n\n range = range.concat(selectedRange);\n\n if (pagesCount - maxSelectedRange > SKIPPED_ITEMS_DELTA) {\n range.push(-1);\n } else {\n fill(range, maxSelectedRange + 1, pagesCount);\n }\n } else if (pagesCount <= 5) {\n fill(range, 2, pagesCount);\n } else {\n range.push(2, 3, -1);\n }\n\n range.push(pagesCount);\n\n return range;\n};\n\nexport default generateRange;\n","export const assocPath =\n <T>([first, ...rest]: string[], value: any) =>\n (sourceObject: T): T =>\n JSON.parse(\n JSON.stringify({\n ...sourceObject,\n [first]: rest.length\n ? assocPath(rest, value)((sourceObject as any)[first])\n : value,\n }),\n ) as T;\n","export const dissocPath =\n <T>(path: string[]) =>\n (sourceObject: T): T => {\n const resultObject = JSON.parse(JSON.stringify(sourceObject));\n\n path.reduce((acc: any, key, index) => {\n if (index === path.length - 1) {\n delete acc[key];\n }\n return acc[key];\n }, resultObject);\n\n return resultObject;\n };\n","export const path =\n <T extends Record<string | number, any>, R>(path: string[]) =>\n (obj: T): unknown =>\n path.reduce((prev, curr: string | number) => prev?.[curr], obj);\n","import { path as originalPath } from './path';\n\nexport const pathOr =\n <T extends Record<string | number, any>, R>(\n defaultValue: any,\n path: string[],\n ) =>\n (obj: T): R => {\n const result = originalPath(path)(obj);\n return result === null || result === undefined ? defaultValue : result;\n };\n","export const prop =\n <T extends Record<string | number, any>, R = any>(propName: string) =>\n (obj: T): R =>\n obj?.[propName];\n","import { prop } from './prop';\n\nexport const propOr =\n <T extends Record<string | number, any>, R = any>(\n defaultValue: any,\n propName: string,\n ) =>\n (obj: T): R => {\n const result = prop(propName)(obj);\n return result === null || result === undefined ? defaultValue : result;\n };\n"],"names":["root","factory","exports","module","define","amd","self","__webpack_require__","definition","key","o","Object","defineProperty","enumerable","get","obj","prop","prototype","hasOwnProperty","call","Symbol","toStringTag","value","callAll","fns","args","forEach","fn","throttle","delayMs","isThrottled","savedArgs","timeoutId","throttledFn","setTimeout","clearTimeout","debounce","func","wait","executedFunction","postponedFn","dayOfWeekNames","periodValues","year","month","week","day","hour","min","formatTime","timestampMs","Date","toLocaleTimeString","minute","formatDayOfWeek","getDay","formatDate","toLocaleDateString","printDayOfTheWeek","getTimeAgo","timeValue","date","getTime","Number","isNaN","Error","diff","Math","floor","now","interval","mapObjIndexed","keys","reduce","result","fill","range","minValue","maxValue","i","push","generateRange","pagesCount","selectedPage","isInteger","selectedRange","getSelectedRange","unshift","minSelectedRange","maxSelectedRange","concat","assocPath","first","rest","sourceObject","JSON","parse","stringify","length","dissocPath","path","resultObject","acc","index","prev","curr","pathOr","defaultValue","originalPath","propName","propOr"],"sourceRoot":""}
@@ -0,0 +1 @@
1
+ export declare const callAll: (...fns: any[]) => (...args: any) => void;
@@ -0,0 +1,5 @@
1
+ export declare const formatTime: (timestampMs: number) => string;
2
+ export declare const formatDayOfWeek: (timestampMs: number) => string;
3
+ export declare const formatDate: (timestampMs: number) => string;
4
+ export declare const printDayOfTheWeek: (day: number) => "Sun" | "Mon" | "Tue" | "Wed" | "Thu" | "Fri" | "Sat" | undefined;
5
+ export declare const getTimeAgo: (timeValue: string | number) => string;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export * as dateFormatters from './dateFormatters';
@@ -0,0 +1,3 @@
1
+ type UnknownFn = (...args: any[]) => unknown;
2
+ export declare const debounce: (func: UnknownFn, wait?: number) => ((...args: any[]) => void)[];
3
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export { debounce } from './debounce';
@@ -0,0 +1 @@
1
+ export declare const assocPath: <T>([first, ...rest]: string[], value: any) => (sourceObject: T) => T;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export declare const dissocPath: <T>(path: string[]) => (sourceObject: T) => T;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ export * from './mapObjIndexed';
2
+ export * from './assocPath';
3
+ export * from './dissocPath';
4
+ export * from './path';
5
+ export * from './pathOr';
6
+ export * from './prop';
7
+ export * from './propOr';
@@ -0,0 +1,3 @@
1
+ type MapObjIndexedFn<T, U> = (value: T, key: string, obj: Record<string, T>) => U;
2
+ export declare const mapObjIndexed: <T, U>(fn: MapObjIndexedFn<T, U>, obj: Record<string, T>) => Record<string, U>;
3
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export declare const path: <T extends Record<string | number, any>, R>(path: string[]) => (obj: T) => unknown;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export declare const pathOr: <T extends Record<string | number, any>, R>(defaultValue: any, path: string[]) => (obj: T) => R;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export declare const prop: <T extends Record<string | number, any>, R = any>(propName: string) => (obj: T) => R;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export declare const propOr: <T extends Record<string | number, any>, R = any>(defaultValue: any, propName: string) => (obj: T) => R;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import { GenerateRangeFn } from './types';
2
+ declare const generateRange: GenerateRangeFn;
3
+ export default generateRange;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export { default as generateRange } from './generateRange';
@@ -0,0 +1 @@
1
+ export type GenerateRangeFn = (pagesCount: number, selectedPage?: number) => Array<number>;
@@ -0,0 +1 @@
1
+ export { throttle } from './throttle';
@@ -0,0 +1,4 @@
1
+ type UnknownFn = (...args: any[]) => unknown;
2
+ type ThrottleFn = (fn: UnknownFn, delayMs: number) => [(...args: unknown[]) => void, () => void];
3
+ export declare const throttle: ThrottleFn;
4
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ export type PathValue = string | number | boolean | null;
2
+ export interface PathObject {
3
+ [key: string | number]: PathValue | PathObject;
4
+ }
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@ssa-ui-kit/utils",
3
+ "version": "0.0.1-alpha",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "private": false,
7
+ "sideEffects": false,
8
+ "author": "SSA Group",
9
+ "license": "MIT",
10
+ "description": "SSA UI Kit utilities",
11
+ "keywords": [
12
+ "react",
13
+ "SSA UI Kit",
14
+ "utils"
15
+ ],
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/ssagroup/ui-kit.git",
19
+ "directory": "packages/utils"
20
+ },
21
+ "devDependencies": {
22
+ "@testing-library/jest-dom": "^5.16.5",
23
+ "extend-expect": "^1.0.2",
24
+ "resolve-tspaths": "^0.8.16",
25
+ "terser-webpack-plugin": "^5.3.9",
26
+ "webpack": "^5.85.0"
27
+ },
28
+ "browserslist": [
29
+ ">0.1%",
30
+ "not dead",
31
+ "not op_mini all"
32
+ ],
33
+ "scripts": {
34
+ "build": "webpack --mode=production --node-env=production && tsc --build --force ./tsconfig.build.json && resolve-tspaths -p ./tsconfig.build.json",
35
+ "ts-show-config": "tsc --showConfig -p ./tsconfig.json",
36
+ "ts-show-config-build": "tsc --showConfig -p ./tsconfig.build.json",
37
+ "ts-check": "tsc -p ./tsconfig.json",
38
+ "ts-check-build": "tsc -p ./tsconfig.build.json"
39
+ }
40
+ }
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ export { callAll } from './utils/CallAll';
2
+ export { throttle } from './utils/throttle';
3
+ export * from './utils/debounce';
4
+ export * as dateFormatters from './utils/dates/dateFormatters';
5
+ export { mapObjIndexed } from './utils/objects/mapObjIndexed';
6
+ export { generateRange } from './utils/pagination';
7
+ export * from './utils/objects';
8
+ export * from './utils/types';
@@ -0,0 +1,5 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ export const callAll =
3
+ (...fns: any[]) =>
4
+ (...args: any) =>
5
+ fns.forEach((fn) => fn?.(...args));
@@ -0,0 +1,61 @@
1
+ const dayOfWeekNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
2
+ const periodValues: Record<string, number> = {
3
+ year: 31536000,
4
+ month: 2592000,
5
+ week: 604800,
6
+ day: 86400,
7
+ hour: 3600,
8
+ min: 60,
9
+ };
10
+
11
+ export const formatTime = (timestampMs: number) =>
12
+ new Date(timestampMs).toLocaleTimeString('en-US', {
13
+ hour: '2-digit',
14
+ minute: '2-digit',
15
+ });
16
+
17
+ export const formatDayOfWeek = (timestampMs: number) =>
18
+ dayOfWeekNames[new Date(timestampMs).getDay()];
19
+
20
+ export const formatDate = (timestampMs: number) =>
21
+ new Date(timestampMs).toLocaleDateString('en-US', {
22
+ day: '2-digit',
23
+ month: 'short',
24
+ });
25
+
26
+ export const printDayOfTheWeek = (day: number) => {
27
+ switch (day) {
28
+ case 0:
29
+ return 'Sun';
30
+ case 1:
31
+ return 'Mon';
32
+ case 2:
33
+ return 'Tue';
34
+ case 3:
35
+ return 'Wed';
36
+ case 4:
37
+ return 'Thu';
38
+ case 5:
39
+ return 'Fri';
40
+ case 6:
41
+ return 'Sat';
42
+ }
43
+ };
44
+
45
+ export const getTimeAgo = (timeValue: string | number) => {
46
+ const date = new Date(timeValue).getTime();
47
+ if (Number.isNaN(date)) {
48
+ throw new Error('Invalid date');
49
+ }
50
+ const diff = Math.floor((Date.now() - date) / 1000);
51
+ let interval;
52
+ for (const key in periodValues) {
53
+ interval = Math.floor(diff / periodValues[key]);
54
+ if (interval >= 1) {
55
+ const pluralValue = interval > 1 ? 's' : '';
56
+ return `${interval} ${key}${pluralValue} ago`;
57
+ }
58
+ }
59
+
60
+ return 'Just Now';
61
+ };
@@ -0,0 +1,127 @@
1
+ import { dateFormatters } from './index';
2
+
3
+ const { getTimeAgo } = dateFormatters;
4
+ const date = Date.now();
5
+ const timeValuesArr = [
6
+ {
7
+ input: date - 6000,
8
+ output: 'Just Now',
9
+ },
10
+ {
11
+ input: date - 60000,
12
+ output: '1 min ago',
13
+ },
14
+ {
15
+ input: date - 120000,
16
+ output: '2 mins ago',
17
+ },
18
+ {
19
+ input: date - 6000000,
20
+ output: '1 hour ago',
21
+ },
22
+ {
23
+ input: date - 8000000,
24
+ output: '2 hours ago',
25
+ },
26
+ {
27
+ input: date - 120000000,
28
+ output: '1 day ago',
29
+ },
30
+ {
31
+ input: date - 240000000,
32
+ output: '2 days ago',
33
+ },
34
+ {
35
+ input: date - 1200000000,
36
+ output: '1 week ago',
37
+ },
38
+ {
39
+ input: date - 1400000000,
40
+ output: '2 weeks ago',
41
+ },
42
+ {
43
+ input: date - 3000000000,
44
+ output: '1 month ago',
45
+ },
46
+ {
47
+ input: date - 6000000000,
48
+ output: '2 months ago',
49
+ },
50
+ {
51
+ input: date - 36000000000,
52
+ output: '1 year ago',
53
+ },
54
+ {
55
+ input: date - 64000000000,
56
+ output: '2 years ago',
57
+ },
58
+ ];
59
+
60
+ describe('dates', () => {
61
+ describe('dateFormatters', () => {
62
+ const testDateMs = 1681228541711; // 2023-04-11 16:55 GMT+0
63
+
64
+ describe('formatTime()', () => {
65
+ it('formats epoch ms as a time string', () => {
66
+ const result = dateFormatters.formatTime(testDateMs);
67
+ expect(result).toEqual('04:55 PM');
68
+ });
69
+ });
70
+
71
+ describe('formatDayOfWeek()', () => {
72
+ it('formats epoch ms as a day of week string', () => {
73
+ const result = dateFormatters.formatDayOfWeek(testDateMs);
74
+ expect(result).toEqual('Tue');
75
+ });
76
+ });
77
+
78
+ describe('formatDate()', () => {
79
+ it('formats epoch ms as a date string', () => {
80
+ const result = dateFormatters.formatDate(testDateMs);
81
+ expect(result).toEqual('Apr 11');
82
+ });
83
+ });
84
+
85
+ describe('printDayOfTheWeek()', () => {
86
+ it('translate getDay() code to string', () => {
87
+ const sunday = dateFormatters.printDayOfTheWeek(0);
88
+ expect(sunday).toEqual('Sun');
89
+
90
+ const monday = dateFormatters.printDayOfTheWeek(1);
91
+ expect(monday).toEqual('Mon');
92
+
93
+ const tuesday = dateFormatters.printDayOfTheWeek(2);
94
+ expect(tuesday).toEqual('Tue');
95
+
96
+ const wednesday = dateFormatters.printDayOfTheWeek(3);
97
+ expect(wednesday).toEqual('Wed');
98
+
99
+ const thursday = dateFormatters.printDayOfTheWeek(4);
100
+ expect(thursday).toEqual('Thu');
101
+
102
+ const friday = dateFormatters.printDayOfTheWeek(5);
103
+ expect(friday).toEqual('Fri');
104
+
105
+ const saturday = dateFormatters.printDayOfTheWeek(6);
106
+ expect(saturday).toEqual('Sat');
107
+ });
108
+ });
109
+
110
+ describe('getTimeAgo()', () => {
111
+ it('Returns an error when passing an invalid time value', () => {
112
+ expect(() => getTimeAgo('Date')).toThrow(new Error('Invalid date'));
113
+ expect(() => getTimeAgo('20-20-23')).toThrow(new Error('Invalid date'));
114
+ expect(() => getTimeAgo('1618301456781')).toThrow(
115
+ new Error('Invalid date'),
116
+ );
117
+ });
118
+
119
+ timeValuesArr.forEach((item) => {
120
+ const periodName = item.output.replace(/[0-9] | ago/g, '');
121
+ it(`Returns the "${periodName}"`, () => {
122
+ expect(getTimeAgo(item.input)).toEqual(item.output);
123
+ });
124
+ });
125
+ });
126
+ });
127
+ });
@@ -0,0 +1 @@
1
+ export * as dateFormatters from './dateFormatters';
@@ -0,0 +1,85 @@
1
+ import { debounce } from '.';
2
+
3
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
4
+ const callNTimes = (fn: any, n: number) => {
5
+ for (let i = 0; i < n; i++) {
6
+ fn();
7
+ }
8
+ };
9
+
10
+ describe('debounce', () => {
11
+ beforeEach(() => {
12
+ jest.useFakeTimers();
13
+ jest.spyOn(global, 'setTimeout');
14
+ jest.spyOn(global, 'clearTimeout');
15
+ });
16
+
17
+ afterAll(() => {
18
+ jest.useRealTimers();
19
+ jest.restoreAllMocks();
20
+ });
21
+
22
+ it('should cancel immediately without executing', () => {
23
+ const fn = jest.fn();
24
+ const [debouncedFn, cancel] = debounce(fn, 100);
25
+
26
+ debouncedFn();
27
+ // Cancel should not execute any pending call
28
+ cancel();
29
+ jest.advanceTimersByTime(150);
30
+ expect(fn).toHaveBeenCalledTimes(0);
31
+ });
32
+
33
+ it('should execute deferred function on the second time, after cancelling for the first time', () => {
34
+ const fn = jest.fn();
35
+ const [debouncedFn, cancel] = debounce(fn, 100);
36
+
37
+ debouncedFn();
38
+ // Cancel should not execute any pending call
39
+ cancel();
40
+ jest.advanceTimersByTime(150);
41
+ expect(fn).toHaveBeenCalledTimes(0);
42
+
43
+ debouncedFn();
44
+ jest.advanceTimersByTime(150);
45
+ expect(fn).toHaveBeenCalledTimes(1);
46
+ });
47
+
48
+ it('delays a function call', () => {
49
+ const fn = jest.fn();
50
+ const [debouncedFn, cancel] = debounce(fn, 100);
51
+
52
+ // Subsequent calls within the throttle window should be throttled
53
+ callNTimes(debouncedFn, 3);
54
+ jest.advanceTimersByTime(50);
55
+ expect(fn).toHaveBeenCalledTimes(0);
56
+ jest.advanceTimersByTime(500);
57
+ expect(fn).toHaveBeenCalledTimes(1);
58
+
59
+ // After the throttle window, the next call should execute
60
+ debouncedFn();
61
+
62
+ // Wait for the throttle window to pass
63
+ jest.advanceTimersByTime(500);
64
+ expect(fn).toHaveBeenCalledTimes(2);
65
+
66
+ // Calling the throttled function after cancel should execute
67
+ debouncedFn();
68
+ // ...but cancelled by triggering the cancel function
69
+ cancel();
70
+ expect(fn).toHaveBeenCalledTimes(2);
71
+ });
72
+
73
+ it('should debounce and execute the last call within debounce window', () => {
74
+ const fn = jest.fn();
75
+ const [debouncedFn] = debounce(fn, 100);
76
+
77
+ callNTimes(debouncedFn, 3);
78
+ jest.advanceTimersByTime(100);
79
+ expect(fn).toHaveBeenCalledTimes(1);
80
+
81
+ debouncedFn();
82
+ jest.advanceTimersByTime(150);
83
+ expect(fn).toHaveBeenCalledTimes(2);
84
+ });
85
+ });
@@ -0,0 +1,24 @@
1
+ type UnknownFn = (...args: any[]) => unknown;
2
+
3
+ export const debounce = (func: UnknownFn, wait = 200) => {
4
+ let timeoutId: NodeJS.Timeout | null = null;
5
+ const executedFunction = (...args: any[]) => {
6
+ const postponedFn = () => {
7
+ if (timeoutId) {
8
+ clearTimeout(timeoutId);
9
+ }
10
+ func(...args);
11
+ };
12
+ if (timeoutId) {
13
+ clearTimeout(timeoutId);
14
+ }
15
+ timeoutId = setTimeout(postponedFn, wait);
16
+ };
17
+
18
+ const cancel = function () {
19
+ if (timeoutId) {
20
+ clearTimeout(timeoutId);
21
+ }
22
+ };
23
+ return [executedFunction, cancel];
24
+ };
@@ -0,0 +1 @@
1
+ export { debounce } from './debounce';
@@ -0,0 +1,53 @@
1
+ import { assocPath } from '.';
2
+
3
+ describe('utils => objects => assocPath', () => {
4
+ it('assocPath should work', () => {
5
+ const data = {
6
+ a: {
7
+ name: 'a',
8
+ value: 1,
9
+ },
10
+ b: {
11
+ name: 'b',
12
+ value: 2,
13
+ },
14
+ };
15
+ const result = assocPath(['c', 'name'], 'c')(data);
16
+ expect(result).toEqual({
17
+ a: {
18
+ name: 'a',
19
+ value: 1,
20
+ },
21
+ b: {
22
+ name: 'b',
23
+ value: 2,
24
+ },
25
+ c: {
26
+ name: 'c',
27
+ },
28
+ });
29
+ });
30
+ it('assocPath should not change source object', () => {
31
+ const data = {
32
+ a: {
33
+ name: 'a',
34
+ value: 1,
35
+ },
36
+ b: {
37
+ name: 'b',
38
+ value: 2,
39
+ },
40
+ };
41
+ assocPath(['c', 'name'], 'c')(data);
42
+ expect(data).toEqual({
43
+ a: {
44
+ name: 'a',
45
+ value: 1,
46
+ },
47
+ b: {
48
+ name: 'b',
49
+ value: 2,
50
+ },
51
+ });
52
+ });
53
+ });
@@ -0,0 +1,11 @@
1
+ export const assocPath =
2
+ <T>([first, ...rest]: string[], value: any) =>
3
+ (sourceObject: T): T =>
4
+ JSON.parse(
5
+ JSON.stringify({
6
+ ...sourceObject,
7
+ [first]: rest.length
8
+ ? assocPath(rest, value)((sourceObject as any)[first])
9
+ : value,
10
+ }),
11
+ ) as T;