-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathutils.ts
58 lines (52 loc) · 2.09 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
48
49
50
51
52
53
54
55
56
57
58
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 determines whether to change the flag keys to camelCase
* @return an `LDFlagSet` with the current flag values from the LDFlagChangeset filtered by `targetFlags`. The returned
* object may be empty `{}` if none of the targetFlags were changed.
*/
export const getFlattenedFlagsFromChangeset = (
changes: LDFlagChangeset,
targetFlags: LDFlagSet | undefined,
reactOptions: LDReactOptions,
): LDFlagSet => {
const flattened: LDFlagSet = {};
for (const key in changes) {
if (!targetFlags || targetFlags[key] !== undefined) {
// tslint:disable-next-line:no-unsafe-any
const flagKey = reactOptions.useCamelCaseFlagKeys ? camelCase(key) : key;
flattened[flagKey] = changes[key].current;
}
}
return flattened;
};
/**
* @deprecated The `camelCaseKeys.camelCaseKeys` property will be removed in a future version,
* please update your code to use the `camelCaseKeys` function directly.
*/
// tslint:disable-next-line deprecation
camelCaseKeys.camelCaseKeys = camelCaseKeys;
export default { camelCaseKeys, getFlattenedFlagsFromChangeset };