Package Exports
- stringops
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (stringops) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
StringOps
StringOps is made to fill in the gaps of String methods in JavaScript that currently exist in other languages but not yet in JavaScript.
More and more methods are being added every week and if using ES6 imports then you can just import the methods you need to save space.
Installation
StringOps is available as both a Node.js package available through NPM and as an ES6 module.
To download StringOps through NPM, use the following command:
$ npm install stringops
From here you can either require the package:
const stringops = require('stringops');
Or if you're developing in an environment that allows the use of imports, you can use:
import * as stringops from './path/to/stringops.js';
Or even further you can import just the methods you need:
import { ltrim, ucwords, nthIndexOf } from './path/to/stringops.js';
API
A breakdown of all of the API can be found
Conversion Functions
bin2Hex(bin) Converts a binary string to hexadecimal.
hex2Bin(hex) Converts a hexadecimal to a binary string.
Search/Find Functions
count(haystack, needle) Count the number of occurances that a substring is found in a string.
countChars(str) Counts the number of times each character appears in the string.
nthIndexOf(str, sub, occurance) Find the position of the nth occurance of a substring in a string.
levenshtein(str1, str2) Calculates the Levenshtein distance between two strings.
Transform Functions
ucword(str) Capitalize the first letter of the first word.
ucwords(str) Capitalizes the first letter of every word.
lcword(str) Make the first letter of the first word lowercase.
lcwords(str) Make the first letters of each word lowercase.
wordwrap(str) Wraps a string to a given number of characters.
reverse(str) Reverses a string.
ltrim(str) Trim all whitespace from the beginning of a string.
rtrim(str) Trim all whitespace from the end of a string.
money(amount, [locale]) Formats a string into a representation of a type of currency. Currently only US/GB is supported is supported but more will be added with later updates.
pad(str, sub, [amount], [side]) Pads a string with a substring on one or both sides.
API
Conversion Methods
bin2Hex(bin)
Converts a binary string to hexadecimal.
Returns: string
- Returns the hexadecimal representation of the binary string.
Version: 0.1.0
Param | Type | Description |
---|---|---|
bin | string |
The binary string to convert to hexadecimal. |
hex2Bin(hex)
Converts a hexadecimal to a binary string.
Returns: string
- Returns the binary representation of the hexadecimal value.
Version: 0.1.0
Param | Type | Description |
---|---|---|
hex | string |
The hexademical value to convert to binary. |
Search Methods
count(haystack, needle)
Count the number of occurances that a substring is found in a string.
Returns: number
- Returns the number of times the substring was found in the string.
Version: 0.1.0
Param | Type | Description |
---|---|---|
haystack | string |
The string to search through. |
needle | string |
The substring to search for. |
countChars(str)
Counts the number of times each character appears in the string.
Returns: Object
- Returns an object with each key being a character and the value being its count.
Version: 0.1.0
Param | Type | Description |
---|---|---|
str | string |
The string to count characters of. |
nthIndexOf(str, sub, occurance)
Find the position of the nth occurance of a substring in a string.
Returns: number
- Returns the index of the substring.
Version: 0.1.0
Param | Type | Description |
---|---|---|
str | string |
The string to search for the substring. |
sub | string |
The substring to searh for. |
occurance | number |
Determines which occurance of the substring will be returned. |
levenshtein(str1, str2)
Calculates the Levenshtein distance between two strings.
Reference: https://gist.github.com/andrei-m/982927
Returns: number
- Returns the number of operations required to match the two strings.
Version: 0.1.0
Param | Type | Description |
---|---|---|
str1 | string |
The first string. |
str2 | string |
The second string. |
Transform Methods
ucword(str)
Capitalize the first letter of the first word.
Returns: string
- Returns the modified string.
Version: 0.1.0
Param | Type | Description |
---|---|---|
str | string |
The string to capitalize. |
ucwords(str)
Capitalizes the first letter of every word.
Returns: string
- Returns the capitalized string.
Version: 0.1.0
Param | Type | Description |
---|---|---|
str | string |
The string to capitalize. |
lcword(str)
Make the first letter of the first word lowercase.
Returns: string
- Returns the modified string.
Version: 0.1.0
Param | Type | Description |
---|---|---|
str | string |
The string to modify. |
lcwords(str)
Make the first letters of each word lowercase.
Returns: string
- Returns the modified string.
Version: 0.1.0
Param | Type | Description |
---|---|---|
str | string |
The string to modify. |
wordwrap(str)
Wraps a string to a given number of characters.
Returns: string
- Returns the wrapped string.
Version: 0.1.0
Param | Type | Description |
---|---|---|
str | string |
The string to wrap. |
reverse(str)
Reverses a string.
Returns: string
- Returns the reversed string.
Version: 0.1.0
Param | Type | Description |
---|---|---|
str | string |
The string to reverse. |
ltrim(str)
Trim all whitespace from the beginning of a string.
Returns: string
- Returns the string without whitespace at the beginning.
Version: 0.1.0
Param | Type | Description |
---|---|---|
str | string |
The string to remove whitespace from. |
rtrim(str)
Trim all whitespace from the end of a string.
Returns: string
- Returns the string without whitespace at the end.
Version: 0.1.0
Param | Type | Description |
---|---|---|
str | string |
The string to remove whitespace from. |
money(amount, locale)
Formats a string into a representation of a type of currency.
Currently only US/GB is supported is supported but more will be added with later updates.
Returns: string
- Returns the amount in a string representing that locale's currency.
Version: 0.1.0
Param | Type | Default | Description |
---|---|---|---|
amount | string | number |
The string or number that represents the money amount. | |
locale | string |
"'US'" |
The locale to use to format the currency. |
pad(str, sub, amount, side)
Pads a string with a substring on one or both sides.
Returns: string
- Returns the padded string.
Version: 0.1.0
Param | Type | Default | Description |
---|---|---|---|
str | string |
The string to pad. | |
sub | string |
The substring to pad the string with. | |
amount | number |
1 |
The amount of times to pad the string with the substring. |
side | string |
"'right'" |
The side of the string to pad. The available options are left, right, or both. If both is selected then the string will be padded evenly on both sides with the right being favored if the amount is not even. |