2.4.0 • Published 8 months ago

string-craft v2.4.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

string-craft

npm npm

Simple string manipulation library for TypeScript.

string-craft is a comprehensive TypeScript library designed to simplify and enhance string manipulation tasks.

CI Status

The following table lists live workflows from various CI providers.

CI ProviderBuild Status
SonarCloudQuality Gate Status
GitHub actionsbuild
GitHub actionsbuild
GitHub actionsbuild
Codecovcodecov
Stryker-mutator dashboardMutation testing badge

Usage

Install

npm i string-craft

Import

import { String } from 'string-craft';

Fields

NameDescription
EMPTYRepresents the empty string. This field is read-only. The value of this field is the zero-length string ("").

EMPTY

String.EMPTY;
// ""

Methods

NameDescriptionInput parametersReturn
[isEmpty](#isEmpty)Indicates whether the specified string is an empty string ("") (reverse with isNotEmpty method).value: stringboolean
[isNullOrEmpty](#isNullOrEmpty)Indicates whether the specified string is null, undefined or an empty string ("") (reverse with isNotNullOrEmpty method).value: string | null | undefinedboolean
[isBlank](#isBlank)Indicates whether a specified string is empty, or consists only of white-space characters (reverse with isNotBlank method).value: stringboolean
[isNullOrBlank](#isNullOrBlank)Indicates whether a specified string is null, undefined, empty, or consists only of white-space characters (reverse with isNotNullOrBlank method).value: string | null | undefinedboolean
[isNumber](#isNumber)Indicates whether the specified string is a valid numeric string (reverse with isNotNumber method).value: string | null | undefinedboolean
[isAlpha](#isAlpha)Indicates whether a given value consists only of alphabetic characters (reverse with isNotAlpha method).value: string | null | undefinedboolean
[isAlphaNumber](#isAlphaNumber)Indicates whether the specified string contains only alphabetic characters and numbers (reverse with isNotAlphaNumber method).value: string | null | undefinedboolean
[isBasicStrongPassword](#isBasicStrongPassword)Indicates whether the specified string contains at least 1 uppercase letter, 1 lowercase letter, 1 number, 1 special character and a minimum length of 12 characters.value: string | null | undefinedboolean
[containsNumber](#containsNumber)Indicates whether the specified string contains at least one numeric digit (reverse with notContainsNumber method).value: string | null | undefinedboolean
[containsAlpha](#containsAlpha)Indicates whether the specified string contains at least one alphabetic character (reverse with notContainsAlpha method).value: string | null | undefinedboolean
[containsSpecialCharacter](#containsSpecialCharacter)Indicates whether a given string contains at least one special character (reverse with notContainsSpecialCharacter method).value: string | null | undefinedboolean
[containsUpperCase](#containsUpperCase)Indicates whether the specified string contains at least one uppercase letter.value: string | null | undefinedboolean
[containsLowerCase](#containsLowerCase)Indicates whether the specified string contains at least one lowercase letter.value: string | null | undefinedboolean
[valueOrEmpty](#valueOrEmpty)Returns an empty string if the value is null, undefined, or blank; otherwise, returns the input value.value: string | null | undefinedstring
[removeAccents](#removeAccents)Removes accents from a given string.value: stringstring
[join](#join)Concatenates an array of strings using the specified separator between each member.separator: string, values: (string | null | undefined)[]string
[countWords](#countWords)Counts the number of words in a sentence.sentence: string | null | undefinednumber
[toNumber](#toNumber)Converts a string representation of a number to a JavaScript number.value: string | null | undefinednumber
[toBoolean](#toBoolean)Converts a string representation to a boolean value.value: string | null | undefinedboolean

isEmpty

String.isEmpty('value');
// false

String.isEmpty('  ');
// false

String.isEmpty('');
// true

isNullOrEmpty

String.isNullOrEmpty('value');
// false

String.isNullOrEmpty('  ');
// false

String.isNullOrEmpty(null);
// true

String.isNullOrEmpty('');
// true

isBlank

String.isBlank('value');
// false

String.isBlank('  ');
// true

String.isBlank('');
// true

isNullOrBlank

String.isNullOrBlank('value');
// false

String.isNullOrBlank('  ');
// true

String.isNullOrBlank(null);
// true

String.isNullOrBlank('');
// true

removeAccents

String.removeAccents('déjà là');
// 'deja la'

join

String.join('; ', 'apple', 'banana', 'orange', 'grape');
// 'apple; banana; orange; grape'

countWords

String.countWords('Hello world');
// 2

String.countWords('hello - all the world ! WAIT!');
// 5

isNumber

String.isNumber('Hello world');
// false

String.isNumber('');
// false

String.isNumber('  ');
// false

String.isNumber(null);
// false

String.isNumber('99');
// true

isAlpha

String.isAlpha('123abc');
// false

String.isAlpha('abc');
// true

isAlphaNumber

String.isAlphaNumber('123abc');
// true

String.isAlphaNumber('abc');
// false

String.isAlphaNumber('123');
// false

String.isAlphaNumber('abc-123');
// false

containsSpecialCharacter

String.containsSpecialCharacter('123abc');
// false

String.containsSpecialCharacter('123abc/');
// true

containsNumber

String.containsNumber('^abc1def+');
// true

String.containsNumber('!abc&def/');
// false

containsAlpha

String.containsAlpha('^123a456+');
// true

String.containsAlpha('!123&456/');
// false

containsUpperCase

String.containsUpperCase('abcDef');
// true

String.containsUpperCase('abcdef');
// false

String.containsUpperCase('12!@');
// false

containsLowerCase

String.containsLowerCase('ABCdEF');
// true

String.containsLowerCase('ABCD');
// false

String.containsLowerCase('12!@');
// false

isBasicStrongPassword

String.isBasicStrongPassword('123456789AB@');
// false

String.isBasicStrongPassword('123456789ab@');
// false

String.isBasicStrongPassword('12345678901@');
// false

String.isBasicStrongPassword('123456789aBC');
// false

String.isBasicStrongPassword('123abC#$');
// false

String.isBasicStrongPassword('1234abcefgH!');
// true

toNumber

String.toNumber(null);
// 0
String.toNumber(undefined);
// 0
String.toNumber('   ');
// 0
String.toNumber('A123@');
// 0
String.toNumber('true');
// 0
String.toNumber('10');
// 10
String.toNumber('-10');
// -10
String.toNumber('10.1234');
// 10.1234

toBoolean

String.toBoolean(undefined);
// false
String.toBoolean(' ');
// false
String.toBoolean('1');
// true
String.toBoolean('true');
// true

valueOrEmpty

String.valueOrEmpty(undefined);
// ''
String.valueOrEmpty(null);
// ''
String.valueOrEmpty('   ');
// ''
String.valueOrEmpty('pomme de terre');
// 'pomme de terre'

License

This software is released under the terms of the MIT license. See LICENSE.

Contribute

The code is written in TDD and therefore has a nice code coverage by the tests, please keep this cap ;)

Install

npm install

Test

Unit test

The code is covered by unit tests with [Vitest](https://vitest.dev/).

npm run test
Coverage
npm run test:coverage

Mutation test

Possibility to run mutation tests with [Stryker](https://stryker-mutator.io/docs/stryker-js/introduction/).

npm run test:mutation