stringoperationsdeprecated

A library of helper methods for working with strings.

Usage no npm install needed!

<script type="module">
  import stringoperations from 'https://cdn.skypack.dev/stringoperations';
</script>

README

stringoperations NPM version

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

import {camelCase, allIndexesOf} from "stringoperations";

camelCase("my-string"); // returns 'myString'
camelCase("my string"); // returns 'myString'
camelCase("MY_STRING"); // returns 'myString'
camelCase("my-complex_string HAS a_SPACE"); // returns 'myComplexStringHasASpace'
allIndexesOf(/_/g, "my string has underscores _here_"); // 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:

import {camelCase, ...} from "stringoperations"; // (standard) Transpiled to ES5.
// or

import {camelCase, ...} from "stringoperations/native" // Transpiled to ES5, native ES-modules.
// or

import {camelCase, ...} from "stringoperations/typed" // Flow typings, ES-modules.

import {camelCase, ...} from "stringoperations/native-stripped" // Native ES-code, but stripped of typings.

Documentation

#allIndexesOf()

Returns all indexes of the given regex matches in the input string.

Signature:

allIndexesOf (regex: RegExp, str: string, startingFrom: number = 0): 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

allIndexesOf(/_/g, "my string has underscores _here_"); // returns [26, 31]

#capitalize()

Capitalizes the given string.

Signature:

capitalize (str: string): string

Arguments:

  • str: string - The string to capitalize.

Returns:

string - The capitalized string.

Throws:

  • TypeError - If given input is not of type 'string'.

Example

capitalize("my string"); // returns 'My string'

#removeWhitespace()

Removes all whitespace from a string.

Signature:

removeWhitespace (str: string, preserveSpaces: boolean = false): 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

removeWhitespace("my string"); // returns 'mystring'
removeWhitespace("my        string", true); // returns 'my string'.
removeWhitespace(`my
                  multiline
                  string
                  `
                ); // returns 'mymultilinestring

#lowerCaseFirst()

Lowercases the first character of the given string.

Signature:

lowerCaseFirst (str: string): 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

lowerCaseFirst("My string"); // returns 'my string'

#camelCase()

camelCases the given string.

Signature:

camelCase (str: string): string

Arguments:

  • str: string - The string to camelCase.

Returns:

string - The camelCased string.

Throws:

  • TypeError - If given input is not of type 'string'.

Example

camelCase("my-string"); // returns 'myString'
camelCase("my string"); // returns 'myString'
camelCase("MY_STRING"); // returns 'myString'
camelCase("my-complex_string HAS a_SPACE"); // returns 'myComplexStringHasASpace'

#kebabCase()

kebab-cases the given string.

Signature:

kebabCase (str: string): 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

kebabCase("myString"); // returns 'my-string'
kebabCase("my string"); // returns 'my-string'
kebabCase("MY_STRING"); // returns 'my-string'
kebabCase("my_string"); // returns 'my-string'
kebabCase("my complex_string HAS stuff+between----words"); // 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:

parsePrimitiveFromString (str: string): 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

parsePrimitiveFromString("true");       // Returns the boolean true
parsePrimitiveFromString("12");         // Returns the number 12.
parsePrimitiveFromString("12 monkeys"); // Returns the string "12 monkeys".