use native ''.trim; rename vars
This commit is contained in:
parent
e480d87b34
commit
68ff06af2a
5 changed files with 138 additions and 157 deletions
|
|
@ -1,6 +1,6 @@
|
|||
'use strict';
|
||||
|
||||
Ox.basename = function(str) {
|
||||
Ox.basename = function(string) {
|
||||
/*
|
||||
fixme: deprecate
|
||||
>>> Ox.basename("foo/bar/foo.bar")
|
||||
|
|
@ -8,13 +8,12 @@ Ox.basename = function(str) {
|
|||
>>> Ox.basename("foo.bar")
|
||||
"foo.bar"
|
||||
*/
|
||||
return str.replace(/^.*[\/\\]/g, '');
|
||||
return string.replace(/^.*[\/\\]/g, '');
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.char <f> Alias for String.fromCharCode
|
||||
@*/
|
||||
// fixme: add some mapping? like Ox.char(9, 13) or Ox.char([9, 13])?
|
||||
Ox.char = String.fromCharCode;
|
||||
|
||||
/*@
|
||||
|
|
@ -30,9 +29,9 @@ Ox.clean <f> Remove leading, trailing and double whitespace from a string
|
|||
> Ox.clean(" foo\tbar ")
|
||||
"foo bar"
|
||||
@*/
|
||||
Ox.clean = function(str) {
|
||||
return Ox.filter(Ox.map(str.split('\n'), function(str) {
|
||||
return Ox.trim(str.replace(/\s+/g, ' ')) || '';
|
||||
Ox.clean = function(string) {
|
||||
return Ox.filter(Ox.map(string.split('\n'), function(string) {
|
||||
return string.replace(/\s+/g, ' ').trim() || '';
|
||||
})).join('\n');
|
||||
};
|
||||
|
||||
|
|
@ -44,11 +43,11 @@ Ox.endsWith <f> Checks if a string ends with a given substring
|
|||
> Ox.endsWith('foobar', 'bar')
|
||||
true
|
||||
@*/
|
||||
Ox.ends = Ox.endsWith = function(str, sub) {
|
||||
Ox.ends = Ox.endsWith = function(string, substring) {
|
||||
// fixme: rename to ends
|
||||
str = str.toString();
|
||||
sub = sub.toString();
|
||||
return str.slice(str.length - sub.length) == sub;
|
||||
string = string.toString();
|
||||
substring = substring.toString();
|
||||
return string.slice(string.length - substring.length) == substring;
|
||||
};
|
||||
|
||||
/*@
|
||||
|
|
@ -159,8 +158,8 @@ Ox.isValidEmail <f> Tests if a string is a valid e-mail address
|
|||
> Ox.isValidEmail("foo@bar..com")
|
||||
false
|
||||
@*/
|
||||
Ox.isValidEmail = function(str) {
|
||||
return !!/^[0-9A-Z\.\+\-_]+@(?:[0-9A-Z\-]+\.)+[A-Z]{2,6}$/i.test(str);
|
||||
Ox.isValidEmail = function(string) {
|
||||
return !!/^[0-9A-Z\.\+\-_]+@(?:[0-9A-Z\-]+\.)+[A-Z]{2,6}$/i.test(string);
|
||||
};
|
||||
|
||||
/*@
|
||||
|
|
@ -176,15 +175,15 @@ Ox.pad <f> Pad a string to a given length
|
|||
> Ox.pad("abc", 6, "123456")
|
||||
"456abc"
|
||||
@*/
|
||||
Ox.pad = function(str, len, pad) {
|
||||
Ox.pad = function(string, length, padding) {
|
||||
// fixme: slighly obscure signature
|
||||
// fixme: weird for negative numbers
|
||||
var pos = len / (len = Math.abs(len));
|
||||
str = str.toString().slice(0, len);
|
||||
pad = Ox.repeat(pad || '0', len - str.length);
|
||||
var pos = length / (length = Math.abs(length));
|
||||
string = string.toString().slice(0, length);
|
||||
padding = Ox.repeat(padding || '0', length - string.length);
|
||||
return pos == 1
|
||||
? (pad + str).slice(-len)
|
||||
: (str + pad).slice(0, len);
|
||||
? (padding + string).slice(-length)
|
||||
: (string + padding).slice(0, length);
|
||||
};
|
||||
|
||||
/*@
|
||||
|
|
@ -202,8 +201,8 @@ Ox.parsePath <f> Returns the components of a path
|
|||
> Ox.parsePath('.foo')
|
||||
{extension: '', filename: '.foo', pathname: ''}
|
||||
@*/
|
||||
Ox.parsePath = function(str) {
|
||||
var matches = /^(.+\/)?(.+?(\..+)?)?$/.exec(str);
|
||||
Ox.parsePath = function(string) {
|
||||
var matches = /^(.+\/)?(.+?(\..+)?)?$/.exec(string);
|
||||
return {
|
||||
pathname: matches[1] || '',
|
||||
filename: matches[2] || '',
|
||||
|
|
@ -221,16 +220,16 @@ Ox.parseSRT <f> Parses an srt subtitle file
|
|||
> Ox.parseSRT('1\n01:02:00,000 --> 01:02:03,400\nHello World')
|
||||
[{'in': 3720, out: 3723.4, text: 'Hello World'}]
|
||||
@*/
|
||||
Ox.parseSRT = function(str, fps) {
|
||||
return str.replace(/\r\n/g, '\n').replace(/\n+$/, '').split('\n\n')
|
||||
Ox.parseSRT = function(string, fps) {
|
||||
return string.replace(/\r\n/g, '\n').replace(/\n+$/, '').split('\n\n')
|
||||
.map(function(block) {
|
||||
var lines = block.split('\n'), points;
|
||||
lines.shift();
|
||||
points = lines.shift().split(' --> ').map(function(point) {
|
||||
return point.replace(',', ':').split(':')
|
||||
.reduce(function(prev, curr, i) {
|
||||
return prev + parseInt(curr, 10) *
|
||||
[3600, 60, 1, 0.001][i];
|
||||
.reduce(function(previous, current, index) {
|
||||
return previous + parseInt(current, 10) *
|
||||
[3600, 60, 1, 0.001][index];
|
||||
}, 0);
|
||||
});
|
||||
if (fps) {
|
||||
|
|
@ -395,15 +394,15 @@ Ox.repeat <f> Repeat a value multiple times
|
|||
> Ox.repeat([{k: "v"}], 3)
|
||||
[{k: "v"}, {k: "v"}, {k: "v"}]
|
||||
@*/
|
||||
Ox.repeat = function(val, num) {
|
||||
Ox.repeat = function(value, times) {
|
||||
var ret;
|
||||
if (Ox.isArray(val)) {
|
||||
if (Ox.isArray(value)) {
|
||||
ret = [];
|
||||
Ox.loop(num, function() {
|
||||
ret = ret.concat(val);
|
||||
Ox.loop(times, function() {
|
||||
ret = ret.concat(value);
|
||||
});
|
||||
} else {
|
||||
ret = num >= 1 ? new Array(num + 1).join(val.toString()) : '';
|
||||
ret = times >= 1 ? new Array(times + 1).join(value.toString()) : '';
|
||||
}
|
||||
return ret;
|
||||
};
|
||||
|
|
@ -416,11 +415,11 @@ Ox.startsWith <f> Checks if a string starts with a given substring
|
|||
> Ox.startsWith('foobar', 'foo')
|
||||
true
|
||||
@*/
|
||||
Ox.starts = Ox.startsWith = function(str, sub) {
|
||||
Ox.starts = Ox.startsWith = function(string, substring) {
|
||||
// fixme: rename to starts
|
||||
str = str.toString();
|
||||
sub = sub.toString();
|
||||
return str.slice(0, sub.length) == sub;
|
||||
string = string.toString();
|
||||
substring = substring.toString();
|
||||
return string.slice(0, substring.length) == substring;
|
||||
};
|
||||
|
||||
/*@
|
||||
|
|
@ -428,8 +427,8 @@ Ox.stripTags <f> Strips HTML tags from a string
|
|||
> Ox.stripTags('f<span>o</span>o')
|
||||
'foo'
|
||||
@*/
|
||||
Ox.stripTags = function(str) {
|
||||
return str.replace(/<.*?>/g, '');
|
||||
Ox.stripTags = function(string) {
|
||||
return string.replace(/<.*?>/g, '');
|
||||
};
|
||||
|
||||
/*@
|
||||
|
|
@ -441,9 +440,9 @@ Ox.toCamelCase <f> Takes a string with '-', '/' or '_', returns a camelCase stri
|
|||
> Ox.toCamelCase('foo_bar_baz')
|
||||
'fooBarBaz'
|
||||
@*/
|
||||
Ox.toCamelCase = function(str) {
|
||||
return str.replace(/[\-\/_][a-z]/g, function(str) {
|
||||
return str[1].toUpperCase();
|
||||
Ox.toCamelCase = function(string) {
|
||||
return string.replace(/[\-\/_][a-z]/g, function(string) {
|
||||
return string[1].toUpperCase();
|
||||
});
|
||||
};
|
||||
|
||||
|
|
@ -452,9 +451,9 @@ Ox.toDashes <f> Takes a camelCase string, returns a string with dashes
|
|||
> Ox.toDashes('fooBarBaz')
|
||||
'foo-bar-baz'
|
||||
@*/
|
||||
Ox.toDashes = function(str) {
|
||||
return str.replace(/[A-Z]/g, function(str) {
|
||||
return '-' + str.toLowerCase();
|
||||
Ox.toDashes = function(string) {
|
||||
return string.replace(/[A-Z]/g, function(string) {
|
||||
return '-' + string.toLowerCase();
|
||||
});
|
||||
};
|
||||
|
||||
|
|
@ -463,11 +462,9 @@ Ox.toSlashes <f> Takes a camelCase string, returns a string with slashes
|
|||
> Ox.toSlashes('fooBarBaz')
|
||||
'foo/bar/baz'
|
||||
@*/
|
||||
Ox.toSlashes = function(str) {
|
||||
/*
|
||||
*/
|
||||
return str.replace(/[A-Z]/g, function(str) {
|
||||
return '/' + str.toLowerCase();
|
||||
Ox.toSlashes = function(string) {
|
||||
return string.replace(/[A-Z]/g, function(string) {
|
||||
return '/' + string.toLowerCase();
|
||||
});
|
||||
};
|
||||
|
||||
|
|
@ -478,14 +475,14 @@ Ox.toTitleCase <f> Returns a string with capitalized words
|
|||
> Ox.toTitleCase('Apple releases iPhone, IBM stock plummets')
|
||||
'Apple Releases iPhone, IBM Stock Plummets'
|
||||
@*/
|
||||
Ox.toTitleCase = function(str) {
|
||||
return str.split(' ').map(function(val) {
|
||||
var sub = val.slice(1),
|
||||
low = sub.toLowerCase();
|
||||
if (sub == low) {
|
||||
val = val.slice(0, 1).toUpperCase() + low;
|
||||
Ox.toTitleCase = function(string) {
|
||||
return string.split(' ').map(function(value) {
|
||||
var substring = value.slice(1),
|
||||
lowercase = substring.toLowerCase();
|
||||
if (substring == lowercase) {
|
||||
value = value.slice(0, 1).toUpperCase() + lowercase;
|
||||
}
|
||||
return val;
|
||||
return value;
|
||||
}).join(' ');
|
||||
};
|
||||
|
||||
|
|
@ -494,20 +491,12 @@ Ox.toUnderscores <f> Takes a camelCase string, returns string with underscores
|
|||
> Ox.toUnderscores('fooBarBaz')
|
||||
'foo_bar_baz'
|
||||
@*/
|
||||
Ox.toUnderscores = function(str) {
|
||||
return str.replace(/[A-Z]/g, function(str) {
|
||||
return '_' + str.toLowerCase();
|
||||
Ox.toUnderscores = function(string) {
|
||||
return string.replace(/[A-Z]/g, function(string) {
|
||||
return '_' + string.toLowerCase();
|
||||
});
|
||||
};
|
||||
|
||||
Ox.trim = function(str) { // is in jQuery, and in JavaScript itself
|
||||
/*
|
||||
Ox.trim(" foo ")
|
||||
"foo"
|
||||
*/
|
||||
return str.replace(/^\s+|\s+$/g, '');
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.truncate <f> Truncate a string to a given length
|
||||
(string, length) <s> Truncated string
|
||||
|
|
@ -523,24 +512,22 @@ Ox.truncate <f> Truncate a string to a given length
|
|||
> Ox.truncate('anticonstitutionellement', 16, '...', 'center')
|
||||
'anticon...lement'
|
||||
@*/
|
||||
Ox.truncate = function(str, len, pad, pos) {
|
||||
var pad = pad || '...',
|
||||
pos = pos || 'right',
|
||||
strlen = str.length,
|
||||
padlen = pad.length,
|
||||
left, right;
|
||||
if (strlen > len) {
|
||||
if (pos == 'left') {
|
||||
str = pad + str.slice(padlen + strlen - len);
|
||||
} else if (pos == 'center') {
|
||||
left = Math.ceil((len - padlen) / 2);
|
||||
right = Math.floor((len - padlen) / 2);
|
||||
str = str.slice(0, left) + pad + str.slice(-right);
|
||||
} else if (pos == 'right') {
|
||||
str = str.slice(0, len - padlen) + pad;
|
||||
Ox.truncate = function(string, length, padding, position) {
|
||||
padding = padding || '...';
|
||||
position = position || 'right';
|
||||
if (string.length > length) {
|
||||
if (position == 'left') {
|
||||
string = padding
|
||||
+ string.slice(padding.length + string.length - length);
|
||||
} else if (position == 'center') {
|
||||
string = string.slice(0, Math.ceil((length - padding.length) / 2))
|
||||
+ padding
|
||||
+ string.slice(-Math.floor((length - padding.length) / 2));
|
||||
} else if (position == 'right') {
|
||||
string = string.slice(0, length - padding.length) + padding;
|
||||
}
|
||||
}
|
||||
return str;
|
||||
return string;
|
||||
};
|
||||
|
||||
/*@
|
||||
|
|
@ -550,28 +537,26 @@ Ox.words <f> Splits a string into words, removing punctuation
|
|||
> Ox.words('Let\'s "split" array-likes into key/value pairs--okay?')
|
||||
["let's", "split", "array-likes", "into", "key", "value", "pairs", "okay"]
|
||||
@*/
|
||||
Ox.words = function(str) {
|
||||
var arr = str.toLowerCase().split(/\b/),
|
||||
chr = "-'",
|
||||
len = arr.length,
|
||||
startsWithWord = /\w/.test(arr[0]);
|
||||
arr.forEach(function(v, i) {
|
||||
Ox.words = function(string) {
|
||||
var array = string.toLowerCase().split(/\b/),
|
||||
length = array.length,
|
||||
startsWithWord = /\w/.test(array[0]);
|
||||
array.forEach(function(v, i) {
|
||||
// find single occurrences of "-" or "'" that are not at the beginning
|
||||
// or end of the string, and join the surrounding words with them
|
||||
if (
|
||||
i > 0 && i < len - 1
|
||||
&& v.length == 1 && chr.indexOf(v) > -1
|
||||
i > 0 && i < length - 1 && (v == '-' || v == '\'')
|
||||
) {
|
||||
arr[i + 1] = arr[i - 1] + arr[i] + arr[i + 1];
|
||||
arr[i - 1] = arr[i] = '';
|
||||
array[i + 1] = array[i - 1] + array[i] + array[i + 1];
|
||||
array[i - 1] = array[i] = '';
|
||||
}
|
||||
});
|
||||
// remove elements that have been emptied above
|
||||
arr = arr.filter(function(v) {
|
||||
array = array.filter(function(v) {
|
||||
return v.length;
|
||||
});
|
||||
// return words, not spaces or punctuation
|
||||
return arr.filter(function(v, i) {
|
||||
return array.filter(function(v, i) {
|
||||
return i % 2 == !startsWithWord;
|
||||
});
|
||||
}
|
||||
|
|
@ -636,8 +621,8 @@ Ox.wordwrap = function(str, len, sep, bal, spa) {
|
|||
});
|
||||
if (!spa) {
|
||||
lines = lines.map(function(line) {
|
||||
return Ox.trim(line);
|
||||
return line.trim();
|
||||
});
|
||||
}
|
||||
return Ox.trim(lines.join(sep));
|
||||
return lines.join(sep).trim();
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue