aboutsummaryrefslogtreecommitdiffstats
path: root/src/jsUtils.ts
blob: f5b39a000ce60ec3c2570ba879e45f092e097167 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// TODO: ifUndefinedString can be removed after ./src/webview/recipe.js is converted to typescript.
export const ifUndefinedString = (
  source: string | undefined | null,
  defaultValue: string,
): string => (source !== undefined && source !== null ? source : defaultValue);

export const ifUndefined = <T>(
  source: undefined | null | T,
  defaultValue: T,
): T => {
  if (source !== undefined && source !== null) {
    return source;
  }

  return defaultValue;
};

export const convertToJSON = (data: string | any | undefined | null) =>
  data && typeof data === 'string' && data.length > 0 ? JSON.parse(data) : data;

export const cleanseJSObject = (data: any | undefined | null) =>
  JSON.parse(JSON.stringify(data));