github

dleitee / strman

  • суббота, 23 апреля 2016 г. в 03:12:39
https://github.com/dleitee/strman

JavaScript
A Javascript string manipulation library without npm dependences.



build

A Javascript string manipulation library without npm dependences.

Install

strman is available on npm:

npm install strman --save

or bower:

bower install strman

Usage

With ES6/import

import {slugify} from 'strman';

let title = "A Javascript string manipulation library.";
let result = slugify(title);
// result => "a-javascript-string-manipulation-library"

With require

var slugify = require('strman').slugify;

let title = "A Javascript string manipulation library.";
let result = slugify(title);
// result => "a-javascript-string-manipulation-library"

With Browser

<script src="./bower_components/strman/dist/strman.js"></script>
var result = _s.isString('strman');
// result => true

Also available for AMD

Available Functions

append([value], ...[append])

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";

appendArray([value], [append = []])

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";

at([value], [index])

Get the character of the [index].

import {at} from 'strman'

let title = "abc";
let result = at(title, 1);
// result => "b";

between([value], [start], [end])

Returns array with strings between [start] and [end].

import {between} from 'strman'

let title = "[abc][def]";
let result = between(title, "[", "]");
// result => ["abc", "def"];

chars([value])

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"];

collapseWhitespace([value])

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";

contains([value], [needle], [caseSensitive = true])

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;

containsAll([value], [needles = []], [caseSensitive = 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;

containsAny([value], [needles = []], [caseSensitive = 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;

countSubstr([value], [substr], [caseSensitive = true], [allowOverlapping = false])

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;

endsWith([value], [search], [position=null], [caseSensitive=true])

Test if [value] ends with [search]

import {endsWith} from 'strman'

let value = "Daniel Leite";
let search = "Leite";
let result = endsWith(value, search);
// result => true;

ensureLeft([value], [substr], [caseSensitive=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";

ensureRight([value], [substr], [caseSensitive=true])

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";

first([value], [n])

Return the first n chars of string.

import {first} from 'strman'

let value = "Daniel";
let result = first(value, 2);
// result => "Da";

indexOf([value], [needle], [offset = 0], [caseSensitive=true])

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;

insert([value], [substr], [index])

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";

isLowerCase([value], [n])

Verify if is lowerCase.

import {hasLowerCase} from 'strman'

let value = "daniel";
let result = hasLowerCase(value);
// result => true;

isString([value])

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

isUpperCase([value], [n])

Verify if is upperCase.

import {hasUpperCase} from 'strman'

let value = "DANIEL";
let result = hasUpperCase(value);
// result => true;

last([value], [n])

Return the last n chars of string.

import {last} from 'strman'

let value = "Daniel";
let result = last(value, 2);
// result => "el";

lastIndexOf([value], [needle], [offset = 0], [caseSensitive=true])

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;

leftPad([value], [length], [char])

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";

leftTrim([value])

Remove all spaces on left.

import {leftTrim} from 'strman'

let title = "     Checks whether a string";
let result = leftTrim(title);
// result => "Checks whether a string";

length([value])

Returns the length of the string.

import {length} from 'strman'

let value = "foo";
let result = length(value);
// result => 3;

prepend([value], ...[prepend])

Prepend Strings [...prepend] on [value].

import {prepend} from 'strman'

let title = "a";
let result = prepend(title, "b", "c");
// result => "bca";

prependArray([value], [prepend = []])

Prepend Strings on array [prepend] on [value].

import {prependArray} from 'strman'

let title = "a";
let result = prependArray(title, ["b", "c"]);
// result => "bca";

removeLeft([value], [prefix], [caseSensitive=true])

Returns a new string with the [prefix] removed, if present.

import {removeLeft} from 'strman'

let title = "preabc";
let result = removeLeft(title, "pre");
// result => "abc";

removeNonChars([value])

Remove all non valid characters.

import {removeNonChars} from 'strman'

let title = "áéíóú";
let result = removeNonChars(title);
// result => "aeiou";

removeNonWords([value], [replace])

Remove all non word characters.

import {removeNonWords} from 'strman'

let title = ".....a";
let result = removeNonWords(title, "");
// result => "a";

removeRight([value], [suffix], [caseSensitive=true])

Returns a new string with the [suffix] removed, if present.

import {removeRight} from 'strman'

let title = "abcpos";
let result = removeRight(title, "pos");
// result => "abc";

removeSpaces([value], [replace])

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";

repeat([value], [multiplier])

Returns a repeated string given a multiplier.

import {repeat} from 'strman'

let result = repeat("a", 4);
// result => "aaaa";

replace([value], [search], [newvalue], [caseSensitive=true])

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";

reverse([value])

Returns a reversed string.

import {reverse} from 'strman'

let result = reverse("abc");
// result => "cba";

rightPad([value], [length], [char])

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__";

rightTrim([value])

Remove all spaces on right.

import {rightTrim} from 'strman'

let title = "Checks whether a string     ";
let result = rightTrim(title);
// result => "Checks whether a string";

safeTruncate([value], [length], [append])

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...";

shuffle([value])

It returns a string with its characters in random order.

import {shuffle} from 'strman'

let title = "strman";
let result = shuffle(title);
// result => "nrtmsa";

slice([value], [beginSlice], [endSlice])

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";

slugify([String])

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"

split([value], [separator], [limit])

Alias to split function.

import {split} from 'strman'

let value = "foo";
let result = substr(split, "");
// result => ["f", "o", "o"];

startsWith([value], [search], [position=0], [caseSensitive=true])

Test if [value] starts with [search]

import {startsWith} from 'strman'

let value = "Daniel Leite";
let search = "Daniel";
let result = startsWith(value, search);
// result => true;

substr([value], [start], [length])

Alias to substr function.

import {substr} from 'strman'

let value = "foo";
let result = substr(value, 1);
// result => "oo";

surround([value], [substr])

Surrounds a [value] with the given [substr].

import {surround} from 'strman'

let value = "foo";
let result = surround(value, "bar");
// result => "barfoobar";

toCamelCase([value])

Transform to camelCase.

import {toCamelCase} from 'strman'

let value = "camel-case";
let result = toCamelCase(value);
// result => "camelCase";

toDecamelize([value], [chr])

Transform to uncamelcase.

import {toDecamelize} from 'strman'

let value = "camelCase";
let result = toDecamelize(value);
// result => "camel_case";

toKebabCase([value])

Transform to kebab-case.

import {toKebabCase} from 'strman'

let value = "camelCase";
let result = toKebabCase(value);
// result => "camel-case";

toLowerCase([value])

Transform to lowercase.

import {toLowerCase} from 'strman'

let value = "camelCase";
let result = toLowerCase(value);
// result => "camelcase";

toSnakeCase([value])

Transform to snake_case.

import {toSnakeCase} from 'strman'

let value = "camelCase";
let result = toSnakeCase(value);
// result => "camel_case";

toStudlyCaps([value])

Transform to StudlyCaps.

import {toStudlyCaps} from 'strman'

let value = "camelCase";
let result = toStudlyCaps(value);
// result => "CamelCase";

toUpperCase([value])

Transform to UPPERCASE.

import {toUpperCase} from 'strman'

let value = "camelCase";
let result = toUpperCase(value);
// result => "CAMELCASE";

trim([value])

Remove all spaces on left and right

Usage

import {trim} from 'strman'

let title = "     Checks whether a string     ";
let result = trim(title);
// result => "Checks whether a string";

truncate([value], [length], [append])

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...";

LICENSE

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.