Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 71 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<div align="center">


![Stringzy banner](./assets/stringzy-banner2.jpg)
![Stringzy banner](./assets/stringzy-banner2.jpg)

![NPM Version](https://img.shields.io/npm/v/stringzy)
![Typescript](https://img.shields.io/badge/TypeScript-3178C6?style=flat&logo=typescript&logoColor=white)
Expand Down Expand Up @@ -95,6 +94,8 @@ const count = stringzy.analyze.wordCount('Hello world'); // 2
- [isCoordinates](#iscoordinates) - Checks if given latitude and longitude are valid coordinates
- [isLowerCase](#islowercase) - Checks if given string only has lower case characters.
- [isUpperCase](#isuppercase) - Checks if given string only has upper case characters.
- [isAlphabetic](#isalphabetic) - Checks if input string contains only Alphabets (case insensitive)
- [isAlphaNumeric](#isalphanumeric) - Checks if input string contains only Alphabets and Digits (case insensitive)

### Analysis

Expand All @@ -105,8 +106,7 @@ const count = stringzy.analyze.wordCount('Hello world'); // 2
- [stringSimilarity](#stringsimilarity) - Calculates the percentage similarity between two strings
- [complexity](#complexity) - Analyzes string complexity including score, uniqueness, and length
- [patternCount](#patterncount) - calculates the number of times a specific pattern occurs in a given text
- [vowelConsonantCount](#vowelconsonantcount) - Counts the number of vowels and consonants in a given string

- [vowelConsonantCount](#vowelconsonantcount) - Counts the number of vowels and consonants in a given string

### Formatting

Expand Down Expand Up @@ -461,7 +461,9 @@ splitChunks('helloworld');
| chunkSize | number | `1` | The size of each chunk in which the string is to be split |

---

#### <a id="numbertotext"></a>`numberToText(num, lang)`

Converts a number to its text representation in the specified language.

```javascript
Expand All @@ -471,14 +473,13 @@ numberToText(12345, 'en'); // Returns: 'twelve thousand three hundred forty-five
numberToText(12345, 'pl'); // Returns: 'dwanaście tysięcy trzysta czterdzieści pięć'
```

| Parameter | Type | Default | Description |
|-----------|--------|---------|-------------|
| num | number | required | The number to convert to text |
| lang | string | 'en' | The language code for the text representation (e.g., 'en' for English, 'pl' for Polish) |
| Parameter | Type | Default | Description |
| --------- | ------ | -------- | --------------------------------------------------------------------------------------- |
| num | number | required | The number to convert to text |
| lang | string | 'en' | The language code for the text representation (e.g., 'en' for English, 'pl' for Polish) |

Available languages: en (English), pl (Polish).


### ✅ Validations

Functions for validating string formats and content.
Expand Down Expand Up @@ -565,19 +566,18 @@ isSlug('hello_world'); // false (underscore not allowed)
Checks if a file or URL has a valid extension for a given type

```javascript
isType("photo.PNG", "image"); // true
isType("https://example.com/logo.svg", "image"); // true
isType({ name: "track.mp3" }, "audio"); // true
isType("filewithoutextension", "image"); // false
isType("document.zip", "document"); // false
isType("video.mp4", "document"); // false
isType('photo.PNG', 'image'); // true
isType('https://example.com/logo.svg', 'image'); // true
isType({ name: 'track.mp3' }, 'audio'); // true
isType('filewithoutextension', 'image'); // false
isType('document.zip', 'document'); // false
isType('video.mp4', 'document'); // false
```

| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| input | string | required | The file name, URL string, or object with .name |
| input | string | required | The file type category to validate (image, video, audio, document, archive) |

| Parameter | Type | Default | Description |
| --------- | ------ | -------- | --------------------------------------------------------------------------- |
| input | string | required | The file name, URL string, or object with .name |
| input | string | required | The file type category to validate (image, video, audio, document, archive) |

#### <a id="isipv4"></a>`isIPv4(text)`

Expand Down Expand Up @@ -625,11 +625,11 @@ The check is case-insensitive and ignores spaces and punctuation.
```javascript
import { isPalindrome } from 'stringzy';

isPalindrome('racecar'); // true
isPalindrome('A man, a plan, a canal: Panama'); // true
isPalindrome('No lemon, no melon'); // true
isPalindrome('hello'); // false
isPalindrome('Was it a car or a cat I saw?'); // true
isPalindrome('racecar'); // true
isPalindrome('A man, a plan, a canal: Panama'); // true
isPalindrome('No lemon, no melon'); // true
isPalindrome('hello'); // false
isPalindrome('Was it a car or a cat I saw?'); // true
```

| Parameter | Type | Default | Description |
Expand All @@ -648,10 +648,10 @@ isCoordinates(40.748817, -73.985428); // true
isCoordinates(9999, -9999); // false
```

| Parameter | Type | Default | Description |
| --------- | ----------- | -------- | ----------------------------------------- |
| latitude | number | required | Latitude to validate |
| longitude | number | required | Longitude to validate |
| Parameter | Type | Default | Description |
| --------- | ------ | -------- | --------------------- |
| latitude | number | required | Latitude to validate |
| longitude | number | required | Longitude to validate |

#### <a id="islowercase"></a>`isLowerCase(str)`

Expand Down Expand Up @@ -689,6 +689,47 @@ isUpperCase('12345'); // false
| --------- | ------ | -------- | ---------------------------------------------------------------------------- |
| str | string | required | The input string to validate as containing uppercase alphabetic letters |

#### <a id="isalphabetic"></a>`isAlphabetic(text)`

Checks if a string contains only alphabetic characters (a-z, A-Z).
Throws an error if the input is not a string.

```javascript
import { isAlphabetic } from 'stringzy';

isAlphabetic('hello'); // true
isAlphabetic('World'); // true
isAlphabetic('helloWORLD'); // true
isAlphabetic('abc123'); // false
isAlphabetic('hello!'); // false
isAlphabetic(''); // false
```

| Parameter | Type | Default | Description |
| --------- | ------ | -------- | --------------------------------------------- |
| text | string | required | The input string to check for alphabetic only |

#### <a id="isalphanumeric"></a>`isAlphaNumeric(text)`

Checks if a string contains only alphanumeric characters (letters and digits).
Throws an error if the input is not a string.

```javascript
import { isAlphaNumeric } from 'stringzy';

isAlphaNumeric('abc123'); // true
isAlphaNumeric('A1B2C3'); // true
isAlphaNumeric('123'); // true
isAlphaNumeric('hello'); // true
isAlphaNumeric('hello!'); // false
isAlphaNumeric('123 456'); // false
isAlphaNumeric(''); // false
```

| Parameter | Type | Default | Description |
| --------- | ------ | -------- | ----------------------------------------------- |
| text | string | required | The input string to check for alphanumeric only |

---

### 📊 Analysis
Expand Down Expand Up @@ -778,8 +819,6 @@ stringSimilarity('flaw', 'lawn', 'Damerau-Levenshtein'); // Returns: 50
| textB | string | required | The second text to compare. |
| algorithm | string | 'Levenshtein' | The algorithm to use: 'Levenshtein' or 'Damerau-Levenshtein'. |



#### <a id="complexity"></a>`complexity(text)`

Analyzes the complexity of a string, returning an object with detailed metrics.
Expand Down Expand Up @@ -807,8 +846,6 @@ complexity('');
- `uniqueness` (number): Measure of character uniqueness
- `length` (number): Length of the input string



#### <a id="patterncount"></a>`patternCount(text, pattern)`

Counts the number of times a substring (pattern) occurs in a string, including overlapping occurrences.
Expand All @@ -832,7 +869,7 @@ Counts the number of vowels and consonants in a given string.
This function is case-insensitive and ignores non-alphabetic characters.

```javascript
vowelConsonantCount('hello');
vowelConsonantCount('hello');
// { vowels: 2, consonants: 3 }

vowelConsonantCount('stringzy');
Expand Down
29 changes: 29 additions & 0 deletions src/tests/validations/isAlphaNumeric.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { describe, it } from 'node:test';
import assert from 'node:assert';
import { isAlphaNumeric } from '../../validations/isAlphaNumeric';

describe('isAlphaNumeric', () => {
it('returns true for alphanumeric strings', () => {
assert.strictEqual(isAlphaNumeric('abc123'), true);
assert.strictEqual(isAlphaNumeric('A1B2C3'), true);
assert.strictEqual(isAlphaNumeric('123'), true);
assert.strictEqual(isAlphaNumeric('abc'), true);
});

it('returns false for strings with special characters or spaces', () => {
assert.strictEqual(isAlphaNumeric('abc!123'), false);
assert.strictEqual(isAlphaNumeric('hello world'), false);
assert.strictEqual(isAlphaNumeric('test@'), false);
});

it('returns false for empty string', () => {
assert.strictEqual(isAlphaNumeric(''), false);
});

it('throws an error if input is not a string', () => {
assert.throws(() => isAlphaNumeric(123 as any), /Input must be a string/);
assert.throws(() => isAlphaNumeric(null as any), /Input must be a string/);
assert.throws(() => isAlphaNumeric(undefined as any), /Input must be a string/);
assert.throws(() => isAlphaNumeric({} as any), /Input must be a string/);
});
});
32 changes: 32 additions & 0 deletions src/tests/validations/isAlphabetic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, it } from 'node:test';
import assert from 'node:assert';
import { isAlphabetic } from '../../validations/isAlphabetic';

describe('isAlphabetic', () => {
it('returns true for purely alphabetic strings', () => {
assert.strictEqual(isAlphabetic('hello'), true);
assert.strictEqual(isAlphabetic('World'), true);
assert.strictEqual(isAlphabetic('TestCase'), true);
});

it('returns false for strings with numbers', () => {
assert.strictEqual(isAlphabetic('hello123'), false);
assert.strictEqual(isAlphabetic('Test1'), false);
});

it('returns false for strings with special characters or spaces', () => {
assert.strictEqual(isAlphabetic('hello!'), false);
assert.strictEqual(isAlphabetic('hello world'), false);
assert.strictEqual(isAlphabetic('Hi-There'), false);
});

it('returns false for empty strings', () => {
assert.strictEqual(isAlphabetic(''), false);
});

it('throws an error if input is not a string', () => {
assert.throws(() => isAlphabetic(123 as any), /Input must be a string/);
assert.throws(() => isAlphabetic(null as any), /Input must be a string/);
assert.throws(() => isAlphabetic(undefined as any), /Input must be a string/);
});
});
8 changes: 7 additions & 1 deletion src/validations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export { isHexColor } from './isHexColor';
export { isPalindrome } from './isPalindrome'
export {isLowerCase} from './isLowerCase';
export {isUpperCase} from './isUpperCase';
export { isAlphabetic } from './isAlphabetic';
export { isAlphaNumeric } from './isAlphaNumeric';

import { isCoordinates } from './isCoordinates';
import { isDate } from './isDate';
Expand All @@ -21,6 +23,8 @@ import { isHexColor } from './isHexColor';
import { isPalindrome } from './isPalindrome';
import { isLowerCase } from './isLowerCase';
import { isUpperCase } from './isUpperCase';
import { isAlphabetic } from './isAlphabetic';
import { isAlphaNumeric } from './isAlphaNumeric';

export const validations = {
isCoordinates,
Expand All @@ -33,5 +37,7 @@ export const validations = {
isHexColor,
isPalindrome,
isLowerCase,
isUpperCase
isUpperCase,
isAlphabetic,
isAlphaNumeric
};
21 changes: 21 additions & 0 deletions src/validations/isAlphaNumeric.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Checks whether the given string is alphanumeric.
*
* The check ensures that the string contains only
* letters (a-z, A-Z) and digits (0-9).
*
* @param {string} str - The input string to check.
* @returns {boolean} True if the input is alphanumeric, false otherwise.
* @throws {TypeError} If the input is not a string.
*/
export function isAlphaNumeric(str: string): boolean {
if (typeof str !== 'string') {
throw new TypeError('Input must be a string');
}

if (str.length === 0) {
return false;
}

return /^[a-z0-9]+$/i.test(str);
}
19 changes: 19 additions & 0 deletions src/validations/isAlphabetic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Checks whether a given string contains only alphabetic characters (A-Z, a-z).
*
*
* @param {string} str - The input string to check.
* @returns {boolean} True if the input contains only alphabetic characters, otherwise false.
* @throws {TypeError} If the input is not a string.
*/
export function isAlphabetic(str: string): boolean {
if (typeof str !== 'string') {
throw new TypeError('Input must be a string');
}

// empty string is not considered alphabetic
if (str === '') return false;

// Regular expression to match only alphabetic characters
return /^[A-Za-z]+$/.test(str);
}