-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathgetDictionaryEntry.ts
More file actions
37 lines (33 loc) · 1.02 KB
/
getDictionaryEntry.ts
File metadata and controls
37 lines (33 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { Dictionary, DictionaryEntry } from '../types/types';
import { get } from './indexDict';
export function isValidDictionaryEntry(
value: unknown
): value is DictionaryEntry {
if (typeof value !== 'object' || value === null) {
return true;
}
if (Array.isArray(value)) {
if (typeof value?.[0] === 'object' && value?.[0] !== null) {
return false;
}
const provisionalMetadata = value?.[1];
if (typeof provisionalMetadata === 'undefined') return true;
if (provisionalMetadata && typeof provisionalMetadata === 'object')
return true;
}
return false;
}
export function getDictionaryEntry<T extends Dictionary>(
dictionary: T,
id: string
): Dictionary | DictionaryEntry | undefined {
let current: Dictionary | DictionaryEntry = dictionary;
const dictionaryPath = id.split('.');
for (const key of dictionaryPath) {
if (typeof current !== 'object' && !Array.isArray(current)) {
return undefined;
}
current = get(current as Dictionary, key);
}
return current;
}