@cerberus-design/react
Version:
The Cerberus Design React component library.
1 lines • 3.25 kB
Source Map (JSON)
{"version":3,"sources":["../../../src/utils/index.ts","../../../src/utils/localStorage.ts"],"sourcesContent":["/**\n * This module contains utility functions that are used across your app.\n * @module Utils\n */\n\n/**\n * Formats the count of notifications to be displayed in the notification badge.\n * @param count - The number of notifications.\n * @returns The formatted count of notifications.\n * @example\n * ```tsx\n * const count = formatNotifyCount(100)\n * console.log(count) // '99+'\n * ```\n */\nexport function formatNotifyCount(count: number): string {\n if (count > 99) return '99+'\n return count.toString()\n}\n\n/**\n * Splits the properties of an object into multiple groups based on lists of keys.\n * @param props - The object to split.\n * @param keyGroups - The lists of keys to include in each group.\n * @returns An array of objects: each containing the properties specified in the corresponding key group, and the last object containing the remaining keys.\n */\nexport function splitProps<T extends object>(\n props: T,\n ...keyGroups: (keyof T)[][]\n): { [K in keyof T]?: T[K] }[] {\n const result = keyGroups.map(() => ({}) as { [K in keyof T]?: T[K] })\n const rest = {} as { [K in keyof T]?: T[K] }\n\n for (const key in props) {\n let assigned = false\n for (let i = 0; i < keyGroups.length; i++) {\n if (keyGroups[i].includes(key as keyof T)) {\n result[i][key as keyof T] = props[key]\n assigned = true\n break\n }\n }\n if (!assigned) {\n rest[key as keyof T] = props[key]\n }\n }\n\n return [...result, rest]\n}\n\nexport * from './localStorage'\n","'use client'\n\n/**\n * A utility function to get a value from local storage.\n * @param key The key to get from local storage.\n * @param defaultValue The fallback value if the key is not found.\n * @returns key or defaultValue\n */\nexport function getLocalStorage<T extends string>(\n key: string,\n defaultValue: T,\n): T {\n const value = window.localStorage.getItem(key)\n if (value) {\n return value as T\n }\n return defaultValue\n}\n\n/**\n * A utility function to set a value in local storage.\n * @param key The key to set in local storage.\n * @param value The value to set in local storage.\n */\nexport function setLocalStorage<T>(key: string, value: T): void {\n const stringValue = typeof value === 'string' ? value : JSON.stringify(value)\n window.localStorage.setItem(key, stringValue)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACQO,SAAS,gBACd,KACA,cACG;AACH,QAAM,QAAQ,OAAO,aAAa,QAAQ,GAAG;AAC7C,MAAI,OAAO;AACT,WAAO;AAAA,EACT;AACA,SAAO;AACT;AAOO,SAAS,gBAAmB,KAAa,OAAgB;AAC9D,QAAM,cAAc,OAAO,UAAU,WAAW,QAAQ,KAAK,UAAU,KAAK;AAC5E,SAAO,aAAa,QAAQ,KAAK,WAAW;AAC9C;;;ADZO,SAAS,kBAAkB,OAAuB;AACvD,MAAI,QAAQ,GAAI,QAAO;AACvB,SAAO,MAAM,SAAS;AACxB;AAQO,SAAS,WACd,UACG,WAC0B;AAC7B,QAAM,SAAS,UAAU,IAAI,OAAO,CAAC,EAA+B;AACpE,QAAM,OAAO,CAAC;AAEd,aAAW,OAAO,OAAO;AACvB,QAAI,WAAW;AACf,aAAS,IAAI,GAAG,IAAI,UAAU,QAAQ,KAAK;AACzC,UAAI,UAAU,CAAC,EAAE,SAAS,GAAc,GAAG;AACzC,eAAO,CAAC,EAAE,GAAc,IAAI,MAAM,GAAG;AACrC,mBAAW;AACX;AAAA,MACF;AAAA,IACF;AACA,QAAI,CAAC,UAAU;AACb,WAAK,GAAc,IAAI,MAAM,GAAG;AAAA,IAClC;AAAA,EACF;AAEA,SAAO,CAAC,GAAG,QAAQ,IAAI;AACzB;","names":[]}