---
title: "@push.rocks/smartstring"
---
# @push.rocks/smartstring
handle strings in smart ways. TypeScript ready.

## Install
To install `@push.rocks/smartstring`, use the following npm command:

```bash
npm install @push.rocks/smartstring --save
```

This will add it to your project's dependencies.

## Usage

The `@push.rocks/smartstring` package provides a powerful set of utilities to handle and manipulate strings in various ways, ready for TypeScript usage. Here's an exhaustive guide to using this package.

### Working with Domain Strings

The `Domain` class helps in parsing and extracting information from domain URLs.

```typescript
import { Domain } from '@push.rocks/smartstring';

// Parse a domain URL
const myDomain = new Domain('https://sub.example.com');
console.log(myDomain.level1); // Output: "com"
console.log(myDomain.level2); // Output: "example"
console.log(myDomain.zoneName); // Output: "example.com"
console.log(myDomain.protocol); // Output: "https"
```

### Handling Git Repositories

The `GitRepo` class is designed for extracting information from Git repository URLs.

```typescript
import { GitRepo } from '@push.rocks/smartstring';

// Parse a Git repository URL
const myGitRepo = new GitRepo('git@github.com:user/repo.git');
console.log(myGitRepo.host); // Output: "github.com"
console.log(myGitRepo.user); // Output: "user"
console.log(myGitRepo.repo); // Output: "repo"
console.log(myGitRepo.sshUrl); // Output: "git@github.com:user/repo.git"
console.log(myGitRepo.httpsUrl); // Output: "https://github.com/user/repo.git"
```

### Encoding and Decoding Base64 Strings

`@push.rocks/smartstring` offers base64 encoding and decoding through the `Base64` class and utility functions.

```typescript
import { Base64, base64 } from '@push.rocks/smartstring';

// Using the Base64 class
const myBase64 = new Base64('hello world', 'string');
console.log(myBase64.base64String); // Encoded string
console.log(myBase64.base64UriString); // Encoded URI compatible string

// Using utility functions
const encoded = base64.encode('hello world');
const decoded = base64.decode(encoded);
console.log(encoded); // Encoded string
console.log(decoded); // "hello world"
```

### Applying Indentation

SmartString allows you to easily indent strings or normalize indentation across a multi-line string.

```typescript
import { indent } from '@push.rocks/smartstring';

// Indent a string by 4 spaces
const indentedString = indent.indent('Some text\nAnother line', 4);
console.log(indentedString);

// Indent using a prefix
const prefixedString = indent.indentWithPrefix('Line 1\nLine 2', '> ');
console.log(prefixedString);

// Normalize indentation
const normalizedString = indent.normalize('    Some indented text\n        Another line');
console.log(normalizedString);
```

### Creating Random or Encrypted Strings

Create random strings based on patterns or generate cryptographically strong random strings.

```typescript
import { create } from '@push.rocks/smartstring';

// Create a random string
const randomString = create.createRandomString('aA0', 10);
console.log(randomString); // Example output: "a9mB8v2Dq1"

// Create a crypto-random string
const cryptoString = create.createCryptoRandomString(10);
console.log(cryptoString); // Example output: "f28Bb90aCc"
```

### Normalizing Strings

Normalize strings by removing leading/trailing whitespace, fixing indentation, and more.

```typescript
import { normalize } from '@push.rocks/smartstring';

// Normalize a multi-line string
const exampleString = `
    This is an example.
        The indentation will be fixed.
`;
const normalized = normalize.standard(exampleString);
console.log(normalized);
```

### Working with Docker Environment Variables

Transform an array of Docker environment variables into an object for easy access.

```typescript
import { docker } from '@push.rocks/smartstring';

const envVars = ['NODE_ENV=production', 'PORT=3000'];
const envObject = docker.makeEnvObject(envVars);
console.log(envObject.NODE_ENV); // Output: "production"
console.log(envObject.PORT); // Output: "3000"
```

This guide covers the primary features of `@push.rocks/smartstring`, making string manipulation and information extraction simple and efficient in your TypeScript projects.

## License and Legal Information

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. 

**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

### Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

### Company Information

Task Venture Capital GmbH  
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
