Comparing version 0.0.1 to 1.0.0
11
index.js
@@ -1,7 +0,8 @@ | ||
require("babel-core/register"); | ||
require('babel-core/register'); | ||
/* | ||
var strman = require('./src/strman'); | ||
var safe = strman.safeTruncate; | ||
//var vitjs = require('./src/vitjs') | ||
//var slugfy = vitjs.slugify | ||
//console.log(slugfy(' Bar bár .')); | ||
console.log(safe('foo bar', 3, '.')); | ||
*/ |
{ | ||
"name": "strman", | ||
"version": "0.0.1", | ||
"version": "1.0.0", | ||
"description": "A Javascript string manipulation library without npm dependences.", | ||
"main": "index.js", | ||
"main": "dist/strman.js", | ||
"directories": { | ||
@@ -10,3 +10,3 @@ "test": "test" | ||
"scripts": { | ||
"test": "eslint . && mocha --compilers js:babel-register" | ||
"test": "eslint ./src && mocha --compilers js:babel-register" | ||
}, | ||
@@ -32,5 +32,15 @@ "repository": { | ||
"babel-preset-es2015": "^6.6.0", | ||
"babel-register": "^6.7.2", | ||
"babelify": "^7.2.0", | ||
"browserify": "^13.0.0", | ||
"chai": "^3.5.0", | ||
"deglobalify": "^0.2.0", | ||
"es6ify": "^1.6.0", | ||
"eslint": "^2.7.0", | ||
"mocha": "^2.4.5" | ||
"gulp": "^3.9.1", | ||
"gulp-uglify": "^1.5.3", | ||
"mocha": "^2.4.5", | ||
"vinyl-buffer": "^1.0.0", | ||
"vinyl-source-stream": "^1.1.0" | ||
} | ||
} |
776
README.md
@@ -1,4 +0,6 @@ | ||
# strman | ||
A Javascript string manipulation library. | ||
# strman | ||
![build](https://travis-ci.org/dleitee/strman.svg?branch=master) | ||
A Javascript string manipulation library without npm dependences. | ||
## Install | ||
@@ -11,2 +13,7 @@ | ||
or bower: | ||
``` | ||
bower install strman | ||
``` | ||
## Usage | ||
@@ -34,41 +41,736 @@ | ||
### With Browser | ||
```html | ||
<script src="./bower_components/strman/dist/strman.js"></script> | ||
``` | ||
```javascript | ||
var result = _s.isString('strman'); | ||
// result => true | ||
``` | ||
Also available for [AMD](https://github.com/amdjs/amdjs-api/wiki/AMD) | ||
# Available Functions | ||
- [x] [slugify](https://github.com/dleitee/strman/blob/master/docs/slugify.md) | ||
- [x] [Utils](https://github.com/dleitee/strman/blob/master/docs/utils.md) | ||
- [x] isString | ||
- [x] trim | ||
- [x] rtrim | ||
- [x] ltrim | ||
- [x] removeSpaces | ||
- [x] replace | ||
- [x] removeNonChars | ||
- [x] removeNonWords | ||
- [x] append | ||
- [x] at | ||
- [x] between | ||
- [x] chars | ||
- [x] collapseWhitespace | ||
- [x] contains | ||
- [x] containsAll | ||
- [x] containsAny | ||
- [x] countSubstr | ||
- [x] endsWith | ||
- [x] startsWith | ||
- [x] ensureLeft | ||
- [x] ensureRight | ||
- [x] first | ||
- [x] last | ||
- [x] hasLowerCase | ||
- [x] hasUpperCase | ||
- [ ] [String Cases](https://github.com/dleitee/strman/blob/master/docs/cases.md) | ||
- [x] toLowerCase | ||
- [ ] toUpperCase | ||
- [ ] toCammelCase | ||
- [ ] toStudlyCaps | ||
- [ ] toDecamelize | ||
- [ ] toSnakeCase | ||
- [ ] toKebabCase | ||
- [x] [append](https://github.com/dleitee/strman#appendvalue-append) | ||
- [x] [appendArray](https://github.com/dleitee/strman#appendarrayvalue-append--) | ||
- [x] [at](https://github.com/dleitee/strman#atvalue-index) | ||
- [x] [between](https://github.com/dleitee/strman#betweenvalue-start-end) | ||
- [x] [chars](https://github.com/dleitee/strman#charsvalue) | ||
- [x] [collapseWhitespace](https://github.com/dleitee/strman#collapsewhitespacevalue) | ||
- [x] [contains](https://github.com/dleitee/strman#containsvalue-needle-casesensitive--true) | ||
- [x] [containsAll](https://github.com/dleitee/strman#containsallvalue-needles---casesensitive--true) | ||
- [x] [containsAny](https://github.com/dleitee/strman#containsanyvalue-needles---casesensitive--true) | ||
- [x] [countSubstr](https://github.com/dleitee/strman#countsubstrvalue-substr-casesensitive--true-allowoverlapping--false) | ||
- [x] [endsWith](https://github.com/dleitee/strman#endswithvalue-search-positionnull) | ||
- [x] [ensureLeft](https://github.com/dleitee/strman#ensureleftvalue-substr) | ||
- [x] [ensureRight](https://github.com/dleitee/strman#ensurerightvalue-substr) | ||
- [x] [first](https://github.com/dleitee/strman#firstvalue-n) | ||
- [x] [includes](https://github.com/dleitee/strman#includes) | ||
- [x] [indexOf](https://github.com/dleitee/strman#indexofvalue-needle-offset--0) | ||
- [x] [insert](https://github.com/dleitee/strman#insertvalue-substr-index) | ||
- [x] [isLowerCase](https://github.com/dleitee/strman#islowercasevalue-n) | ||
- [x] [isString](https://github.com/dleitee/strman#isstringvalue) | ||
- [x] [isUpperCase](https://github.com/dleitee/strman#isuppercasevalue-n) | ||
- [x] [last](https://github.com/dleitee/strman#lastvalue-n) | ||
- [x] [lastIndexOf](https://github.com/dleitee/strman#lastindexofvalue-needle-offset--0) | ||
- [x] [leftPad](https://github.com/dleitee/strman#leftpadvalue-length-char) | ||
- [x] [leftTrim](https://github.com/dleitee/strman#lefttrimvalue) | ||
- [x] [length](https://github.com/dleitee/strman#lengthvalue) | ||
- [x] [prepend](https://github.com/dleitee/strman#prependvalue-prepend) | ||
- [x] [prependArray](https://github.com/dleitee/strman#prependarrayvalue-prepend--) | ||
- [x] [removeLeft](https://github.com/dleitee/strman#removeleftvalue-prefix) | ||
- [x] [removeNonChars](https://github.com/dleitee/strman#removenoncharsvalue) | ||
- [x] [removeNonWords](https://github.com/dleitee/strman#removenonwordsvalue-replace) | ||
- [x] [removeRight](https://github.com/dleitee/strman#removerightvalue-suffix) | ||
- [x] [removeSpaces](https://github.com/dleitee/strman#removespacesvalue-replace) | ||
- [x] [repeat](https://github.com/dleitee/strman#repeatvalue-multiplier) | ||
- [x] [replace](https://github.com/dleitee/strman#replacevalue-search-newvalue) | ||
- [x] [reverse](https://github.com/dleitee/strman#reversevalue) | ||
- [x] [rightPad](https://github.com/dleitee/strman#rightpadvalue-length-char) | ||
- [x] [rightTrim](https://github.com/dleitee/strman#righttrimvalue) | ||
- [x] [safeTruncate](https://github.com/dleitee/strman#safetruncatevalue-length-append) | ||
- [x] [shuffle](https://github.com/dleitee/strman#shufflevalue) | ||
- [x] [slice](https://github.com/dleitee/strman#slicevalue-beginslice-endslice) | ||
- [x] [slugify](https://github.com/dleitee/strman#slugifystring) | ||
- [x] [split](https://github.com/dleitee/strman#splitvalue-separator-limit) | ||
- [x] [startsWith](https://github.com/dleitee/strman#startswithvalue-search-position0) | ||
- [x] [substr](https://github.com/dleitee/strman#substrvalue-start-length) | ||
- [x] [surround](https://github.com/dleitee/strman#surroundvalue-substr) | ||
- [x] [toCamelCase](https://github.com/dleitee/strman#tocamelcasevalue) | ||
- [x] [toDecamelize](https://github.com/dleitee/strman#todecamelizevalue-chr) | ||
- [x] [toKebabCase](https://github.com/dleitee/strman#tokebabcasevalue) | ||
- [x] [toLowerCase](https://github.com/dleitee/strman#tolowercasevalue) | ||
- [x] [toSnakeCase](https://github.com/dleitee/strman#tosnakecasevalue) | ||
- [x] [toStudlyCaps](https://github.com/dleitee/strman#tostudlycapsvalue) | ||
- [x] [toUpperCase](https://github.com/dleitee/strman#touppercasevalue) | ||
- [x] [trim](https://github.com/dleitee/strman#trimvalue) | ||
- [x] [truncate](https://github.com/dleitee/strman#truncatevalue-length-append) | ||
## append([value], ...[append]) | ||
Method to add [...append] on the end of [value]. | ||
```es6 | ||
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]. | ||
```es6 | ||
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]. | ||
```es6 | ||
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]. | ||
```es6 | ||
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. | ||
```es6 | ||
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. | ||
```es6 | ||
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. | ||
```es6 | ||
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. | ||
```es6 | ||
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. | ||
```es6 | ||
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. | ||
```es6 | ||
import {countSubstr} from 'strman' | ||
let title = "Daniel Leite"; | ||
let substr = "Leite"; | ||
let result = countSubstr(title, substr); | ||
// result => 1; | ||
``` | ||
## endsWith([value], [search], [position=null]) | ||
Test if [value] ends with [search] | ||
```es6 | ||
import {endsWith} from 'strman' | ||
let value = "Daniel Leite"; | ||
let search = "Leite"; | ||
let result = endsWith(value, search); | ||
// result => true; | ||
``` | ||
## ensureLeft([value], [substr]) | ||
Ensures that the [value] begins with [substr]. If it doesn't, it's prepended. | ||
```es6 | ||
import {ensureLeft} from 'strman' | ||
let value = "Leite"; | ||
let substr = "Daniel "; | ||
let result = ensureLeft(value, substr); | ||
// result => "Daniel Leite"; | ||
``` | ||
## ensureRight([value], [substr]) | ||
Ensures that the [value] ends with [substr]. If it doesn't, it's appended. | ||
```es6 | ||
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. | ||
```es6 | ||
import {first} from 'strman' | ||
let value = "Daniel"; | ||
let result = first(value, 2); | ||
// result => "Da"; | ||
``` | ||
## includes | ||
Verifies that the needle is contained in value. | ||
```es6 | ||
import {includes} from 'strman' | ||
let title = "Daniel Leite"; | ||
let needle = "Leite"; | ||
let result = includes(title, needle, true); | ||
// result => true; | ||
``` | ||
## indexOf([value], [needle], [offset = 0]) | ||
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. | ||
```es6 | ||
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. | ||
```es6 | ||
import {insert} from 'strman' | ||
let value = "foo"; | ||
let result = insert(value, " bar", 3); | ||
// result => "foo bar"; | ||
``` | ||
## isLowerCase([value], [n]) | ||
Verify if is lowerCase. | ||
```es6 | ||
import {hasLowerCase} from 'strman' | ||
let value = "daniel"; | ||
let result = hasLowerCase(value); | ||
// result => true; | ||
``` | ||
## isString([value]) | ||
Checks whether a string | ||
```es6 | ||
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. | ||
```es6 | ||
import {hasUpperCase} from 'strman' | ||
let value = "DANIEL"; | ||
let result = hasUpperCase(value); | ||
// result => true; | ||
``` | ||
## last([value], [n]) | ||
Return the last n chars of string. | ||
```es6 | ||
import {last} from 'strman' | ||
let value = "Daniel"; | ||
let result = last(value, 2); | ||
// result => "el"; | ||
``` | ||
## lastIndexOf([value], [needle], [offset = 0]) | ||
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. | ||
```es6 | ||
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. | ||
```es6 | ||
import {leftPad} from 'strman' | ||
let value = "foo"; | ||
let result = leftPad(value, 5, '_'); | ||
// result => "__foo"; | ||
``` | ||
## leftTrim([value]) | ||
Remove all spaces on left. | ||
```es6 | ||
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. | ||
```es6 | ||
import {length} from 'strman' | ||
let value = "foo"; | ||
let result = length(value); | ||
// result => 3; | ||
``` | ||
## prepend([value], ...[prepend]) | ||
Prepend Strings [...prepend] on [value]. | ||
```es6 | ||
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]. | ||
```es6 | ||
import {prependArray} from 'strman' | ||
let title = "a"; | ||
let result = prependArray(title, ["b", "c"]); | ||
// result => "bca"; | ||
``` | ||
## removeLeft([value], [prefix]) | ||
Returns a new string with the [prefix] removed, if present. | ||
```es6 | ||
import {removeLeft} from 'strman' | ||
let title = "preabc"; | ||
let result = removeLeft(title, "pre"); | ||
// result => "abc"; | ||
``` | ||
## removeNonChars([value]) | ||
Remove all non valid characters. | ||
```es6 | ||
import {removeNonChars} from 'strman' | ||
let title = "áéíóú"; | ||
let result = removeNonChars(title); | ||
// result => "aeiou"; | ||
``` | ||
## removeNonWords([value], [replace]) | ||
Remove all non word characters. | ||
```es6 | ||
import {removeNonWords} from 'strman' | ||
let title = ".....a"; | ||
let result = removeNonWords(title, ""); | ||
// result => "a"; | ||
``` | ||
## removeRight([value], [suffix]) | ||
Returns a new string with the [suffix] removed, if present. | ||
```es6 | ||
import {removeRight} from 'strman' | ||
let title = "abcpos"; | ||
let result = removeRight(title, "pos"); | ||
// result => "abc"; | ||
``` | ||
## removeSpaces([value], [replace]) | ||
Remove all spaces and replace for value. | ||
```es6 | ||
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. | ||
```es6 | ||
import {repeat} from 'strman' | ||
let result = repeat("a", 4); | ||
// result => "aaaa"; | ||
``` | ||
## replace([value], [search], [newvalue]) | ||
Replace [search] value to [newvalue] | ||
```es6 | ||
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. | ||
```es6 | ||
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. | ||
```es6 | ||
import {rightPad} from 'strman' | ||
let value = "foo"; | ||
let result = rightPad(value, 5, '_'); | ||
// result => "foo__"; | ||
``` | ||
## rightTrim([value]) | ||
Remove all spaces on right. | ||
```es6 | ||
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. | ||
```es6 | ||
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. | ||
```es6 | ||
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. | ||
```es6 | ||
import {slice} from 'strman' | ||
let title = "strman"; | ||
let result = slice(title, 1, 3); | ||
// result => "trm"; | ||
``` | ||
## slugify([String]) | ||
Converts a string to a slug. | ||
```es6 | ||
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. | ||
```es6 | ||
import {split} from 'strman' | ||
let value = "foo"; | ||
let result = substr(split, ""); | ||
// result => ["f", "o", "o"]; | ||
``` | ||
## startsWith([value], [search], [position=0]) | ||
Test if [value] starts with [search] | ||
```es6 | ||
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. | ||
```es6 | ||
import {substr} from 'strman' | ||
let value = "foo"; | ||
let result = substr(value, 1); | ||
// result => "oo"; | ||
``` | ||
## surround([value], [substr]) | ||
Surrounds a [value] with the given [substr]. | ||
```es6 | ||
import {surround} from 'strman' | ||
let value = "foo"; | ||
let result = surround(value, "bar"); | ||
// result => "barfoobar"; | ||
``` | ||
## toCamelCase([value]) | ||
Transform to camelCase. | ||
```es6 | ||
import {toCamelCase} from 'strman' | ||
let value = "camel-case"; | ||
let result = toCamelCase(value); | ||
// result => "camelCase"; | ||
``` | ||
## toDecamelize([value], [chr]) | ||
Transform to uncamelcase. | ||
```es6 | ||
import {toDecamelize} from 'strman' | ||
let value = "camelCase"; | ||
let result = toDecamelize(value); | ||
// result => "camel_case"; | ||
``` | ||
## toKebabCase([value]) | ||
Transform to kebab-case. | ||
```es6 | ||
import {toKebabCase} from 'strman' | ||
let value = "camelCase"; | ||
let result = toKebabCase(value); | ||
// result => "camel-case"; | ||
``` | ||
## toLowerCase([value]) | ||
Transform to lowercase. | ||
```es6 | ||
import {toLowerCase} from 'strman' | ||
let value = "camelCase"; | ||
let result = toLowerCase(value); | ||
// result => "camelcase"; | ||
``` | ||
## toSnakeCase([value]) | ||
Transform to snake_case. | ||
```es6 | ||
import {toSnakeCase} from 'strman' | ||
let value = "camelCase"; | ||
let result = toSnakeCase(value); | ||
// result => "camel_case"; | ||
``` | ||
## toStudlyCaps([value]) | ||
Transform to StudlyCaps. | ||
```es6 | ||
import {toStudlyCaps} from 'strman' | ||
let value = "camelCase"; | ||
let result = toStudlyCaps(value); | ||
// result => "CamelCase"; | ||
``` | ||
## toUpperCase([value]) | ||
Transform to UPPERCASE. | ||
```es6 | ||
import {toUpperCase} from 'strman' | ||
let value = "camelCase"; | ||
let result = toUpperCase(value); | ||
// result => "CAMELCASE"; | ||
``` | ||
## trim([value]) | ||
Remove all spaces on left and right | ||
#### Usage | ||
```es6 | ||
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. | ||
```es6 | ||
import {truncate} from 'strman' | ||
let title = "Checks whether a string "; | ||
let result = truncate(title, 7, "..."); | ||
// result => "Chec..."; | ||
``` | ||
# LICENSE | ||
The MIT License (MIT) | ||
@@ -75,0 +777,0 @@ |
@@ -5,3 +5,3 @@ export const _isArray = value => { | ||
Object.prototype.toString.call(value) === '[object Array]'); | ||
} | ||
}; | ||
@@ -14,2 +14,2 @@ export const _pop = array => { | ||
return newarray; | ||
} | ||
}; |
@@ -130,5 +130,5 @@ export const ascii = { | ||
'ZH': ['Ж'], | ||
' ': ["\xC2\xA0", "\xE2\x80\x80", "\xE2\x80\x81", "\xE2\x80\x82", "\xE2\x80\x83", | ||
"\xE2\x80\x84", "\xE2\x80\x85", "\xE2\x80\x86", "\xE2\x80\x87", "\xE2\x80\x88", | ||
"\xE2\x80\x89", "\xE2\x80\x8A", "\xE2\x80\xAF", "\xE2\x81\x9F", "\xE3\x80\x80"] | ||
' ': ["\xC2\xA0", "\xE2\x80\x80", "\xE2\x80\x81", "\xE2\x80\x82", "\xE2\x80\x83", // eslint-disable-line | ||
"\xE2\x80\x84", "\xE2\x80\x85", "\xE2\x80\x86", "\xE2\x80\x87", "\xE2\x80\x88", // eslint-disable-line | ||
"\xE2\x80\x89", "\xE2\x80\x8A", "\xE2\x80\xAF", "\xE2\x81\x9F", "\xE3\x80\x80"] // eslint-disable-line | ||
}; |
@@ -1,1 +0,1 @@ | ||
export const _isNumber = value => Object.prototype.toString.call(value) === '[object Number]' | ||
export const _isNumber = value => Object.prototype.toString.call(value) === '[object Number]'; |
@@ -1,24 +0,23 @@ | ||
import {toLowerCase, trim, removeSpaces, replace, removeNonChars} from './string.utils'; | ||
import {format} from './lib/format'; | ||
import {toLowerCase, trim, removeSpaces, replace, removeNonChars} from './strman'; | ||
/* | ||
* Converts a string to a slug. | ||
* Example: slugify("A Javascript string manipulation library") => "a-javascript-string-manipulation-library" | ||
* @param string - value for slugfy | ||
* @return String - returns a slugfy string. | ||
* Converts a value to a slug. | ||
* Example: slugify('A Javascript string manipulation library') => 'a-javascript-string-manipulation-library' | ||
* @param value - value for slugify | ||
* @return String - returns a slugify value. | ||
*/ | ||
export function slugify(string) { | ||
const slugify = (value) => { | ||
return format(string, | ||
[ | ||
toLowerCase, | ||
trim, | ||
removeSpaces("-"), | ||
replace("&","-and-"), | ||
replace("\-\-+","-"), | ||
removeNonChars, | ||
replace("[^\\w\\-]+", "") | ||
] | ||
); | ||
} | ||
let result = value; | ||
result = toLowerCase(result); | ||
result = trim(result); | ||
result = removeSpaces(result, '-'); | ||
result = replace(result, '&','-and-'); | ||
result = replace(result, '\-\-+','-'); | ||
result = removeNonChars(result); | ||
result = replace(result, '[^\\w\\-]+', ''); | ||
return result; | ||
}; | ||
export {slugify}; |
@@ -1,4 +0,5 @@ | ||
import {ascii} from "./lib/ascii"; | ||
import {_isNumber} from "./lib/number"; | ||
import {_pop} from "./lib/array"; | ||
import {ascii} from './lib/ascii'; | ||
import {_pop} from './lib/array'; | ||
import {validString, validArrayString, validNumber, validCharLength} from './lib/validate'; | ||
import {toUpperCase} from './string.cases'; | ||
@@ -10,11 +11,6 @@ /* | ||
*/ | ||
export const isString = value => | ||
const isString = value => | ||
Object.prototype.toString.call(value) === '[object String]'; | ||
/* | ||
* Transform to lowercase | ||
* @params value - String to lowercase | ||
* @return String | ||
*/ | ||
export const toLowerCase = value => value.toLowerCase(); | ||
export {isString}; | ||
@@ -26,4 +22,6 @@ /* | ||
*/ | ||
export const trim = value => _ltrim(_rtrim(value)); | ||
const trim = value => leftTrim(rightTrim(value)); | ||
export {trim}; | ||
/* | ||
@@ -34,4 +32,5 @@ * Remove spaces left | ||
*/ | ||
const leftTrim = value => replace(value, '^\\s+', ''); | ||
export const ltrim = value => _ltrim(value); | ||
export {leftTrim}; | ||
@@ -43,23 +42,28 @@ /* | ||
*/ | ||
export const rtrim = value => _rtrim(value); | ||
const rightTrim = value => replace(value, '\\s+$', ''); | ||
export {rightTrim}; | ||
/* | ||
* Remove all spaces and replace for value | ||
* @paramsClojure replace - Value to replace | ||
* @param replace - Value to replace | ||
* @param value - The string being searched and replaced on. | ||
* @return String without spaces | ||
*/ | ||
export const removeSpaces = (replace = "") => (value) => _replace(value, "\\s+", replace); | ||
const removeSpaces = (value, replaced = '') => replace(value, '\\s+', replaced); | ||
export {removeSpaces}; | ||
/* | ||
* Replace [search] value to [newvalue] | ||
* @paramsClojure search - String to search | ||
* @paramsClojure newvalue - String to replace | ||
* @param search - String to search | ||
* @param newvalue - String to replace | ||
* @params value - The string being searched and replaced on. | ||
* @return String replaced | ||
*/ | ||
export const replace = | ||
(search = "", newvalue = "") => | ||
(value) => _replace(value, search, newvalue); | ||
const replace = (value, search = '', newvalue = '') => | ||
value.replace(new RegExp(search, 'g'), newvalue); | ||
export {replace}; | ||
/* | ||
@@ -71,12 +75,14 @@ * Remove all non valid characters | ||
*/ | ||
export const removeNonChars = (value) => { | ||
const removeNonChars = (value) => { | ||
let result = value; | ||
for(let key in ascii){ | ||
for(let char in ascii[key]){ | ||
result = _replace(result, ascii[key][char], key); | ||
result = replace(result, ascii[key][char], key); | ||
} | ||
} | ||
return result; | ||
} | ||
}; | ||
export {removeNonChars}; | ||
/* | ||
@@ -88,24 +94,52 @@ * Append Strings on Value | ||
*/ | ||
export const append = (value, ...append) => { | ||
if(!isString(value)){ | ||
throw new Error("Value is not a String."); | ||
const append = (value, ...appends) => { | ||
validString(value); | ||
validArrayString(appends); | ||
if(length(appends) === 0){ | ||
return value; | ||
} | ||
append.map((data) => { | ||
if(!isString(data)){ | ||
throw new Error("Append is not a String."); | ||
} | ||
return data; | ||
}); | ||
return value + append.join(""); | ||
} | ||
return value + appends.join(''); | ||
}; | ||
export {append}; | ||
/* | ||
* Append Array of Strings on Value | ||
* @param value String initial | ||
* @param ...append - array with strings to append | ||
* @return string | ||
*/ | ||
const appendArray = (value, appends = []) => { | ||
validString(value); | ||
validArrayString(appends); | ||
if(length(appends) === 0){ | ||
return value; | ||
} | ||
return value + appends.join(''); | ||
}; | ||
export {appendArray}; | ||
/* | ||
* Get the character at index | ||
* @param string | ||
* @param value | ||
* @param index | ||
* @return char | ||
*/ | ||
export const at = (string, index) => _at(string, index); | ||
const at = (value, index) => { | ||
validString(value); | ||
validNumber(index); | ||
return substr(value, index, 1); | ||
}; | ||
export {at}; | ||
/* | ||
@@ -118,22 +152,12 @@ * Returns array with strings between [start] and [end] | ||
*/ | ||
export const between = (value, start, end) => { | ||
const between = (value, start, end) => { | ||
let result = null; | ||
if(!isString(value)){ | ||
throw new Error("Value is not a String."); | ||
} | ||
validArrayString([value, start, end]); | ||
if(!isString(start)){ | ||
throw new Error("Start is not a String."); | ||
} | ||
result = split(value, end); | ||
if(!isString(end)){ | ||
throw new Error("End is not a String."); | ||
} | ||
result = value.split(end); | ||
result = result.map((text) => { | ||
return text.substr(text.indexOf(start)+start.length); | ||
return substr(text, indexOf(text, start)+length(start)); | ||
}); | ||
@@ -144,4 +168,6 @@ | ||
return result; | ||
} | ||
}; | ||
export {between}; | ||
/* | ||
@@ -152,15 +178,15 @@ * Returns an array consisting of the characters in the string. | ||
*/ | ||
export const chars = (value) => { | ||
let chars = []; | ||
const chars = value => { | ||
let _chars = []; | ||
if(!isString(value)){ | ||
throw new Error("Value is not a String."); | ||
} | ||
validString(value); | ||
for(let i = 0; i < value.length; i++){ | ||
chars[i] = _at(value, i); | ||
for(let i = 0; i < length(value); i++){ | ||
_chars[i] = at(value, i); | ||
} | ||
return chars; | ||
} | ||
return _chars; | ||
}; | ||
export {chars}; | ||
/* | ||
@@ -171,4 +197,6 @@ * Replaces consecutive whitespace characters with a single space | ||
*/ | ||
export const collapseWhitespace = (value) => _rtrim(_ltrim(_replace(value, "\\s\\s+"," "))); | ||
const collapseWhitespace = (value) => trim(replace(value, '\\s\\s+',' ')); | ||
export {collapseWhitespace}; | ||
/* | ||
@@ -181,5 +209,18 @@ * Remove all non word characters | ||
*/ | ||
export const removeNonWords = (replace ="") => (value) => _replace(value, "[^\\w]+", replace); | ||
const removeNonWords = (value, replaced = '') => replace(value, '[^\\w]+', replaced); | ||
export {removeNonWords}; | ||
/* | ||
* Alias for contains | ||
* @param value | ||
* @param needle | ||
* @param caseSensitive - default true | ||
* @return boolean | ||
*/ | ||
const includes = (value, needle, caseSensitive = true) => contains(value, needle, caseSensitive); | ||
export {includes}; | ||
/* | ||
* Verifies that the needle is contained in value | ||
@@ -191,5 +232,14 @@ * @param value | ||
*/ | ||
export const contains = (value, needle, caseSensitive = true) => | ||
_contains(value, needle, caseSensitive); | ||
const contains = (value, needle, caseSensitive = true) => { | ||
if(caseSensitive){ | ||
return indexOf(value, needle) > -1; | ||
} | ||
return indexOf(toUpperCase(value), toUpperCase(needle)) > -1; | ||
}; | ||
export {contains}; | ||
/* | ||
@@ -202,5 +252,5 @@ * Verifies that all needles are contained in value | ||
*/ | ||
export const containsAll = (value, needles, caseSensitive = true) => { | ||
for(let i = 0; i < needles.length; i++){ | ||
if(!_contains(value, needles[i], caseSensitive)){ | ||
const containsAll = (value, needles, caseSensitive = true) => { | ||
for(let i = 0; i < length(needles); i++){ | ||
if(!contains(value, needles[i], caseSensitive)){ | ||
return false; | ||
@@ -212,2 +262,4 @@ } | ||
export {containsAll}; | ||
/* | ||
@@ -220,5 +272,5 @@ * Verifies that one or more of needles are contained in value | ||
*/ | ||
export const containsAny = (value, needles, caseSensitive = true) => { | ||
for(let i = 0; i < needles.length; i++){ | ||
if(_contains(value, needles[i], caseSensitive)){ | ||
const containsAny = (value, needles, caseSensitive = true) => { | ||
for(let i = 0; i < length(needles); i++){ | ||
if(contains(value, needles[i], caseSensitive)){ | ||
return true; | ||
@@ -230,3 +282,30 @@ } | ||
export {containsAny}; | ||
/* | ||
* Polyfill to countSubstr function | ||
* @param value, | ||
* @param substr, | ||
* @param position = 0, | ||
* @param count = 0, | ||
* @param allowOverlapping = false | ||
* @return integer | ||
*/ | ||
const _countSubstring = (value, _substr, allowOverlapping = false, position = 0, count = 0) => { | ||
let _position = indexOf(value, _substr, position); | ||
if(_position === -1){ | ||
return count; | ||
} | ||
if(!allowOverlapping){ | ||
_position = _position + length(_substr) - 1; | ||
} | ||
return _countSubstring(value, _substr, allowOverlapping, _position + 1, count + 1); | ||
}; | ||
/* | ||
* Count the number of times substr appears in value | ||
@@ -239,14 +318,15 @@ * @param value, | ||
*/ | ||
export const countSubstr = (value, substr, caseSensitive = true, allowOverlapping = false) => { | ||
const countSubstr = (value, _substr, caseSensitive = true, allowOverlapping = false) => { | ||
// TODO: update with polyfill | ||
if(!caseSensitive){ | ||
value = value.toUpperCase(); | ||
substr = substr.toUpperCase(); | ||
value = toUpperCase(value); | ||
_substr = toUpperCase(_substr); | ||
} | ||
return _countSubstring(value, substr, allowOverlapping); | ||
return _countSubstring(value, _substr, allowOverlapping); | ||
}; | ||
export {countSubstr}; | ||
/* | ||
@@ -259,4 +339,20 @@ * Test if [value] ends with [search] | ||
*/ | ||
export const endsWith = (value, search, position = null) => _endsWith(value, search, position); | ||
const endsWith = (value, search, position = null) => { | ||
let lastIndex = null; | ||
if (typeof position !== 'number' || !isFinite(position) | ||
|| Math.floor(position) !== position || position > length(value)) { | ||
position = length(value); | ||
} | ||
position -= length(search); | ||
lastIndex = indexOf(value, search, position); | ||
return lastIndex !== -1 && lastIndex === position; | ||
}; | ||
export {endsWith}; | ||
/* | ||
@@ -269,4 +365,7 @@ * Test if [value] starts with [search] | ||
*/ | ||
export const startsWith = (value, search, position = 0) => _startsWith(value, search, position); | ||
const startsWith = (value, search, position = 0) => | ||
substr(value, position, length(search)) === search; | ||
export {startsWith}; | ||
/* | ||
@@ -278,5 +377,5 @@ * Ensures that the [value] begins with [substr]. If it doesn't, it's prepended. | ||
*/ | ||
export const ensureLeft = (value, substr) => { | ||
if(!_startsWith(value, substr)){ | ||
return substr + "" + value; | ||
const ensureLeft = (value, _substr) => { | ||
if(!startsWith(value, _substr)){ | ||
return append(_substr, value); | ||
} | ||
@@ -287,2 +386,4 @@ | ||
export {ensureLeft}; | ||
/* | ||
@@ -294,6 +395,6 @@ * Ensures that the [value] ends with [substr]. If it doesn't, it's appended. | ||
*/ | ||
export const ensureRight = (value, substr) => { | ||
const ensureRight = (value, _substr) => { | ||
if(!_endsWith(value, substr)){ | ||
return value + "" + substr; | ||
if(!endsWith(value, _substr)){ | ||
return append(value, _substr); | ||
} | ||
@@ -304,2 +405,4 @@ | ||
export {ensureRight}; | ||
/* | ||
@@ -311,4 +414,6 @@ * Return the first n chars of string. | ||
*/ | ||
export const first = (value, n) => value.substr(0, n); | ||
const first = (value, n) => substr(value, 0, n); | ||
export {first}; | ||
/* | ||
@@ -320,130 +425,363 @@ * Return the last n chars of string. | ||
*/ | ||
export const last = (value, n) => value.substr(-1 * n, n); | ||
const last = (value, n) => substr(value, -1 * n, n); | ||
export {last}; | ||
/* | ||
* Verify if has lowerCase | ||
* 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. | ||
* | ||
* @param value | ||
* @return boolean | ||
* @param needle | ||
* @param offset | ||
* @return integer | ||
*/ | ||
// TODO: update to polyfill | ||
export const hasLowerCase = (value) => value === value.toLowerCase(); | ||
const indexOf = (value, needle, offset = 0) => value.indexOf(needle, offset); | ||
export {indexOf}; | ||
/* | ||
* Verify if has upperCase | ||
* 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. | ||
* | ||
* @param value | ||
* @return boolean | ||
* @param needle | ||
* @param offset | ||
* @return integer | ||
*/ | ||
// TODO: update to polyfill | ||
export const hasUpperCase = (value) => value === value.toUpperCase(); | ||
const lastIndexOf = (value, needle, offset = undefined) => value.lastIndexOf(needle, offset); | ||
export {lastIndexOf}; | ||
/* | ||
* Polyfill to startsWith function | ||
* Inserts [substr] into the [value] at the [index] provided. | ||
* @param value | ||
* @param search | ||
* @param position | ||
* @return boolean | ||
* @param substr | ||
* @param index | ||
* @return string | ||
*/ | ||
let _startsWith = (value, search, position = 0) => value.substr(position, search.length) === search; | ||
const insert = (value, _substr, index) => { | ||
let start = null; | ||
let end = null; | ||
if(index > length(value)){ | ||
return value; | ||
} | ||
start = substr(value, 0, index); | ||
end = substr(value, index, length(value)); | ||
return append(start, _substr, end); | ||
}; | ||
export {insert}; | ||
/* | ||
* Polyfill to endsWith function | ||
* Returns the length of the string | ||
* @param value | ||
* @param search | ||
* @param position | ||
* @return boolean | ||
* @return integer | ||
*/ | ||
let _endsWith = (value, search, position = null) => { | ||
const length = value => value.length; | ||
let lastIndex = null; | ||
export {length}; | ||
if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > value.length) { | ||
position = value.length; | ||
/* | ||
* Returns a new string of a given length such that the beginning of the string is padded. | ||
* @param value | ||
* @param length | ||
* @param char | ||
* @return string | ||
*/ | ||
const leftPad = (value, _length, char = ' ') => { | ||
let result = value; | ||
char = String(char); | ||
if(length(char) > 1){ | ||
char = substr(char, 0, 1); | ||
} | ||
position -= search.length; | ||
lastIndex = value.indexOf(search, position); | ||
validCharLength(char); | ||
return lastIndex !== -1 && lastIndex === position; | ||
_length = _length - length(value); | ||
result = append(repeat(char, _length), result); | ||
return result; | ||
}; | ||
export {leftPad}; | ||
/* | ||
* Polyfill to countSubstr function | ||
* @param value, | ||
* @param substr, | ||
* @param position = 0, | ||
* @param count = 0, | ||
* @param allowOverlapping = false | ||
* @return integer | ||
* Returns a new string of a given length such that the ending of the string is padded. | ||
* @param value | ||
* @param length | ||
* @param char | ||
* @return string | ||
*/ | ||
let _countSubstring = (value, substr, allowOverlapping = false, position = 0, count = 0) => { | ||
const rightPad = (value, _length, char = ' ') => { | ||
let _position = value.indexOf(substr, position); | ||
let result = value; | ||
char = String(char); | ||
if(_position === -1){ | ||
return count; | ||
if(length(char) > 1){ | ||
char = substr(char, 0, 1); | ||
} | ||
if(!allowOverlapping){ | ||
_position = _position + substr.length - 1; | ||
} | ||
validCharLength(char); | ||
return _countSubstring(value, substr, allowOverlapping, _position + 1, count + 1); | ||
_length = _length - length(value); | ||
result = append(result, repeat(char, _length)); | ||
return result; | ||
}; | ||
export {rightPad}; | ||
/* | ||
* Polyfill to contains function | ||
* Alias to substr function | ||
* @param value | ||
* @param needle | ||
* @param caseSensitive - default true | ||
* return boolean | ||
* @param start | ||
* @param length = undefined | ||
* @return string | ||
*/ | ||
let _contains = (value, needle, caseSensitive = true) => { | ||
if(caseSensitive){ | ||
return value.indexOf(needle) > -1; | ||
const substr = (value, start, length = undefined) => value.substr(start, length); | ||
export {substr}; | ||
/* | ||
* Alias to split function | ||
* @param value | ||
* @param separator | ||
* @param limit = undefined | ||
* @return string | ||
*/ | ||
const split = (value, separator, limit = undefined) => value.split(separator, limit); | ||
export {split}; | ||
/* | ||
* Returns a new string starting with [prepends]. | ||
* @param value | ||
* @param ...prepends | ||
* @return string | ||
*/ | ||
const prepend = (value, ...prepends) => { | ||
validString(value); | ||
validArrayString(prepends); | ||
if(length(prepends) === 0){ | ||
return value; | ||
} | ||
// TODO: update to polyfill | ||
return value.toUpperCase().indexOf(needle.toUpperCase()) > -1; | ||
return prepends.join('') + value; | ||
}; | ||
export {prepend}; | ||
/* | ||
* Returns a new string starting with [prepends]. | ||
* @param value | ||
* @param ...prepends | ||
* @return string | ||
*/ | ||
const prependArray = (value, prepends = []) => { | ||
validString(value); | ||
validArrayString(prepends); | ||
if(length(prepends) === 0){ | ||
return value; | ||
} | ||
return prepends.join('') + value; | ||
}; | ||
export {prependArray}; | ||
/* | ||
* Polyfill to replace function | ||
* @params value - The string being searched and replaced on. | ||
* @return This function returns a string with the replaced values. | ||
* Returns a new string with the [prefix] removed, if present. | ||
* @param value | ||
* @param prefix | ||
* @return string | ||
*/ | ||
const removeLeft = (value, prefix) => { | ||
if(startsWith(value, prefix)){ | ||
return substr(value, length(prefix)); | ||
} | ||
return value; | ||
}; | ||
export {removeLeft}; | ||
/* | ||
* Returns a new string with the [suffix] removed, if present. | ||
* @param value | ||
* @param prefix | ||
* @return string | ||
*/ | ||
const removeRight = (value, suffix) => { | ||
let _length = length(value) - length(suffix); | ||
if(endsWith(value, suffix)){ | ||
return substr(value, 0, _length); | ||
} | ||
return value; | ||
}; | ||
export {removeRight}; | ||
/* | ||
* Returns a repeated string given a multiplier. | ||
* @param value | ||
* @param multiplier | ||
* @return string | ||
*/ | ||
const repeat = (value, multiplier) => { | ||
let i = 0; | ||
let result = ''; | ||
while(multiplier > i++) { | ||
result += value; | ||
} | ||
return result; | ||
}; | ||
export {repeat}; | ||
/* | ||
* Returns a reversed string. | ||
* @param value | ||
* @return string | ||
*/ | ||
const reverse = (value) => { | ||
let i = 0; | ||
let reversed = ''; | ||
while(length(value) > i++){ | ||
reversed = append(reversed, substr(value, -1*i, 1)); | ||
} | ||
return reversed; | ||
}; | ||
export {reverse}; | ||
/* | ||
* A multibyte str_shuffle() function. It returns a string with its characters in random order. | ||
* @param value | ||
* @return string | ||
*/ | ||
const _shuffle =(array) => { | ||
let j; | ||
let x; | ||
let i; | ||
for (i = length(array); i; i -= 1) { | ||
j = Math.floor(Math.random() * i); | ||
x = array[i - 1]; | ||
array[i - 1] = array[j]; | ||
array[j] = x; | ||
} | ||
return array; | ||
}; | ||
const shuffle = (value) => _shuffle(split(value)).join(''); | ||
export {shuffle}; | ||
/* | ||
* Surrounds a [value] with the given [substr]. | ||
* @param value | ||
* @param substr | ||
* @return string | ||
*/ | ||
let _replace = (value, search, newvalue) => | ||
value.replace(new RegExp(search, "g"), newvalue); | ||
const surround = (value, _substr = '') => append(_substr, value, _substr); | ||
export {surround}; | ||
/* | ||
* Polyfill to ltrim function | ||
* @param value - value to ltrim | ||
* The slice method extracts a section of a string and returns a new string. | ||
* @param value | ||
* @param beginSlice | ||
* @param endSlice | ||
* @return string | ||
*/ | ||
let _ltrim = (value) => _replace(value, "^\\s+", ''); | ||
const slice = (value, beginSlice, endSlice = undefined) => value.slice(beginSlice, endSlice); | ||
export {slice}; | ||
/* | ||
* Polyfill to rtrim function | ||
* @param value - value to rtrim | ||
* Truncate the string securely, not cutting a word in half. It always returns the last full word. | ||
* @param value | ||
* @param _length | ||
* @param _append = '' | ||
* @return string | ||
*/ | ||
let _rtrim = (value) => _replace(value, "\\s+$", ''); | ||
const safeTruncate = (value, _length, _append = '') => { | ||
let truncated = ''; | ||
if(_length === 0){ | ||
return ''; | ||
} | ||
if (_length >= length(value)) { | ||
return value; | ||
} | ||
if(_length === indexOf(value, ' ', 0)){ | ||
return substr(value, 0, _length); | ||
} | ||
_length -= length(_append) ; | ||
truncated = substr(value, 0, _length); | ||
let position = indexOf(value, ' ', _length - 1); | ||
if(position !== _length){ | ||
let lastPos = lastIndexOf(truncated, ' ', 0); | ||
truncated = substr(truncated, 0, lastPos); | ||
} | ||
return append(truncated, _append); | ||
}; | ||
export {safeTruncate}; | ||
/* | ||
* Polyfill to at function | ||
* @param string | ||
* @param int | ||
* @return char | ||
* Truncate the unsecured form string, cutting the independent string of required position. | ||
* @param value | ||
* @param _length | ||
* @param _append = '' | ||
* @return string | ||
*/ | ||
let _at = (string, index) => { | ||
if(!isString(string)){ | ||
throw new Error("Value is not a String."); | ||
const truncate = (value, _length, _append = '') => { | ||
let truncated = ''; | ||
if(_length === 0){ | ||
return ''; | ||
} | ||
if(!_isNumber(index)){ | ||
throw new Error("Index is not a Number."); | ||
if (_length >= length(value)) { | ||
return value; | ||
} | ||
return string.substr(index, 1); | ||
} | ||
_length -= length(_append) ; | ||
truncated = substr(value, 0, _length); | ||
return append(truncated, _append); | ||
}; | ||
export {truncate}; |
@@ -1,3 +0,5 @@ | ||
export * from './slugify' | ||
export * from './slugify'; | ||
export * from './string.utils' | ||
export * from './string.utils'; | ||
export * from './string.cases'; |
import chai from 'chai'; | ||
import {slugify} from '../src/strman' | ||
import {slugify} from '../src/strman'; | ||
@@ -16,3 +16,3 @@ describe('Slugfiy function', () => { | ||
'FOO bar' | ||
] | ||
]; | ||
@@ -22,3 +22,3 @@ fixtures.forEach(el => { | ||
}); | ||
}) | ||
}); | ||
it('should be foo-and-bar', () => { | ||
@@ -35,3 +35,3 @@ let fixtures = [ | ||
'FOO & bar' | ||
] | ||
]; | ||
@@ -41,3 +41,3 @@ fixtures.forEach(el => { | ||
}); | ||
}) | ||
}); | ||
@@ -57,3 +57,3 @@ it('should be throw', () => { | ||
}); | ||
}) | ||
}) | ||
}); | ||
}); |
import chai from 'chai'; | ||
import {isString, trim, removeSpaces, replace, removeNonChars, removeNonWords, append, | ||
at, between, chars, collapseWhitespace, contains, containsAll, containsAny, countSubstr, | ||
endsWith, startsWith, ensureLeft, ensureRight, first, last, hasLowerCase, hasUpperCase} | ||
from '../src/strman' | ||
endsWith, startsWith, ensureLeft, ensureRight, first, last, indexOf, lastIndexOf, insert, | ||
length, leftPad, rightPad, prepend, removeLeft, appendArray, prependArray, removeRight, | ||
repeat, reverse, shuffle, surround, safeTruncate, truncate} | ||
from '../src/strman'; | ||
@@ -15,3 +17,3 @@ describe('isString function', () => { | ||
{} | ||
] | ||
]; | ||
@@ -25,4 +27,4 @@ fixtures.forEach(el => { | ||
'string', | ||
"string" | ||
] | ||
'string' | ||
]; | ||
@@ -38,12 +40,12 @@ fixtures.forEach(el => { | ||
let fixtures = [ | ||
"foo bar", | ||
"foo bar ", | ||
" foo bar", | ||
" foo bar " | ||
] | ||
'foo bar', | ||
'foo bar ', | ||
' foo bar', | ||
' foo bar ' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(trim(el)).to.equal("foo bar"); | ||
chai.expect(trim(el)).to.equal('foo bar'); | ||
}); | ||
}) | ||
}); | ||
}); | ||
@@ -54,21 +56,21 @@ | ||
let fixtures = [ | ||
"foo bar", | ||
"foo bar ", | ||
" foo bar", | ||
" foo bar " | ||
] | ||
'foo bar', | ||
'foo bar ', | ||
' foo bar', | ||
' foo bar ' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(removeSpaces()(el)).to.equal("foobar"); | ||
chai.expect(removeSpaces(el)).to.equal('foobar'); | ||
}); | ||
}) | ||
}); | ||
it('should be foo-bar', () => { | ||
let fixtures = [ | ||
"foo bar" | ||
] | ||
'foo bar' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(removeSpaces("-")(el)).to.equal("foo-bar"); | ||
chai.expect(removeSpaces(el, '-')).to.equal('foo-bar'); | ||
}); | ||
}) | ||
}); | ||
}); | ||
@@ -79,7 +81,7 @@ | ||
let fixtures = [ | ||
"foo bar" | ||
] | ||
'foo bar' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(replace("foo", "bar")(el)).to.equal("bar bar"); | ||
chai.expect(replace(el, 'foo', 'bar')).to.equal('bar bar'); | ||
}); | ||
@@ -89,9 +91,9 @@ }); | ||
let fixtures = [ | ||
"foo bar foo" | ||
] | ||
'foo bar foo' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(replace("foo", "bar")(el)).to.equal("bar bar bar"); | ||
chai.expect(replace(el, 'foo', 'bar')).to.equal('bar bar bar'); | ||
}); | ||
}) | ||
}); | ||
}); | ||
@@ -102,7 +104,7 @@ | ||
let fixtures = [ | ||
"fóõ bár" | ||
] | ||
'fóõ bár' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(removeNonChars(el)).to.equal("foo bar"); | ||
chai.expect(removeNonChars(el)).to.equal('foo bar'); | ||
}); | ||
@@ -112,37 +114,82 @@ }); | ||
describe('removeNonWords function', () => { | ||
describe('append function', () => { | ||
it('should be foobar', () => { | ||
let fixtures = [ | ||
"foo bar", | ||
"foo&bar-" | ||
] | ||
chai.expect(append('f', 'o', 'o', 'b', 'a', 'r')).to.equal('foobar'); | ||
chai.expect(append('foobar')).to.equal('foobar'); | ||
chai.expect(append('', 'foobar')).to.equal('foobar'); | ||
}); | ||
it('should be throw', () => { | ||
chai.assert.throws(append.bind(this, '', 1), Error); | ||
chai.assert.throws(append.bind(this, '', true), Error); | ||
chai.assert.throws(append.bind(this, '', false), Error); | ||
chai.assert.throws(append.bind(this, '', 1.2), Error); | ||
chai.assert.throws(append.bind(this, '', {}), Error); | ||
chai.assert.throws(append.bind(this, '', []), Error); | ||
}); | ||
}); | ||
fixtures.forEach(el => { | ||
chai.expect(removeNonWords()(el)).to.equal("foobar"); | ||
}); | ||
describe('appendArray function', () => { | ||
it('should be foobar', () => { | ||
chai.expect(appendArray('f', ['o', 'o', 'b', 'a', 'r'])).to.equal('foobar'); | ||
chai.expect(appendArray('foobar')).to.equal('foobar'); | ||
chai.expect(appendArray('', ['foobar'])).to.equal('foobar'); | ||
}); | ||
it('should be throw', () => { | ||
chai.assert.throws(appendArray.bind(this, '', 1), Error); | ||
chai.assert.throws(appendArray.bind(this, '', true), Error); | ||
chai.assert.throws(appendArray.bind(this, '', false), Error); | ||
chai.assert.throws(appendArray.bind(this, '', 1.2), Error); | ||
chai.assert.throws(appendArray.bind(this, '', {}), Error); | ||
chai.assert.throws(appendArray.bind(this, '', [1]), Error); | ||
chai.assert.throws(appendArray.bind(this, '', [true]), Error); | ||
chai.assert.throws(appendArray.bind(this, '', [false]), Error); | ||
chai.assert.throws(appendArray.bind(this, '', [1.2]), Error); | ||
chai.assert.throws(appendArray.bind(this, '', [{}]), Error); | ||
}); | ||
}); | ||
describe('append function', () => { | ||
describe('prepend function', () => { | ||
it('should be foobar', () => { | ||
chai.expect(append("f", "o", "o", "b", "a", "r")).to.equal("foobar"); | ||
chai.expect(append("foobar")).to.equal("foobar"); | ||
chai.expect(append("", "foobar")).to.equal("foobar"); | ||
chai.expect(prepend('r', 'f', 'o', 'o', 'b', 'a')).to.equal('foobar'); | ||
chai.expect(prepend('foobar')).to.equal('foobar'); | ||
chai.expect(prepend('', 'foobar')).to.equal('foobar'); | ||
chai.expect(prepend('bar', 'foo')).to.equal('foobar'); | ||
}); | ||
it('should be throw', () => { | ||
chai.assert.throws(append.bind(this, "", 1), Error); | ||
chai.assert.throws(append.bind(this, "", []), Error); | ||
chai.assert.throws(append.bind(this, "", true), Error); | ||
chai.assert.throws(append.bind(this, "", false), Error); | ||
chai.assert.throws(append.bind(this, "", 1.2), Error); | ||
chai.assert.throws(append.bind(this, "", {}), Error); | ||
chai.assert.throws(prepend.bind(this, '', 1), Error); | ||
chai.assert.throws(prepend.bind(this, '', []), Error); | ||
chai.assert.throws(prepend.bind(this, '', true), Error); | ||
chai.assert.throws(prepend.bind(this, '', false), Error); | ||
chai.assert.throws(prepend.bind(this, '', 1.2), Error); | ||
chai.assert.throws(prepend.bind(this, '', {}), Error); | ||
}); | ||
}); | ||
describe('prependArray function', () => { | ||
it('should be foobar', () => { | ||
chai.expect(prependArray('r', ['f', 'o', 'o', 'b', 'a'])).to.equal('foobar'); | ||
chai.expect(prependArray('foobar')).to.equal('foobar'); | ||
chai.expect(prependArray('', ['foobar'])).to.equal('foobar'); | ||
chai.expect(prependArray('bar', ['foo'])).to.equal('foobar'); | ||
}); | ||
it('should be throw', () => { | ||
chai.assert.throws(prependArray.bind(this, '', 1), Error); | ||
chai.assert.throws(prependArray.bind(this, '', true), Error); | ||
chai.assert.throws(prependArray.bind(this, '', false), Error); | ||
chai.assert.throws(prependArray.bind(this, '', 1.2), Error); | ||
chai.assert.throws(prependArray.bind(this, '', {}), Error); | ||
chai.assert.throws(prependArray.bind(this, '', [1]), Error); | ||
chai.assert.throws(prependArray.bind(this, '', [true]), Error); | ||
chai.assert.throws(prependArray.bind(this, '', [false]), Error); | ||
chai.assert.throws(prependArray.bind(this, '', [1.2]), Error); | ||
chai.assert.throws(prependArray.bind(this, '', [{}]), Error); | ||
}); | ||
}); | ||
describe('at function', () => { | ||
it('should be f', () => { | ||
chai.expect(at("foobar", 0)).to.equal("f"); | ||
chai.expect(at("ofobar", 1)).to.equal("f"); | ||
chai.expect(at("oobarf", -1)).to.equal("f"); | ||
chai.expect(at("oobafr", -2)).to.equal("f"); | ||
chai.expect(at('foobar', 0)).to.equal('f'); | ||
chai.expect(at('ofobar', 1)).to.equal('f'); | ||
chai.expect(at('oobarf', -1)).to.equal('f'); | ||
chai.expect(at('oobafr', -2)).to.equal('f'); | ||
@@ -157,7 +204,7 @@ }); | ||
chai.assert.throws(at.bind(this, {}, 1), Error); | ||
chai.assert.throws(at.bind(this, "", ""), Error); | ||
chai.assert.throws(at.bind(this, "", []), Error); | ||
chai.assert.throws(at.bind(this, "", true), Error); | ||
chai.assert.throws(at.bind(this, "", false), Error); | ||
chai.assert.throws(at.bind(this, "", {}), Error); | ||
chai.assert.throws(at.bind(this, '', ''), Error); | ||
chai.assert.throws(at.bind(this, '', []), Error); | ||
chai.assert.throws(at.bind(this, '', true), Error); | ||
chai.assert.throws(at.bind(this, '', false), Error); | ||
chai.assert.throws(at.bind(this, '', {}), Error); | ||
}); | ||
@@ -168,26 +215,26 @@ }); | ||
it('should be ["foo"]', () => { | ||
chai.expect(between("[foo]", "[", "]")[0]).to.equal("foo"); | ||
chai.expect(between("<span>foo</span>", "<span>", "</span>")[0]).to.equal("foo"); | ||
chai.expect(between("<span>bar</span><span>foo</span>", "<span>", "</span>")[0]).to.equal("bar"); | ||
chai.expect(between("<span>bar</span><span>foo</span>", "<span>", "</span>")[1]).to.equal("foo"); | ||
chai.expect(between('[foo]', '[', ']')[0]).to.equal('foo'); | ||
chai.expect(between('<span>foo</span>', '<span>', '</span>')[0]).to.equal('foo'); | ||
chai.expect(between('<span>bar</span><span>foo</span>', '<span>', '</span>')[0]).to.equal('bar'); | ||
chai.expect(between('<span>bar</span><span>foo</span>', '<span>', '</span>')[1]).to.equal('foo'); | ||
}); | ||
it('should be throw', () => { | ||
chai.assert.throws(between.bind(this, "", "", 1), Error); | ||
chai.assert.throws(between.bind(this, "", "", []), Error); | ||
chai.assert.throws(between.bind(this, "", "", {}), Error); | ||
chai.assert.throws(between.bind(this, "", "", true), Error); | ||
chai.assert.throws(between.bind(this, "", "", false), Error); | ||
chai.assert.throws(between.bind(this, "", "", 1.2), Error); | ||
chai.assert.throws(between.bind(this, "", 1, ""), Error); | ||
chai.assert.throws(between.bind(this, "", [], ""), Error); | ||
chai.assert.throws(between.bind(this, "", {}, ""), Error); | ||
chai.assert.throws(between.bind(this, "", true, ""), Error); | ||
chai.assert.throws(between.bind(this, "", false, ""), Error); | ||
chai.assert.throws(between.bind(this, "", 1.2, ""), Error); | ||
chai.assert.throws(between.bind(this, 1, "", ""), Error); | ||
chai.assert.throws(between.bind(this, [], "", ""), Error); | ||
chai.assert.throws(between.bind(this, {}, "", ""), Error); | ||
chai.assert.throws(between.bind(this, true, "", ""), Error); | ||
chai.assert.throws(between.bind(this, false, "", ""), Error); | ||
chai.assert.throws(between.bind(this, 1.2, "", ""), Error); | ||
chai.assert.throws(between.bind(this, '', '', 1), Error); | ||
chai.assert.throws(between.bind(this, '', '', []), Error); | ||
chai.assert.throws(between.bind(this, '', '', {}), Error); | ||
chai.assert.throws(between.bind(this, '', '', true), Error); | ||
chai.assert.throws(between.bind(this, '', '', false), Error); | ||
chai.assert.throws(between.bind(this, '', '', 1.2), Error); | ||
chai.assert.throws(between.bind(this, '', 1, ''), Error); | ||
chai.assert.throws(between.bind(this, '', [], ''), Error); | ||
chai.assert.throws(between.bind(this, '', {}, ''), Error); | ||
chai.assert.throws(between.bind(this, '', true, ''), Error); | ||
chai.assert.throws(between.bind(this, '', false, ''), Error); | ||
chai.assert.throws(between.bind(this, '', 1.2, ''), Error); | ||
chai.assert.throws(between.bind(this, 1, '', ''), Error); | ||
chai.assert.throws(between.bind(this, [], '', ''), Error); | ||
chai.assert.throws(between.bind(this, {}, '', ''), Error); | ||
chai.assert.throws(between.bind(this, true, '', ''), Error); | ||
chai.assert.throws(between.bind(this, false, '', ''), Error); | ||
chai.assert.throws(between.bind(this, 1.2, '', ''), Error); | ||
}); | ||
@@ -198,8 +245,8 @@ }); | ||
it('should be ["t", "i", "t", "l", "e"]', () => { | ||
let title = "title"; | ||
chai.expect(chars(title)[0]).to.equal("t"); | ||
chai.expect(chars(title)[1]).to.equal("i"); | ||
chai.expect(chars(title)[2]).to.equal("t"); | ||
chai.expect(chars(title)[3]).to.equal("l"); | ||
chai.expect(chars(title)[4]).to.equal("e"); | ||
let title = 'title'; | ||
chai.expect(chars(title)[0]).to.equal('t'); | ||
chai.expect(chars(title)[1]).to.equal('i'); | ||
chai.expect(chars(title)[2]).to.equal('t'); | ||
chai.expect(chars(title)[3]).to.equal('l'); | ||
chai.expect(chars(title)[4]).to.equal('e'); | ||
}); | ||
@@ -219,12 +266,12 @@ it('should be throw', () => { | ||
let fixtures = [ | ||
"foo bar", | ||
" foo bar ", | ||
" foo bar ", | ||
" foo bar " | ||
] | ||
'foo bar', | ||
' foo bar ', | ||
' foo bar ', | ||
' foo bar ' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(collapseWhitespace(el)).to.equal("foo bar"); | ||
chai.expect(collapseWhitespace(el)).to.equal('foo bar'); | ||
}); | ||
}) | ||
}); | ||
}); | ||
@@ -235,8 +282,8 @@ | ||
let fixtures = [ | ||
"foo bar", | ||
"foo&bar-" | ||
] | ||
'foo bar', | ||
'foo&bar-' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(removeNonWords()(el)).to.equal("foobar"); | ||
chai.expect(removeNonWords(el)).to.equal('foobar'); | ||
}); | ||
@@ -249,51 +296,51 @@ }); | ||
let fixtures = [ | ||
"foo bar", | ||
"bar foo", | ||
"foobar", | ||
"foo" | ||
] | ||
'foo bar', | ||
'bar foo', | ||
'foobar', | ||
'foo' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(contains(el, "foo", true)).to.equal(true); | ||
chai.expect(contains(el, 'foo', true)).to.equal(true); | ||
}); | ||
}) | ||
}); | ||
it('should be true, caseSensitive = false', () => { | ||
let fixtures = [ | ||
"foo bar", | ||
"bar foo", | ||
"foobar", | ||
"foo" | ||
] | ||
'foo bar', | ||
'bar foo', | ||
'foobar', | ||
'foo' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(contains(el, "FOO", false)).to.equal(true); | ||
chai.expect(contains(el, 'FOO', false)).to.equal(true); | ||
}); | ||
}) | ||
}); | ||
it('should be false, caseSensitive = true', () => { | ||
let fixtures = [ | ||
"foo bar", | ||
"bar foo", | ||
"foobar", | ||
"foo" | ||
] | ||
'foo bar', | ||
'bar foo', | ||
'foobar', | ||
'foo' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(contains(el, "FOO", true)).to.equal(false); | ||
chai.expect(contains(el, 'FOO', true)).to.equal(false); | ||
}); | ||
}) | ||
}); | ||
it('should be false, caseSensitive = false', () => { | ||
let fixtures = [ | ||
"foo bar", | ||
"bar foo", | ||
"foobar", | ||
"foo" | ||
] | ||
'foo bar', | ||
'bar foo', | ||
'foobar', | ||
'foo' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(contains(el, "dleitee", false)).to.equal(false); | ||
chai.expect(contains(el, 'dleitee', false)).to.equal(false); | ||
}); | ||
}) | ||
}); | ||
}); | ||
@@ -304,49 +351,49 @@ | ||
let fixtures = [ | ||
"foo bar", | ||
"bar foo", | ||
"foobar" | ||
] | ||
'foo bar', | ||
'bar foo', | ||
'foobar' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(containsAll(el, ["foo", "bar"], true)).to.equal(true); | ||
chai.expect(containsAll(el, ['foo', 'bar'], true)).to.equal(true); | ||
}); | ||
}) | ||
}); | ||
it('should be true, caseSensitive = false', () => { | ||
let fixtures = [ | ||
"foo bar", | ||
"bar foo", | ||
"foobar" | ||
] | ||
'foo bar', | ||
'bar foo', | ||
'foobar' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(containsAll(el, ["FOO", "BAR"], false)).to.equal(true); | ||
chai.expect(containsAll(el, ['FOO', 'BAR'], false)).to.equal(true); | ||
}); | ||
}) | ||
}); | ||
it('should be false, caseSensitive = true', () => { | ||
let fixtures = [ | ||
"foo", | ||
"bar foo", | ||
"foobar", | ||
"foo" | ||
] | ||
'foo', | ||
'bar foo', | ||
'foobar', | ||
'foo' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(containsAll(el, ["FOO", "BAR"], true)).to.equal(false); | ||
chai.expect(containsAll(el, ['FOO', 'BAR'], true)).to.equal(false); | ||
}); | ||
}) | ||
}); | ||
it('should be false, caseSensitive = false', () => { | ||
let fixtures = [ | ||
"foo bar", | ||
"bar foo", | ||
"foobar", | ||
"foo" | ||
] | ||
'foo bar', | ||
'bar foo', | ||
'foobar', | ||
'foo' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(containsAll(el, ["foo", "bar", "dleitee"], false)).to.equal(false); | ||
chai.expect(containsAll(el, ['foo', 'bar', 'dleitee'], false)).to.equal(false); | ||
}); | ||
}) | ||
}); | ||
}); | ||
@@ -358,49 +405,49 @@ | ||
let fixtures = [ | ||
"foo bar", | ||
"bar foo", | ||
"foobar" | ||
] | ||
'foo bar', | ||
'bar foo', | ||
'foobar' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(containsAny(el, ["foo", "bar", "test"], true)).to.equal(true); | ||
chai.expect(containsAny(el, ['foo', 'bar', 'test'], true)).to.equal(true); | ||
}); | ||
}) | ||
}); | ||
it('should be true, caseSensitive = false', () => { | ||
let fixtures = [ | ||
"foo bar", | ||
"bar foo", | ||
"foobar" | ||
] | ||
'foo bar', | ||
'bar foo', | ||
'foobar' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(containsAny(el, ["FOO", "BAR", "Test"], false)).to.equal(true); | ||
chai.expect(containsAny(el, ['FOO', 'BAR', 'Test'], false)).to.equal(true); | ||
}); | ||
}) | ||
}); | ||
it('should be false, caseSensitive = true', () => { | ||
let fixtures = [ | ||
"foo", | ||
"bar foo", | ||
"foobar", | ||
"foo" | ||
] | ||
'foo', | ||
'bar foo', | ||
'foobar', | ||
'foo' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(containsAny(el, ["FOO", "BAR", "TEST"], true)).to.equal(false); | ||
chai.expect(containsAny(el, ['FOO', 'BAR', 'TEST'], true)).to.equal(false); | ||
}); | ||
}) | ||
}); | ||
it('should be false, caseSensitive = false', () => { | ||
let fixtures = [ | ||
"foo bar", | ||
"bar foo", | ||
"foobar", | ||
"foo" | ||
] | ||
'foo bar', | ||
'bar foo', | ||
'foobar', | ||
'foo' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(containsAny(el, ["dleitee"], false)).to.equal(false); | ||
chai.expect(containsAny(el, ['dleitee'], false)).to.equal(false); | ||
}); | ||
}) | ||
}); | ||
}); | ||
@@ -411,10 +458,10 @@ | ||
let fixtures = [ | ||
"aaaaaAaaAA", | ||
"faaaAAaaaaAA", | ||
"aaAAaaaaafA", | ||
"AAaaafaaaaAAAA" | ||
] | ||
'aaaaaAaaAA', | ||
'faaaAAaaaaAA', | ||
'aaAAaaaaafA', | ||
'AAaaafaaaaAAAA' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(countSubstr(el, "a")).to.equal(7); | ||
chai.expect(countSubstr(el, 'a')).to.equal(7); | ||
}); | ||
@@ -425,10 +472,10 @@ }); | ||
let fixtures = [ | ||
"aaaaaaa", | ||
"faaaaaaa", | ||
"aaaaaaaf", | ||
"aaafaaaa" | ||
] | ||
'aaaaaaa', | ||
'faaaaaaa', | ||
'aaaaaaaf', | ||
'aaafaaaa' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(countSubstr(el, "A", false)).to.equal(7); | ||
chai.expect(countSubstr(el, 'A', false)).to.equal(7); | ||
}); | ||
@@ -439,7 +486,7 @@ }); | ||
it('should be 2 with allowOverlaping', () => { | ||
chai.expect(countSubstr("aaa", "aa", true, true)).to.equal(2); | ||
chai.expect(countSubstr('aaa', 'aa', true, true)).to.equal(2); | ||
}); | ||
it('should be 1 without allowOverlaping', () => { | ||
chai.expect(countSubstr("aaa", "aa", true, false)).to.equal(1); | ||
chai.expect(countSubstr('aaa', 'aa', true, false)).to.equal(1); | ||
}); | ||
@@ -451,17 +498,17 @@ }); | ||
let fixtures = [ | ||
"foo bar", | ||
"bar" | ||
] | ||
'foo bar', | ||
'bar' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(endsWith(el, "bar")).to.equal(true); | ||
chai.expect(endsWith(el, 'bar')).to.equal(true); | ||
}); | ||
let fixtures2 = [ | ||
"foo barr", | ||
"barr" | ||
] | ||
'foo barr', | ||
'barr' | ||
]; | ||
fixtures2.forEach(el => { | ||
chai.expect(endsWith(el, "bar", el.length-1)).to.equal(true); | ||
chai.expect(endsWith(el, 'bar', el.length-1)).to.equal(true); | ||
}); | ||
@@ -474,18 +521,18 @@ }); | ||
let fixtures = [ | ||
"foo bar", | ||
"foobar", | ||
"foo" | ||
] | ||
'foo bar', | ||
'foobar', | ||
'foo' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(startsWith(el, "foo")).to.equal(true); | ||
chai.expect(startsWith(el, 'foo')).to.equal(true); | ||
}); | ||
let fixtures2 = [ | ||
"afoo barr", | ||
"afoo" | ||
] | ||
'afoo barr', | ||
'afoo' | ||
]; | ||
fixtures2.forEach(el => { | ||
chai.expect(startsWith(el, "foo", 1)).to.equal(true); | ||
chai.expect(startsWith(el, 'foo', 1)).to.equal(true); | ||
}); | ||
@@ -498,8 +545,8 @@ }); | ||
let fixtures = [ | ||
"bar", | ||
"foobar" | ||
] | ||
'bar', | ||
'foobar' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(ensureLeft(el, "foo")).to.equal("foobar"); | ||
chai.expect(ensureLeft(el, 'foo')).to.equal('foobar'); | ||
}); | ||
@@ -512,8 +559,8 @@ }); | ||
let fixtures = [ | ||
"foo", | ||
"foobar" | ||
] | ||
'foo', | ||
'foobar' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(ensureRight(el, "bar")).to.equal("foobar"); | ||
chai.expect(ensureRight(el, 'bar')).to.equal('foobar'); | ||
}); | ||
@@ -526,8 +573,8 @@ }); | ||
let fixtures = [ | ||
"foo", | ||
"foobar" | ||
] | ||
'foo', | ||
'foobar' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(first(el, 3)).to.equal("foo"); | ||
chai.expect(first(el, 3)).to.equal('foo'); | ||
}); | ||
@@ -540,8 +587,8 @@ }); | ||
let fixtures = [ | ||
"foo", | ||
"foobarfoo" | ||
] | ||
'foo', | ||
'foobarfoo' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(last(el, 3)).to.equal("foo"); | ||
chai.expect(last(el, 3)).to.equal('foo'); | ||
}); | ||
@@ -551,48 +598,153 @@ }); | ||
describe('hasLowerCase function', () => { | ||
describe('indexOf function', () => { | ||
it('should be true', () => { | ||
let fixtures = [ | ||
"", | ||
"foo", | ||
"foobarfoo" | ||
] | ||
let value = 'foobar'; | ||
chai.expect(indexOf(value, 'f')).to.equal(0); | ||
chai.expect(indexOf(value, 'o')).to.equal(1); | ||
chai.expect(indexOf(value, 'b')).to.equal(3); | ||
chai.expect(indexOf(value, 'a')).to.equal(4); | ||
chai.expect(indexOf(value, 'r')).to.equal(5); | ||
chai.expect(indexOf(value, 't')).to.equal(-1); | ||
}); | ||
}); | ||
fixtures.forEach(el => { | ||
chai.expect(hasLowerCase(el)).to.equal(true); | ||
}); | ||
describe('lastIndexOf function', () => { | ||
it('should be true', () => { | ||
let value = 'foobarfoobar'; | ||
chai.expect(lastIndexOf(value, 'f')).to.equal(6); | ||
chai.expect(lastIndexOf(value, 'o')).to.equal(8); | ||
chai.expect(lastIndexOf(value, 'b')).to.equal(9); | ||
chai.expect(lastIndexOf(value, 'a')).to.equal(10); | ||
chai.expect(lastIndexOf(value, 'r')).to.equal(11); | ||
chai.expect(lastIndexOf(value, 't')).to.equal(-1); | ||
}); | ||
it('should be false', () => { | ||
let fixtures = [ | ||
"fooA", | ||
"foobarfoAo" | ||
] | ||
}); | ||
fixtures.forEach(el => { | ||
chai.expect(hasLowerCase(el)).to.equal(false); | ||
}); | ||
describe('insert function', () => { | ||
it('should be foo bar', () => { | ||
chai.expect(insert('fbar', 'oo ', 1)).to.equal('foo bar'); | ||
chai.expect(insert('foo', ' bar', 3)).to.equal('foo bar'); | ||
chai.expect(insert('foo bar', 'asadasd', 13)).to.equal('foo bar'); | ||
}); | ||
}); | ||
describe('hasUpperCase function', () => { | ||
describe('length function', () => { | ||
it('should be 3', () => { | ||
chai.expect(length('foo')).to.equal(3); | ||
chai.expect(length('bar')).to.equal(3); | ||
chai.expect(length('dan')).to.equal(3); | ||
}); | ||
}); | ||
describe('leftPad function', () => { | ||
it('should be 00001', () => { | ||
chai.expect(leftPad('1', 5, 0)).to.equal('00001'); | ||
chai.expect(leftPad('01', 5, 0)).to.equal('00001'); | ||
chai.expect(leftPad('001', 5, 0)).to.equal('00001'); | ||
chai.expect(leftPad('0001', 5, 0)).to.equal('00001'); | ||
chai.expect(leftPad('00001', 5, 0)).to.equal('00001'); | ||
}); | ||
}); | ||
describe('rightPad function', () => { | ||
it('should be 10000', () => { | ||
chai.expect(rightPad('1', 5, 0)).to.equal('10000'); | ||
chai.expect(rightPad('10', 5, 0)).to.equal('10000'); | ||
chai.expect(rightPad('100', 5, 0)).to.equal('10000'); | ||
chai.expect(rightPad('1000', 5, 0)).to.equal('10000'); | ||
chai.expect(rightPad('10000', 5, 0)).to.equal('10000'); | ||
}); | ||
}); | ||
describe('removeLeft function', () => { | ||
it('should be true', () => { | ||
let fixtures = [ | ||
"", | ||
"FOO", | ||
"FOOBARFOO" | ||
] | ||
'foobar', | ||
'bar' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(hasUpperCase(el)).to.equal(true); | ||
chai.expect(removeLeft(el, 'foo')).to.equal('bar'); | ||
}); | ||
}); | ||
it('should be false', () => { | ||
}); | ||
describe('removeRight function', () => { | ||
it('should be true', () => { | ||
let fixtures = [ | ||
"FOOa", | ||
"FOOBARFOOa" | ||
] | ||
'foobar', | ||
'foo' | ||
]; | ||
fixtures.forEach(el => { | ||
chai.expect(hasUpperCase(el)).to.equal(false); | ||
chai.expect(removeRight(el, 'bar')).to.equal('foo'); | ||
}); | ||
chai.expect(removeRight('foofoofoobar', 'bar')).to.equal('foofoofoo'); | ||
chai.expect(removeRight('foofoofoobar', 'foobar')).to.equal('foofoo'); | ||
}); | ||
}); | ||
describe('repeat function', () => { | ||
it('should be 1 repeated', () => { | ||
chai.expect(repeat('1', 1)).to.equal('1'); | ||
chai.expect(repeat('1', 2)).to.equal('11'); | ||
chai.expect(repeat('1', 3)).to.equal('111'); | ||
chai.expect(repeat('1', 4)).to.equal('1111'); | ||
chai.expect(repeat('1', 5)).to.equal('11111'); | ||
}); | ||
}); | ||
describe('reverse function', () => { | ||
it('should be strings reverse', () => { | ||
chai.expect(reverse('foo')).to.equal('oof'); | ||
chai.expect(reverse('daniel')).to.equal('leinad'); | ||
chai.expect(reverse('')).to.equal(''); | ||
chai.expect(reverse('bar')).to.equal('rab'); | ||
chai.expect(reverse('foo_')).to.equal('_oof'); | ||
chai.expect(reverse('f')).to.equal('f'); | ||
}); | ||
}); | ||
describe('shuffle function', () => { | ||
it('should be strings shuffle', () => { | ||
chai.expect(length(shuffle('foo'))).to.equal(3); | ||
chai.expect(length(shuffle('daniel'))).to.equal(6); | ||
chai.expect(shuffle('')).to.equal(''); | ||
chai.expect(shuffle('b')).to.equal('b'); | ||
}); | ||
}); | ||
describe('surround function', () => { | ||
it('should be strings surround', () => { | ||
chai.expect(surround('foo', 'bar')).to.equal('barfoobar'); | ||
chai.expect(surround('daniel', '_')).to.equal('_daniel_'); | ||
chai.expect(surround('', '>')).to.equal('>>'); | ||
chai.expect(surround('bar', '')).to.equal('bar'); | ||
chai.expect(surround('f')).to.equal('f'); | ||
}); | ||
}); | ||
describe('safeTruncate function', () => { | ||
it('should be strings safeTruncated', () => { | ||
chai.expect(safeTruncate('foo bar', 0, '.')).to.equal(''); | ||
chai.expect(safeTruncate('foo bar', 3, '.')).to.equal('foo'); | ||
chai.expect(safeTruncate('foo bar', 2, '.')).to.equal('.'); | ||
chai.expect(safeTruncate('foo bar', 4, '.')).to.equal('foo.'); | ||
chai.expect(safeTruncate('foo bar', 7, '.')).to.equal('foo bar'); | ||
chai.expect(safeTruncate('foo bar', 8, '.')).to.equal('foo bar'); | ||
}); | ||
}); | ||
describe('truncate function', () => { | ||
it('should be strings truncated', () => { | ||
chai.expect(truncate('foo bar', 0, '.')).to.equal(''); | ||
chai.expect(truncate('foo bar', 3, '.')).to.equal('fo.'); | ||
chai.expect(truncate('foo bar', 2, '.')).to.equal('f.'); | ||
chai.expect(truncate('foo bar', 4, '.')).to.equal('foo.'); | ||
chai.expect(truncate('foo bar', 7, '.')).to.equal('foo bar'); | ||
chai.expect(truncate('foo bar', 8, '.')).to.equal('foo bar'); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
104746
25
1753
796
14
2
4
1