A Javascript string manipulation library without npm dependences.
strman is available on npm:
npm install strman --save
or bower:
bower install strman
import {slugify} from 'strman';
let title = "A Javascript string manipulation library.";
let result = slugify(title);
// result => "a-javascript-string-manipulation-library"
var slugify = require('strman').slugify;
let title = "A Javascript string manipulation library.";
let result = slugify(title);
// result => "a-javascript-string-manipulation-library"
<script src="./bower_components/strman/dist/strman.js"></script>
var result = _s.isString('strman');
// result => true
Also available for AMD
- append
- appendArray
- at
- between
- chars
- collapseWhitespace
- contains
- containsAll
- containsAny
- countSubstr
- endsWith
- ensureLeft
- ensureRight
- first
- indexOf
- insert
- isLowerCase
- isString
- isUpperCase
- last
- lastIndexOf
- leftPad
- leftTrim
- length
- prepend
- prependArray
- removeLeft
- removeNonChars
- removeNonWords
- removeRight
- removeSpaces
- repeat
- replace
- reverse
- rightPad
- rightTrim
- safeTruncate
- shuffle
- slice
- slugify
- split
- startsWith
- substr
- surround
- toCamelCase
- toDecamelize
- toKebabCase
- toLowerCase
- toSnakeCase
- toStudlyCaps
- toUpperCase
- trim
- truncate
Method to add [...append] on the end of [value].
import {append} from 'strman'
let title = "s";
let result = append(title, "tr", "m", "an");
// result => "strman";
Method to add [append[]] on the end of [value].
import {append} from 'strman'
let title = "s";
let result = append(title, ["tr", "m", "an"]);
// result => "strman";
Get the character of the [index].
import {at} from 'strman'
let title = "abc";
let result = at(title, 1);
// result => "b";
Returns array with strings between [start] and [end].
import {between} from 'strman'
let title = "[abc][def]";
let result = between(title, "[", "]");
// result => ["abc", "def"];
Returns an array consisting of the characters in the string.
import {chars} from 'strman'
let title = "abc";
let result = chars(title);
// result => ["a", "b", "c"];
Replaces consecutive whitespace characters with a single space.
import {collapseWhitespace} from 'strman'
let title = " a b c ";
let result = collapseWhitespace(title);
// result => "a b c";
Verifies that the needle is contained in value.
import {contains} from 'strman'
let title = "Daniel Leite";
let needle = "Leite";
let result = contains(title, needle, true);
// result => true;
Verifies that all needles are contained in value.
import {containsAll} from 'strman'
let title = "Daniel Leite";
let needles = ["Leite", "Daniel"];
let result = containsAll(title, needles, true);
// result => true;
Verifies that one or more of needles are contained in value.
import {containsAny} from 'strman'
let title = "Daniel Leite";
let needles = ["Leite", "Daniel", "Oliveira"];
let result = containsAny(title, needles, true);
// result => true;
Count the number of times substr appears in value.
import {countSubstr} from 'strman'
let title = "Daniel Leite";
let substr = "Leite";
let result = countSubstr(title, substr);
// result => 1;
Test if [value] ends with [search]
import {endsWith} from 'strman'
let value = "Daniel Leite";
let search = "Leite";
let result = endsWith(value, search);
// result => true;
Ensures that the [value] begins with [substr]. If it doesn't, it's prepended.
import {ensureLeft} from 'strman'
let value = "Leite";
let substr = "Daniel ";
let result = ensureLeft(value, substr);
// result => "Daniel Leite";
Ensures that the [value] ends with [substr]. If it doesn't, it's appended.
import {ensureRight} from 'strman'
let value = "Daniel";
let substr = " Leite";
let result = ensureRight(value, substr);
// result => "Daniel Leite";
Return the first n chars of string.
import {first} from 'strman'
let value = "Daniel";
let result = first(value, 2);
// result => "Da";
The indexOf method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.
import {indexOf} from 'strman'
let value = "daniel";
let result = indexOf(value, "niel");
// result => 2;
Inserts [substr] into the [value] at the [index] provided.
import {insert} from 'strman'
let value = "foo";
let result = insert(value, " bar", 3);
// result => "foo bar";
Verify if is lowerCase.
import {hasLowerCase} from 'strman'
let value = "daniel";
let result = hasLowerCase(value);
// result => true;
Checks whether a string
import {isString} from 'strman'
let title = "Checks whether a string";
let result = isString(title);
// result => true
let number = 1;
let result = isString(number);
// result => false
Verify if is upperCase.
import {hasUpperCase} from 'strman'
let value = "DANIEL";
let result = hasUpperCase(value);
// result => true;
Return the last n chars of string.
import {last} from 'strman'
let value = "Daniel";
let result = last(value, 2);
// result => "el";
The lastIndexOf method returns the index within the calling String object of the last occurrence of the specified value, searching backwards from fromIndex. Returns -1 if the value is not found.
import {lastIndexOf} from 'strman'
let value = "daniel leite";
let result = lastIndexOf(value, "l");
// result => 7;
Returns a new string of a given length such that the beginning of the string is padded.
import {leftPad} from 'strman'
let value = "foo";
let result = leftPad(value, 5, '_');
// result => "__foo";
Remove all spaces on left.
import {leftTrim} from 'strman'
let title = " Checks whether a string";
let result = leftTrim(title);
// result => "Checks whether a string";
Returns the length of the string.
import {length} from 'strman'
let value = "foo";
let result = length(value);
// result => 3;
Prepend Strings [...prepend] on [value].
import {prepend} from 'strman'
let title = "a";
let result = prepend(title, "b", "c");
// result => "bca";
Prepend Strings on array [prepend] on [value].
import {prependArray} from 'strman'
let title = "a";
let result = prependArray(title, ["b", "c"]);
// result => "bca";
Returns a new string with the [prefix] removed, if present.
import {removeLeft} from 'strman'
let title = "preabc";
let result = removeLeft(title, "pre");
// result => "abc";
Remove all non valid characters.
import {removeNonChars} from 'strman'
let title = "รกรฉรญรณรบ";
let result = removeNonChars(title);
// result => "aeiou";
Remove all non word characters.
import {removeNonWords} from 'strman'
let title = ".....a";
let result = removeNonWords(title, "");
// result => "a";
Returns a new string with the [suffix] removed, if present.
import {removeRight} from 'strman'
let title = "abcpos";
let result = removeRight(title, "pos");
// result => "abc";
Remove all spaces and replace for value.
import {removeSpaces} from 'strman'
let title = "Remove all spaces and replace for value";
let result = removeSpaces(title, "-");
// result => "Checks-whether-a-string";
let result = removeSpaces(title);
// result => "Checkswhetherastring";
Returns a repeated string given a multiplier.
import {repeat} from 'strman'
let result = repeat("a", 4);
// result => "aaaa";
Replace [search] value to [newvalue]
import {replace} from 'strman'
let title = "My car is black";
let result = replace(title, "black", "white");
// result => "My car is white";
Returns a reversed string.
import {reverse} from 'strman'
let result = reverse("abc");
// result => "cba";
Returns a new string of a given length such that the ending of the string is padded.
import {rightPad} from 'strman'
let value = "foo";
let result = rightPad(value, 5, '_');
// result => "foo__";
Remove all spaces on right.
import {rightTrim} from 'strman'
let title = "Checks whether a string ";
let result = rightTrim(title);
// result => "Checks whether a string";
Truncate the string securely, not cutting a word in half. It always returns the last full word.
import {safeTruncate} from 'strman'
let title = "Checks whether a string ";
let result = safeTruncate(title, 9, "...");
// result => "Checks...";
It returns a string with its characters in random order.
import {shuffle} from 'strman'
let title = "strman";
let result = shuffle(title);
// result => "nrtmsa";
The slice method extracts a section of a string and returns a new string.
import {slice} from 'strman'
let title = "strman";
let result = slice(title, 1, 3);
// result => "trm";
Converts a string to a slug.
import {slugify} from 'strman'
let title = "Converts a string to a slug.";
let result = slugify(title);
// result => "converts-a-string-to-a-slug"
Alias to split function.
import {split} from 'strman'
let value = "foo";
let result = substr(split, "");
// result => ["f", "o", "o"];
Test if [value] starts with [search]
import {startsWith} from 'strman'
let value = "Daniel Leite";
let search = "Daniel";
let result = startsWith(value, search);
// result => true;
Alias to substr function.
import {substr} from 'strman'
let value = "foo";
let result = substr(value, 1);
// result => "oo";
Surrounds a [value] with the given [substr].
import {surround} from 'strman'
let value = "foo";
let result = surround(value, "bar");
// result => "barfoobar";
Transform to camelCase.
import {toCamelCase} from 'strman'
let value = "camel-case";
let result = toCamelCase(value);
// result => "camelCase";
Transform to uncamelcase.
import {toDecamelize} from 'strman'
let value = "camelCase";
let result = toDecamelize(value);
// result => "camel_case";
Transform to kebab-case.
import {toKebabCase} from 'strman'
let value = "camelCase";
let result = toKebabCase(value);
// result => "camel-case";
Transform to lowercase.
import {toLowerCase} from 'strman'
let value = "camelCase";
let result = toLowerCase(value);
// result => "camelcase";
Transform to snake_case.
import {toSnakeCase} from 'strman'
let value = "camelCase";
let result = toSnakeCase(value);
// result => "camel_case";
Transform to StudlyCaps.
import {toStudlyCaps} from 'strman'
let value = "camelCase";
let result = toStudlyCaps(value);
// result => "CamelCase";
Transform to UPPERCASE.
import {toUpperCase} from 'strman'
let value = "camelCase";
let result = toUpperCase(value);
// result => "CAMELCASE";
Remove all spaces on left and right
import {trim} from 'strman'
let title = " Checks whether a string ";
let result = trim(title);
// result => "Checks whether a string";
Truncate the unsecured form string, cutting the independent string of required position.
import {truncate} from 'strman'
let title = "Checks whether a string ";
let result = truncate(title, 7, "...");
// result => "Chec...";
The MIT License (MIT)
Copyright (c) 2015 Daniel Leite de Oliveira
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.