MCPcopy Index your code
hub / github.com/Kong/httpsnippet / literalRepresentation

Function literalRepresentation

src/targets/python/helpers.ts:38–87  ·  view source on GitHub ↗
(
  value: any,
  opts: Record<string, any>,
  indentLevel?: number,
)

Source from the content-addressed store, hash-verified

36 * @return {string}
37 */
38export const literalRepresentation = (
39 value: any,
40 opts: Record<string, any>,
41 indentLevel?: number,
42): any => {
43 indentLevel = indentLevel === undefined ? 1 : indentLevel + 1;
44
45 switch (Object.prototype.toString.call(value)) {
46 case '[object Number]':
47 return value;
48
49 case '[object Array]': {
50 let pretty = false;
51 const valuesRepresentation: any = (value as any[]).map(v => {
52 // Switch to prettify if the value is a dictionary with multiple keys
53 if (Object.prototype.toString.call(v) === '[object Object]') {
54 pretty = Object.keys(v).length > 1;
55 }
56 return literalRepresentation(v, opts, indentLevel);
57 });
58 return concatValues('array', valuesRepresentation, pretty, opts.indent, indentLevel);
59 }
60
61 case '[object Object]': {
62 const keyValuePairs = [];
63 for (const key in value) {
64 keyValuePairs.push(`"${key}": ${literalRepresentation(value[key], opts, indentLevel)}`);
65 }
66 return concatValues(
67 'object',
68 keyValuePairs,
69 opts.pretty && keyValuePairs.length > 1,
70 opts.indent,
71 indentLevel,
72 );
73 }
74
75 case '[object Null]':
76 return 'None';
77
78 case '[object Boolean]':
79 return value ? 'True' : 'False';
80
81 default:
82 if (value === null || value === undefined) {
83 return '';
84 }
85 return `"${value.toString().replace(/"/g, '\\"')}"`;
86 }
87};

Callers 1

client.tsFile · 0.90

Calls 1

concatValuesFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…