stringoperations
A library of helper methods for working with strings.
Installation
Simply do: npm install stringoperations
.
What is it?
It's simply a collection of helper methods for working with strings such as camelCase
, removeWhitespace
and kebabCase
.
Comes with Typescript and flow typings
Example
; ; // returns 'myString'; // returns 'myString'; // returns 'myString'; // returns 'myComplexStringHasASpace'; // returns [26, 31]
Changelog:
v0.08:
- Added a
native-stripped.js
version that is non-transpiled, non-minified, but stripped of types.
v0.07:
- Added Typescript typings and .js.flow files.
v0.06:
- Fixed a few bugs with
parsePrimitiveFromString
with converting strings to floating point numbers.
v0.05:
- Improved the
kebabCase()
method.
v0.04:
- Added a new method:
parsePrimitiveFromString()
.
v0.03:
- Fixed a small bug in the
kebabCase
method.
v0.01:
- First release!
Usage
Import it in your project like this:
; // (standard) Transpiled to ES5.// or // or
Documentation
#allIndexesOf()
Returns all indexes of the given regex matches in the input string.
Signature:
: number
Arguments:
-
regex: RegExp
- The Regular Expression to match. -
str: string
- The string to match occurrences in. -
startingFrom: number
- The index position to start from.default = 0
Returns:
number[]
- An array of index matches
Throws:
-
TypeError
- If the first argument is not of type 'RegExp'. -
TypeError
- If the second argument is not of type 'string'. -
TypeError
- If the third argument is not of type 'number'. -
RangeError
- If the given 'startingFrom' value is out of bounds.
Example
; // returns [26, 31]
#capitalize()
Capitalizes the given string.
Signature:
: string
Arguments:
str: string
- The string to capitalize.
Returns:
string
- The capitalized string.
Throws:
TypeError
- If given input is not of type 'string'.
Example
; // returns 'My string'
#removeWhitespace()
Removes all whitespace from a string.
Signature:
: string
Arguments:
-
str: string
- The string to remove whitespace from. -
preserveSpaces: boolean
- If spaces should be preserved. Tabs will be converted to spaces. Multiple spaces will be collapsed to one.default: false
Returns:
string
- The string with all whitespace removed.
Throws:
TypeError
- If given input is not of type 'string'.
Example
; // returns 'mystring'; // returns 'my string'.; // returns 'mymultilinestring
#lowerCaseFirst()
Lowercases the first character of the given string.
Signature:
: string
Arguments:
str: string
- The string to lowercase the first character of.
Returns:
string
- The string with the first character lowercased.
Throws:
TypeError
- If given input is not of type 'string'.
Example
; // returns 'my string'
#camelCase()
camelCases the given string.
Signature:
: string
Arguments:
str: string
- The string to camelCase.
Returns:
string
- The camelCased string.
Throws:
TypeError
- If given input is not of type 'string'.
Example
; // returns 'myString'; // returns 'myString'; // returns 'myString'; // returns 'myComplexStringHasASpace'
#kebabCase()
kebab-cases the given string.
Signature:
: string
Arguments:
str: string
- The string to kebab-case.
Returns:
string
- The kebab-cased string.
Throws:
TypeError
- If given input is not of type 'string'.
Example
; // returns 'my-string'; // returns 'my-string'; // returns 'my-string'; // returns 'my-string'; // returns 'my-complex-string-has-stuff-between-words'
#parsePrimitiveFromString()
Will return a matching primitive value from a given string. For instance, the string 'true' will return the boolean value true.
Signature:
: any
Arguments:
str: string
- The string to convert to a native primitive type.
Returns:
any
- The primitive value or the string itself.
Throws:
TypeError
- If given input is not of type 'string'.
Example
; // Returns the boolean true; // Returns the number 12.; // Returns the string "12 monkeys".