forked from launchdarkly/react-client-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
47 lines (43 loc) · 1.87 KB
/
utils.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
import {LDFlagChangeset, LDFlagSet} from 'launchdarkly-js-client-sdk';
import camelCase from 'lodash.camelcase';
import {LDReactOptions} from "./types";
/**
* Transforms a set of flags so that their keys are camelCased. This function ignores
* flag keys which start with `$`.
*
* @param rawFlags A mapping of flag keys and their values
* @return A transformed `LDFlagSet` with camelCased flag keys
*/
export const camelCaseKeys = (rawFlags: LDFlagSet) => {
const flags: LDFlagSet = {};
for (const rawFlag in rawFlags) {
// Exclude system keys
if (rawFlag.indexOf('$') !== 0) {
flags[camelCase(rawFlag)] = rawFlags[rawFlag]; // tslint:disable-line:no-unsafe-any
}
}
return flags;
};
/**
* Gets the flags to pass to the provider from the changeset.
*
* @param changes the `LDFlagChangeset` from the ldClient onchange handler.
* @param targetFlags if targetFlags are specified, changes to other flags are ignored and not returned in the
* flattened `LDFlagSet`
* @param reactOptions reactOptions.useCamelCaseFlagKeys is used to determine whether to
* @return an `LDFlagSet` with the current flag values from the LDFlagChangeset filtered by `targetFlags`
* or null if none of the targetFlags were changed
*/
export const getFlattenedFlagsFromChangeset = (changes: LDFlagChangeset, targetFlags: LDFlagSet | undefined,
reactOptions: LDReactOptions): LDFlagSet | null => {
const flattened: LDFlagSet = {};
for (const key in changes) {
if (targetFlags === undefined || targetFlags[key] !== undefined) {
// tslint:disable-next-line:no-unsafe-any
const flagKey = reactOptions.useCamelCaseFlagKeys ? camelCase(key) : key;
flattened[flagKey] = changes[key].current;
}
}
return Object.keys(flattened).length > 0 ? flattened : null;
}
export default { camelCaseKeys, getFlattenedFlagsFromChangeset };