diff --git a/source/Ox/js/Math.js b/source/Ox/js/Math.js index 7391edc9..41438a09 100644 --- a/source/Ox/js/Math.js +++ b/source/Ox/js/Math.js @@ -32,9 +32,9 @@ Ox.divideInt Divides a number by another and returns an array of integers [16, 16, 17, 17, 17, 17] @*/ // fixme: is splitInt a better name? -Ox.divideInt = function(num, by) { - var div = parseInt(num / by), - mod = num % by; +Ox.divideInt = function(number, by) { + var div = parseInt(number / by), + mod = number % by; return Ox.range(by).map(function(i) { return div + (i > by - 1 - mod); }); @@ -59,12 +59,11 @@ Ox.limit Limits a number by a given mininum and maximum > Ox.limit(-1, -2) -2 @*/ -Ox.limit = function(/*num[[, min], max]*/) { - var len = arguments.length, - num = arguments[0], - min = len == 3 ? arguments[1] : -Infinity, - max = arguments[len - 1]; - return Math.min(Math.max(num, min), max); +Ox.limit = function(/*number[[, min], max]*/) { + var number = arguments[0], + min = arguments.length == 3 ? arguments[1] : -Infinity, + max = arguments[arguments.length - 1]; + return Math.min(Math.max(number, min), max); }; /*@ @@ -75,8 +74,8 @@ Ox.log Returns the logarithm of a given number to a given base > Ox.log(Math.E) 1 @*/ -Ox.log = function(num, base) { - return Math.log(num) / Math.log(base || Math.E); +Ox.log = function(number, base) { + return Math.log(number) / Math.log(base || Math.E); }; /*@ @@ -88,8 +87,8 @@ Ox.mod Modulo function > Ox.mod(-11, 10) 9 @*/ -Ox.mod = function(num, by) { - return (num % by + by) % by; +Ox.mod = function(number, by) { + return (number % by + by) % by; }; /*@ @@ -115,9 +114,8 @@ Ox.random Returns a random integer within a given range true @*/ Ox.random = function() { - var len = arguments.length, - min = len == 2 ? arguments[0] : 0, - max = len ? arguments[len - 1] : 2; + var min = arguments.length == 2 ? arguments[0] : 0, + max = arguments.length ? Ox.last(arguments) : 2; return min + Math.floor(Math.random() * (max - min)); }; @@ -130,9 +128,9 @@ Ox.round Rounds a number with a given number of decimals > Ox.round(1 / 2) 1 @*/ -Ox.round = function(num, dec) { - var pow = Math.pow(10, dec || 0); - return Math.round(num * pow) / pow; +Ox.round = function(number, decimals) { + var pow = Math.pow(10, decimals || 0); + return Math.round(number * pow) / pow; }; /*@ diff --git a/source/Ox/js/Object.js b/source/Ox/js/Object.js index c8253781..7b8f52a6 100644 --- a/source/Ox/js/Object.js +++ b/source/Ox/js/Object.js @@ -5,13 +5,13 @@ Ox.extend Extends an object with one or more other objects > Ox.extend({a: 1, b: 1, c: 1}, {b: 2, c: 2}, {c: 3}) {a: 1, b: 2, c: 3} @*/ -Ox.extend = function(obj) { - Ox.forEach(Ox.slice(arguments, 1), function(arg, i) { - Ox.forEach(arg, function(val, key) { - obj[key] = val; +Ox.extend = function(object) { + Ox.forEach(Ox.slice(arguments, 1), function(argument, i) { + Ox.forEach(argument, function(value, key) { + object[key] = value; }); }); - return obj; + return object; }; /*@ @@ -76,8 +76,8 @@ Ox.getset = function(object, args, callback, that) { return ret; }; -Ox.hasOwn = function(obj, val) { - return Object.prototype.hasOwnProperty.call(obj, val) +Ox.hasOwn = function(object, value) { + return Object.prototype.hasOwnProperty.call(object, value) }; /*@ @@ -85,10 +85,10 @@ Ox.keyOf Equivalent of [].indexOf for objects > Ox.keyOf({a: 1, b: 2, c: 3}, 1) 'a' @*/ -Ox.keyOf = function(obj, val) { +Ox.keyOf = function(object, value) { var key; - Ox.forEach(obj, function(v, k) { - if (v === val) { + Ox.forEach(object, function(v, k) { + if (v === value) { key = k; Ox.Break(); } @@ -126,18 +126,18 @@ Ox.methods Returns a sorted list of all method names of an object > Ox.methods({a: [], b: false, f: function() {}, n: 0, o: {}, s: ''}) ['f'] @*/ -Ox.methods = function(obj, includePrototype) { +Ox.methods = function(object, includePrototype) { var key, keys; if (includePrototype) { keys = []; - for (var key in obj) { + for (var key in object) { keys.push(key); } } else { - keys = Object.keys(obj); + keys = Object.keys(object); } return keys.filter(function(key) { - return Ox.isFunction(obj[key]); + return Ox.isFunction(object[key]); }).sort(); }; @@ -150,14 +150,14 @@ Ox.serialize Parses an object into query parameters > Ox.serialize({string: 'foo', empty: {}, null: null, undefined: void 0}) 'string=foo' @*/ -Ox.serialize = function(obj) { - var arr = []; - Ox.forEach(obj, function(val, key) { - if (!Ox.isEmpty(val) && !Ox.isNull(val) && !Ox.isUndefined(val)) { - arr.push(key + '=' + val); +Ox.serialize = function(object) { + var ret = []; + Ox.forEach(object, function(value, key) { + if (!Ox.isEmpty(value) && !Ox.isNull(value) && !Ox.isUndefined(value)) { + ret.push(key + '=' + value); } }); - return arr.join('&'); + return ret.join('&'); }; /*@ @@ -169,19 +169,17 @@ Ox.unserialize Parses query parameters into an object > Ox.unserialize('a=1&b=&c&a=0', true) {a: 0} @*/ -Ox.unserialize = function(str, toNumber) { - var obj = {}; - str.split('&').forEach(function(val) { - if (val) { - var arr = val.split('='); - if (arr[1]) { - obj[arr[0]] = !toNumber ? arr[1] - : arr[1].indexOf(',') == -1 ? +arr[1] - : arr[1].split(',').map(function(val) { - return +val; - }); - } +Ox.unserialize = function(string, toNumber) { + var ret = {}; + Ox.filter(string.split('&')).forEach(function(value) { + var array = value.split('='); + if (array[1]) { + ret[array[0]] = !toNumber ? array[1] + : array[1].indexOf(',') == -1 ? +array[1] + : array[1].split(',').map(function(value) { + return +value; + }); } }); - return obj; + return ret; }; \ No newline at end of file diff --git a/source/Ox/js/RegExp.js b/source/Ox/js/RegExp.js index 2da70a6d..a1a2aeb5 100644 --- a/source/Ox/js/RegExp.js +++ b/source/Ox/js/RegExp.js @@ -4,6 +4,6 @@ Ox.escapeRegExp Escapes a string for use in a regular expression str String @*/ // see https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions -Ox.escapeRegExp = function(str) { - return (str + '').replace(/([\/\\^$*+?.-|(){}[\]])/g, '\\$1'); +Ox.escapeRegExp = function(string) { + return (string + '').replace(/([\/\\^$*+?.-|(){}[\]])/g, '\\$1'); }; \ No newline at end of file diff --git a/source/Ox/js/Request.js b/source/Ox/js/Request.js index d223f9ae..d6a38c59 100644 --- a/source/Ox/js/Request.js +++ b/source/Ox/js/Request.js @@ -189,23 +189,23 @@ Ox.loadAsync Runs an asynchonous function on array elements callback Callback function results Results @*/ -Ox.loadAsync = function(arr, map, callback) { - arr = Ox.makeArray(arr); - var i = 0, n = arr.length, results = {}; - if (arr.some(Ox.isArray)) { +Ox.loadAsync = function(array, map, callback) { + array = Ox.makeArray(array); + var i = 0, n = array.length, results = {}; + if (array.some(Ox.isArray)) { iterate(); } else { - arr.forEach(function(item) { - map(item, function(result) { + array.forEach(function(value) { + map(value, function(result) { Ox.extend(results, result); ++i == n && callback(results); }); }); } function iterate() { - Ox.loadAsync(arr.shift(), function(result) { + Ox.loadAsync(array.shift(), function(result) { Ox.extend(results, result); - arr.length ? iterate() : callback(results); + array.length ? iterate() : callback(results); }); } }; diff --git a/source/Ox/js/String.js b/source/Ox/js/String.js index fdd9b463..80f9f34f 100644 --- a/source/Ox/js/String.js +++ b/source/Ox/js/String.js @@ -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 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 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 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 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 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 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 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 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 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 Strips HTML tags from a string > Ox.stripTags('foo') 'foo' @*/ -Ox.stripTags = function(str) { - return str.replace(/<.*?>/g, ''); +Ox.stripTags = function(string) { + return string.replace(/<.*?>/g, ''); }; /*@ @@ -441,9 +440,9 @@ Ox.toCamelCase 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 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 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 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 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 Truncate a string to a given length (string, length) Truncated string @@ -523,24 +512,22 @@ Ox.truncate 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 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(); };