1.1.5 • Published 3 months ago

@termehui/utils v1.1.5

Weekly downloads
-
License
ISC
Repository
github
Last release
3 months ago

Utils

Utility functions for different data types and operations.

Installation

CDN

This package published as utils module in umd.

<script src="https://unpkg.com/@termehui/utils"></script>

NPM

npm i @termehui/utils

Number

unifySeparator

Replace all non-numeric character in string with separator character.

import { unifySeparator } from "@termehui/utils";
unifySeparator("-3,233,312.33", "/"); // -3/233/312.33
unifySeparator("123   ,   456", ","); // 123,456

formatNumber

Format number with separator.

import { formatNumber } from "@termehui/utils";
formatNumber(12345.456, ","); // 12,345.345
formatNumber("-3214.222", ","); // -3,214.222

extractNumeric

Extract numeric character from string.

Caution: this function extract all numeric characters (-, [0-9], .)/ for parsing number use parseNumber function.

import { extractNumeric } from "@termehui/utils";
extractNumeric("$ 12345.00002"); // "12345.00002"
extractNumeric("Balance is : -32123.0001"); // "-32123.0001"
parseNumber("with none - string number ."); // "-."

parseNumber

Parse number from string.

import { parseNumber } from "@termehui/utils";
parseNumber("$ 12345.00002"); // 12345.00002
parseNumber("with none . string number -"); // NaN

Object

deepClone

Create a deep clone of object.

function deepClone<T = any>(v: unknown): T;

String

alter

Return alter for empty value.

slugify

Make slugify string from strings. join multiple strings and remove spaces.

mapper

Search for value in replacement object and return replacement if found, otherwise returns value itself.

Note: you can use * key in replacement to define default value if no replacement is defined.

truncate

Truncate string and add ... to end of string if string length bigger than truncate length.

str

Convert value to string. if falsy value passed this function returns empty string.

joiner

Concatenate non-falsy value with space.

Colors

Helper class to generate colors for svg shape and chart.

import { ColorSpace } from "@termehui/utils";

enum Color {
  Primary,
  Red,
}

const colors = new ColorSpace<Color>();
colors.AddColor(Color.Primary, "red", "orange", 255, 10, 0);

const red = colors.color(Color.Primary);
const palette = colors.all();
const alpha = colors.alpha(Color.Primary, 0.5);
const linearAlphaBG = colors.linearAlpha(Color.Primary, svgContext, svgArea);
const linearBG = colors.linear(Color.Primary, svgContext, svgArea);
const linearIBG = colors.linearInverse(Color.Primary, svgContext, svgArea);
const radialBG = colors.radial(Color.Primary, svgContext, svgArea);
const radialIBG = colors.radialInverse(Color.Primary, svgContext, svgArea);
1.1.5

3 months ago

1.1.4

3 months ago

1.1.3

3 months ago

1.1.1

3 months ago

1.1.0

3 months ago

1.1.2

3 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago