A library for handling strings in smart ways, including manipulation and encoding, with TypeScript support.
Go to file
2024-05-29 14:16:41 +02:00
.gitea/workflows fix(core): update 2023-12-27 21:57:45 +01:00
.vscode fix(core): update 2022-03-18 22:52:38 +01:00
test fix(core): update 2024-03-01 23:34:43 +01:00
ts fix(core): update 2024-03-03 12:35:59 +01:00
.gitignore fix(core): update 2020-12-31 03:56:40 +00:00
license fix(core): update 2022-03-18 22:52:38 +01:00
npmextra.json update tsconfig 2024-04-14 18:26:18 +02:00
package.json update description 2024-05-29 14:16:41 +02:00
pnpm-lock.yaml fix(core): update 2024-03-01 23:34:43 +01:00
readme.hints.md update tsconfig 2024-04-14 18:26:18 +02:00
readme.md update tsconfig 2024-04-14 18:26:18 +02:00
tsconfig.json fix(core): update 2024-03-03 12:35:59 +01:00

@push.rocks/smartstring

handle strings in smart ways. TypeScript ready.

Install

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

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.

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.

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.

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.

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.

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.

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.

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.

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 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.