Skip to content

Jojo-craft/string-craft

Repository files navigation

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 Provider Build Status
SonarCloud Quality Gate Status
GitHub actions build
GitHub actions build
GitHub actions build
Codecov codecov
Stryker-mutator dashboard Mutation testing badge

Usage

Install

npm i string-craft

Import

import { String } from 'string-craft';

Fields

Name Description
EMPTY Represents the empty string. This field is read-only. The value of this field is the zero-length string ("").
String.EMPTY;
// ""

Methods

Name Description Input parameters Return
isEmpty Indicates whether the specified string is an empty string ("") (reverse with isNotEmpty method). value: string boolean
isNullOrEmpty Indicates whether the specified string is null, undefined or an empty string ("") (reverse with isNotNullOrEmpty method). value: string | null | undefined boolean
isBlank Indicates whether a specified string is empty, or consists only of white-space characters (reverse with isNotBlank method). value: string boolean
isNullOrBlank Indicates whether a specified string is null, undefined, empty, or consists only of white-space characters (reverse with isNotNullOrBlank method). value: string | null | undefined boolean
isNumber Indicates whether the specified string is a valid numeric string (reverse with isNotNumber method). value: string | null | undefined boolean
isAlpha Indicates whether a given value consists only of alphabetic characters (reverse with isNotAlpha method). value: string | null | undefined boolean
isAlphaNumber Indicates whether the specified string contains only alphabetic characters and numbers (reverse with isNotAlphaNumber method). value: string | null | undefined boolean
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 | undefined boolean
containsNumber Indicates whether the specified string contains at least one numeric digit (reverse with notContainsNumber method). value: string | null | undefined boolean
containsAlpha Indicates whether the specified string contains at least one alphabetic character (reverse with notContainsAlpha method). value: string | null | undefined boolean
containsSpecialCharacter Indicates whether a given string contains at least one special character (reverse with notContainsSpecialCharacter method). value: string | null | undefined boolean
containsUpperCase Indicates whether the specified string contains at least one uppercase letter. value: string | null | undefined boolean
containsLowerCase Indicates whether the specified string contains at least one lowercase letter. value: string | null | undefined boolean
valueOrEmpty Returns an empty string if the value is null, undefined, or blank; otherwise, returns the input value. value: string | null | undefined string
removeAccents Removes accents from a given string. value: string string
join Concatenates an array of strings using the specified separator between each member. separator: string, values: (string | null | undefined)[] string
countWords Counts the number of words in a sentence. sentence: string | null | undefined number
toNumber Converts a string representation of a number to a JavaScript number. value: string | null | undefined number
toBoolean Converts a string representation to a boolean value. value: string | null | undefined boolean
String.isEmpty('value');
// false

String.isEmpty('  ');
// false

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

String.isNullOrEmpty('  ');
// false

String.isNullOrEmpty(null);
// true

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

String.isBlank('  ');
// true

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

String.isNullOrBlank('  ');
// true

String.isNullOrBlank(null);
// true

String.isNullOrBlank('');
// true
String.removeAccents('déjà là');
// 'deja la'
String.join('; ', 'apple', 'banana', 'orange', 'grape');
// 'apple; banana; orange; grape'
String.countWords('Hello world');
// 2

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

String.isNumber('');
// false

String.isNumber('  ');
// false

String.isNumber(null);
// false

String.isNumber('99');
// true
String.isAlpha('123abc');
// false

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

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

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

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

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

String.containsNumber('!abc&def/');
// false
String.containsAlpha('^123a456+');
// true

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

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

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

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

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

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

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

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

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

String.isBasicStrongPassword('1234abcefgH!');
// true
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
String.toBoolean(undefined);
// false
String.toBoolean(' ');
// false
String.toBoolean('1');
// true
String.toBoolean('true');
// true
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.

npm run test
Coverage
npm run test:coverage

Mutation test

Possibility to run mutation tests with Stryker.

npm run test:mutation