(namespace: string, initialValue: T)
| 24 | * @returns {*} |
| 25 | */ |
| 26 | const getSharedResource = <T>(namespace: string, initialValue: T): T => { |
| 27 | const parts = namespace.split("."); |
| 28 | let current = getSharedResourcesInstance() as Record<string, any>; |
| 29 | |
| 30 | if (!current) { |
| 31 | return initialValue; |
| 32 | } |
| 33 | |
| 34 | for (let i = 0; i < parts.length; i++) { |
| 35 | const part = parts[i]; |
| 36 | const lastPart = i === parts.length - 1; |
| 37 | if (!Object.prototype.hasOwnProperty.call(current, part)) { |
| 38 | current[part] = lastPart ? initialValue : {}; |
| 39 | } |
| 40 | current = current[part]; |
| 41 | } |
| 42 | |
| 43 | return current as T; |
| 44 | }; |
| 45 | |
| 46 | export default getSharedResource; |
no test coverage detected
searching dependent graphs…