or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

array.mddate.mdfunction.mdindex.mdlocalization.mdnumber.mdobject.mdrange.mdregexp.mdstring.md

string.mddocs/

0

# Sugar String Module

1

2

Comprehensive string manipulation methods providing case conversion, encoding, formatting, inflection, language detection, and advanced text processing capabilities for JavaScript strings.

3

4

## Core Imports

5

6

```typescript

7

import Sugar from "sugar";

8

```

9

10

For CommonJS:

11

12

```javascript

13

const Sugar = require("sugar");

14

```

15

16

## Basic Usage

17

18

```typescript

19

import Sugar from "sugar";

20

21

// String case conversion

22

const text = "hello world";

23

const camelCase = Sugar.String.camelize(text);

24

// "helloWorld"

25

26

// String manipulation

27

const padded = Sugar.String.pad("hello", 10, "*");

28

// "**hello***"

29

30

// String encoding

31

const encoded = Sugar.String.encodeBase64("Hello World");

32

// "SGVsbG8gV29ybGQ="

33

34

// Inflection

35

const word = "child";

36

const plural = Sugar.String.pluralize(word);

37

// "children"

38

39

// Language detection

40

const japanese = "こんにちは";

41

const hasHiragana = Sugar.String.hasHiragana(japanese);

42

// true

43

```

44

45

## Capabilities

46

47

### String Creation

48

49

Methods for creating string ranges and sequences.

50

51

```typescript { .api }

52

/**

53

* Creates string range from start to end character

54

* @param start - Starting character (default: 'a')

55

* @param end - Ending character

56

* @returns Array of characters in range

57

*/

58

function range(start?: string, end?: string): string[];

59

```

60

61

**Usage Examples:**

62

63

```typescript

64

// Create character ranges

65

const alphabet = Sugar.String.range('a', 'z');

66

// ['a', 'b', 'c', ..., 'z']

67

68

const numbers = Sugar.String.range('0', '9');

69

// ['0', '1', '2', ..., '9']

70

```

71

72

### String Access

73

74

Methods for accessing specific parts or characters of strings.

75

76

```typescript { .api }

77

/**

78

* Get character at index with optional looping

79

* @param instance - Source string

80

* @param index - Character index (supports negative indexing)

81

* @param loop - Whether to loop around if index exceeds bounds

82

* @returns Character at index

83

*/

84

function at<T>(instance: string, index: number, loop?: boolean): string;

85

86

/**

87

* Get first n characters of string

88

* @param instance - Source string

89

* @param n - Number of characters to get (default: 1)

90

* @returns First n characters

91

*/

92

function first(instance: string, n?: number): string;

93

94

/**

95

* Get substring from index to end

96

* @param instance - Source string

97

* @param index - Starting index (default: 0)

98

* @returns Substring from index

99

*/

100

function from(instance: string, index?: number): string;

101

102

/**

103

* Get last n characters of string

104

* @param instance - Source string

105

* @param n - Number of characters to get (default: 1)

106

* @returns Last n characters

107

*/

108

function last(instance: string, n?: number): string;

109

110

/**

111

* Get substring from start to index

112

* @param instance - Source string

113

* @param index - Ending index

114

* @returns Substring to index

115

*/

116

function to(instance: string, index?: number): string;

117

```

118

119

**Usage Examples:**

120

121

```typescript

122

const text = "Hello World";

123

124

// Access with looping

125

const char = Sugar.String.at(text, -1);

126

// "d"

127

128

// Get first/last characters

129

const first3 = Sugar.String.first(text, 3);

130

// "Hel"

131

132

const last3 = Sugar.String.last(text, 3);

133

// "rld"

134

135

// Substring operations

136

const fromIndex = Sugar.String.from(text, 6);

137

// "World"

138

139

const toIndex = Sugar.String.to(text, 5);

140

// "Hello"

141

```

142

143

### String Case Conversion

144

145

Methods for converting between different case formats.

146

147

```typescript { .api }

148

/**

149

* Convert string to camelCase

150

* @param instance - Source string

151

* @param upper - Whether to use UpperCamelCase (default: false)

152

* @returns Camelized string

153

*/

154

function camelize(instance: string, upper?: boolean): string;

155

156

/**

157

* Capitalize first letter of words

158

* @param instance - Source string

159

* @param lower - Whether to lowercase other letters (default: false)

160

* @param all - Whether to capitalize all words (default: false)

161

* @returns Capitalized string

162

*/

163

function capitalize(instance: string, lower?: boolean, all?: boolean): string;

164

165

/**

166

* Convert to dash-case format

167

* @param instance - Source string

168

* @returns Dasherized string

169

*/

170

function dasherize(instance: string): string;

171

172

/**

173

* Convert to URL-friendly parameter format

174

* @param instance - Source string

175

* @returns Parameterized string

176

*/

177

function parameterize(instance: string): string;

178

179

/**

180

* Add spaces before capital letters

181

* @param instance - Source string

182

* @returns Spacified string

183

*/

184

function spacify(instance: string): string;

185

186

/**

187

* Convert to Title Case

188

* @param instance - Source string

189

* @returns Titleized string

190

*/

191

function titleize(instance: string): string;

192

193

/**

194

* Convert to underscore_case format

195

* @param instance - Source string

196

* @returns Underscored string

197

*/

198

function underscore(instance: string): string;

199

```

200

201

**Usage Examples:**

202

203

```typescript

204

const text = "hello world example";

205

206

// Various case conversions

207

const camel = Sugar.String.camelize(text);

208

// "helloWorldExample"

209

210

const upperCamel = Sugar.String.camelize(text, true);

211

// "HelloWorldExample"

212

213

const capitalized = Sugar.String.capitalize(text, true, true);

214

// "Hello World Example"

215

216

const dashed = Sugar.String.dasherize("HelloWorld");

217

// "hello-world"

218

219

const param = Sugar.String.parameterize("Hello World! 123");

220

// "hello-world-123"

221

222

const spaced = Sugar.String.spacify("HelloWorld");

223

// "Hello World"

224

225

const title = Sugar.String.titleize(text);

226

// "Hello World Example"

227

228

const underscore = Sugar.String.underscore("HelloWorld");

229

// "hello_world"

230

```

231

232

### String Manipulation

233

234

Methods for modifying, transforming, and manipulating string content.

235

236

```typescript { .api }

237

/**

238

* Remove extra whitespace and compact string

239

* @param instance - Source string

240

* @returns Compacted string

241

*/

242

function compact(instance: string): string;

243

244

/**

245

* Insert string at specified index

246

* @param instance - Source string

247

* @param str - String to insert

248

* @param index - Index to insert at (default: length)

249

* @returns String with insertion

250

*/

251

function insert(instance: string, str: string, index?: number): string;

252

253

/**

254

* Pad string to specified length (both sides)

255

* @param instance - Source string

256

* @param num - Target length

257

* @param padding - Padding character (default: ' ')

258

* @returns Padded string

259

*/

260

function pad(instance: string, num: number, padding?: string): string;

261

262

/**

263

* Pad string to specified length (left side)

264

* @param instance - Source string

265

* @param num - Target length

266

* @param padding - Padding character (default: ' ')

267

* @returns Left-padded string

268

*/

269

function padLeft(instance: string, num: number, padding?: string): string;

270

271

/**

272

* Pad string to specified length (right side)

273

* @param instance - Source string

274

* @param num - Target length

275

* @param padding - Padding character (default: ' ')

276

* @returns Right-padded string

277

*/

278

function padRight(instance: string, num: number, padding?: string): string;

279

280

/**

281

* Remove first occurrence of pattern

282

* @param instance - Source string

283

* @param pattern - Pattern to remove (string or RegExp)

284

* @returns String with first match removed

285

*/

286

function remove(instance: string, pattern: any): string;

287

288

/**

289

* Remove all occurrences of pattern

290

* @param instance - Source string

291

* @param pattern - Pattern to remove (string or RegExp)

292

* @returns String with all matches removed

293

*/

294

function removeAll(instance: string, pattern: any): string;

295

296

/**

297

* Replace all occurrences of pattern

298

* @param instance - Source string

299

* @param pattern - Pattern to replace (string or RegExp)

300

* @param args - Replacement arguments

301

* @returns String with all matches replaced

302

*/

303

function replaceAll(instance: string, pattern: any, ...args: any[]): string;

304

305

/**

306

* Reverse string character order

307

* @param instance - Source string

308

* @returns Reversed string

309

*/

310

function reverse(instance: string): string;

311

312

/**

313

* Truncate string to specified length

314

* @param instance - Source string

315

* @param length - Maximum length

316

* @param from - Truncation direction ('left' | 'right' | 'middle')

317

* @param ellipsis - Ellipsis string (default: '...')

318

* @returns Truncated string

319

*/

320

function truncate(instance: string, length: number, from?: string, ellipsis?: string): string;

321

322

/**

323

* Truncate string at word boundary

324

* @param instance - Source string

325

* @param length - Maximum length

326

* @param from - Truncation direction ('left' | 'right' | 'middle')

327

* @param ellipsis - Ellipsis string (default: '...')

328

* @returns Truncated string at word boundary

329

*/

330

function truncateOnWord(instance: string, length: number, from?: string, ellipsis?: string): string;

331

```

332

333

**Usage Examples:**

334

335

```typescript

336

// String manipulation examples

337

const text = " hello world ";

338

const compacted = Sugar.String.compact(text);

339

// "hello world"

340

341

// Insertion

342

const inserted = Sugar.String.insert("Hello World", "Beautiful ", 6);

343

// "Hello Beautiful World"

344

345

// Padding

346

const padded = Sugar.String.pad("hello", 10, "*");

347

// "**hello***"

348

349

const leftPadded = Sugar.String.padLeft("42", 5, "0");

350

// "00042"

351

352

// Removal and replacement

353

const removed = Sugar.String.remove("hello world", "world");

354

// "hello "

355

356

const replaced = Sugar.String.replaceAll("hello world hello", "hello", "hi");

357

// "hi world hi"

358

359

// Reversal

360

const reversed = Sugar.String.reverse("hello");

361

// "olleh"

362

363

// Truncation

364

const truncated = Sugar.String.truncate("This is a very long string", 10);

365

// "This is..."

366

367

const wordTruncated = Sugar.String.truncateOnWord("This is a very long string", 10);

368

// "This is..."

369

```

370

371

### String Encoding

372

373

Methods for encoding and decoding strings in various formats.

374

375

```typescript { .api }

376

/**

377

* Decode Base64 encoded string

378

* @param instance - Base64 encoded string

379

* @returns Decoded string

380

*/

381

function decodeBase64(instance: string): string;

382

383

/**

384

* Encode string to Base64

385

* @param instance - Source string

386

* @returns Base64 encoded string

387

*/

388

function encodeBase64(instance: string): string;

389

390

/**

391

* Escape HTML entities in string

392

* @param instance - Source string

393

* @returns HTML escaped string

394

*/

395

function escapeHTML(instance: string): string;

396

397

/**

398

* Unescape HTML entities in string

399

* @param instance - HTML escaped string

400

* @returns Unescaped string

401

*/

402

function unescapeHTML(instance: string): string;

403

404

/**

405

* URL encode string

406

* @param instance - Source string

407

* @param param - Whether encoding for parameter (default: false)

408

* @param partial - Whether partial encoding (default: false)

409

* @returns URL encoded string

410

*/

411

function escapeURL(instance: string, param?: boolean, partial?: boolean): string;

412

413

/**

414

* URL decode string

415

* @param instance - URL encoded string

416

* @param param - Whether decoding parameter (default: false)

417

* @param partial - Whether partial decoding (default: false)

418

* @returns URL decoded string

419

*/

420

function unescapeURL(instance: string, param?: boolean, partial?: boolean): string;

421

```

422

423

**Usage Examples:**

424

425

```typescript

426

// Base64 encoding

427

const original = "Hello World";

428

const encoded = Sugar.String.encodeBase64(original);

429

// "SGVsbG8gV29ybGQ="

430

431

const decoded = Sugar.String.decodeBase64(encoded);

432

// "Hello World"

433

434

// HTML encoding

435

const htmlText = "<h1>Hello & Goodbye</h1>";

436

const escaped = Sugar.String.escapeHTML(htmlText);

437

// "&lt;h1&gt;Hello &amp; Goodbye&lt;/h1&gt;"

438

439

const unescaped = Sugar.String.unescapeHTML(escaped);

440

// "<h1>Hello & Goodbye</h1>"

441

442

// URL encoding

443

const urlText = "hello world!";

444

const urlEncoded = Sugar.String.escapeURL(urlText);

445

// "hello%20world%21"

446

447

const urlDecoded = Sugar.String.unescapeURL(urlEncoded);

448

// "hello world!"

449

```

450

451

### String Tag Methods

452

453

Methods for working with HTML tags in strings.

454

455

```typescript { .api }

456

/**

457

* Remove HTML tags from string

458

* @param instance - Source string with HTML

459

* @param tag - Specific tag to remove (default: all tags)

460

* @param replace - Replacement string (default: '')

461

* @returns String with tags removed

462

*/

463

function removeTags(instance: string, tag?: string, replace?: string): string;

464

465

/**

466

* Strip HTML tags from string (alias for removeTags)

467

* @param instance - Source string with HTML

468

* @param tag - Specific tag to remove (default: all tags)

469

* @param replace - Replacement string (default: '')

470

* @returns String with tags stripped

471

*/

472

function stripTags(instance: string, tag?: string, replace?: string): string;

473

```

474

475

**Usage Examples:**

476

477

```typescript

478

const htmlString = "<p>Hello <strong>World</strong>!</p>";

479

480

// Remove all tags

481

const noTags = Sugar.String.removeTags(htmlString);

482

// "Hello World!"

483

484

// Remove specific tags

485

const noStrong = Sugar.String.removeTags(htmlString, "strong");

486

// "<p>Hello World!</p>"

487

488

// Replace with custom text

489

const replaced = Sugar.String.removeTags(htmlString, "strong", "[BOLD]");

490

// "<p>Hello [BOLD]World[BOLD]!</p>"

491

```

492

493

### String Tests

494

495

Methods for testing string properties and content.

496

497

```typescript { .api }

498

/**

499

* Test if string is blank (contains only whitespace)

500

* @param instance - Source string

501

* @returns True if string is blank

502

*/

503

function isBlank(instance: string): boolean;

504

505

/**

506

* Test if string is empty (zero length)

507

* @param instance - Source string

508

* @returns True if string is empty

509

*/

510

function isEmpty(instance: string): boolean;

511

```

512

513

**Usage Examples:**

514

515

```typescript

516

// String testing

517

const empty = "";

518

const whitespace = " \n\t ";

519

const text = "hello";

520

521

Sugar.String.isEmpty(empty); // true

522

Sugar.String.isEmpty(whitespace); // false

523

Sugar.String.isEmpty(text); // false

524

525

Sugar.String.isBlank(empty); // true

526

Sugar.String.isBlank(whitespace); // true

527

Sugar.String.isBlank(text); // false

528

```

529

530

### String Iteration

531

532

Methods for iterating over string components like characters, lines, and words.

533

534

```typescript { .api }

535

/**

536

* Iterate over string characters

537

* @param instance - Source string

538

* @param eachCharFn - Function to call for each character

539

* @returns Array of characters or mapped values

540

*/

541

function chars<T>(instance: string, eachCharFn?: Function): T[];

542

543

/**

544

* Iterate over string character codes

545

* @param instance - Source string

546

* @param eachCodeFn - Function to call for each character code

547

* @returns Array of character codes or mapped values

548

*/

549

function codes<T>(instance: string, eachCodeFn?: Function): T[];

550

551

/**

552

* Iterate over pattern matches in string

553

* @param instance - Source string

554

* @param search - Search pattern (string or RegExp)

555

* @param eachFn - Function to call for each match

556

* @returns Array of matches or mapped values

557

*/

558

function forEach<T>(instance: string, search?: any, eachFn?: Function): T[];

559

560

/**

561

* Iterate over string lines

562

* @param instance - Source string

563

* @param eachLineFn - Function to call for each line

564

* @returns Array of lines or mapped values

565

*/

566

function lines<T>(instance: string, eachLineFn?: Function): T[];

567

568

/**

569

* Shift string characters by n positions

570

* @param instance - Source string

571

* @param n - Number of positions to shift

572

* @returns Shifted string

573

*/

574

function shift<T>(instance: string, n: number): string;

575

576

/**

577

* Iterate over string words

578

* @param instance - Source string

579

* @param eachWordFn - Function to call for each word

580

* @returns Array of words or mapped values

581

*/

582

function words<T>(instance: string, eachWordFn?: Function): T[];

583

```

584

585

**Usage Examples:**

586

587

```typescript

588

const text = "Hello World\nHow are you?";

589

590

// Character iteration

591

const characters = Sugar.String.chars(text);

592

// ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '\n', ...]

593

594

// Character codes

595

const codes = Sugar.String.codes("ABC");

596

// [65, 66, 67]

597

598

// Line iteration

599

const lines = Sugar.String.lines(text);

600

// ["Hello World", "How are you?"]

601

602

// Word iteration

603

const words = Sugar.String.words(text);

604

// ["Hello", "World", "How", "are", "you"]

605

606

// Character shifting (Caesar cipher style)

607

const shifted = Sugar.String.shift("abc", 1);

608

// "bcd"

609

```

610

611

### String Conversion

612

613

Methods for converting strings to other types and formats.

614

615

```typescript { .api }

616

/**

617

* Format string with arguments (sprintf-style)

618

* @param instance - Format string

619

* @param args - Arguments to substitute

620

* @returns Formatted string

621

*/

622

function format(instance: string, ...args: any[]): string;

623

624

/**

625

* Convert string to number

626

* @param instance - Source string

627

* @param base - Number base (default: 10)

628

* @returns Parsed number or NaN

629

*/

630

function toNumber(instance: string, base?: number): number;

631

632

/**

633

* Trim whitespace from left side of string

634

* @param instance - Source string

635

* @returns Left-trimmed string

636

*/

637

function trimLeft(instance: string): string;

638

639

/**

640

* Trim whitespace from right side of string

641

* @param instance - Source string

642

* @returns Right-trimmed string

643

*/

644

function trimRight(instance: string): string;

645

```

646

647

**Usage Examples:**

648

649

```typescript

650

// String formatting

651

const formatted = Sugar.String.format("Hello {0}, you are {1} years old", "Alice", 30);

652

// "Hello Alice, you are 30 years old"

653

654

// Number conversion

655

const number = Sugar.String.toNumber("42");

656

// 42

657

658

const hex = Sugar.String.toNumber("ff", 16);

659

// 255

660

661

// Trimming

662

const leftTrimmed = Sugar.String.trimLeft(" hello ");

663

// "hello "

664

665

const rightTrimmed = Sugar.String.trimRight(" hello ");

666

// " hello"

667

```

668

669

### String Inflections

670

671

Methods for grammatical inflection of words (pluralization, singularization, humanization).

672

673

```typescript { .api }

674

/**

675

* Pluralize word based on number

676

* @param instance - Source word

677

* @param num - Number to determine plural form (default: 2)

678

* @returns Pluralized word

679

*/

680

function pluralize(instance: string, num?: number): string;

681

682

/**

683

* Singularize plural word

684

* @param instance - Plural word

685

* @returns Singularized word

686

*/

687

function singularize(instance: string): string;

688

689

/**

690

* Humanize string (make it readable)

691

* @param instance - Source string

692

* @returns Humanized string

693

*/

694

function humanize(instance: string): string;

695

696

/**

697

* Add custom acronym for inflection recognition

698

* @param src - Acronym string

699

*/

700

function addAcronym(src: string): void;

701

702

/**

703

* Add custom pluralization rule

704

* @param singular - Singular form

705

* @param plural - Plural form (optional, derived if not provided)

706

*/

707

function addPlural(singular: string, plural?: string): void;

708

709

/**

710

* Add custom humanization rule

711

* @param src - Source string

712

* @param human - Human-readable form

713

*/

714

function addHuman(src: string, human: string): void;

715

```

716

717

**Usage Examples:**

718

719

```typescript

720

// Basic inflection

721

const singular = "child";

722

const plural = Sugar.String.pluralize(singular);

723

// "children"

724

725

const backToSingular = Sugar.String.singularize("children");

726

// "child"

727

728

// Conditional pluralization

729

const item1 = Sugar.String.pluralize("apple", 1);

730

// "apple"

731

732

const items5 = Sugar.String.pluralize("apple", 5);

733

// "apples"

734

735

// Humanization

736

const humanized = Sugar.String.humanize("first_name");

737

// "First name"

738

739

// Custom rules

740

Sugar.String.addPlural("person", "people");

741

Sugar.String.addAcronym("API");

742

Sugar.String.addHuman("api_key", "API Key");

743

```

744

745

### Language & Script Detection

746

747

Methods for detecting languages, scripts, and character sets in strings.

748

749

```typescript { .api }

750

/**

751

* Test if string contains Arabic characters

752

* @param instance - Source string

753

* @returns True if contains Arabic characters

754

*/

755

function hasArabic(instance: string): boolean;

756

757

/**

758

* Test if string contains Cyrillic characters

759

* @param instance - Source string

760

* @returns True if contains Cyrillic characters

761

*/

762

function hasCyrillic(instance: string): boolean;

763

764

/**

765

* Test if string contains Devanagari characters

766

* @param instance - Source string

767

* @returns True if contains Devanagari characters

768

*/

769

function hasDevanagari(instance: string): boolean;

770

771

/**

772

* Test if string contains Greek characters

773

* @param instance - Source string

774

* @returns True if contains Greek characters

775

*/

776

function hasGreek(instance: string): boolean;

777

778

/**

779

* Test if string contains Hangul characters

780

* @param instance - Source string

781

* @returns True if contains Hangul characters

782

*/

783

function hasHangul(instance: string): boolean;

784

785

/**

786

* Test if string contains Han (Chinese) characters

787

* @param instance - Source string

788

* @returns True if contains Han characters

789

*/

790

function hasHan(instance: string): boolean;

791

792

/**

793

* Test if string contains Hebrew characters

794

* @param instance - Source string

795

* @returns True if contains Hebrew characters

796

*/

797

function hasHebrew(instance: string): boolean;

798

799

/**

800

* Test if string contains Hiragana characters

801

* @param instance - Source string

802

* @returns True if contains Hiragana characters

803

*/

804

function hasHiragana(instance: string): boolean;

805

806

/**

807

* Test if string contains Kana characters (Hiragana or Katakana)

808

* @param instance - Source string

809

* @returns True if contains Kana characters

810

*/

811

function hasKana(instance: string): boolean;

812

813

/**

814

* Test if string contains Katakana characters

815

* @param instance - Source string

816

* @returns True if contains Katakana characters

817

*/

818

function hasKatakana(instance: string): boolean;

819

820

/**

821

* Test if string contains Latin characters

822

* @param instance - Source string

823

* @returns True if contains Latin characters

824

*/

825

function hasLatin(instance: string): boolean;

826

827

/**

828

* Test if string contains Thai characters

829

* @param instance - Source string

830

* @returns True if contains Thai characters

831

*/

832

function hasThai(instance: string): boolean;

833

834

/**

835

* Test if string is purely Arabic

836

* @param instance - Source string

837

* @returns True if string is purely Arabic

838

*/

839

function isArabic(instance: string): boolean;

840

841

/**

842

* Test if string is purely Cyrillic

843

* @param instance - Source string

844

* @returns True if string is purely Cyrillic

845

*/

846

function isCyrillic(instance: string): boolean;

847

848

/**

849

* Test if string is purely Devanagari

850

* @param instance - Source string

851

* @returns True if string is purely Devanagari

852

*/

853

function isDevanagari(instance: string): boolean;

854

855

/**

856

* Test if string is purely Greek

857

* @param instance - Source string

858

* @returns True if string is purely Greek

859

*/

860

function isGreek(instance: string): boolean;

861

862

/**

863

* Test if string is purely Hangul

864

* @param instance - Source string

865

* @returns True if string is purely Hangul

866

*/

867

function isHangul(instance: string): boolean;

868

869

/**

870

* Test if string is purely Han (Chinese)

871

* @param instance - Source string

872

* @returns True if string is purely Han

873

*/

874

function isHan(instance: string): boolean;

875

876

/**

877

* Test if string is purely Hebrew

878

* @param instance - Source string

879

* @returns True if string is purely Hebrew

880

*/

881

function isHebrew(instance: string): boolean;

882

883

/**

884

* Test if string is purely Hiragana

885

* @param instance - Source string

886

* @returns True if string is purely Hiragana

887

*/

888

function isHiragana(instance: string): boolean;

889

890

/**

891

* Test if string is purely Kana (Hiragana or Katakana)

892

* @param instance - Source string

893

* @returns True if string is purely Kana

894

*/

895

function isKana(instance: string): boolean;

896

897

/**

898

* Test if string is purely Katakana

899

* @param instance - Source string

900

* @returns True if string is purely Katakana

901

*/

902

function isKatakana(instance: string): boolean;

903

904

/**

905

* Test if string is purely Latin

906

* @param instance - Source string

907

* @returns True if string is purely Latin

908

*/

909

function isLatin(instance: string): boolean;

910

911

/**

912

* Test if string is purely Thai

913

* @param instance - Source string

914

* @returns True if string is purely Thai

915

*/

916

function isThai(instance: string): boolean;

917

```

918

919

**Usage Examples:**

920

921

```typescript

922

// Script detection

923

const mixed = "Hello こんにちは مرحبا";

924

925

Sugar.String.hasLatin(mixed); // true

926

Sugar.String.hasHiragana(mixed); // true

927

Sugar.String.hasArabic(mixed); // true

928

929

// Pure script tests

930

const japanese = "こんにちは";

931

const english = "Hello";

932

933

Sugar.String.isHiragana(japanese); // true

934

Sugar.String.isLatin(english); // true

935

Sugar.String.isHiragana(english); // false

936

937

// Other scripts

938

const cyrillic = "Привет";

939

Sugar.String.hasCyrillic(cyrillic); // true

940

Sugar.String.isCyrillic(cyrillic); // true

941

942

const chinese = "你好";

943

Sugar.String.hasHan(chinese); // true

944

Sugar.String.isHan(chinese); // true

945

```

946

947

### Character Width Conversion

948

949

Methods for converting between full-width and half-width characters, and between Japanese writing systems.

950

951

```typescript { .api }

952

/**

953

* Convert half-width to full-width characters

954

* @param instance - Source string

955

* @returns String with full-width characters

956

*/

957

function hankakuToZenkaku(instance: string): string;

958

959

/**

960

* Convert full-width to half-width characters

961

* @param instance - Source string

962

* @returns String with half-width characters

963

*/

964

function zenkakuToHankaku(instance: string): string;

965

966

/**

967

* Convert Hiragana to Katakana

968

* @param instance - Source string with Hiragana

969

* @returns String with Katakana characters

970

*/

971

function hiraganaToKatakana(instance: string): string;

972

973

/**

974

* Convert Katakana to Hiragana

975

* @param instance - Source string with Katakana

976

* @returns String with Hiragana characters

977

*/

978

function katakanaToHiragana(instance: string): string;

979

```

980

981

**Usage Examples:**

982

983

```typescript

984

// Character width conversion

985

const halfWidth = "ABC123";

986

const fullWidth = Sugar.String.hankakuToZenkaku(halfWidth);

987

// "ABC123"

988

989

const backToHalf = Sugar.String.zenkakuToHankaku(fullWidth);

990

// "ABC123"

991

992

// Japanese character conversion

993

const hiragana = "こんにちは";

994

const katakana = Sugar.String.hiraganaToKatakana(hiragana);

995

// "コンニチハ"

996

997

const backToHiragana = Sugar.String.katakanaToHiragana(katakana);

998

// "こんにちは"

999

```

1000

1001

## Types

1002

1003

```typescript { .api }

1004

/**

1005

* Function type for mapping operations

1006

*/

1007

type mapFn<T, U> = (item: T, index?: number) => U;

1008

1009

/**

1010

* Configuration options for extending native objects

1011

*/

1012

interface ExtendOptions {

1013

methods?: Array<string>;

1014

except?: Array<string>;

1015

namespaces?: Array<any>;

1016

enhance?: boolean;

1017

enhanceString?: boolean;

1018

enhanceArray?: boolean;

1019

objectPrototype?: boolean;

1020

}

1021

1022

/**

1023

* Chainable interface for Sugar operations

1024

*/

1025

interface SugarDefaultChainable<RawValue> {

1026

raw: RawValue;

1027

valueOf(): RawValue extends string | number | boolean ? RawValue : any;

1028

toString(): string;

1029

}

1030

```