or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

configuration.mdcore-parsing.mddetailed-parsing.mdindex.mdparser-options.mdstring-utilities.mdtokenize-arg-string.md

string-utilities.mddocs/

0

# String Utilities

1

2

yargs-parser provides utility functions for string manipulation and number detection that are also exposed as part of the public API. These utilities are used internally by the parser and can be useful for external applications.

3

4

## Import

5

6

```typescript

7

import parser from "yargs-parser";

8

```

9

10

## API

11

12

```typescript { .api }

13

function parser.camelCase(str: string): string;

14

function parser.decamelize(str: string, joinString?: string): string;

15

function parser.looksLikeNumber(x: null | undefined | number | string): boolean;

16

```

17

18

## camelCase

19

20

Converts hyphenated or underscored strings to camelCase format. This is the same function used internally for camel-case expansion.

21

22

### Usage

23

24

```typescript

25

import parser from "yargs-parser";

26

27

console.log(parser.camelCase('foo-bar')); // 'fooBar'

28

console.log(parser.camelCase('hello_world')); // 'helloWorld'

29

console.log(parser.camelCase('multi-word-option')); // 'multiWordOption'

30

console.log(parser.camelCase('already-camelCase')); // 'alreadyCamelCase'

31

console.log(parser.camelCase('UPPER-CASE')); // 'upperCase'

32

```

33

34

### Special Cases

35

36

```typescript

37

// Preserves existing camelCase

38

console.log(parser.camelCase('fooBar')); // 'fooBar'

39

40

// Handles leading hyphens

41

console.log(parser.camelCase('--foo-bar')); // 'fooBar'

42

43

// Mixed case handling

44

console.log(parser.camelCase('XMLHttpRequest')); // 'XMLHttpRequest' (unchanged)

45

46

// Numbers and special characters

47

console.log(parser.camelCase('option-123-test')); // 'option123Test'

48

console.log(parser.camelCase('foo--bar')); // 'fooBar'

49

```

50

51

### Empty and Edge Cases

52

53

```typescript

54

console.log(parser.camelCase('')); // ''

55

console.log(parser.camelCase('a')); // 'a'

56

console.log(parser.camelCase('-')); // ''

57

console.log(parser.camelCase('---')); // ''

58

console.log(parser.camelCase('a-')); // 'a'

59

console.log(parser.camelCase('-a')); // 'a'

60

```

61

62

## decamelize

63

64

Converts camelCase strings to hyphenated format (or custom separator). This is the reverse operation of camelCase.

65

66

### Basic Usage

67

68

```typescript

69

import parser from "yargs-parser";

70

71

console.log(parser.decamelize('fooBar')); // 'foo-bar'

72

console.log(parser.decamelize('helloWorld')); // 'hello-world'

73

console.log(parser.decamelize('multiWordOption')); // 'multi-word-option'

74

console.log(parser.decamelize('XMLHttpRequest')); // 'x-m-l-http-request'

75

```

76

77

### Custom Separator

78

79

```typescript

80

// Using underscore separator

81

console.log(parser.decamelize('fooBar', '_')); // 'foo_bar'

82

console.log(parser.decamelize('helloWorld', '_')); // 'hello_world'

83

84

// Using dot separator

85

console.log(parser.decamelize('fooBar', '.')); // 'foo.bar'

86

87

// Using space separator

88

console.log(parser.decamelize('helloWorld', ' ')); // 'hello world'

89

```

90

91

### Special Cases

92

93

```typescript

94

// Already lowercase

95

console.log(parser.decamelize('lowercase')); // 'lowercase'

96

97

// Single character

98

console.log(parser.decamelize('a')); // 'a'

99

console.log(parser.decamelize('A')); // 'a'

100

101

// Numbers

102

console.log(parser.decamelize('option123Test')); // 'option123-test'

103

104

// Consecutive capitals

105

console.log(parser.decamelize('HTTPSProxy')); // 'h-t-t-p-s-proxy'

106

```

107

108

### Default Separator Behavior

109

110

```typescript

111

// Without separator parameter, defaults to hyphen

112

console.log(parser.decamelize('fooBar')); // 'foo-bar'

113

console.log(parser.decamelize('fooBar', undefined)); // 'foo-bar'

114

console.log(parser.decamelize('fooBar', '')); // 'foobar' (empty separator)

115

```

116

117

## looksLikeNumber

118

119

Determines if a value appears to be a number that can be parsed. This is used internally to decide when to apply automatic number conversion.

120

121

### Basic Usage

122

123

```typescript

124

import parser from "yargs-parser";

125

126

// Integer-like strings

127

console.log(parser.looksLikeNumber('123')); // true

128

console.log(parser.looksLikeNumber('0')); // true

129

console.log(parser.looksLikeNumber('-456')); // true

130

131

// Decimal numbers

132

console.log(parser.looksLikeNumber('3.14')); // true

133

console.log(parser.looksLikeNumber('-2.5')); // true

134

console.log(parser.looksLikeNumber('.5')); // true

135

136

// Scientific notation

137

console.log(parser.looksLikeNumber('1e5')); // true

138

console.log(parser.looksLikeNumber('2.5e-3')); // true

139

console.log(parser.looksLikeNumber('1E10')); // true

140

```

141

142

### Hexadecimal Numbers

143

144

```typescript

145

// Hexadecimal notation

146

console.log(parser.looksLikeNumber('0x1F')); // true

147

console.log(parser.looksLikeNumber('0XFF')); // true

148

console.log(parser.looksLikeNumber('0x0')); // true

149

```

150

151

### Non-Number Values

152

153

```typescript

154

// Strings that are not numbers

155

console.log(parser.looksLikeNumber('hello')); // false

156

console.log(parser.looksLikeNumber('123abc')); // false

157

console.log(parser.looksLikeNumber('')); // false

158

159

// Special string cases

160

console.log(parser.looksLikeNumber('NaN')); // false

161

console.log(parser.looksLikeNumber('Infinity')); // false

162

console.log(parser.looksLikeNumber('-Infinity')); // false

163

```

164

165

### Octal Numbers (Special Handling)

166

167

```typescript

168

// Leading zeros are treated specially to avoid octal interpretation

169

console.log(parser.looksLikeNumber('0123')); // false (to prevent octal)

170

console.log(parser.looksLikeNumber('0.123')); // true (decimal with leading zero)

171

console.log(parser.looksLikeNumber('007')); // false (to prevent octal)

172

```

173

174

### Null and Undefined

175

176

```typescript

177

// Null and undefined values

178

console.log(parser.looksLikeNumber(null)); // false

179

console.log(parser.looksLikeNumber(undefined)); // false

180

181

// Actual numbers (already parsed)

182

console.log(parser.looksLikeNumber(123)); // true

183

console.log(parser.looksLikeNumber(0)); // true

184

console.log(parser.looksLikeNumber(-456)); // true

185

```

186

187

## Integration Examples

188

189

### Using Utilities with Parsing

190

191

```typescript

192

import parser from "yargs-parser";

193

194

// Check if values look like numbers before parsing

195

const args = ['--port', '3000', '--name', 'server', '--timeout', '30s'];

196

197

args.forEach((arg, index) => {

198

if (arg.startsWith('--') && index + 1 < args.length) {

199

const value = args[index + 1];

200

console.log(`${arg}: ${value} -> looks like number: ${parser.looksLikeNumber(value)}`);

201

}

202

});

203

// Output:

204

// --port: 3000 -> looks like number: true

205

// --name: server -> looks like number: false

206

// --timeout: 30s -> looks like number: false

207

```

208

209

### Converting Between Formats

210

211

```typescript

212

// Convert CLI options to different formats

213

function convertOptionFormat(option: string, toCamelCase: boolean = false) {

214

if (toCamelCase) {

215

return parser.camelCase(option);

216

} else {

217

// Assume input is camelCase, convert to hyphenated

218

return parser.decamelize(option);

219

}

220

}

221

222

console.log(convertOptionFormat('database-url', true)); // 'databaseUrl'

223

console.log(convertOptionFormat('databaseUrl', false)); // 'database-url'

224

```

225

226

### Custom Number Validation

227

228

```typescript

229

// Strict number validation using looksLikeNumber

230

function parseStrictNumbers(args: string[]) {

231

return parser(args, {

232

coerce: {

233

port: (arg: any) => {

234

if (!parser.looksLikeNumber(arg)) {

235

throw new Error(`Port must be a number, got: ${arg}`);

236

}

237

return parseInt(arg, 10);

238

}

239

}

240

});

241

}

242

243

try {

244

const result = parseStrictNumbers(['--port', '3000abc']);

245

} catch (error) {

246

console.error(error.message); // "Port must be a number, got: 3000abc"

247

}

248

```

249

250

### Option Name Standardization

251

252

```typescript

253

// Standardize option names across different input formats

254

function standardizeOptions(options: Record<string, any>) {

255

const standardized: Record<string, any> = {};

256

257

for (const [key, value] of Object.entries(options)) {

258

// Convert all keys to camelCase for consistent internal representation

259

const standardKey = parser.camelCase(key);

260

standardized[standardKey] = value;

261

}

262

263

return standardized;

264

}

265

266

const mixed = {

267

'database-url': 'postgres://localhost',

268

'max_connections': 100,

269

'enableLogging': true

270

};

271

272

console.log(standardizeOptions(mixed));

273

// Output: { databaseUrl: 'postgres://localhost', maxConnections: 100, enableLogging: true }

274

```