or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

index.mdpromises.mdstring-formatting.mdtype-checking.mdutilities.md

string-formatting.mddocs/

0

# String Formatting

1

2

Essential tools for formatted string output and detailed object inspection with customizable styling and output options.

3

4

## Capabilities

5

6

### Format Function

7

8

Printf-style string formatting with placeholder substitution.

9

10

```javascript { .api }

11

/**

12

* Formats a string using printf-style placeholders

13

* @param {string} f - Format string with placeholders (%s, %d, %j, %%)

14

* @param {...any} args - Arguments to substitute into placeholders

15

* @returns {string} Formatted string

16

*/

17

function format(f, ...args): string;

18

```

19

20

Supported placeholders:

21

- `%s` - String conversion

22

- `%d` - Number conversion

23

- `%j` - JSON.stringify conversion

24

- `%%` - Literal percent sign

25

26

**Usage Examples:**

27

28

```javascript

29

const util = require('util');

30

31

// Basic formatting

32

util.format('Hello %s', 'world'); // "Hello world"

33

util.format('Number: %d', 42); // "Number: 42"

34

util.format('JSON: %j', { a: 1 }); // "JSON: {"a":1}"

35

util.format('Percent: %%'); // "Percent: %"

36

37

// Multiple placeholders

38

util.format('%s is %d years old', 'Alice', 25); // "Alice is 25 years old"

39

40

// Extra arguments are appended

41

util.format('%s %s', 'a', 'b', 'c'); // "a b c"

42

43

// Non-string format argument

44

util.format(123, 'abc'); // "123 abc"

45

```

46

47

### Inspect Function

48

49

Returns a string representation of objects with detailed formatting options.

50

51

```javascript { .api }

52

/**

53

* Returns a string representation of an object

54

* @param {any} obj - Object to inspect

55

* @param {InspectOptions} [opts] - Inspection options

56

* @returns {string} String representation of the object

57

*/

58

function inspect(obj, opts?): string;

59

60

interface InspectOptions {

61

/** Show hidden (non-enumerable) properties */

62

showHidden?: boolean;

63

/** Recursion depth (default: 2) */

64

depth?: number;

65

/** Use ANSI color codes for styling */

66

colors?: boolean;

67

/** Enable custom inspect functions */

68

customInspect?: boolean;

69

}

70

```

71

72

**Usage Examples:**

73

74

```javascript

75

const util = require('util');

76

77

const obj = {

78

name: 'example',

79

data: { numbers: [1, 2, 3], active: true },

80

method: function() { return 'hello'; }

81

};

82

83

// Basic inspection

84

console.log(util.inspect(obj));

85

86

// With colors (for terminal output)

87

console.log(util.inspect(obj, { colors: true }));

88

89

// Show hidden properties

90

console.log(util.inspect(obj, { showHidden: true }));

91

92

// Control depth

93

console.log(util.inspect(obj, { depth: 1 }));

94

95

// Inspect primitive values

96

util.inspect(123); // "123"

97

util.inspect('hello'); // "'hello'"

98

util.inspect(true); // "true"

99

util.inspect(null); // "null"

100

```

101

102

### Inspect Styling

103

104

Configuration objects for customizing inspect output appearance.

105

106

```javascript { .api }

107

// Color codes for ANSI styling

108

inspect.colors: {

109

bold: [number, number];

110

italic: [number, number];

111

underline: [number, number];

112

inverse: [number, number];

113

white: [number, number];

114

grey: [number, number];

115

black: [number, number];

116

blue: [number, number];

117

cyan: [number, number];

118

green: [number, number];

119

magenta: [number, number];

120

red: [number, number];

121

yellow: [number, number];

122

};

123

124

// Style mappings for different value types

125

inspect.styles: {

126

special: string;

127

number: string;

128

boolean: string;

129

undefined: string;

130

null: string;

131

string: string;

132

date: string;

133

regexp: string;

134

};

135

```

136

137

**Usage Examples:**

138

139

```javascript

140

const util = require('util');

141

142

// Customize colors

143

util.inspect.colors.custom = [95, 39]; // Custom color code

144

util.inspect.styles.myType = 'custom';

145

146

// Default color mappings

147

console.log(util.inspect.styles.number); // "yellow"

148

console.log(util.inspect.styles.string); // "green"

149

console.log(util.inspect.styles.boolean); // "yellow"

150

```