-
Notifications
You must be signed in to change notification settings - Fork 46
/
collector.ts
53 lines (49 loc) · 1.58 KB
/
collector.ts
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* @forgerock/javascript-sdk
*
* collector.ts
*
* Copyright (c) 2020 ForgeRock. All rights reserved.
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
import type { StringDict } from '../shared/interfaces';
/**
* @class Collector - base class for FRDevice
* Generic collector functions for collecting a device profile attributes
*/
class Collector {
/**
* @method reduceToObject - goes one to two levels into source to collect attribute
* @param props - array of strings; can use dot notation for two level lookup
* @param src - source of attributes to check
*/
// eslint-disable-next-line
reduceToObject(props: string[], src: StringDict<any>): StringDict<string> {
return props.reduce((prev, curr) => {
if (curr.includes('.')) {
const propArr = curr.split('.');
const prop1 = propArr[0];
const prop2 = propArr[1];
const prop = src[prop1] && src[prop1][prop2];
prev[prop2] = prop != undefined ? prop : '';
} else {
prev[curr] = src[curr] != undefined ? src[curr] : null;
}
return prev;
}, {} as StringDict<string>);
}
/**
* @method reduceToString - goes one level into source to collect attribute
* @param props - array of strings
* @param src - source of attributes to check
*/
// eslint-disable-next-line
reduceToString(props: string[], src: any): string {
return props.reduce((prev, curr) => {
prev = `${prev}${src[curr].filename};`;
return prev;
}, '');
}
}
export default Collector;