use native ''.trim; rename vars
This commit is contained in:
parent
e480d87b34
commit
68ff06af2a
5 changed files with 138 additions and 157 deletions
|
@ -32,9 +32,9 @@ Ox.divideInt <f> Divides a number by another and returns an array of integers
|
||||||
[16, 16, 17, 17, 17, 17]
|
[16, 16, 17, 17, 17, 17]
|
||||||
@*/
|
@*/
|
||||||
// fixme: is splitInt a better name?
|
// fixme: is splitInt a better name?
|
||||||
Ox.divideInt = function(num, by) {
|
Ox.divideInt = function(number, by) {
|
||||||
var div = parseInt(num / by),
|
var div = parseInt(number / by),
|
||||||
mod = num % by;
|
mod = number % by;
|
||||||
return Ox.range(by).map(function(i) {
|
return Ox.range(by).map(function(i) {
|
||||||
return div + (i > by - 1 - mod);
|
return div + (i > by - 1 - mod);
|
||||||
});
|
});
|
||||||
|
@ -59,12 +59,11 @@ Ox.limit <f> Limits a number by a given mininum and maximum
|
||||||
> Ox.limit(-1, -2)
|
> Ox.limit(-1, -2)
|
||||||
-2
|
-2
|
||||||
@*/
|
@*/
|
||||||
Ox.limit = function(/*num[[, min], max]*/) {
|
Ox.limit = function(/*number[[, min], max]*/) {
|
||||||
var len = arguments.length,
|
var number = arguments[0],
|
||||||
num = arguments[0],
|
min = arguments.length == 3 ? arguments[1] : -Infinity,
|
||||||
min = len == 3 ? arguments[1] : -Infinity,
|
max = arguments[arguments.length - 1];
|
||||||
max = arguments[len - 1];
|
return Math.min(Math.max(number, min), max);
|
||||||
return Math.min(Math.max(num, min), max);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*@
|
/*@
|
||||||
|
@ -75,8 +74,8 @@ Ox.log <f> Returns the logarithm of a given number to a given base
|
||||||
> Ox.log(Math.E)
|
> Ox.log(Math.E)
|
||||||
1
|
1
|
||||||
@*/
|
@*/
|
||||||
Ox.log = function(num, base) {
|
Ox.log = function(number, base) {
|
||||||
return Math.log(num) / Math.log(base || Math.E);
|
return Math.log(number) / Math.log(base || Math.E);
|
||||||
};
|
};
|
||||||
|
|
||||||
/*@
|
/*@
|
||||||
|
@ -88,8 +87,8 @@ Ox.mod <f> Modulo function
|
||||||
> Ox.mod(-11, 10)
|
> Ox.mod(-11, 10)
|
||||||
9
|
9
|
||||||
@*/
|
@*/
|
||||||
Ox.mod = function(num, by) {
|
Ox.mod = function(number, by) {
|
||||||
return (num % by + by) % by;
|
return (number % by + by) % by;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*@
|
/*@
|
||||||
|
@ -115,9 +114,8 @@ Ox.random <f> Returns a random integer within a given range
|
||||||
true
|
true
|
||||||
@*/
|
@*/
|
||||||
Ox.random = function() {
|
Ox.random = function() {
|
||||||
var len = arguments.length,
|
var min = arguments.length == 2 ? arguments[0] : 0,
|
||||||
min = len == 2 ? arguments[0] : 0,
|
max = arguments.length ? Ox.last(arguments) : 2;
|
||||||
max = len ? arguments[len - 1] : 2;
|
|
||||||
return min + Math.floor(Math.random() * (max - min));
|
return min + Math.floor(Math.random() * (max - min));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -130,9 +128,9 @@ Ox.round <f> Rounds a number with a given number of decimals
|
||||||
> Ox.round(1 / 2)
|
> Ox.round(1 / 2)
|
||||||
1
|
1
|
||||||
@*/
|
@*/
|
||||||
Ox.round = function(num, dec) {
|
Ox.round = function(number, decimals) {
|
||||||
var pow = Math.pow(10, dec || 0);
|
var pow = Math.pow(10, decimals || 0);
|
||||||
return Math.round(num * pow) / pow;
|
return Math.round(number * pow) / pow;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*@
|
/*@
|
||||||
|
|
|
@ -5,13 +5,13 @@ Ox.extend <function> Extends an object with one or more other objects
|
||||||
> Ox.extend({a: 1, b: 1, c: 1}, {b: 2, c: 2}, {c: 3})
|
> Ox.extend({a: 1, b: 1, c: 1}, {b: 2, c: 2}, {c: 3})
|
||||||
{a: 1, b: 2, c: 3}
|
{a: 1, b: 2, c: 3}
|
||||||
@*/
|
@*/
|
||||||
Ox.extend = function(obj) {
|
Ox.extend = function(object) {
|
||||||
Ox.forEach(Ox.slice(arguments, 1), function(arg, i) {
|
Ox.forEach(Ox.slice(arguments, 1), function(argument, i) {
|
||||||
Ox.forEach(arg, function(val, key) {
|
Ox.forEach(argument, function(value, key) {
|
||||||
obj[key] = val;
|
object[key] = value;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
return obj;
|
return object;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*@
|
/*@
|
||||||
|
@ -76,8 +76,8 @@ Ox.getset = function(object, args, callback, that) {
|
||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
Ox.hasOwn = function(obj, val) {
|
Ox.hasOwn = function(object, value) {
|
||||||
return Object.prototype.hasOwnProperty.call(obj, val)
|
return Object.prototype.hasOwnProperty.call(object, value)
|
||||||
};
|
};
|
||||||
|
|
||||||
/*@
|
/*@
|
||||||
|
@ -85,10 +85,10 @@ Ox.keyOf <f> Equivalent of [].indexOf for objects
|
||||||
> Ox.keyOf({a: 1, b: 2, c: 3}, 1)
|
> Ox.keyOf({a: 1, b: 2, c: 3}, 1)
|
||||||
'a'
|
'a'
|
||||||
@*/
|
@*/
|
||||||
Ox.keyOf = function(obj, val) {
|
Ox.keyOf = function(object, value) {
|
||||||
var key;
|
var key;
|
||||||
Ox.forEach(obj, function(v, k) {
|
Ox.forEach(object, function(v, k) {
|
||||||
if (v === val) {
|
if (v === value) {
|
||||||
key = k;
|
key = k;
|
||||||
Ox.Break();
|
Ox.Break();
|
||||||
}
|
}
|
||||||
|
@ -126,18 +126,18 @@ Ox.methods <f> Returns a sorted list of all method names of an object
|
||||||
> Ox.methods({a: [], b: false, f: function() {}, n: 0, o: {}, s: ''})
|
> Ox.methods({a: [], b: false, f: function() {}, n: 0, o: {}, s: ''})
|
||||||
['f']
|
['f']
|
||||||
@*/
|
@*/
|
||||||
Ox.methods = function(obj, includePrototype) {
|
Ox.methods = function(object, includePrototype) {
|
||||||
var key, keys;
|
var key, keys;
|
||||||
if (includePrototype) {
|
if (includePrototype) {
|
||||||
keys = [];
|
keys = [];
|
||||||
for (var key in obj) {
|
for (var key in object) {
|
||||||
keys.push(key);
|
keys.push(key);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
keys = Object.keys(obj);
|
keys = Object.keys(object);
|
||||||
}
|
}
|
||||||
return keys.filter(function(key) {
|
return keys.filter(function(key) {
|
||||||
return Ox.isFunction(obj[key]);
|
return Ox.isFunction(object[key]);
|
||||||
}).sort();
|
}).sort();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -150,14 +150,14 @@ Ox.serialize <f> Parses an object into query parameters
|
||||||
> Ox.serialize({string: 'foo', empty: {}, null: null, undefined: void 0})
|
> Ox.serialize({string: 'foo', empty: {}, null: null, undefined: void 0})
|
||||||
'string=foo'
|
'string=foo'
|
||||||
@*/
|
@*/
|
||||||
Ox.serialize = function(obj) {
|
Ox.serialize = function(object) {
|
||||||
var arr = [];
|
var ret = [];
|
||||||
Ox.forEach(obj, function(val, key) {
|
Ox.forEach(object, function(value, key) {
|
||||||
if (!Ox.isEmpty(val) && !Ox.isNull(val) && !Ox.isUndefined(val)) {
|
if (!Ox.isEmpty(value) && !Ox.isNull(value) && !Ox.isUndefined(value)) {
|
||||||
arr.push(key + '=' + val);
|
ret.push(key + '=' + value);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return arr.join('&');
|
return ret.join('&');
|
||||||
};
|
};
|
||||||
|
|
||||||
/*@
|
/*@
|
||||||
|
@ -169,19 +169,17 @@ Ox.unserialize <f> Parses query parameters into an object
|
||||||
> Ox.unserialize('a=1&b=&c&a=0', true)
|
> Ox.unserialize('a=1&b=&c&a=0', true)
|
||||||
{a: 0}
|
{a: 0}
|
||||||
@*/
|
@*/
|
||||||
Ox.unserialize = function(str, toNumber) {
|
Ox.unserialize = function(string, toNumber) {
|
||||||
var obj = {};
|
var ret = {};
|
||||||
str.split('&').forEach(function(val) {
|
Ox.filter(string.split('&')).forEach(function(value) {
|
||||||
if (val) {
|
var array = value.split('=');
|
||||||
var arr = val.split('=');
|
if (array[1]) {
|
||||||
if (arr[1]) {
|
ret[array[0]] = !toNumber ? array[1]
|
||||||
obj[arr[0]] = !toNumber ? arr[1]
|
: array[1].indexOf(',') == -1 ? +array[1]
|
||||||
: arr[1].indexOf(',') == -1 ? +arr[1]
|
: array[1].split(',').map(function(value) {
|
||||||
: arr[1].split(',').map(function(val) {
|
return +value;
|
||||||
return +val;
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return obj;
|
return ret;
|
||||||
};
|
};
|
|
@ -4,6 +4,6 @@ Ox.escapeRegExp <f> Escapes a string for use in a regular expression
|
||||||
str <s> String
|
str <s> String
|
||||||
@*/
|
@*/
|
||||||
// see https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions
|
// see https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions
|
||||||
Ox.escapeRegExp = function(str) {
|
Ox.escapeRegExp = function(string) {
|
||||||
return (str + '').replace(/([\/\\^$*+?.-|(){}[\]])/g, '\\$1');
|
return (string + '').replace(/([\/\\^$*+?.-|(){}[\]])/g, '\\$1');
|
||||||
};
|
};
|
|
@ -189,23 +189,23 @@ Ox.loadAsync <f> Runs an asynchonous function on array elements
|
||||||
callback <f> Callback function
|
callback <f> Callback function
|
||||||
results <o> Results
|
results <o> Results
|
||||||
@*/
|
@*/
|
||||||
Ox.loadAsync = function(arr, map, callback) {
|
Ox.loadAsync = function(array, map, callback) {
|
||||||
arr = Ox.makeArray(arr);
|
array = Ox.makeArray(array);
|
||||||
var i = 0, n = arr.length, results = {};
|
var i = 0, n = array.length, results = {};
|
||||||
if (arr.some(Ox.isArray)) {
|
if (array.some(Ox.isArray)) {
|
||||||
iterate();
|
iterate();
|
||||||
} else {
|
} else {
|
||||||
arr.forEach(function(item) {
|
array.forEach(function(value) {
|
||||||
map(item, function(result) {
|
map(value, function(result) {
|
||||||
Ox.extend(results, result);
|
Ox.extend(results, result);
|
||||||
++i == n && callback(results);
|
++i == n && callback(results);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
function iterate() {
|
function iterate() {
|
||||||
Ox.loadAsync(arr.shift(), function(result) {
|
Ox.loadAsync(array.shift(), function(result) {
|
||||||
Ox.extend(results, result);
|
Ox.extend(results, result);
|
||||||
arr.length ? iterate() : callback(results);
|
array.length ? iterate() : callback(results);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
Ox.basename = function(str) {
|
Ox.basename = function(string) {
|
||||||
/*
|
/*
|
||||||
fixme: deprecate
|
fixme: deprecate
|
||||||
>>> Ox.basename("foo/bar/foo.bar")
|
>>> Ox.basename("foo/bar/foo.bar")
|
||||||
|
@ -8,13 +8,12 @@ Ox.basename = function(str) {
|
||||||
>>> Ox.basename("foo.bar")
|
>>> Ox.basename("foo.bar")
|
||||||
"foo.bar"
|
"foo.bar"
|
||||||
*/
|
*/
|
||||||
return str.replace(/^.*[\/\\]/g, '');
|
return string.replace(/^.*[\/\\]/g, '');
|
||||||
};
|
};
|
||||||
|
|
||||||
/*@
|
/*@
|
||||||
Ox.char <f> Alias for String.fromCharCode
|
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;
|
Ox.char = String.fromCharCode;
|
||||||
|
|
||||||
/*@
|
/*@
|
||||||
|
@ -30,9 +29,9 @@ Ox.clean <f> Remove leading, trailing and double whitespace from a string
|
||||||
> Ox.clean(" foo\tbar ")
|
> Ox.clean(" foo\tbar ")
|
||||||
"foo bar"
|
"foo bar"
|
||||||
@*/
|
@*/
|
||||||
Ox.clean = function(str) {
|
Ox.clean = function(string) {
|
||||||
return Ox.filter(Ox.map(str.split('\n'), function(str) {
|
return Ox.filter(Ox.map(string.split('\n'), function(string) {
|
||||||
return Ox.trim(str.replace(/\s+/g, ' ')) || '';
|
return string.replace(/\s+/g, ' ').trim() || '';
|
||||||
})).join('\n');
|
})).join('\n');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -44,11 +43,11 @@ Ox.endsWith <f> Checks if a string ends with a given substring
|
||||||
> Ox.endsWith('foobar', 'bar')
|
> Ox.endsWith('foobar', 'bar')
|
||||||
true
|
true
|
||||||
@*/
|
@*/
|
||||||
Ox.ends = Ox.endsWith = function(str, sub) {
|
Ox.ends = Ox.endsWith = function(string, substring) {
|
||||||
// fixme: rename to ends
|
// fixme: rename to ends
|
||||||
str = str.toString();
|
string = string.toString();
|
||||||
sub = sub.toString();
|
substring = substring.toString();
|
||||||
return str.slice(str.length - sub.length) == sub;
|
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")
|
> Ox.isValidEmail("foo@bar..com")
|
||||||
false
|
false
|
||||||
@*/
|
@*/
|
||||||
Ox.isValidEmail = function(str) {
|
Ox.isValidEmail = function(string) {
|
||||||
return !!/^[0-9A-Z\.\+\-_]+@(?:[0-9A-Z\-]+\.)+[A-Z]{2,6}$/i.test(str);
|
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")
|
> Ox.pad("abc", 6, "123456")
|
||||||
"456abc"
|
"456abc"
|
||||||
@*/
|
@*/
|
||||||
Ox.pad = function(str, len, pad) {
|
Ox.pad = function(string, length, padding) {
|
||||||
// fixme: slighly obscure signature
|
// fixme: slighly obscure signature
|
||||||
// fixme: weird for negative numbers
|
// fixme: weird for negative numbers
|
||||||
var pos = len / (len = Math.abs(len));
|
var pos = length / (length = Math.abs(length));
|
||||||
str = str.toString().slice(0, len);
|
string = string.toString().slice(0, length);
|
||||||
pad = Ox.repeat(pad || '0', len - str.length);
|
padding = Ox.repeat(padding || '0', length - string.length);
|
||||||
return pos == 1
|
return pos == 1
|
||||||
? (pad + str).slice(-len)
|
? (padding + string).slice(-length)
|
||||||
: (str + pad).slice(0, len);
|
: (string + padding).slice(0, length);
|
||||||
};
|
};
|
||||||
|
|
||||||
/*@
|
/*@
|
||||||
|
@ -202,8 +201,8 @@ Ox.parsePath <f> Returns the components of a path
|
||||||
> Ox.parsePath('.foo')
|
> Ox.parsePath('.foo')
|
||||||
{extension: '', filename: '.foo', pathname: ''}
|
{extension: '', filename: '.foo', pathname: ''}
|
||||||
@*/
|
@*/
|
||||||
Ox.parsePath = function(str) {
|
Ox.parsePath = function(string) {
|
||||||
var matches = /^(.+\/)?(.+?(\..+)?)?$/.exec(str);
|
var matches = /^(.+\/)?(.+?(\..+)?)?$/.exec(string);
|
||||||
return {
|
return {
|
||||||
pathname: matches[1] || '',
|
pathname: matches[1] || '',
|
||||||
filename: matches[2] || '',
|
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')
|
> Ox.parseSRT('1\n01:02:00,000 --> 01:02:03,400\nHello World')
|
||||||
[{'in': 3720, out: 3723.4, text: 'Hello World'}]
|
[{'in': 3720, out: 3723.4, text: 'Hello World'}]
|
||||||
@*/
|
@*/
|
||||||
Ox.parseSRT = function(str, fps) {
|
Ox.parseSRT = function(string, fps) {
|
||||||
return str.replace(/\r\n/g, '\n').replace(/\n+$/, '').split('\n\n')
|
return string.replace(/\r\n/g, '\n').replace(/\n+$/, '').split('\n\n')
|
||||||
.map(function(block) {
|
.map(function(block) {
|
||||||
var lines = block.split('\n'), points;
|
var lines = block.split('\n'), points;
|
||||||
lines.shift();
|
lines.shift();
|
||||||
points = lines.shift().split(' --> ').map(function(point) {
|
points = lines.shift().split(' --> ').map(function(point) {
|
||||||
return point.replace(',', ':').split(':')
|
return point.replace(',', ':').split(':')
|
||||||
.reduce(function(prev, curr, i) {
|
.reduce(function(previous, current, index) {
|
||||||
return prev + parseInt(curr, 10) *
|
return previous + parseInt(current, 10) *
|
||||||
[3600, 60, 1, 0.001][i];
|
[3600, 60, 1, 0.001][index];
|
||||||
}, 0);
|
}, 0);
|
||||||
});
|
});
|
||||||
if (fps) {
|
if (fps) {
|
||||||
|
@ -395,15 +394,15 @@ Ox.repeat <f> Repeat a value multiple times
|
||||||
> Ox.repeat([{k: "v"}], 3)
|
> Ox.repeat([{k: "v"}], 3)
|
||||||
[{k: "v"}, {k: "v"}, {k: "v"}]
|
[{k: "v"}, {k: "v"}, {k: "v"}]
|
||||||
@*/
|
@*/
|
||||||
Ox.repeat = function(val, num) {
|
Ox.repeat = function(value, times) {
|
||||||
var ret;
|
var ret;
|
||||||
if (Ox.isArray(val)) {
|
if (Ox.isArray(value)) {
|
||||||
ret = [];
|
ret = [];
|
||||||
Ox.loop(num, function() {
|
Ox.loop(times, function() {
|
||||||
ret = ret.concat(val);
|
ret = ret.concat(value);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
ret = num >= 1 ? new Array(num + 1).join(val.toString()) : '';
|
ret = times >= 1 ? new Array(times + 1).join(value.toString()) : '';
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
|
@ -416,11 +415,11 @@ Ox.startsWith <f> Checks if a string starts with a given substring
|
||||||
> Ox.startsWith('foobar', 'foo')
|
> Ox.startsWith('foobar', 'foo')
|
||||||
true
|
true
|
||||||
@*/
|
@*/
|
||||||
Ox.starts = Ox.startsWith = function(str, sub) {
|
Ox.starts = Ox.startsWith = function(string, substring) {
|
||||||
// fixme: rename to starts
|
// fixme: rename to starts
|
||||||
str = str.toString();
|
string = string.toString();
|
||||||
sub = sub.toString();
|
substring = substring.toString();
|
||||||
return str.slice(0, sub.length) == sub;
|
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')
|
> Ox.stripTags('f<span>o</span>o')
|
||||||
'foo'
|
'foo'
|
||||||
@*/
|
@*/
|
||||||
Ox.stripTags = function(str) {
|
Ox.stripTags = function(string) {
|
||||||
return str.replace(/<.*?>/g, '');
|
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')
|
> Ox.toCamelCase('foo_bar_baz')
|
||||||
'fooBarBaz'
|
'fooBarBaz'
|
||||||
@*/
|
@*/
|
||||||
Ox.toCamelCase = function(str) {
|
Ox.toCamelCase = function(string) {
|
||||||
return str.replace(/[\-\/_][a-z]/g, function(str) {
|
return string.replace(/[\-\/_][a-z]/g, function(string) {
|
||||||
return str[1].toUpperCase();
|
return string[1].toUpperCase();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -452,9 +451,9 @@ Ox.toDashes <f> Takes a camelCase string, returns a string with dashes
|
||||||
> Ox.toDashes('fooBarBaz')
|
> Ox.toDashes('fooBarBaz')
|
||||||
'foo-bar-baz'
|
'foo-bar-baz'
|
||||||
@*/
|
@*/
|
||||||
Ox.toDashes = function(str) {
|
Ox.toDashes = function(string) {
|
||||||
return str.replace(/[A-Z]/g, function(str) {
|
return string.replace(/[A-Z]/g, function(string) {
|
||||||
return '-' + str.toLowerCase();
|
return '-' + string.toLowerCase();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -463,11 +462,9 @@ Ox.toSlashes <f> Takes a camelCase string, returns a string with slashes
|
||||||
> Ox.toSlashes('fooBarBaz')
|
> Ox.toSlashes('fooBarBaz')
|
||||||
'foo/bar/baz'
|
'foo/bar/baz'
|
||||||
@*/
|
@*/
|
||||||
Ox.toSlashes = function(str) {
|
Ox.toSlashes = function(string) {
|
||||||
/*
|
return string.replace(/[A-Z]/g, function(string) {
|
||||||
*/
|
return '/' + string.toLowerCase();
|
||||||
return str.replace(/[A-Z]/g, function(str) {
|
|
||||||
return '/' + str.toLowerCase();
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -478,14 +475,14 @@ Ox.toTitleCase <f> Returns a string with capitalized words
|
||||||
> Ox.toTitleCase('Apple releases iPhone, IBM stock plummets')
|
> Ox.toTitleCase('Apple releases iPhone, IBM stock plummets')
|
||||||
'Apple Releases iPhone, IBM Stock Plummets'
|
'Apple Releases iPhone, IBM Stock Plummets'
|
||||||
@*/
|
@*/
|
||||||
Ox.toTitleCase = function(str) {
|
Ox.toTitleCase = function(string) {
|
||||||
return str.split(' ').map(function(val) {
|
return string.split(' ').map(function(value) {
|
||||||
var sub = val.slice(1),
|
var substring = value.slice(1),
|
||||||
low = sub.toLowerCase();
|
lowercase = substring.toLowerCase();
|
||||||
if (sub == low) {
|
if (substring == lowercase) {
|
||||||
val = val.slice(0, 1).toUpperCase() + low;
|
value = value.slice(0, 1).toUpperCase() + lowercase;
|
||||||
}
|
}
|
||||||
return val;
|
return value;
|
||||||
}).join(' ');
|
}).join(' ');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -494,20 +491,12 @@ Ox.toUnderscores <f> Takes a camelCase string, returns string with underscores
|
||||||
> Ox.toUnderscores('fooBarBaz')
|
> Ox.toUnderscores('fooBarBaz')
|
||||||
'foo_bar_baz'
|
'foo_bar_baz'
|
||||||
@*/
|
@*/
|
||||||
Ox.toUnderscores = function(str) {
|
Ox.toUnderscores = function(string) {
|
||||||
return str.replace(/[A-Z]/g, function(str) {
|
return string.replace(/[A-Z]/g, function(string) {
|
||||||
return '_' + str.toLowerCase();
|
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
|
Ox.truncate <f> Truncate a string to a given length
|
||||||
(string, length) <s> Truncated string
|
(string, length) <s> Truncated string
|
||||||
|
@ -523,24 +512,22 @@ Ox.truncate <f> Truncate a string to a given length
|
||||||
> Ox.truncate('anticonstitutionellement', 16, '...', 'center')
|
> Ox.truncate('anticonstitutionellement', 16, '...', 'center')
|
||||||
'anticon...lement'
|
'anticon...lement'
|
||||||
@*/
|
@*/
|
||||||
Ox.truncate = function(str, len, pad, pos) {
|
Ox.truncate = function(string, length, padding, position) {
|
||||||
var pad = pad || '...',
|
padding = padding || '...';
|
||||||
pos = pos || 'right',
|
position = position || 'right';
|
||||||
strlen = str.length,
|
if (string.length > length) {
|
||||||
padlen = pad.length,
|
if (position == 'left') {
|
||||||
left, right;
|
string = padding
|
||||||
if (strlen > len) {
|
+ string.slice(padding.length + string.length - length);
|
||||||
if (pos == 'left') {
|
} else if (position == 'center') {
|
||||||
str = pad + str.slice(padlen + strlen - len);
|
string = string.slice(0, Math.ceil((length - padding.length) / 2))
|
||||||
} else if (pos == 'center') {
|
+ padding
|
||||||
left = Math.ceil((len - padlen) / 2);
|
+ string.slice(-Math.floor((length - padding.length) / 2));
|
||||||
right = Math.floor((len - padlen) / 2);
|
} else if (position == 'right') {
|
||||||
str = str.slice(0, left) + pad + str.slice(-right);
|
string = string.slice(0, length - padding.length) + padding;
|
||||||
} else if (pos == 'right') {
|
|
||||||
str = str.slice(0, len - padlen) + pad;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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?')
|
> Ox.words('Let\'s "split" array-likes into key/value pairs--okay?')
|
||||||
["let's", "split", "array-likes", "into", "key", "value", "pairs", "okay"]
|
["let's", "split", "array-likes", "into", "key", "value", "pairs", "okay"]
|
||||||
@*/
|
@*/
|
||||||
Ox.words = function(str) {
|
Ox.words = function(string) {
|
||||||
var arr = str.toLowerCase().split(/\b/),
|
var array = string.toLowerCase().split(/\b/),
|
||||||
chr = "-'",
|
length = array.length,
|
||||||
len = arr.length,
|
startsWithWord = /\w/.test(array[0]);
|
||||||
startsWithWord = /\w/.test(arr[0]);
|
array.forEach(function(v, i) {
|
||||||
arr.forEach(function(v, i) {
|
|
||||||
// find single occurrences of "-" or "'" that are not at the beginning
|
// find single occurrences of "-" or "'" that are not at the beginning
|
||||||
// or end of the string, and join the surrounding words with them
|
// or end of the string, and join the surrounding words with them
|
||||||
if (
|
if (
|
||||||
i > 0 && i < len - 1
|
i > 0 && i < length - 1 && (v == '-' || v == '\'')
|
||||||
&& v.length == 1 && chr.indexOf(v) > -1
|
|
||||||
) {
|
) {
|
||||||
arr[i + 1] = arr[i - 1] + arr[i] + arr[i + 1];
|
array[i + 1] = array[i - 1] + array[i] + array[i + 1];
|
||||||
arr[i - 1] = arr[i] = '';
|
array[i - 1] = array[i] = '';
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// remove elements that have been emptied above
|
// remove elements that have been emptied above
|
||||||
arr = arr.filter(function(v) {
|
array = array.filter(function(v) {
|
||||||
return v.length;
|
return v.length;
|
||||||
});
|
});
|
||||||
// return words, not spaces or punctuation
|
// return words, not spaces or punctuation
|
||||||
return arr.filter(function(v, i) {
|
return array.filter(function(v, i) {
|
||||||
return i % 2 == !startsWithWord;
|
return i % 2 == !startsWithWord;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -636,8 +621,8 @@ Ox.wordwrap = function(str, len, sep, bal, spa) {
|
||||||
});
|
});
|
||||||
if (!spa) {
|
if (!spa) {
|
||||||
lines = lines.map(function(line) {
|
lines = lines.map(function(line) {
|
||||||
return Ox.trim(line);
|
return line.trim();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return Ox.trim(lines.join(sep));
|
return lines.join(sep).trim();
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue