https://github.com/lexriver/string-methods
https://github.com/lexriver/string-methods
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/lexriver/string-methods
- Owner: LexRiver
- Created: 2020-12-03T15:27:39.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-09-25T17:13:27.000Z (almost 2 years ago)
- Last Synced: 2025-06-13T10:48:21.054Z (about 1 year ago)
- Language: TypeScript
- Size: 21.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# StringMethods
Few methods for working with strings
## Install
```
npm install @lexriver/string-methods
```
## Import
``` typescript
import {StringMethods} from '@lexriver/string-methods'
import {StringMethods, LocaleSettings} from '@lexriver/string-methods'
```
## Methods
### `StringMethods.isDigits(x:string):boolean`
Checks if string contains only digits: 0123456790
``` typescript
StringMethods.isDigits('345') // true
```
### `StringMethods.isValidEmail(x:any):boolean`
Simple check if string contains some text before @ and at least one dot and text after dot after @
``` typescript
StringMethods.isValidEmail('test@me.pls') // true
```
### `StringMethods.getSimpleHash(value:string):number`
Get simple fast hash of a string
https://stackoverflow.com/questions/6122571/simple-non-secure-hash-function-for-javascript
``` typescript
StringMethods.getSimpleHash('test') === StringMethods.getSimpleHash('test') // true
StringMethods.getSimpleHash('test') === StringMethods.getSimpleHash('tset') // false
```
### `StringMethods.formatPluralNumber(p)`
Get a nice string for plural number.
Arguments
```typescript
StringMethods.formatPluralNumber(p:{
number: number,
settings: { // StringMethods.LocaleSettings type
locale?:string,
ifZero:(x:number)=>any,
ifOne:(x:number)=>any,
ifFew:(x:number)=>any,
ifMany:(x:number)=>any
}
}
```
Example
```typescript
let settings:StringMethods.LocaleSettings = {
locale:'ru',
ifZero:(x) => `товары не найдены`,
ifOne:(x) => `${x} товар`,
ifFew:(x) => `${x} товара`,
ifMany:(x) => `${x} товаров`
}
StringMethods.formatPluralNumber({ number: 33, settings })) // 33 товара
```