Some simple Helpers to manipulate strings and arrays for JavaScript and Typescript.
If you like this package you can Buy me a Coffee ☕️
# To install this package, run:
$ npm install helpers-ts
# or
$ npm i helpers-ts
This package is intended to help you manipulate strings and arrays, in JavaScript and Typescript. It is available from the NPM Registry.
import { toCamel } from 'helpers-ts'
const str = 'Hello world'
console.log(toCamel(str))
// Output: 'helloWorld'
Converts a string into slug.
slugify('Hello world') // 'hello_world'
slugify('Hello world', '-') // 'hello-world'
Converts a string into camelCase.
toCamel('Hello world') // 'helloWorld'
Converts a string into PascalCase.
slugify('Hello world') // 'HelloWorld'
Checks if a string is a valid uuid.
isUuid('5c5a300f-20fb-416f-b026-6f53f8bdc7f5') // true
isUuid('not-uuid') // false
Returns a random and unique Uuid.
uuid() // '5c5a300f-20fb-416f-b026-6f53f8bdc7f5'
Limits a string to a specified number of characters.
limitStr('This is a long string that needs to be limited.', 20)
// 'This is a long strin...'
Returns a random string with a specified number of characters.
randomStr(6) // 'ab12c3'
Replaces occurrences of a string by another.
replaceStr('world', 'earth', 'Hello world') // 'Hello earth'
Trims and removes extra spaces between words with a specified number of space.
squish(' Hello world ', 5) // 'Hello world'
Checks if a word is in a string.
contains('world', 'Hello world') // true
contains('earth', 'Hello world') // false
Check if many words are in a string.
containsAll(['string', 'test'], 'This is a string to test.') // true
containsAll(['number', 'test'], 'This is a string to test.') // false
Joins arrays and returns all possible combinations of input arrays.
You can pass more than 2 arrays.
crossJoin(['red', 'green'], ['small', 'medium'])
// [['red', 'small'], ['red','medium'], ['green', 'small'], ['green','medium']]
Checks if a key exists in an array.
keyExists('foo', ['foo', 'bar', 'baz']) // true
keyExists('qux', ['foo', 'bar', 'baz']) // false
keyExists('foo', { foo: 'bar', baz: 'qux' }) // true
Returns first or last key.
firstKey(['foo', 'bar', 'baz']) // 'foo'
lastKey(['foo', 'bar', 'baz']) // 'baz'
Converts an array into string.
implode(['Foo', 'Bar']) // 'Foo Bar'
implode(['Foo', 'Bar'], ', ') // 'Foo, Bar'
Converts a string into array.
explode('Foo,Bar') // ['Foo', 'Bar']
implode('Foo Bar', ' ') // ['Foo', 'Bar']
Checks if an array is empty.
isEmpty([]) // true
isEmpty([1, 2, 3]) // false
Shuffles array keys.
shuffle([1, 2, 3, 4]) // [3, 1, 4, 2]
Returns a random number between min
and max
.
randBetween(5, 10) // 8
Please see CHANGELOG for more information on what has changed recently.
If you are interested in this project and want to improve it, fix errors or bugs, you're welcome to contribute.
The MIT License (MIT).
Please see License File for more information.