2011-11-05 16:46:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
|
|
|
Ox.char <f> Alias for String.fromCharCode
|
|
|
|
@*/
|
|
|
|
Ox.char = String.fromCharCode;
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.clean <f> Remove leading, trailing and double whitespace from a string
|
2012-06-20 15:16:58 +00:00
|
|
|
> Ox.clean('foo bar')
|
|
|
|
'foo bar'
|
|
|
|
> Ox.clean(' foo bar ')
|
|
|
|
'foo bar'
|
|
|
|
> Ox.clean(' foo \n bar ')
|
|
|
|
'foo\nbar'
|
|
|
|
> Ox.clean(' \nfoo\n\nbar\n ')
|
|
|
|
'foo\nbar'
|
|
|
|
> Ox.clean(' foo\tbar ')
|
|
|
|
'foo bar'
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2012-05-25 12:32:17 +00:00
|
|
|
Ox.clean = function(string) {
|
|
|
|
return Ox.filter(Ox.map(string.split('\n'), function(string) {
|
|
|
|
return string.replace(/\s+/g, ' ').trim() || '';
|
2012-05-22 14:29:37 +00:00
|
|
|
})).join('\n');
|
2011-10-07 01:04:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
2012-06-04 09:22:40 +00:00
|
|
|
Ox.endsWith <f> Tests if a string ends with a given substring
|
|
|
|
Equivalent to `new RegExp(Ox.escapeRegExp(substring) + '$').test(string)`.
|
|
|
|
(string, substring) -> <b> True if string ends with substring
|
2011-10-07 01:04:47 +00:00
|
|
|
> Ox.endsWith('foobar', 'bar')
|
|
|
|
true
|
2012-06-04 09:22:40 +00:00
|
|
|
> Ox.endsWith('foobar', 'foo')
|
|
|
|
false
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2012-06-04 09:22:40 +00:00
|
|
|
Ox.endsWith = function(string, substring) {
|
2012-05-25 12:32:17 +00:00
|
|
|
string = string.toString();
|
|
|
|
substring = substring.toString();
|
|
|
|
return string.slice(string.length - substring.length) == substring;
|
2011-10-07 01:04:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.isValidEmail <f> Tests if a string is a valid e-mail address
|
|
|
|
(str) -> <b> True if the string is a valid e-mail address
|
|
|
|
str <s> Any string
|
2012-06-20 15:16:58 +00:00
|
|
|
> Ox.isValidEmail('foo@bar.com')
|
2011-10-07 01:04:47 +00:00
|
|
|
true
|
2012-06-20 15:16:58 +00:00
|
|
|
> Ox.isValidEmail('foo.bar@foobar.co.uk')
|
2011-10-07 01:04:47 +00:00
|
|
|
true
|
2012-06-20 15:16:58 +00:00
|
|
|
> Ox.isValidEmail('foo@bar')
|
2011-10-07 01:04:47 +00:00
|
|
|
false
|
2012-06-20 15:16:58 +00:00
|
|
|
> Ox.isValidEmail('foo@bar..com')
|
2011-10-07 01:04:47 +00:00
|
|
|
false
|
|
|
|
@*/
|
2012-05-25 12:32:17 +00:00
|
|
|
Ox.isValidEmail = function(string) {
|
|
|
|
return !!/^[0-9A-Z\.\+\-_]+@(?:[0-9A-Z\-]+\.)+[A-Z]{2,6}$/i.test(string);
|
2011-11-02 17:27:20 +00:00
|
|
|
};
|
2011-10-07 01:04:47 +00:00
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.pad <f> Pad a string to a given length
|
2012-06-04 09:08:38 +00:00
|
|
|
(string[, position], length[, padding]) -> <s> Padded string
|
|
|
|
string <s> String
|
|
|
|
position <s|'right'> Position ('left' or 'right')
|
|
|
|
When passing a number as `string`, the default position is 'left'.
|
|
|
|
length <n> Length
|
|
|
|
padding <s|' '> Padding
|
|
|
|
When passing a number as `string`, and leaving out or passing 'left' as
|
|
|
|
`position`, the default padding is '0'.
|
|
|
|
> Ox.pad('foo', 6)
|
|
|
|
'foo '
|
|
|
|
> Ox.pad('foo', 'left', 6)
|
|
|
|
' foo'
|
|
|
|
> Ox.pad('foo', 6, '.')
|
|
|
|
'foo...'
|
|
|
|
> Ox.pad('foo', 'left', 6, '.')
|
|
|
|
'...foo'
|
2011-10-07 01:04:47 +00:00
|
|
|
> Ox.pad(1, 2)
|
2012-06-04 09:08:38 +00:00
|
|
|
'01'
|
|
|
|
> Ox.pad(1, 2, ' ')
|
|
|
|
' 1'
|
|
|
|
> Ox.pad(1, 'right', 2)
|
|
|
|
'1 '
|
|
|
|
> Ox.pad(1, 'right', 2, '_')
|
|
|
|
'1_'
|
|
|
|
> Ox.pad('foo', 6, '123456')
|
|
|
|
'foo123'
|
|
|
|
> Ox.pad('foo', 'left', 6, '123456')
|
|
|
|
'456foo'
|
|
|
|
> Ox.pad('foobar', 3)
|
|
|
|
'foo'
|
|
|
|
> Ox.pad('foobar', 'left', 3)
|
|
|
|
'bar'
|
2012-06-04 09:22:40 +00:00
|
|
|
> Ox.pad('foo', -1)
|
|
|
|
''
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2012-06-04 09:08:38 +00:00
|
|
|
Ox.pad = function(string, position, length, padding) {
|
|
|
|
var hasPosition = Ox.isString(arguments[1]),
|
|
|
|
isNumber = Ox.isNumber(arguments[0]),
|
|
|
|
last = Ox.last(arguments);
|
|
|
|
position = hasPosition ? arguments[1] : isNumber ? 'left' : 'right';
|
2012-06-04 09:22:40 +00:00
|
|
|
length = Math.max(hasPosition ? arguments[2] : arguments[1], 0);
|
2012-06-04 09:08:38 +00:00
|
|
|
padding = Ox.isString(last) ? last
|
|
|
|
: isNumber && position == 'left' ? '0' : ' ';
|
|
|
|
string = string.toString();
|
|
|
|
padding = Ox.repeat(padding, length - string.length);
|
|
|
|
return position == 'left'
|
2012-05-25 12:32:17 +00:00
|
|
|
? (padding + string).slice(-length)
|
|
|
|
: (string + padding).slice(0, length);
|
2011-10-07 01:04:47 +00:00
|
|
|
};
|
|
|
|
|
2012-05-27 14:51:49 +00:00
|
|
|
/*@
|
|
|
|
Ox.parseDuration <f> Takes a formatted duration, returns seconds
|
2013-08-01 08:55:14 +00:00
|
|
|
> Ox.parseDuration('1:02:03:04.05')
|
|
|
|
93784.05
|
2012-05-27 14:51:49 +00:00
|
|
|
> Ox.parseDuration('3')
|
|
|
|
3
|
|
|
|
> Ox.parseDuration('2:')
|
|
|
|
120
|
|
|
|
> Ox.parseDuration('1::')
|
|
|
|
3600
|
|
|
|
@*/
|
|
|
|
Ox.parseDuration = function(string) {
|
2013-08-01 08:55:14 +00:00
|
|
|
return string.split(':').reverse().slice(0, 4).reduce(function(p, c, i) {
|
|
|
|
return p + (parseFloat(c) || 0) * (i == 3 ? 86400 : Math.pow(60, i));
|
2012-05-27 14:51:49 +00:00
|
|
|
}, 0);
|
|
|
|
};
|
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
|
|
|
Ox.parsePath <f> Returns the components of a path
|
|
|
|
(str) -> <o> Path
|
|
|
|
extension <s> File extension
|
|
|
|
filename <s> Filename
|
|
|
|
pathname <s> Pathname
|
|
|
|
> Ox.parsePath('/foo/bar/foo.bar')
|
|
|
|
{extension: 'bar', filename: 'foo.bar', pathname: '/foo/bar/'}
|
|
|
|
> Ox.parsePath('foo/')
|
|
|
|
{extension: '', filename: '', pathname: 'foo/'}
|
|
|
|
> Ox.parsePath('foo')
|
|
|
|
{extension: '', filename: 'foo', pathname: ''}
|
|
|
|
> Ox.parsePath('.foo')
|
|
|
|
{extension: '', filename: '.foo', pathname: ''}
|
|
|
|
@*/
|
2012-05-25 12:32:17 +00:00
|
|
|
Ox.parsePath = function(string) {
|
|
|
|
var matches = /^(.+\/)?(.+?(\..+)?)?$/.exec(string);
|
2011-10-07 01:04:47 +00:00
|
|
|
return {
|
|
|
|
pathname: matches[1] || '',
|
|
|
|
filename: matches[2] || '',
|
2012-05-24 09:47:33 +00:00
|
|
|
extension: matches[3] ? matches[3].slice(1) : ''
|
2011-10-07 01:04:47 +00:00
|
|
|
};
|
2012-06-04 09:08:38 +00:00
|
|
|
};
|
2011-10-07 01:04:47 +00:00
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.parseSRT <f> Parses an srt subtitle file
|
|
|
|
(str) -> <o> Parsed subtitles
|
|
|
|
in <n> In point (sec)
|
|
|
|
out <n> Out point (sec)
|
|
|
|
text <s> Text
|
|
|
|
str <s> Contents of 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'}]
|
|
|
|
@*/
|
2012-05-25 12:32:17 +00:00
|
|
|
Ox.parseSRT = function(string, fps) {
|
|
|
|
return string.replace(/\r\n/g, '\n').replace(/\n+$/, '').split('\n\n')
|
2011-10-07 01:04:47 +00:00
|
|
|
.map(function(block) {
|
|
|
|
var lines = block.split('\n'), points;
|
|
|
|
lines.shift();
|
|
|
|
points = lines.shift().split(' --> ').map(function(point) {
|
|
|
|
return point.replace(',', ':').split(':')
|
2012-05-25 12:32:17 +00:00
|
|
|
.reduce(function(previous, current, index) {
|
|
|
|
return previous + parseInt(current, 10) *
|
|
|
|
[3600, 60, 1, 0.001][index];
|
2011-10-07 01:04:47 +00:00
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
if (fps) {
|
|
|
|
points = points.map(function(point) {
|
|
|
|
return Math.round(point * fps) / fps;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
'in': points[0],
|
|
|
|
out: points[1],
|
|
|
|
text: lines.join('\n')
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2012-05-27 10:31:09 +00:00
|
|
|
/*@
|
|
|
|
Ox.parseURL <f> Takes a URL, returns its components
|
|
|
|
(url) -> <o> URL components
|
2012-06-03 09:12:21 +00:00
|
|
|
hash <s> Hash
|
|
|
|
host <s> Host
|
|
|
|
hostname <s> Hostname
|
|
|
|
origin <s> Origin
|
|
|
|
pathname <s> Pathname
|
|
|
|
port <s> Port
|
|
|
|
protocol <s> Protocol
|
|
|
|
search <s> Search
|
2012-05-27 10:31:09 +00:00
|
|
|
url <s> URL
|
2012-06-03 09:12:21 +00:00
|
|
|
> Ox.parseURL('http://www.foo.com:8080/bar/index.html?a=0&b=1#c').hash
|
2012-05-27 10:31:09 +00:00
|
|
|
'#c'
|
2012-06-03 09:12:21 +00:00
|
|
|
> Ox.parseURL('http://www.foo.com:8080/bar/index.html?a=0&b=1#c').host
|
2012-05-27 10:31:09 +00:00
|
|
|
'www.foo.com:8080'
|
2012-06-03 09:12:21 +00:00
|
|
|
> Ox.parseURL('http://www.foo.com:8080/bar/index.html?a=0&b=1#c').hostname
|
2012-05-27 10:31:09 +00:00
|
|
|
'www.foo.com'
|
2012-06-03 09:12:21 +00:00
|
|
|
> Ox.parseURL('http://www.foo.com:8080/bar/index.html?a=0&b=1#c').origin
|
2012-05-27 10:31:09 +00:00
|
|
|
'http://www.foo.com:8080'
|
2012-06-03 09:12:21 +00:00
|
|
|
> Ox.parseURL('http://www.foo.com:8080/bar/index.html?a=0&b=1#c').pathname
|
2012-05-27 10:31:09 +00:00
|
|
|
'/bar/index.html'
|
2012-06-03 09:12:21 +00:00
|
|
|
> Ox.parseURL('http://www.foo.com:8080/bar/index.html?a=0&b=1#c').port
|
2012-05-27 10:31:09 +00:00
|
|
|
'8080'
|
2012-06-03 09:13:39 +00:00
|
|
|
> Ox.parseURL('http://www.foo.com:8080/bar/index.html?a=0&b=1#c').protocol
|
2012-05-27 10:31:09 +00:00
|
|
|
'http:'
|
2012-06-03 09:12:21 +00:00
|
|
|
> Ox.parseURL('http://www.foo.com:8080/bar/index.html?a=0&b=1#c').search
|
2012-05-27 10:31:09 +00:00
|
|
|
'?a=0&b=1'
|
|
|
|
@*/
|
|
|
|
Ox.parseURL = (function() {
|
|
|
|
var a = document.createElement('a'),
|
|
|
|
keys = ['hash', 'host', 'hostname', 'origin',
|
|
|
|
'pathname', 'port', 'protocol', 'search'];
|
|
|
|
return function(string) {
|
|
|
|
var ret = {};
|
|
|
|
a.href = string;
|
|
|
|
keys.forEach(function(key) {
|
|
|
|
ret[key] = a[key];
|
|
|
|
});
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
}());
|
|
|
|
|
2012-06-20 15:16:58 +00:00
|
|
|
// FIXME: can we get rid of this?
|
2012-03-21 06:54:02 +00:00
|
|
|
Ox.parseUserAgent = function(userAgent) {
|
|
|
|
var aliases = {
|
|
|
|
browser: {
|
|
|
|
'Firefox': /(Fennec|Firebird|Iceweasel|Minefield|Namoroka|Phoenix|SeaMonkey|Shiretoko)/
|
|
|
|
},
|
|
|
|
system: {
|
|
|
|
'BSD': /(FreeBSD|NetBSD|OpenBSD)/,
|
|
|
|
'Linux': /(CrOS|MeeGo|webOS)/,
|
|
|
|
'Unix': /(AIX|HP-UX|IRIX|SunOS)/
|
|
|
|
}
|
|
|
|
},
|
|
|
|
names = {
|
|
|
|
browser: {
|
|
|
|
'chromeframe': 'Chrome Frame',
|
|
|
|
'MSIE': 'Internet Explorer'
|
|
|
|
},
|
|
|
|
system: {
|
2012-03-21 08:36:26 +00:00
|
|
|
'CPU OS': 'iOS',
|
2012-03-21 06:54:02 +00:00
|
|
|
'iPhone OS': 'iOS',
|
|
|
|
'Macintosh': 'Mac OS X'
|
|
|
|
}
|
2011-12-18 09:29:35 +00:00
|
|
|
},
|
|
|
|
regexps = {
|
|
|
|
browser: [
|
2012-03-21 06:54:02 +00:00
|
|
|
/(Camino)\/(\d+)/,
|
2011-12-18 09:29:35 +00:00
|
|
|
/(chromeframe)\/(\d+)/,
|
|
|
|
/(Chrome)\/(\d+)/,
|
2012-03-21 06:54:02 +00:00
|
|
|
/(Epiphany)\/(\d+)/,
|
2011-12-18 09:29:35 +00:00
|
|
|
/(Firefox)\/(\d+)/,
|
2012-03-21 06:54:02 +00:00
|
|
|
/(Galeon)\/(\d+)/,
|
|
|
|
/(Googlebot)\/(\d+)/,
|
|
|
|
/(Konqueror)\/(\d+)/,
|
|
|
|
/(MSIE) (\d+)/,
|
|
|
|
/(Netscape)\d?\/(\d+)/,
|
|
|
|
/(NokiaBrowser)\/(\d+)/,
|
|
|
|
/(Opera) (\d+)/,
|
2011-12-18 09:29:35 +00:00
|
|
|
/(Opera)\/.+Version\/(\d+)/,
|
|
|
|
/Version\/(\d+).+(Safari)/
|
|
|
|
],
|
|
|
|
system: [
|
2012-03-21 06:54:02 +00:00
|
|
|
/(Android) (\d+)/,
|
|
|
|
/(BeOS)/,
|
|
|
|
/(BlackBerry) (\d+)/,
|
|
|
|
/(Darwin)/,
|
|
|
|
/(BSD) (FreeBSD|NetBSD|OpenBSD)/,
|
2012-03-21 08:36:26 +00:00
|
|
|
/(CPU OS) (\d+)/,
|
2011-12-18 09:29:35 +00:00
|
|
|
/(iPhone OS) (\d+)/,
|
2012-03-21 06:54:02 +00:00
|
|
|
/(Linux).+(CentOS|CrOS|Debian|Fedora|Gentoo|Mandriva|MeeGo|Mint|Red Hat|SUSE|Ubuntu|webOS)/,
|
2012-03-21 08:36:26 +00:00
|
|
|
/(CentOS|CrOS|Debian|Fedora|Gentoo|Mandriva|MeeGo|Mint|Red Hat|SUSE|Ubuntu|webOS).+(Linux)/,
|
2012-03-21 06:54:02 +00:00
|
|
|
/(Linux)/,
|
2011-12-18 09:29:35 +00:00
|
|
|
/(Mac OS X) (10.\d)/,
|
2012-03-21 06:54:02 +00:00
|
|
|
/(Mac OS X)/,
|
|
|
|
/(Macintosh)/,
|
|
|
|
/(SymbianOS)\/(\d+)/,
|
|
|
|
/(SymbOS)/,
|
|
|
|
/(OS\/2)/,
|
|
|
|
/(Unix) (AIX|HP-UX|IRIX|SunOS)/,
|
|
|
|
/(Unix)/,
|
|
|
|
/(Windows) (NT \d\.\d)/,
|
|
|
|
/(Windows) (95|98|2000|2003|ME|NT|XP)/, // Opera
|
|
|
|
/(Windows).+(Win 9x 4\.90)/, // Firefox
|
|
|
|
/(Windows).+(Win9\d)/, // Firefox
|
|
|
|
/(Windows).+(WinNT4.0)/ // Firefox
|
2011-12-18 09:29:35 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
versions = {
|
2012-03-21 06:54:02 +00:00
|
|
|
browser: {},
|
|
|
|
system: {
|
2012-03-21 08:36:26 +00:00
|
|
|
'10.0': '10.0 (Cheetah)',
|
|
|
|
'10.1': '10.1 (Puma)',
|
2012-03-21 06:54:02 +00:00
|
|
|
'10.2': '10.2 (Jaguar)',
|
|
|
|
'10.3': '10.3 (Panther)',
|
|
|
|
'10.4': '10.4 (Tiger)',
|
|
|
|
'10.5': '10.5 (Leopard)',
|
|
|
|
'10.6': '10.6 (Snow Leopard)',
|
|
|
|
'10.7': '10.7 (Lion)',
|
2012-03-21 08:36:26 +00:00
|
|
|
'10.8': '10.8 (Mountain Lion)',
|
2012-03-21 06:54:02 +00:00
|
|
|
'CrOS': 'Chrome OS',
|
|
|
|
'NT 4.0': 'NT 4.0 (Windows NT)',
|
|
|
|
'NT 4.1': 'NT 4.1 (Windows 98)',
|
|
|
|
'Win 9x 4.90': 'NT 4.9 (Windows ME)',
|
|
|
|
'NT 5.0': 'NT 5.0 (Windows 2000)',
|
|
|
|
'NT 5.1': 'NT 5.1 (Windows XP)',
|
|
|
|
'NT 5.2': 'NT 5.2 (Windows 2003)',
|
|
|
|
'NT 6.0': 'NT 6.0 (Windows Vista)',
|
|
|
|
'NT 6.1': 'NT 6.1 (Windows 7)',
|
|
|
|
'NT 6.2': 'NT 6.2 (Windows 8)',
|
|
|
|
'95': 'NT 4.0 (Windows 95)',
|
|
|
|
'NT': 'NT 4.0 (Windows NT)',
|
|
|
|
'98': 'NT 4.1 (Windows 98)',
|
|
|
|
'ME': 'NT 4.9 (Windows ME)',
|
|
|
|
'2000': 'NT 5.0 (Windows 2000)',
|
|
|
|
'2003': 'NT 5.2 (Windows 2003)',
|
|
|
|
'XP': 'NT 5.1 (Windows XP)',
|
|
|
|
'Win95': 'NT 4.0 (Windows 95)',
|
|
|
|
'WinNT4.0': 'NT 4.0 (Windows NT)',
|
|
|
|
'Win98': 'NT 4.1 (Windows 98)'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
userAgentData = {};
|
2011-12-18 09:29:35 +00:00
|
|
|
Ox.forEach(regexps, function(regexps, key) {
|
2012-03-21 06:54:02 +00:00
|
|
|
userAgentData[key] = {name: '', string: '', version: ''};
|
|
|
|
Ox.forEach(aliases[key], function(regexp, alias) {
|
|
|
|
userAgent = userAgent.replace(
|
|
|
|
regexp, key == 'browser' ? alias : alias + ' $1'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
Ox.forEach(regexps, function(regexp) {
|
|
|
|
var matches = userAgent.match(regexp),
|
|
|
|
name, string, swap, version;
|
2011-12-18 09:29:35 +00:00
|
|
|
if (matches) {
|
|
|
|
matches[2] = matches[2] || '';
|
2012-03-21 08:36:26 +00:00
|
|
|
swap = matches[1].match(/^\d/) || matches[2] == 'Linux';
|
2011-12-18 09:29:35 +00:00
|
|
|
name = matches[swap ? 2 : 1];
|
|
|
|
version = matches[swap ? 1 : 2].replace('_', '.');
|
2012-03-21 06:54:02 +00:00
|
|
|
name = names[key][name] || name,
|
|
|
|
version = versions[key][version] || version;
|
2012-03-21 08:36:26 +00:00
|
|
|
string = name;
|
|
|
|
if (version) {
|
|
|
|
string += ' ' + (
|
|
|
|
['BSD', 'Linux', 'Unix'].indexOf(name) > -1
|
|
|
|
? '(' + version + ')'
|
|
|
|
: version
|
|
|
|
)
|
|
|
|
}
|
2012-03-21 06:54:02 +00:00
|
|
|
userAgentData[key] = {
|
2011-12-18 09:29:35 +00:00
|
|
|
name: names[name] || name,
|
2012-03-21 06:54:02 +00:00
|
|
|
string: string,
|
2011-12-18 09:29:35 +00:00
|
|
|
version: versions[version] || version
|
|
|
|
};
|
2012-07-05 08:58:08 +00:00
|
|
|
return false; // break
|
2011-12-18 09:29:35 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2012-03-21 06:54:02 +00:00
|
|
|
return userAgentData;
|
2011-12-18 09:29:35 +00:00
|
|
|
};
|
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
|
|
|
Ox.repeat <f> Repeat a value multiple times
|
|
|
|
Works for arrays, numbers and strings
|
|
|
|
> Ox.repeat(1, 3)
|
2012-06-20 15:16:58 +00:00
|
|
|
'111'
|
|
|
|
> Ox.repeat('foo', 3)
|
|
|
|
'foofoofoo'
|
2011-10-07 01:04:47 +00:00
|
|
|
> Ox.repeat([1, 2], 3)
|
|
|
|
[1, 2, 1, 2, 1, 2]
|
2012-06-20 15:16:58 +00:00
|
|
|
> Ox.repeat([{k: 'v'}], 3)
|
|
|
|
[{k: 'v'}, {k: 'v'}, {k: 'v'}]
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2012-05-25 12:32:17 +00:00
|
|
|
Ox.repeat = function(value, times) {
|
2011-10-07 01:04:47 +00:00
|
|
|
var ret;
|
2012-05-25 12:32:17 +00:00
|
|
|
if (Ox.isArray(value)) {
|
2011-10-07 01:04:47 +00:00
|
|
|
ret = [];
|
2012-05-25 12:32:17 +00:00
|
|
|
Ox.loop(times, function() {
|
|
|
|
ret = ret.concat(value);
|
2011-10-07 01:04:47 +00:00
|
|
|
});
|
|
|
|
} else {
|
2012-05-25 12:32:17 +00:00
|
|
|
ret = times >= 1 ? new Array(times + 1).join(value.toString()) : '';
|
2011-10-07 01:04:47 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2012-05-26 13:05:11 +00:00
|
|
|
/*@
|
2012-06-02 11:06:44 +00:00
|
|
|
Ox.splice <f> `[].splice` for strings, returns a new string
|
2012-05-26 13:05:11 +00:00
|
|
|
> Ox.splice('12xxxxx89', 2, 5, 3, 4, 5, 6, 7)
|
|
|
|
'123456789'
|
|
|
|
@*/
|
|
|
|
Ox.splice = function(string, index, remove) {
|
|
|
|
var array = string.split('');
|
|
|
|
Array.prototype.splice.apply(array, Ox.slice(arguments, 1));
|
|
|
|
return array.join('');
|
|
|
|
};
|
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
2012-06-04 09:22:40 +00:00
|
|
|
Ox.startsWith <f> Tests if a string ends with a given substring
|
|
|
|
Equivalent to `new RegExp('^' + Ox.escapeRegExp(substring)).test(string)`.
|
|
|
|
(string, substring) -> <b> True if string starts with substring
|
2011-10-07 01:04:47 +00:00
|
|
|
> Ox.startsWith('foobar', 'foo')
|
|
|
|
true
|
2012-06-04 09:22:40 +00:00
|
|
|
> Ox.startsWith('foobar', 'bar')
|
|
|
|
false
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2012-06-04 09:22:40 +00:00
|
|
|
Ox.startsWith = function(string, substring) {
|
2012-05-25 12:32:17 +00:00
|
|
|
string = string.toString();
|
|
|
|
substring = substring.toString();
|
|
|
|
return string.slice(0, substring.length) == substring;
|
2011-10-07 01:04:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.toCamelCase <f> Takes a string with '-', '/' or '_', returns a camelCase string
|
|
|
|
> Ox.toCamelCase('foo-bar-baz')
|
|
|
|
'fooBarBaz'
|
|
|
|
> Ox.toCamelCase('foo/bar/baz')
|
|
|
|
'fooBarBaz'
|
|
|
|
> Ox.toCamelCase('foo_bar_baz')
|
|
|
|
'fooBarBaz'
|
|
|
|
@*/
|
2012-05-25 12:32:17 +00:00
|
|
|
Ox.toCamelCase = function(string) {
|
|
|
|
return string.replace(/[\-\/_][a-z]/g, function(string) {
|
|
|
|
return string[1].toUpperCase();
|
2011-10-07 01:04:47 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.toDashes <f> Takes a camelCase string, returns a string with dashes
|
|
|
|
> Ox.toDashes('fooBarBaz')
|
|
|
|
'foo-bar-baz'
|
|
|
|
@*/
|
2012-05-25 12:32:17 +00:00
|
|
|
Ox.toDashes = function(string) {
|
|
|
|
return string.replace(/[A-Z]/g, function(string) {
|
|
|
|
return '-' + string.toLowerCase();
|
2011-10-07 01:04:47 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.toSlashes <f> Takes a camelCase string, returns a string with slashes
|
|
|
|
> Ox.toSlashes('fooBarBaz')
|
|
|
|
'foo/bar/baz'
|
|
|
|
@*/
|
2012-05-25 12:32:17 +00:00
|
|
|
Ox.toSlashes = function(string) {
|
|
|
|
return string.replace(/[A-Z]/g, function(string) {
|
|
|
|
return '/' + string.toLowerCase();
|
2011-10-07 01:04:47 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.toTitleCase <f> Returns a string with capitalized words
|
|
|
|
> Ox.toTitleCase('foo')
|
|
|
|
'Foo'
|
|
|
|
> Ox.toTitleCase('Apple releases iPhone, IBM stock plummets')
|
|
|
|
'Apple Releases iPhone, IBM Stock Plummets'
|
|
|
|
@*/
|
2012-05-25 12:32:17 +00:00
|
|
|
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;
|
2011-10-07 01:04:47 +00:00
|
|
|
}
|
2012-05-25 12:32:17 +00:00
|
|
|
return value;
|
2011-10-07 01:04:47 +00:00
|
|
|
}).join(' ');
|
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.toUnderscores <f> Takes a camelCase string, returns string with underscores
|
|
|
|
> Ox.toUnderscores('fooBarBaz')
|
|
|
|
'foo_bar_baz'
|
|
|
|
@*/
|
2012-05-25 12:32:17 +00:00
|
|
|
Ox.toUnderscores = function(string) {
|
|
|
|
return string.replace(/[A-Z]/g, function(string) {
|
|
|
|
return '_' + string.toLowerCase();
|
2011-10-07 01:04:47 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.truncate <f> Truncate a string to a given length
|
2012-06-03 09:07:39 +00:00
|
|
|
(string[, position], length[, padding]) -> Truncated string
|
2012-06-04 09:08:38 +00:00
|
|
|
string <s> String
|
|
|
|
position <s|'right'> Position ('left', 'center' or 'right')
|
|
|
|
length <n> Length
|
|
|
|
padding <s|'…'> Padding
|
2011-10-07 01:04:47 +00:00
|
|
|
> Ox.truncate('anticonstitutionellement', 16)
|
2012-06-03 09:07:39 +00:00
|
|
|
'anticonstitutio…'
|
|
|
|
> Ox.truncate('anticonstitutionellement', 'left', 16)
|
|
|
|
'…itutionellement'
|
|
|
|
> Ox.truncate('anticonstitutionellement', 16, '...')
|
2011-10-07 01:04:47 +00:00
|
|
|
'anticonstitut...'
|
2012-06-03 09:07:39 +00:00
|
|
|
> Ox.truncate('anticonstitutionellement', 'center', 16, '...')
|
2011-10-07 01:04:47 +00:00
|
|
|
'anticon...lement'
|
|
|
|
@*/
|
2012-06-03 09:07:39 +00:00
|
|
|
Ox.truncate = function(string, position, length, padding) {
|
|
|
|
var hasPosition = Ox.isString(arguments[1]), last = Ox.last(arguments);
|
|
|
|
position = hasPosition ? arguments[1] : 'right';
|
|
|
|
length = hasPosition ? arguments[2] : arguments[1];
|
|
|
|
padding = Ox.isString(last) ? last : '…';
|
2012-05-25 12:32:17 +00:00
|
|
|
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;
|
2011-10-07 01:04:47 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-25 12:32:17 +00:00
|
|
|
return string;
|
2011-10-07 01:04:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.words <f> Splits a string into words, removing punctuation
|
|
|
|
(string) -> <[s]> Array of words
|
|
|
|
string <s> Any string
|
|
|
|
> Ox.words('Let\'s "split" array-likes into key/value pairs--okay?')
|
2012-06-20 20:11:05 +00:00
|
|
|
['let\'s', 'split', 'array-likes', 'into', 'key', 'value', 'pairs', 'okay']
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2012-05-25 12:32:17 +00:00
|
|
|
Ox.words = function(string) {
|
|
|
|
var array = string.toLowerCase().split(/\b/),
|
|
|
|
length = array.length,
|
|
|
|
startsWithWord = /\w/.test(array[0]);
|
|
|
|
array.forEach(function(v, i) {
|
2011-10-07 01:04:47 +00:00
|
|
|
// find single occurrences of "-" or "'" that are not at the beginning
|
|
|
|
// or end of the string, and join the surrounding words with them
|
|
|
|
if (
|
2012-05-25 12:32:17 +00:00
|
|
|
i > 0 && i < length - 1 && (v == '-' || v == '\'')
|
2011-10-07 01:04:47 +00:00
|
|
|
) {
|
2012-05-25 12:32:17 +00:00
|
|
|
array[i + 1] = array[i - 1] + array[i] + array[i + 1];
|
|
|
|
array[i - 1] = array[i] = '';
|
2011-10-07 01:04:47 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
// remove elements that have been emptied above
|
2012-05-25 12:32:17 +00:00
|
|
|
array = array.filter(function(v) {
|
2011-10-07 01:04:47 +00:00
|
|
|
return v.length;
|
|
|
|
});
|
|
|
|
// return words, not spaces or punctuation
|
2012-05-25 12:32:17 +00:00
|
|
|
return array.filter(function(v, i) {
|
2011-10-07 01:04:47 +00:00
|
|
|
return i % 2 == !startsWithWord;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.wordwrap <f> Wrap a string at word boundaries
|
2012-06-04 11:49:10 +00:00
|
|
|
(string, length) -> <s> Wrapped string
|
|
|
|
(string, length, newline) -> <s> Wrapped string
|
|
|
|
(string, length, balanced) -> <s> Wrapped string
|
|
|
|
(string, length, balanced, newline) -> <s> Wrapped string
|
|
|
|
(string, length, newline, balanced) -> <s> Wrapped string
|
|
|
|
string <s> String
|
|
|
|
length <n|80> Line length
|
|
|
|
balanced <b|false> If true, lines will have similar length
|
2012-06-04 12:07:49 +00:00
|
|
|
newline <s|'\n'> Newline character or string
|
2012-06-04 11:49:10 +00:00
|
|
|
> Ox.wordwrap('Anticonstitutionellement, Paris s\'eveille', 25)
|
|
|
|
'Anticonstitutionellement, \nParis s\'eveille'
|
|
|
|
> Ox.wordwrap('Anticonstitutionellement, Paris s\'eveille', 25, '<br>')
|
|
|
|
'Anticonstitutionellement, <br>Paris s\'eveille'
|
|
|
|
> Ox.wordwrap('Anticonstitutionellement, Paris s\'eveille', 16, '<br>')
|
|
|
|
'Anticonstitution<br>ellement, Paris <br>s\'eveille'
|
|
|
|
> Ox.wordwrap('These are short words', 16)
|
|
|
|
'These are short \nwords'
|
|
|
|
> Ox.wordwrap('These are short words', 16, true)
|
|
|
|
'These are \nshort words'
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2012-06-04 11:49:10 +00:00
|
|
|
Ox.wordwrap = function(string, length) {
|
|
|
|
var balanced, lines, max, newline, words;
|
|
|
|
string = String(string);
|
|
|
|
length = length || 80;
|
|
|
|
balanced = Ox.isBoolean(arguments[2]) ? arguments[2]
|
|
|
|
: Ox.isBoolean(arguments[3]) ? arguments[3]
|
|
|
|
: false;
|
|
|
|
newline = Ox.isString(arguments[2]) ? arguments[2]
|
|
|
|
: Ox.isString(arguments[3]) ? arguments[3]
|
|
|
|
: '\n';
|
|
|
|
words = string.split(' ');
|
|
|
|
if (balanced) {
|
|
|
|
// balanced lines: test if same number of lines
|
2011-10-07 01:04:47 +00:00
|
|
|
// can be achieved with a shorter line length
|
2012-06-04 11:49:10 +00:00
|
|
|
lines = Ox.wordwrap(string, length, newline).split(newline);
|
2011-10-07 01:04:47 +00:00
|
|
|
if (lines.length > 1) {
|
|
|
|
// test shorter line, unless
|
|
|
|
// that means cutting a word
|
2012-06-04 11:49:10 +00:00
|
|
|
max = Ox.max(words.map(function(word) {
|
2011-10-07 01:04:47 +00:00
|
|
|
return word.length;
|
|
|
|
}));
|
2012-06-04 11:49:10 +00:00
|
|
|
while (length > max) {
|
|
|
|
if (Ox.wordwrap(
|
|
|
|
string, --length, newline
|
|
|
|
).split(newline).length > lines.length) {
|
|
|
|
length++;
|
2011-10-07 01:04:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lines = [''];
|
2012-05-22 07:11:26 +00:00
|
|
|
words.forEach(function(word) {
|
2012-06-04 11:49:10 +00:00
|
|
|
var index;
|
|
|
|
if ((lines[lines.length - 1] + word).length <= length) {
|
2011-10-07 01:04:47 +00:00
|
|
|
// word fits in current line
|
|
|
|
lines[lines.length - 1] += word + ' ';
|
|
|
|
} else {
|
2012-06-04 11:49:10 +00:00
|
|
|
if (word.length <= length) {
|
2011-10-07 01:04:47 +00:00
|
|
|
// word fits in next line
|
|
|
|
lines.push(word + ' ');
|
|
|
|
} else {
|
|
|
|
// word is longer than line
|
2012-06-04 11:49:10 +00:00
|
|
|
index = length - lines[lines.length - 1].length;
|
|
|
|
lines[lines.length - 1] += word.slice(0, index);
|
|
|
|
while (index < word.length) {
|
|
|
|
lines.push(word.substr(index, length));
|
|
|
|
index += length;
|
|
|
|
}
|
2011-10-07 01:04:47 +00:00
|
|
|
lines[lines.length - 1] += ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2012-06-04 11:49:10 +00:00
|
|
|
return lines.join(newline).trim();
|
2012-01-31 12:00:34 +00:00
|
|
|
};
|