Comparing version 0.2.0 to 0.3.0
mout changelog | ||
============== | ||
v0.3.0 | ||
------ | ||
- add `lang/clone`. | ||
- add `lang/toString`. | ||
- add `string/replace`. | ||
- add `string/WHITE_SPACES` | ||
- rename `function/curry` to `function/partial`. | ||
- allow custom chars in `string/trim`, `ltrim`, and `rtrim`. | ||
- convert values to strings in the `string/*` functions. | ||
v0.2.0 | ||
@@ -5,0 +17,0 @@ ------ |
@@ -17,3 +17,3 @@ # function # | ||
See: [`curry()`](#curry) | ||
See: [`partial()`](#partial) | ||
@@ -41,24 +41,2 @@ | ||
## curry(fn, [...args]):Function | ||
Return a partially applied function supplying default arguments. | ||
This method is similar to [`bind`](#bind), except it does not alter the this | ||
binding. | ||
### Arguments | ||
1. `fn` (Function) : Target Function | ||
3. `[...args]` (*) : Arguments (0...n arguments) | ||
See: [`bind()`](#bind) | ||
```js | ||
function add(a, b){ return a + b } | ||
var add10 = curry(add, 10); | ||
console.log( add10(2) ); // 12 | ||
``` | ||
## debounce(fn, delay[, isAsap]):Function | ||
@@ -108,2 +86,24 @@ | ||
## partial(fn, [...args]):Function | ||
Return a partially applied function supplying default arguments. | ||
This method is similar to [`bind`](#bind), except it does not alter the this | ||
binding. | ||
### Arguments | ||
1. `fn` (Function) : Target Function | ||
3. `[...args]` (*) : Arguments (0...n arguments) | ||
See: [`bind()`](#bind) | ||
```js | ||
function add(a, b){ return a + b } | ||
var add10 = partial(add, 10); | ||
console.log( add10(2) ); // 12 | ||
``` | ||
## prop(name):Function | ||
@@ -110,0 +110,0 @@ |
@@ -7,10 +7,9 @@ # lang # | ||
## deepClone(val, [instanceClone]):* | ||
## clone(val):* | ||
Deep clone native types like Object, Array, RegExp, Date and primitives. | ||
Clone native types like Object, Array, RegExp, Date and primitives. | ||
The `instanceClone` function will be invoked to clone objects that are not | ||
"plain" objects (as defined by [`isPlainObject`](#isPlainObject)) if it is | ||
provided. If `instanceClone` is not specified, it will not attempt to clone | ||
non-plain objects, and will copy the object reference. | ||
This method will not clone values that are referenced within `val`. It will | ||
only copy the value reference to the new value. If the value is not a plain | ||
object but is an object, it will return the value unchanged. | ||
@@ -20,24 +19,17 @@ ### Example | ||
```js | ||
var a = {foo:'bar', obj: {a:1, b:2}}; | ||
var b = deepClone(a); // {foo:'bar', obj: {a:1, b:2}} | ||
console.log( a === b ); // false | ||
console.log( a.obj === b.obj ); // false | ||
var a = { foo: 'bar' }; | ||
var b = clone(a); | ||
console.log(a === b); // false | ||
console.log(a.foo === b.foo); // true | ||
var c = [1, 2, [3, 4]]; | ||
var d = deepClone(c); // [1, 2, [3, 4]] | ||
var e = c.concat(); // [1, 2, [3, 4]] | ||
var c = [1, 2, 3]; | ||
var d = clone(b); | ||
console.log(c === d); // false | ||
console.log(c); // [1, 2, 3] | ||
``` | ||
console.log( c[2] === d[2] ); // false | ||
// concat doesn't do a deep clone, arrays are passed by reference | ||
console.log( e[2] === d[2] ); // true | ||
See: [`deepClone()`](#deepClone) | ||
function Custom() { } | ||
function cloneCustom(x) { return new Custom(); } | ||
var f = { test: new Custom() }; | ||
var g = deepClone(f, cloneCustom); | ||
g.test === f.test // false, since new Custom instance will be created | ||
``` | ||
## createObject(parent, [props]):Object | ||
@@ -96,2 +88,38 @@ | ||
## deepClone(val, [instanceClone]):* | ||
Deep clone native types like Object, Array, RegExp, Date and primitives. | ||
The `instanceClone` function will be invoked to clone objects that are not | ||
"plain" objects (as defined by [`isPlainObject`](#isPlainObject)) if it is | ||
provided. If `instanceClone` is not specified, it will not attempt to clone | ||
non-plain objects, and will copy the object reference. | ||
### Example | ||
```js | ||
var a = {foo:'bar', obj: {a:1, b:2}}; | ||
var b = deepClone(a); // {foo:'bar', obj: {a:1, b:2}} | ||
console.log( a === b ); // false | ||
console.log( a.obj === b.obj ); // false | ||
var c = [1, 2, [3, 4]]; | ||
var d = deepClone(c); // [1, 2, [3, 4]] | ||
var e = c.concat(); // [1, 2, [3, 4]] | ||
console.log( c[2] === d[2] ); // false | ||
// concat doesn't do a deep clone, arrays are passed by reference | ||
console.log( e[2] === d[2] ); // true | ||
function Custom() { } | ||
function cloneCustom(x) { return new Custom(); } | ||
var f = { test: new Custom() }; | ||
var g = deepClone(f, cloneCustom); | ||
g.test === f.test // false, since new Custom instance will be created | ||
``` | ||
See: [`clone()`](#clone) | ||
## defaults(val, ...defaults):void | ||
@@ -296,2 +324,12 @@ | ||
## toString(val):String | ||
Convert any value to its string representation. | ||
Will return an empty string for `undefined` or `null`, otherwise will convert | ||
the value to its string representation. | ||
------------------------------------------------------------------------------- | ||
@@ -298,0 +336,0 @@ |
@@ -182,6 +182,9 @@ # string # | ||
## ltrim(str):String | ||
## ltrim(str, [chars]):String | ||
Remove white-spaces from beginning of string. | ||
Remove chars or white-spaces from beginning of string. | ||
`chars` is an array of chars to remove from the beginning of the string. If | ||
`chars` is not specified, Unicode whitespace chars will be used instead. | ||
See: [`rtrim()`](#rtrim), [`trim()`](#trim) | ||
@@ -192,3 +195,4 @@ | ||
```js | ||
ltrim(' lorem ipsum '); // "lorem ipsum " | ||
ltrim(' lorem ipsum '); // "lorem ipsum " | ||
ltrim('--lorem ipsum--', ['-']); // "lorem ipsum--" | ||
``` | ||
@@ -302,2 +306,23 @@ | ||
## replace(str, search, replace):String | ||
Replace string(s) with the replacement(s) in the source. | ||
`search` and `replace` can be an array, or a single item. For every item in | ||
`search`, it will call `str.replace` with the search item and the matching | ||
replacement in `replace`. If `replace` only contains one replacement, it will | ||
be used for all the searches, otherwise it will use the replacement at the same | ||
index as the search. | ||
### Example | ||
```js | ||
replace('foo bar', 'foo', 'test'); // "test bar" | ||
replace('test 1 2', ['1', '2'], 'n'); // "test n n" | ||
replace('test 1 2', ['1', '2'], ['one', 'two']); // "test one two" | ||
replace('123abc', [/\d/g, /[a-z]/g], ['0', '.']); // "000..." | ||
``` | ||
## replaceAccents(str):String | ||
@@ -334,6 +359,9 @@ | ||
## rtrim(str):String | ||
## rtrim(str, [chars]):String | ||
Remove white-spaces from end of string. | ||
Remove chars or white-spaces from end of string. | ||
`chars` is an array of chars to remove from the end of the string. If | ||
`chars` is not specified, Unicode whitespace chars will be used instead. | ||
See: [`trim()`](#trim), [`ltrim()`](#ltrim) | ||
@@ -344,3 +372,4 @@ | ||
```js | ||
rtrim(' lorem ipsum '); // " lorem ipsum" | ||
rtrim(' lorem ipsum '); // " lorem ipsum" | ||
rtrim('--lorem ipsum--', ['-']); // "--lorem ipsum" | ||
``` | ||
@@ -410,5 +439,9 @@ | ||
## trim(str):String | ||
## trim(str, [chars]):String | ||
Remove chars or white-spaces from beginning and end of string. | ||
Remove white-spaces from beginning and end of string. | ||
`chars` is an array of chars to remove from the beginning and end of the | ||
string. If `chars` is not specified, Unicode whitespace chars will be used | ||
instead. | ||
@@ -420,3 +453,4 @@ See: [`rtrim()`](#rtrim), [`ltrim()`](#ltrim) | ||
```js | ||
trim(' lorem ipsum '); // "lorem ipsum" | ||
trim(' lorem ipsum '); // "lorem ipsum" | ||
trim('-+-lorem ipsum-+-', ['-', '+']); // "lorem ipsum" | ||
``` | ||
@@ -551,2 +585,11 @@ | ||
## WHITE_SPACES:Array | ||
Constant array of all [Unicode white-space | ||
characters](http://en.wikipedia.org/wiki/Whitespace_character). | ||
------------------------------------------------------------------------------- | ||
@@ -553,0 +596,0 @@ |
@@ -8,5 +8,5 @@ | ||
'compose' : require('./function/compose'), | ||
'curry' : require('./function/curry'), | ||
'debounce' : require('./function/debounce'), | ||
'func' : require('./function/func'), | ||
'partial' : require('./function/partial'), | ||
'prop' : require('./function/prop'), | ||
@@ -13,0 +13,0 @@ 'series' : require('./function/series'), |
@@ -6,3 +6,3 @@ | ||
module.exports = { | ||
'VERSION' : '0.2.0', | ||
'VERSION' : '0.3.0', | ||
'array' : require('./array'), | ||
@@ -9,0 +9,0 @@ 'collection' : require('./collection'), |
@@ -6,2 +6,3 @@ | ||
module.exports = { | ||
'clone' : require('./lang/clone'), | ||
'createObject' : require('./lang/createObject'), | ||
@@ -29,5 +30,6 @@ 'ctorApply' : require('./lang/ctorApply'), | ||
'kindOf' : require('./lang/kindOf'), | ||
'toArray' : require('./lang/toArray') | ||
'toArray' : require('./lang/toArray'), | ||
'toString' : require('./lang/toString') | ||
}; | ||
@@ -0,1 +1,2 @@ | ||
var clone = require('./clone'); | ||
var forOwn = require('../object/forOwn'); | ||
@@ -6,23 +7,13 @@ var kindOf = require('./kindOf'); | ||
/** | ||
* Clone native types. | ||
* Recursively clone native types. | ||
*/ | ||
function deepClone(val, instanceClone) { | ||
var result; | ||
switch ( kindOf(val) ) { | ||
case 'Object': | ||
result = cloneObject(val, instanceClone); | ||
break; | ||
return cloneObject(val, instanceClone); | ||
case 'Array': | ||
result = cloneArray(val, instanceClone); | ||
break; | ||
case 'RegExp': | ||
result = cloneRegExp(val); | ||
break; | ||
case 'Date': | ||
result = cloneDate(val); | ||
break; | ||
return cloneArray(val, instanceClone); | ||
default: | ||
result = val; | ||
return clone(val); | ||
} | ||
return result; | ||
} | ||
@@ -44,14 +35,2 @@ | ||
function cloneRegExp(r) { | ||
var flags = ''; | ||
flags += r.multiline? 'm' : ''; | ||
flags += r.global? 'g' : ''; | ||
flags += r.ignoreCase? 'i' : ''; | ||
return new RegExp(r.source, flags); | ||
} | ||
function cloneDate(date) { | ||
return new Date( date.getTime() ); | ||
} | ||
function cloneArray(arr, instanceClone) { | ||
@@ -58,0 +37,0 @@ var out = [], |
{ | ||
"name": "mout", | ||
"description": "Modular Utilities", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"homepage": "http://moutjs.com/", | ||
"contributors": [ | ||
"Adam Nowotny", | ||
"André Cruz", | ||
"André Cruz <amdfcruz@gmail.com>", | ||
"Conrad Zimmerman (http://www.conradz.com)", | ||
@@ -37,3 +37,3 @@ "Friedemann Altrock <frodenius@gmail.com>", | ||
"prepublish": "node build cjs .", | ||
"pretest" : "node build pkg", | ||
"pretest": "node build pkg", | ||
"test": "node node_modules/istanbul/lib/cli test tests/runner.js --hook-run-in-context" | ||
@@ -46,9 +46,10 @@ }, | ||
"istanbul": "~0.1.27", | ||
"jasmine-node" : "~1.0.28", | ||
"requirejs" : "2.x", | ||
"jasmine-node": "~1.0.28", | ||
"requirejs": "2.x", | ||
"nodefy": "*", | ||
"mdoc": "~0.3.2", | ||
"handlebars": "~1.0.6", | ||
"commander": "~1.0.5" | ||
"commander": "~1.0.5", | ||
"rocambole": "~0.2.3" | ||
} | ||
} | ||
} |
@@ -8,5 +8,5 @@ define(function(require){ | ||
'compose' : require('./function/compose'), | ||
'curry' : require('./function/curry'), | ||
'debounce' : require('./function/debounce'), | ||
'func' : require('./function/func'), | ||
'partial' : require('./function/partial'), | ||
'prop' : require('./function/prop'), | ||
@@ -13,0 +13,0 @@ 'series' : require('./function/series'), |
@@ -6,3 +6,3 @@ define(function(require){ | ||
return { | ||
'VERSION' : '0.2.0', | ||
'VERSION' : '0.3.0', | ||
'array' : require('./array'), | ||
@@ -9,0 +9,0 @@ 'collection' : require('./collection'), |
@@ -6,2 +6,3 @@ define(function(require){ | ||
return { | ||
'clone' : require('./lang/clone'), | ||
'createObject' : require('./lang/createObject'), | ||
@@ -29,5 +30,6 @@ 'ctorApply' : require('./lang/ctorApply'), | ||
'kindOf' : require('./lang/kindOf'), | ||
'toArray' : require('./lang/toArray') | ||
'toArray' : require('./lang/toArray'), | ||
'toString' : require('./lang/toString') | ||
}; | ||
}); |
@@ -1,25 +0,15 @@ | ||
define(['../object/forOwn', './kindOf', './isPlainObject'], function (forOwn, kindOf, isPlainObject) { | ||
define(['./clone', '../object/forOwn', './kindOf', './isPlainObject'], function (clone, forOwn, kindOf, isPlainObject) { | ||
/** | ||
* Clone native types. | ||
* Recursively clone native types. | ||
*/ | ||
function deepClone(val, instanceClone) { | ||
var result; | ||
switch ( kindOf(val) ) { | ||
case 'Object': | ||
result = cloneObject(val, instanceClone); | ||
break; | ||
return cloneObject(val, instanceClone); | ||
case 'Array': | ||
result = cloneArray(val, instanceClone); | ||
break; | ||
case 'RegExp': | ||
result = cloneRegExp(val); | ||
break; | ||
case 'Date': | ||
result = cloneDate(val); | ||
break; | ||
return cloneArray(val, instanceClone); | ||
default: | ||
result = val; | ||
return clone(val); | ||
} | ||
return result; | ||
} | ||
@@ -41,14 +31,2 @@ | ||
function cloneRegExp(r) { | ||
var flags = ''; | ||
flags += r.multiline? 'm' : ''; | ||
flags += r.global? 'g' : ''; | ||
flags += r.ignoreCase? 'i' : ''; | ||
return new RegExp(r.source, flags); | ||
} | ||
function cloneDate(date) { | ||
return new Date( date.getTime() ); | ||
} | ||
function cloneArray(arr, instanceClone) { | ||
@@ -55,0 +33,0 @@ var out = [], |
@@ -6,2 +6,3 @@ define(function(require){ | ||
return { | ||
'WHITE_SPACES' : require('./string/WHITE_SPACES'), | ||
'camelCase' : require('./string/camelCase'), | ||
@@ -26,2 +27,3 @@ 'contains' : require('./string/contains'), | ||
'repeat' : require('./string/repeat'), | ||
'replace' : require('./string/replace'), | ||
'replaceAccents' : require('./string/replaceAccents'), | ||
@@ -28,0 +30,0 @@ 'rpad' : require('./string/rpad'), |
@@ -1,16 +0,13 @@ | ||
define(['./replaceAccents', './removeNonWord', './upperCase', './lowerCase'], function(replaceAccents, removeNonWord, upperCase, lowerCase){ | ||
define(['../lang/toString', './replaceAccents', './removeNonWord', './upperCase', './lowerCase'], function(toString, replaceAccents, removeNonWord, upperCase, lowerCase){ | ||
/** | ||
* Convert string to camelCase text. | ||
* - ported from Miller Medeiros Eclipse Monkey Scripts | ||
* @example camelCase('my -- awesome-text') -> 'myAwesomeText'; | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
function camelCase(str){ | ||
str = toString(str); | ||
str = replaceAccents(str); | ||
str = removeNonWord(str) | ||
.replace(/\-/g, ' ') //convert all hyphens to spaces | ||
.replace(/\s[a-z]/g, upperCase) //convert first char of each word to UPPERCASE | ||
.replace(/\s+/g, '') //remove spaces | ||
.replace(/^[A-Z]/g, lowerCase); //convert first char to lowercase | ||
.replace(/\-/g, ' ') //convert all hyphens to spaces | ||
.replace(/\s[a-z]/g, upperCase) //convert first char of each word to UPPERCASE | ||
.replace(/\s+/g, '') //remove spaces | ||
.replace(/^[A-Z]/g, lowerCase); //convert first char to lowercase | ||
return str; | ||
@@ -17,0 +14,0 @@ } |
@@ -1,7 +0,10 @@ | ||
define(function () { | ||
define(['../lang/toString'], function(toString) { | ||
/** | ||
* searches for a given substring | ||
* Searches for a given substring | ||
*/ | ||
function contains(str, substring){ | ||
str = toString(str); | ||
substring = toString(substring); | ||
return str.indexOf(substring) !== -1; | ||
@@ -8,0 +11,0 @@ } |
@@ -1,2 +0,2 @@ | ||
define(['./truncate'], function (truncate) { | ||
define(['../lang/toString', './truncate'], function (toString, truncate) { | ||
/** | ||
@@ -6,3 +6,4 @@ * Truncate string at full words. | ||
function crop(str, maxChars, append) { | ||
return truncate(str, maxChars, append, true); | ||
str = toString(str); | ||
return truncate(str, maxChars, append, true); | ||
} | ||
@@ -9,0 +10,0 @@ |
@@ -1,16 +0,13 @@ | ||
define(function () { | ||
define(['../lang/toString'], function(toString) { | ||
/** | ||
* Checks if string ends with specified suffix. | ||
* @example endsWith('lorem ipsum', 'ipsum') -> true | ||
* @example endsWith('lorem ipsum', 'lorem') -> false | ||
* @param {string} str | ||
* @param {string} suffix | ||
* @return {bool} | ||
*/ | ||
* Checks if string ends with specified suffix. | ||
*/ | ||
function endsWith(str, suffix) { | ||
str = (str || ''); | ||
suffix = (suffix || ''); | ||
str = toString(str); | ||
suffix = toString(suffix); | ||
return str.indexOf(suffix, str.length - suffix.length) !== -1; | ||
} | ||
return endsWith; | ||
}); |
@@ -1,2 +0,2 @@ | ||
define(function () { | ||
define(['../lang/toString'], function(toString) { | ||
@@ -7,8 +7,8 @@ /** | ||
function escapeHtml(str){ | ||
str = (str || '') | ||
.replace(/&/g, '&') | ||
.replace(/</g, '<') | ||
.replace(/>/g, '>') | ||
.replace(/'/g, ''') | ||
.replace(/"/g, '"'); | ||
str = toString(str) | ||
.replace(/&/g, '&') | ||
.replace(/</g, '<') | ||
.replace(/>/g, '>') | ||
.replace(/'/g, ''') | ||
.replace(/"/g, '"'); | ||
return str; | ||
@@ -15,0 +15,0 @@ } |
@@ -1,4 +0,4 @@ | ||
define(function () { | ||
define(['../lang/toString'], function(toString) { | ||
var _rEscapeChars; | ||
var ESCAPE_CHARS = /[\\.+*?\^$\[\](){}\/'#]/g; | ||
@@ -9,6 +9,4 @@ /** | ||
function escapeRegExp(str) { | ||
if (! _rEscapeChars) { | ||
_rEscapeChars = /[\\.+*?\^$\[\](){}\/'#]/g; | ||
} | ||
return str.replace(_rEscapeChars,'\\$&'); | ||
str = toString(str); | ||
return str.replace(ESCAPE_CHARS,'\\$&'); | ||
} | ||
@@ -15,0 +13,0 @@ |
@@ -1,2 +0,2 @@ | ||
define(function () { | ||
define(['../lang/toString'], function(toString) { | ||
@@ -7,3 +7,3 @@ /** | ||
function escapeUnicode(str, shouldEscapePrintable){ | ||
if (!str) return ''; | ||
str = toString(str); | ||
return str.replace(/[\s\S]/g, function(ch){ | ||
@@ -10,0 +10,0 @@ // skip printable ASCII chars if we should not escape them |
@@ -1,11 +0,12 @@ | ||
define(['./slugify', './unCamelCase'], function(slugify, unCamelCase){ | ||
define(['../lang/toString', './slugify', './unCamelCase'], function(toString, slugify, unCamelCase){ | ||
/** | ||
* Replaces spaces with hyphens, split camelCase text, remove non-word chars, remove accents and convert to lower case. | ||
* @example hyphenate('loremIpsum dolor spéçïãl chârs') -> 'lorem-ipsum-dolor-special-chars' | ||
*/ | ||
* Replaces spaces with hyphens, split camelCase text, remove non-word chars, remove accents and convert to lower case. | ||
*/ | ||
function hyphenate(str){ | ||
str = toString(str); | ||
str = unCamelCase(str); | ||
return slugify(str, "-"); | ||
} | ||
return hyphenate; | ||
}); |
@@ -1,2 +0,2 @@ | ||
define(function () { | ||
define(['../lang/toString'], function(toString) { | ||
@@ -9,4 +9,5 @@ var stache = /\{\{(\w+)\}\}/g; //mustache-like | ||
function interpolate(template, replacements, syntax){ | ||
template = toString(template); | ||
var replaceFn = function(match, prop){ | ||
return (prop in replacements)? replacements[prop] : ''; | ||
return (prop in replacements)? toString(replacements[prop]) : ''; | ||
}; | ||
@@ -13,0 +14,0 @@ return template.replace(syntax || stache, replaceFn); |
@@ -1,9 +0,11 @@ | ||
define(function(){ | ||
define(['../lang/toString'], function(toString){ | ||
/** | ||
* "Safer" String.toLowerCase() | ||
*/ | ||
function upperCase(str){ | ||
return (str || '').toLowerCase(); | ||
function lowerCase(str){ | ||
str = toString(str); | ||
return str.toLowerCase(); | ||
} | ||
return upperCase; | ||
return lowerCase; | ||
}); |
@@ -1,2 +0,2 @@ | ||
define(['./repeat'], function (repeat) { | ||
define(['../lang/toString', './repeat'], function(toString, repeat) { | ||
@@ -7,4 +7,7 @@ /** | ||
function lpad(str, minLen, ch) { | ||
str = toString(str); | ||
ch = ch || ' '; | ||
return (str.length < minLen)? repeat(ch, minLen - str.length) + str : str; | ||
return ((str.length < minLen) | ||
? repeat(ch, minLen - str.length) + str : str); | ||
} | ||
@@ -11,0 +14,0 @@ |
@@ -1,12 +0,33 @@ | ||
define(function(){ | ||
define(['../lang/toString', './WHITE_SPACES'], function(toString, WHITE_SPACES){ | ||
/** | ||
* Remove white-spaces from beginning of string. | ||
* @example stringUtils.ltrim(' lorem ipsum ') -> 'lorem ipsum ' | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
function ltrim(str){ | ||
return (str || '').replace(/^\s+/g, ''); | ||
* Remove chars from beginning of string. | ||
*/ | ||
function ltrim(str, chars) { | ||
str = toString(str); | ||
chars = chars || WHITE_SPACES; | ||
var start = 0, | ||
len = str.length, | ||
charLen = chars.length, | ||
found = true, | ||
i, c; | ||
while (found && start < len) { | ||
found = false; | ||
i = -1; | ||
c = str.charAt(start); | ||
while (++i < charLen) { | ||
if (c === chars[i]) { | ||
found = true; | ||
start++; | ||
break; | ||
} | ||
} | ||
} | ||
return (start >= len) ? '' : str.substr(start, len); | ||
} | ||
return ltrim; | ||
}); |
@@ -1,2 +0,2 @@ | ||
define(function () { | ||
define(['../lang/toString'], function (toString) { | ||
@@ -7,7 +7,9 @@ /** | ||
function normalizeLineBreaks(str, lineEnd) { | ||
str = toString(str); | ||
lineEnd = lineEnd || '\n'; | ||
return str | ||
.replace(/\r\n/g, lineEnd) // DOS | ||
.replace(/\r/g, lineEnd) // Mac | ||
.replace(/\n/g, lineEnd); // Unix | ||
.replace(/\r\n/g, lineEnd) // DOS | ||
.replace(/\r/g, lineEnd) // Mac | ||
.replace(/\n/g, lineEnd); // Unix | ||
} | ||
@@ -14,0 +16,0 @@ |
@@ -1,9 +0,11 @@ | ||
define(['./camelCase', './upperCase'], function(camelCase, upperCase){ | ||
define(['../lang/toString', './camelCase', './upperCase'], function(toString, camelCase, upperCase){ | ||
/** | ||
* camelCase + uppercase first char | ||
* camelCase + UPPERCASE first char | ||
*/ | ||
function pascalCase(str){ | ||
str = toString(str); | ||
return camelCase(str).replace(/^[a-z]/, upperCase); | ||
} | ||
return pascalCase; | ||
}); |
@@ -1,13 +0,11 @@ | ||
define(['./lowerCase', './upperCase'], function(lowerCase, upperCase){ | ||
define(['../lang/toString', './lowerCase', './upperCase'], function(toString, lowerCase, upperCase){ | ||
/** | ||
* UPPERCASE first char of each word. | ||
* - ported from Miller Medeiros Eclipse Monkey Scripts | ||
* @example properCase('loRem iPSum') -> 'Lorem Ipsum' | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
function properCase(str){ | ||
return lowerCase(str).replace(/^\w|\s\w/g, upperCase); //replace first char of each word to UPPERCASE | ||
str = toString(str); | ||
return lowerCase(str).replace(/^\w|\s\w/g, upperCase); | ||
} | ||
return properCase; | ||
}); |
@@ -1,11 +0,14 @@ | ||
define(function(){ | ||
define(['../lang/toString'], function(toString){ | ||
/** | ||
* Remove non-printable ASCII chars | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
* Remove non-printable ASCII chars | ||
*/ | ||
function removeNonASCII(str){ | ||
return (str || '').replace(/[^\x20-\x7E]/g, ''); //matches non-printable ASCII chars - http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters | ||
str = toString(str); | ||
// Matches non-printable ASCII chars - | ||
// http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters | ||
return str.replace(/[^\x20-\x7E]/g, ''); | ||
} | ||
return removeNonASCII; | ||
}); |
@@ -1,12 +0,11 @@ | ||
define(function(){ | ||
define(['../lang/toString'], function(toString){ | ||
/** | ||
* Remove non-word chars. | ||
* @example removeNonWord('lorem! ipsum?') -> 'lorem ipsum' | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
function removeNonWord(str){ | ||
return (str || '').replace(/[^0-9a-zA-Z\xC0-\xFF \-]/g, ''); //remove non-word chars | ||
str = toString(str); | ||
return str.replace(/[^0-9a-zA-Z\xC0-\xFF \-]/g, ''); | ||
} | ||
return removeNonWord; | ||
}); |
@@ -1,2 +0,2 @@ | ||
define(function(){ | ||
define(['../lang/toString'], function(toString){ | ||
@@ -7,3 +7,4 @@ /** | ||
function repeat(str, n){ | ||
return (new Array(n + 1)).join(str); | ||
str = toString(str); | ||
return (new Array(n + 1)).join(str); | ||
} | ||
@@ -10,0 +11,0 @@ |
@@ -1,12 +0,8 @@ | ||
define(function(){ | ||
define(['../lang/toString'], function(toString){ | ||
/** | ||
* Replaces all accented chars with regular ones | ||
* - ported from Miller Medeiros AS3 StringUtils.replaceAccents | ||
* - only covers Basic Latin and Latin-1 unicode chars. | ||
* @example stringUtils.replaceAccents('lõrêm ípsûm') -> 'lorem ipsum' | ||
* @param {string} str | ||
* @return {string} formated string | ||
*/ | ||
function replaceAccents(str){ | ||
str = str || ''; | ||
str = toString(str); | ||
// verifies if the String has accents and replace them | ||
@@ -13,0 +9,0 @@ if (str.search(/[\xC0-\xFF]/g) > -1) { |
@@ -1,2 +0,2 @@ | ||
define(['./repeat'], function (repeat) { | ||
define(['../lang/toString', './repeat'], function (toString, repeat) { | ||
@@ -7,2 +7,3 @@ /** | ||
function rpad(str, minLen, ch) { | ||
str = toString(str); | ||
ch = ch || ' '; | ||
@@ -9,0 +10,0 @@ return (str.length < minLen)? str + repeat(ch, minLen - str.length) : str; |
@@ -1,12 +0,32 @@ | ||
define(function(){ | ||
define(['../lang/toString', './WHITE_SPACES'], function(toString, WHITE_SPACES){ | ||
/** | ||
* Remove white-spaces from end of string. | ||
* @example stringUtils.rtrim(' lorem ipsum ') -> ' lorem ipsum' | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
function rtrim(str){ | ||
return (str || '').replace(/\s+$/g, ''); | ||
* Remove chars from end of string. | ||
*/ | ||
function rtrim(str, chars) { | ||
str = toString(str); | ||
chars = chars || WHITE_SPACES; | ||
var end = str.length - 1, | ||
charLen = chars.length, | ||
found = true, | ||
i, c; | ||
while (found && end >= 0) { | ||
found = false; | ||
i = -1; | ||
c = str.charAt(end); | ||
while (++i < charLen) { | ||
if (c === chars[i]) { | ||
found = true; | ||
end--; | ||
break; | ||
} | ||
} | ||
} | ||
return (end >= 0) ? str.substring(0, end + 1) : ''; | ||
} | ||
return rtrim; | ||
}); |
@@ -1,13 +0,13 @@ | ||
define(['./lowerCase', './upperCase'], function(lowerCase, upperCase){ | ||
define(['../lang/toString', './lowerCase', './upperCase'], function(toString, lowerCase, upperCase){ | ||
/** | ||
* UPPERCASE first char of each sentence and lowercase other chars. | ||
* - ported from Miller Medeiros Eclipse Monkey Scripts | ||
* @example sentenceCase('Lorem IpSum DoLOr. maeCeNnas Ullamcor.') -> 'Lorem ipsum dolor. Maecennas ullamcor.' | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
function sentenceCase(str){ | ||
return lowerCase(str).replace(/(^\w)|\.\s+(\w)/gm, upperCase); //replace first char of each sentence (new line or after '.\s+') to UPPERCASE | ||
str = toString(str); | ||
// Replace first char of each sentence (new line or after '.\s+') to | ||
// UPPERCASE | ||
return lowerCase(str).replace(/(^\w)|\.\s+(\w)/gm, upperCase); | ||
} | ||
return sentenceCase; | ||
}); |
@@ -1,2 +0,2 @@ | ||
define(['./replaceAccents', './removeNonWord', './trim'], function(replaceAccents, removeNonWord, trim){ | ||
define(['../lang/toString', './replaceAccents', './removeNonWord', './trim'], function(toString, replaceAccents, removeNonWord, trim){ | ||
/** | ||
@@ -6,9 +6,6 @@ * Convert to lower case, remove accents, remove non-word chars and | ||
* Does not split camelCase text. | ||
* - ported from Miller Medeiros Eclipse Monkey Scripts | ||
* @example slugify('loremIpsum dolor spéçïãl chârs', '_') -> 'loremipsum_dolor_special_chars' | ||
* @param {string} str | ||
* @param {string} [delimeter="-"] | ||
* @return {string} | ||
*/ | ||
function slugify(str, delimeter){ | ||
str = toString(str); | ||
if (delimeter == null) { | ||
@@ -15,0 +12,0 @@ delimeter = "-"; |
@@ -1,16 +0,13 @@ | ||
define(function () { | ||
define(['../lang/toString'], function (toString) { | ||
/** | ||
* Checks if string starts with specified prefix. | ||
* @example startsWith('lorem ipsum', 'ipsum') -> false | ||
* @example startsWith('lorem ipsum', 'lorem') -> true | ||
* @param {string} str | ||
* @param {string} prefix | ||
* @return {bool} | ||
*/ | ||
* Checks if string starts with specified prefix. | ||
*/ | ||
function startsWith(str, prefix) { | ||
str = (str || ''); | ||
prefix = (prefix || ''); | ||
str = toString(str); | ||
prefix = toString(prefix); | ||
return str.indexOf(prefix) === 0; | ||
} | ||
return startsWith; | ||
}); |
@@ -1,12 +0,11 @@ | ||
define(function(){ | ||
define(['../lang/toString'], function(toString){ | ||
/** | ||
* Remove HTML tags from string. | ||
* @example stripHtmlTags('<p><em>lorem</em> <strong>ipsum</strong></p>') -> 'lorem ipsum' | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
* Remove HTML tags from string. | ||
*/ | ||
function stripHtmlTags(str){ | ||
return (str || '').replace(/<[^>]*>/g, ''); | ||
str = toString(str); | ||
return str.replace(/<[^>]*>/g, ''); | ||
} | ||
return stripHtmlTags; | ||
}); |
@@ -1,12 +0,12 @@ | ||
define(function(){ | ||
define(['../lang/toString', './WHITE_SPACES', './ltrim', './rtrim'], function(toString, WHITE_SPACES, ltrim, rtrim){ | ||
/** | ||
* Remove white-spaces from beginning and end of string. | ||
* @example trim(' lorem ipsum ') -> 'lorem ipsum' | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
function trim(str){ | ||
return (str || '').replace(/^\s+|\s+$/g, ''); | ||
* Remove white-spaces from beginning and end of string. | ||
*/ | ||
function trim(str, chars) { | ||
str = toString(str); | ||
chars = chars || WHITE_SPACES; | ||
return ltrim(rtrim(str, chars), chars); | ||
} | ||
return trim; | ||
}); |
@@ -1,6 +0,7 @@ | ||
define(['./trim'], function(trim){ | ||
define(['../lang/toString', './trim'], function(toString, trim){ | ||
/** | ||
* Limit number of chars. | ||
*/ | ||
* Limit number of chars. | ||
*/ | ||
function truncate(str, maxChars, append, onlyFullWords){ | ||
str = toString(str); | ||
append = append || '...'; | ||
@@ -7,0 +8,0 @@ maxChars = onlyFullWords? maxChars + 1 : maxChars; |
@@ -1,12 +0,12 @@ | ||
define(function(){ | ||
define(['../lang/toString'], function(toString){ | ||
/** | ||
* Add space between camelCase text. | ||
* @example unCamelCase('loremIpsumDolor') -> 'lorem ipsum dolor' | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
function unCamelCase(str){ | ||
return (str || '').replace(/([a-z\xE0-\xFF])([A-Z\xC0\xDF])/g, '$1 $2').toLowerCase(); //add space between camelCase text | ||
str = toString(str); | ||
str = str.replace(/([a-z\xE0-\xFF])([A-Z\xC0\xDF])/g, '$1 $2') | ||
str = str.toLowerCase(); //add space between camelCase text | ||
return str; | ||
} | ||
return unCamelCase; | ||
}); |
@@ -1,2 +0,2 @@ | ||
define(['./slugify', './unCamelCase'], function(slugify, unCamelCase){ | ||
define(['../lang/toString', './slugify', './unCamelCase'], function(toString, slugify, unCamelCase){ | ||
/** | ||
@@ -6,2 +6,3 @@ * Replaces spaces with underscores, split camelCase text, remove non-word chars, remove accents and convert to lower case. | ||
function underscore(str){ | ||
str = toString(str); | ||
str = unCamelCase(str); | ||
@@ -8,0 +9,0 @@ return slugify(str, "_"); |
@@ -1,2 +0,2 @@ | ||
define(function () { | ||
define(['../lang/toString'], function (toString) { | ||
@@ -7,8 +7,8 @@ /** | ||
function unescapeHtml(str){ | ||
str = (str || '') | ||
.replace(/&/g , '&') | ||
.replace(/</g , '<') | ||
.replace(/>/g , '>') | ||
.replace(/'/g , "'") | ||
.replace(/"/g, '"'); | ||
str = toString(str) | ||
.replace(/&/g , '&') | ||
.replace(/</g , '<') | ||
.replace(/>/g , '>') | ||
.replace(/'/g , "'") | ||
.replace(/"/g, '"'); | ||
return str; | ||
@@ -15,0 +15,0 @@ } |
@@ -1,2 +0,2 @@ | ||
define(function () { | ||
define(['../lang/toString'], function(toString) { | ||
@@ -7,3 +7,3 @@ /** | ||
function unescapeUnicode(str){ | ||
if (!str) return ''; | ||
str = toString(str); | ||
return str.replace(/\\u[0-9a-f]{4}/g, function(ch){ | ||
@@ -10,0 +10,0 @@ var code = parseInt(ch.slice(2), 16); |
@@ -1,10 +0,10 @@ | ||
define(function(){ | ||
define(['../lang/toString'], function(toString){ | ||
/** | ||
* Replaces hyphens with spaces. (only hyphens between word chars) | ||
* @example unhyphenate('lorem-ipsum-dolor') -> 'lorem ipsum dolor' | ||
*/ | ||
function unhyphenate(str){ | ||
return (str || '').replace(/(\w)(-)(\w)/g, '$1 $3'); //convert hyphens between word chars to spaces | ||
str = toString(str); | ||
return str.replace(/(\w)(-)(\w)/g, '$1 $3'); | ||
} | ||
return unhyphenate; | ||
}); |
@@ -1,2 +0,2 @@ | ||
define(function(){ | ||
define(['../lang/toString'], function(toString){ | ||
/** | ||
@@ -6,5 +6,6 @@ * "Safer" String.toUpperCase() | ||
function upperCase(str){ | ||
return (str || '').toUpperCase(); | ||
str = toString(str); | ||
return str.toUpperCase(); | ||
} | ||
return upperCase; | ||
}); |
@@ -6,2 +6,3 @@ | ||
module.exports = { | ||
'WHITE_SPACES' : require('./string/WHITE_SPACES'), | ||
'camelCase' : require('./string/camelCase'), | ||
@@ -26,2 +27,3 @@ 'contains' : require('./string/contains'), | ||
'repeat' : require('./string/repeat'), | ||
'replace' : require('./string/replace'), | ||
'replaceAccents' : require('./string/replaceAccents'), | ||
@@ -28,0 +30,0 @@ 'rpad' : require('./string/rpad'), |
@@ -0,1 +1,2 @@ | ||
var toString = require('../lang/toString'); | ||
var replaceAccents = require('./replaceAccents'); | ||
@@ -7,14 +8,11 @@ var removeNonWord = require('./removeNonWord'); | ||
* Convert string to camelCase text. | ||
* - ported from Miller Medeiros Eclipse Monkey Scripts | ||
* @example camelCase('my -- awesome-text') -> 'myAwesomeText'; | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
function camelCase(str){ | ||
str = toString(str); | ||
str = replaceAccents(str); | ||
str = removeNonWord(str) | ||
.replace(/\-/g, ' ') //convert all hyphens to spaces | ||
.replace(/\s[a-z]/g, upperCase) //convert first char of each word to UPPERCASE | ||
.replace(/\s+/g, '') //remove spaces | ||
.replace(/^[A-Z]/g, lowerCase); //convert first char to lowercase | ||
.replace(/\-/g, ' ') //convert all hyphens to spaces | ||
.replace(/\s[a-z]/g, upperCase) //convert first char of each word to UPPERCASE | ||
.replace(/\s+/g, '') //remove spaces | ||
.replace(/^[A-Z]/g, lowerCase); //convert first char to lowercase | ||
return str; | ||
@@ -21,0 +19,0 @@ } |
@@ -0,7 +1,10 @@ | ||
var toString = require('../lang/toString'); | ||
/** | ||
* searches for a given substring | ||
* Searches for a given substring | ||
*/ | ||
function contains(str, substring){ | ||
str = toString(str); | ||
substring = toString(substring); | ||
return str.indexOf(substring) !== -1; | ||
@@ -8,0 +11,0 @@ } |
@@ -0,1 +1,2 @@ | ||
var toString = require('../lang/toString'); | ||
var truncate = require('./truncate'); | ||
@@ -6,3 +7,4 @@ /** | ||
function crop(str, maxChars, append) { | ||
return truncate(str, maxChars, append, true); | ||
str = toString(str); | ||
return truncate(str, maxChars, append, true); | ||
} | ||
@@ -9,0 +11,0 @@ |
@@ -1,16 +0,13 @@ | ||
var toString = require('../lang/toString'); | ||
/** | ||
* Checks if string ends with specified suffix. | ||
* @example endsWith('lorem ipsum', 'ipsum') -> true | ||
* @example endsWith('lorem ipsum', 'lorem') -> false | ||
* @param {string} str | ||
* @param {string} suffix | ||
* @return {bool} | ||
*/ | ||
* Checks if string ends with specified suffix. | ||
*/ | ||
function endsWith(str, suffix) { | ||
str = (str || ''); | ||
suffix = (suffix || ''); | ||
str = toString(str); | ||
suffix = toString(suffix); | ||
return str.indexOf(suffix, str.length - suffix.length) !== -1; | ||
} | ||
module.exports = endsWith; | ||
@@ -0,3 +1,3 @@ | ||
var toString = require('../lang/toString'); | ||
/** | ||
@@ -7,8 +7,8 @@ * Escapes a string for insertion into HTML. | ||
function escapeHtml(str){ | ||
str = (str || '') | ||
.replace(/&/g, '&') | ||
.replace(/</g, '<') | ||
.replace(/>/g, '>') | ||
.replace(/'/g, ''') | ||
.replace(/"/g, '"'); | ||
str = toString(str) | ||
.replace(/&/g, '&') | ||
.replace(/</g, '<') | ||
.replace(/>/g, '>') | ||
.replace(/'/g, ''') | ||
.replace(/"/g, '"'); | ||
return str; | ||
@@ -15,0 +15,0 @@ } |
@@ -0,5 +1,5 @@ | ||
var toString = require('../lang/toString'); | ||
var ESCAPE_CHARS = /[\\.+*?\^$\[\](){}\/'#]/g; | ||
var _rEscapeChars; | ||
/** | ||
@@ -9,6 +9,4 @@ * Escape RegExp string chars. | ||
function escapeRegExp(str) { | ||
if (! _rEscapeChars) { | ||
_rEscapeChars = /[\\.+*?\^$\[\](){}\/'#]/g; | ||
} | ||
return str.replace(_rEscapeChars,'\\$&'); | ||
str = toString(str); | ||
return str.replace(ESCAPE_CHARS,'\\$&'); | ||
} | ||
@@ -15,0 +13,0 @@ |
@@ -0,3 +1,3 @@ | ||
var toString = require('../lang/toString'); | ||
/** | ||
@@ -7,3 +7,3 @@ * Escape string into unicode sequences | ||
function escapeUnicode(str, shouldEscapePrintable){ | ||
if (!str) return ''; | ||
str = toString(str); | ||
return str.replace(/[\s\S]/g, function(ch){ | ||
@@ -10,0 +10,0 @@ // skip printable ASCII chars if we should not escape them |
@@ -0,12 +1,14 @@ | ||
var toString = require('../lang/toString'); | ||
var slugify = require('./slugify'); | ||
var unCamelCase = require('./unCamelCase'); | ||
/** | ||
* Replaces spaces with hyphens, split camelCase text, remove non-word chars, remove accents and convert to lower case. | ||
* @example hyphenate('loremIpsum dolor spéçïãl chârs') -> 'lorem-ipsum-dolor-special-chars' | ||
*/ | ||
* Replaces spaces with hyphens, split camelCase text, remove non-word chars, remove accents and convert to lower case. | ||
*/ | ||
function hyphenate(str){ | ||
str = toString(str); | ||
str = unCamelCase(str); | ||
return slugify(str, "-"); | ||
} | ||
module.exports = hyphenate; | ||
@@ -0,3 +1,3 @@ | ||
var toString = require('../lang/toString'); | ||
var stache = /\{\{(\w+)\}\}/g; //mustache-like | ||
@@ -9,4 +9,5 @@ | ||
function interpolate(template, replacements, syntax){ | ||
template = toString(template); | ||
var replaceFn = function(match, prop){ | ||
return (prop in replacements)? replacements[prop] : ''; | ||
return (prop in replacements)? toString(replacements[prop]) : ''; | ||
}; | ||
@@ -13,0 +14,0 @@ return template.replace(syntax || stache, replaceFn); |
@@ -1,9 +0,11 @@ | ||
var toString = require('../lang/toString'); | ||
/** | ||
* "Safer" String.toLowerCase() | ||
*/ | ||
function upperCase(str){ | ||
return (str || '').toLowerCase(); | ||
function lowerCase(str){ | ||
str = toString(str); | ||
return str.toLowerCase(); | ||
} | ||
module.exports = upperCase; | ||
module.exports = lowerCase; | ||
@@ -0,1 +1,2 @@ | ||
var toString = require('../lang/toString'); | ||
var repeat = require('./repeat'); | ||
@@ -7,4 +8,7 @@ | ||
function lpad(str, minLen, ch) { | ||
str = toString(str); | ||
ch = ch || ' '; | ||
return (str.length < minLen)? repeat(ch, minLen - str.length) + str : str; | ||
return ((str.length < minLen) | ||
? repeat(ch, minLen - str.length) + str : str); | ||
} | ||
@@ -11,0 +15,0 @@ |
@@ -0,12 +1,34 @@ | ||
var toString = require('../lang/toString'); | ||
var WHITE_SPACES = require('./WHITE_SPACES'); | ||
/** | ||
* Remove chars from beginning of string. | ||
*/ | ||
function ltrim(str, chars) { | ||
str = toString(str); | ||
chars = chars || WHITE_SPACES; | ||
/** | ||
* Remove white-spaces from beginning of string. | ||
* @example stringUtils.ltrim(' lorem ipsum ') -> 'lorem ipsum ' | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
function ltrim(str){ | ||
return (str || '').replace(/^\s+/g, ''); | ||
var start = 0, | ||
len = str.length, | ||
charLen = chars.length, | ||
found = true, | ||
i, c; | ||
while (found && start < len) { | ||
found = false; | ||
i = -1; | ||
c = str.charAt(start); | ||
while (++i < charLen) { | ||
if (c === chars[i]) { | ||
found = true; | ||
start++; | ||
break; | ||
} | ||
} | ||
} | ||
return (start >= len) ? '' : str.substr(start, len); | ||
} | ||
module.exports = ltrim; | ||
@@ -0,3 +1,3 @@ | ||
var toString = require('../lang/toString'); | ||
/** | ||
@@ -7,7 +7,9 @@ * Convert line-breaks from DOS/MAC to a single standard (UNIX by default) | ||
function normalizeLineBreaks(str, lineEnd) { | ||
str = toString(str); | ||
lineEnd = lineEnd || '\n'; | ||
return str | ||
.replace(/\r\n/g, lineEnd) // DOS | ||
.replace(/\r/g, lineEnd) // Mac | ||
.replace(/\n/g, lineEnd); // Unix | ||
.replace(/\r\n/g, lineEnd) // DOS | ||
.replace(/\r/g, lineEnd) // Mac | ||
.replace(/\n/g, lineEnd); // Unix | ||
} | ||
@@ -14,0 +16,0 @@ |
@@ -0,10 +1,13 @@ | ||
var toString = require('../lang/toString'); | ||
var camelCase = require('./camelCase'); | ||
var upperCase = require('./upperCase'); | ||
/** | ||
* camelCase + uppercase first char | ||
* camelCase + UPPERCASE first char | ||
*/ | ||
function pascalCase(str){ | ||
str = toString(str); | ||
return camelCase(str).replace(/^[a-z]/, upperCase); | ||
} | ||
module.exports = pascalCase; | ||
@@ -0,1 +1,2 @@ | ||
var toString = require('../lang/toString'); | ||
var lowerCase = require('./lowerCase'); | ||
@@ -5,11 +6,9 @@ var upperCase = require('./upperCase'); | ||
* UPPERCASE first char of each word. | ||
* - ported from Miller Medeiros Eclipse Monkey Scripts | ||
* @example properCase('loRem iPSum') -> 'Lorem Ipsum' | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
function properCase(str){ | ||
return lowerCase(str).replace(/^\w|\s\w/g, upperCase); //replace first char of each word to UPPERCASE | ||
str = toString(str); | ||
return lowerCase(str).replace(/^\w|\s\w/g, upperCase); | ||
} | ||
module.exports = properCase; | ||
@@ -1,11 +0,14 @@ | ||
var toString = require('../lang/toString'); | ||
/** | ||
* Remove non-printable ASCII chars | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
* Remove non-printable ASCII chars | ||
*/ | ||
function removeNonASCII(str){ | ||
return (str || '').replace(/[^\x20-\x7E]/g, ''); //matches non-printable ASCII chars - http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters | ||
str = toString(str); | ||
// Matches non-printable ASCII chars - | ||
// http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters | ||
return str.replace(/[^\x20-\x7E]/g, ''); | ||
} | ||
module.exports = removeNonASCII; | ||
@@ -1,12 +0,11 @@ | ||
var toString = require('../lang/toString'); | ||
/** | ||
* Remove non-word chars. | ||
* @example removeNonWord('lorem! ipsum?') -> 'lorem ipsum' | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
function removeNonWord(str){ | ||
return (str || '').replace(/[^0-9a-zA-Z\xC0-\xFF \-]/g, ''); //remove non-word chars | ||
str = toString(str); | ||
return str.replace(/[^0-9a-zA-Z\xC0-\xFF \-]/g, ''); | ||
} | ||
module.exports = removeNonWord; | ||
@@ -0,3 +1,3 @@ | ||
var toString = require('../lang/toString'); | ||
/** | ||
@@ -7,3 +7,4 @@ * Repeat string n times | ||
function repeat(str, n){ | ||
return (new Array(n + 1)).join(str); | ||
str = toString(str); | ||
return (new Array(n + 1)).join(str); | ||
} | ||
@@ -10,0 +11,0 @@ |
@@ -1,12 +0,8 @@ | ||
var toString = require('../lang/toString'); | ||
/** | ||
* Replaces all accented chars with regular ones | ||
* - ported from Miller Medeiros AS3 StringUtils.replaceAccents | ||
* - only covers Basic Latin and Latin-1 unicode chars. | ||
* @example stringUtils.replaceAccents('lõrêm ípsûm') -> 'lorem ipsum' | ||
* @param {string} str | ||
* @return {string} formated string | ||
*/ | ||
function replaceAccents(str){ | ||
str = str || ''; | ||
str = toString(str); | ||
// verifies if the String has accents and replace them | ||
@@ -13,0 +9,0 @@ if (str.search(/[\xC0-\xFF]/g) > -1) { |
@@ -0,1 +1,2 @@ | ||
var toString = require('../lang/toString'); | ||
var repeat = require('./repeat'); | ||
@@ -7,2 +8,3 @@ | ||
function rpad(str, minLen, ch) { | ||
str = toString(str); | ||
ch = ch || ' '; | ||
@@ -9,0 +11,0 @@ return (str.length < minLen)? str + repeat(ch, minLen - str.length) : str; |
@@ -0,12 +1,33 @@ | ||
var toString = require('../lang/toString'); | ||
var WHITE_SPACES = require('./WHITE_SPACES'); | ||
/** | ||
* Remove chars from end of string. | ||
*/ | ||
function rtrim(str, chars) { | ||
str = toString(str); | ||
chars = chars || WHITE_SPACES; | ||
/** | ||
* Remove white-spaces from end of string. | ||
* @example stringUtils.rtrim(' lorem ipsum ') -> ' lorem ipsum' | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
function rtrim(str){ | ||
return (str || '').replace(/\s+$/g, ''); | ||
var end = str.length - 1, | ||
charLen = chars.length, | ||
found = true, | ||
i, c; | ||
while (found && end >= 0) { | ||
found = false; | ||
i = -1; | ||
c = str.charAt(end); | ||
while (++i < charLen) { | ||
if (c === chars[i]) { | ||
found = true; | ||
end--; | ||
break; | ||
} | ||
} | ||
} | ||
return (end >= 0) ? str.substring(0, end + 1) : ''; | ||
} | ||
module.exports = rtrim; | ||
@@ -0,1 +1,2 @@ | ||
var toString = require('../lang/toString'); | ||
var lowerCase = require('./lowerCase'); | ||
@@ -5,11 +6,11 @@ var upperCase = require('./upperCase'); | ||
* UPPERCASE first char of each sentence and lowercase other chars. | ||
* - ported from Miller Medeiros Eclipse Monkey Scripts | ||
* @example sentenceCase('Lorem IpSum DoLOr. maeCeNnas Ullamcor.') -> 'Lorem ipsum dolor. Maecennas ullamcor.' | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
function sentenceCase(str){ | ||
return lowerCase(str).replace(/(^\w)|\.\s+(\w)/gm, upperCase); //replace first char of each sentence (new line or after '.\s+') to UPPERCASE | ||
str = toString(str); | ||
// Replace first char of each sentence (new line or after '.\s+') to | ||
// UPPERCASE | ||
return lowerCase(str).replace(/(^\w)|\.\s+(\w)/gm, upperCase); | ||
} | ||
module.exports = sentenceCase; | ||
@@ -0,1 +1,2 @@ | ||
var toString = require('../lang/toString'); | ||
var replaceAccents = require('./replaceAccents'); | ||
@@ -8,9 +9,6 @@ var removeNonWord = require('./removeNonWord'); | ||
* Does not split camelCase text. | ||
* - ported from Miller Medeiros Eclipse Monkey Scripts | ||
* @example slugify('loremIpsum dolor spéçïãl chârs', '_') -> 'loremipsum_dolor_special_chars' | ||
* @param {string} str | ||
* @param {string} [delimeter="-"] | ||
* @return {string} | ||
*/ | ||
function slugify(str, delimeter){ | ||
str = toString(str); | ||
if (delimeter == null) { | ||
@@ -17,0 +15,0 @@ delimeter = "-"; |
@@ -1,16 +0,13 @@ | ||
var toString = require('../lang/toString'); | ||
/** | ||
* Checks if string starts with specified prefix. | ||
* @example startsWith('lorem ipsum', 'ipsum') -> false | ||
* @example startsWith('lorem ipsum', 'lorem') -> true | ||
* @param {string} str | ||
* @param {string} prefix | ||
* @return {bool} | ||
*/ | ||
* Checks if string starts with specified prefix. | ||
*/ | ||
function startsWith(str, prefix) { | ||
str = (str || ''); | ||
prefix = (prefix || ''); | ||
str = toString(str); | ||
prefix = toString(prefix); | ||
return str.indexOf(prefix) === 0; | ||
} | ||
module.exports = startsWith; | ||
@@ -1,12 +0,11 @@ | ||
var toString = require('../lang/toString'); | ||
/** | ||
* Remove HTML tags from string. | ||
* @example stripHtmlTags('<p><em>lorem</em> <strong>ipsum</strong></p>') -> 'lorem ipsum' | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
* Remove HTML tags from string. | ||
*/ | ||
function stripHtmlTags(str){ | ||
return (str || '').replace(/<[^>]*>/g, ''); | ||
str = toString(str); | ||
return str.replace(/<[^>]*>/g, ''); | ||
} | ||
module.exports = stripHtmlTags; | ||
@@ -1,12 +0,15 @@ | ||
var toString = require('../lang/toString'); | ||
var WHITE_SPACES = require('./WHITE_SPACES'); | ||
var ltrim = require('./ltrim'); | ||
var rtrim = require('./rtrim'); | ||
/** | ||
* Remove white-spaces from beginning and end of string. | ||
* @example trim(' lorem ipsum ') -> 'lorem ipsum' | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
function trim(str){ | ||
return (str || '').replace(/^\s+|\s+$/g, ''); | ||
* Remove white-spaces from beginning and end of string. | ||
*/ | ||
function trim(str, chars) { | ||
str = toString(str); | ||
chars = chars || WHITE_SPACES; | ||
return ltrim(rtrim(str, chars), chars); | ||
} | ||
module.exports = trim; | ||
@@ -0,6 +1,8 @@ | ||
var toString = require('../lang/toString'); | ||
var trim = require('./trim'); | ||
/** | ||
* Limit number of chars. | ||
*/ | ||
* Limit number of chars. | ||
*/ | ||
function truncate(str, maxChars, append, onlyFullWords){ | ||
str = toString(str); | ||
append = append || '...'; | ||
@@ -7,0 +9,0 @@ maxChars = onlyFullWords? maxChars + 1 : maxChars; |
@@ -1,12 +0,12 @@ | ||
var toString = require('../lang/toString'); | ||
/** | ||
* Add space between camelCase text. | ||
* @example unCamelCase('loremIpsumDolor') -> 'lorem ipsum dolor' | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
function unCamelCase(str){ | ||
return (str || '').replace(/([a-z\xE0-\xFF])([A-Z\xC0\xDF])/g, '$1 $2').toLowerCase(); //add space between camelCase text | ||
str = toString(str); | ||
str = str.replace(/([a-z\xE0-\xFF])([A-Z\xC0\xDF])/g, '$1 $2') | ||
str = str.toLowerCase(); //add space between camelCase text | ||
return str; | ||
} | ||
module.exports = unCamelCase; | ||
@@ -0,1 +1,2 @@ | ||
var toString = require('../lang/toString'); | ||
var slugify = require('./slugify'); | ||
@@ -7,2 +8,3 @@ var unCamelCase = require('./unCamelCase'); | ||
function underscore(str){ | ||
str = toString(str); | ||
str = unCamelCase(str); | ||
@@ -9,0 +11,0 @@ return slugify(str, "_"); |
@@ -0,3 +1,3 @@ | ||
var toString = require('../lang/toString'); | ||
/** | ||
@@ -7,8 +7,8 @@ * Unescapes HTML special chars | ||
function unescapeHtml(str){ | ||
str = (str || '') | ||
.replace(/&/g , '&') | ||
.replace(/</g , '<') | ||
.replace(/>/g , '>') | ||
.replace(/'/g , "'") | ||
.replace(/"/g, '"'); | ||
str = toString(str) | ||
.replace(/&/g , '&') | ||
.replace(/</g , '<') | ||
.replace(/>/g , '>') | ||
.replace(/'/g , "'") | ||
.replace(/"/g, '"'); | ||
return str; | ||
@@ -15,0 +15,0 @@ } |
@@ -0,3 +1,3 @@ | ||
var toString = require('../lang/toString'); | ||
/** | ||
@@ -7,3 +7,3 @@ * Unescape unicode char sequences | ||
function unescapeUnicode(str){ | ||
if (!str) return ''; | ||
str = toString(str); | ||
return str.replace(/\\u[0-9a-f]{4}/g, function(ch){ | ||
@@ -10,0 +10,0 @@ var code = parseInt(ch.slice(2), 16); |
@@ -1,10 +0,10 @@ | ||
var toString = require('../lang/toString'); | ||
/** | ||
* Replaces hyphens with spaces. (only hyphens between word chars) | ||
* @example unhyphenate('lorem-ipsum-dolor') -> 'lorem ipsum dolor' | ||
*/ | ||
function unhyphenate(str){ | ||
return (str || '').replace(/(\w)(-)(\w)/g, '$1 $3'); //convert hyphens between word chars to spaces | ||
str = toString(str); | ||
return str.replace(/(\w)(-)(\w)/g, '$1 $3'); | ||
} | ||
module.exports = unhyphenate; | ||
@@ -1,2 +0,2 @@ | ||
var toString = require('../lang/toString'); | ||
/** | ||
@@ -6,5 +6,6 @@ * "Safer" String.toUpperCase() | ||
function upperCase(str){ | ||
return (str || '').toUpperCase(); | ||
str = toString(str); | ||
return str.toUpperCase(); | ||
} | ||
module.exports = upperCase; | ||
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
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
3198838
629
6049
8