1.0.5 • Published 3 months ago

lkt-string-tools v1.0.5

Weekly downloads
-
License
MIT
Repository
-
Last release
3 months ago

LKT String Tools

Functions: Generators

generateRandomString

Returns a random string

ArgTypeDefaultDescription
lengthinteger10Length of the random string

Usage

import {generateRandomString} from "lkt-string-tools";

const str1 = generateRandomString();
const str2 = generateRandomString(14);

Functions: HTML

stripTags

Removes HTML tags from input except if defined in allowed

ArgTypeDefaultDescription
inputstringString to be cleaned
allowedstringThe format is like following: <a><b><c> (only tags in lowercase)

Usage

import {stripTags} from "lkt-string-tools";

const str1 = stripTags('<span>Hello world</span>', ''); // Returns "Hello world"
const str2 = stripTags('<span>Hello world</span>', '<span>'); // Returns "<span>Hello world</span>"

htmlEntities

Convert input to HTML entities

ArgTypeDefaultDescription
inputstringString to be converted

Usage

import {htmlEntities} from "lkt-string-tools";

const str1 = htmlEntities('lorem ipsum dolor sit amet');

Functions: replacements

replaceAll

Replaces all occurrences of search in target with replacement

ArgTypeDefaultDescription
targetstringString which will be used to replace occurrences
searchstringSearch pattern
replacementstringReplacement

Usage

import {replaceAll} from "lkt-string-tools";

const str1 = replaceAll('hey you!', 'h', 'H'); // Returns "Hey you!"

replaceSingleWhiteSpaces

Replaces all whitespaces which only has one occurrence at a time in target with replacement

ArgTypeDefaultDescription
targetstringString which will be used to replace occurrences
replacementstringReplacement

Usage

import {replaceSingleWhiteSpaces} from "lkt-string-tools";

const str1 = replaceSingleWhiteSpaces('hey you!', '_'); // Returns: "hey_you!"

trim

Removes starting and ending whitespaces from target

ArgTypeDefaultDescription
targetstringString to be trimmed
valuestring''Search string

Usage

import {trim} from "lkt-string-tools";

const str1 = trim('   lorem ipsum   ') // Returns: "lorem ipsum"

Functions: status

isFilled

Checks if a string has content of it's empty

ArgTypeDefaultDescription
targetstringString to check

Usage

import {isFilled} from "lkt-string-tools";

if (isFilled('lorem')) {
    //do something
}

isBase64Image

Checks if a string is base64 encoded image

ArgTypeDefaultDescription
targetstringString to check

Usage

import {isBase64Image} from "lkt-string-tools";

if (isBase64Image('lorem')) {
    //do something
}

Functions: text style

kebabCaseToCamelCase

Convert from kebab case to camel case

ArgTypeDefaultDescription
targetstringString to convert

Usage

import {kebabCaseToCamelCase} from "lkt-string-tools";

console.log(kebabCaseToCamelCase('lorem-ipsum-dolor-sit-amet'))

ucfirst

Converts first letter to uppercase

ArgTypeDefaultDescription
targetstringString to convert

Usage

import {ucfirst} from "lkt-string-tools";

console.log(ucfirst('lorem ipsum'))

formatNumber

Format a number

ArgTypeDefaultDescription
numberNumberNumber to format
decimalsintegerAmount of decimals
decimalPointstringDecimal point separator
thousandsSeparatorstringThousands point separator

Usage

import {formatNumber} from "lkt-string-tools";

console.log(formatNumber(20, 2, '.', ',')) // 20.00
console.log(formatNumber(2000, 4, '.', ','))// 2,000.0000

Functions: typing

isString

Checks if a given var is string

ArgTypeDefaultDescription
targetanyVar to check

Usage

import {isString} from "lkt-string-tools";

console.log(isString('lorem-ipsum-dolor-sit-amet'))

isEmail

Checks if a given string content has email format

ArgTypeDefaultDescription
targetstringString to check

Usage

import {isEmail} from "lkt-string-tools";

console.log(isEmail('lorem ipsum'))

toString

Cast to string

ArgTypeDefaultDescription
targetanyData to convert to string

Usage

import {toString} from "lkt-string-tools";

console.log(toString(20))

cloneString

Creates a copy of the string without the reference to the original var.

ArgTypeDefaultDescription
targetstringString to clone

Usage

import {cloneString} from "lkt-string-tools";

console.log(cloneString('my awesome string'))

Functions: url

decodeUrl

Decodes and url encoded string

ArgTypeDefaultDescription
targetstringString to decode

Usage

import {decodeUrl} from "lkt-string-tools";

console.log(decodeUrl('lorem-ipsum-dolor-sit-amet'))

getUrlSlug

Removes invalid chars to retrieve a nice url slug

ArgTypeDefaultDescription
targetstringString to clean

Usage

import {getUrlSlug} from "lkt-string-tools";

console.log(getUrlSlug('lorem ipsum'))

Functions: UTF

utf8Encode

Encodes to UTF8

ArgTypeDefaultDescription
targetstringString to encode

Usage

import {utf8Encode} from "lkt-string-tools";

console.log(utf8Encode('lorem-ipsum-dolor-sit-amet'))

utf8Decode

Decodes from UTF8

ArgTypeDefaultDescription
targetstringString to decode

Usage

import {utf8Decode} from "lkt-string-tools";

console.log(utf8Decode('lorem ipsum'))