rather use ''.slice than ''.substr

This commit is contained in:
rolux 2012-05-24 11:47:33 +02:00
parent f990f3b857
commit 1608664bb6
20 changed files with 69 additions and 71 deletions

View file

@ -355,7 +355,7 @@ Ox.load.Image = function(options, callback) {
while (str.length < cap) {
str += str.substr(4, len);
}
str = str.substr(0, Math.ceil(cap));
str = str.slice(0, Math.ceil(cap));
// Create an array of bit values
bin = Ox.flatten(Ox.map(str.split(''), function(chr) {
return Ox.range(8).map(function(i) {

View file

@ -30,7 +30,7 @@ Ox.SourceViewer = function(options, self) {
return match ? match[0].length : 0;
}));
return '<pre>' + lines.map(function(line) {
return line.substr(indent);
return line.slice(indent);
}).join('\n') + '</pre>';
}
]

View file

@ -336,10 +336,10 @@ Ox.URL = function(options) {
}
if (['=', '!='].indexOf(condition.operator) > -1) {
if (condition.value[0] == '*') {
condition.value = condition.value.substr(1);
condition.value = condition.value.slice(1);
condition.operator = condition.operator.replace('=', '$')
} else if (condition.value[condition.value.length - 1] == '*') {
condition.value = condition.value.substr(0, condition.value.length - 1);
condition.value = condition.value.slice(0, -1);
condition.operator = condition.operator.replace('=', '^')
}
}
@ -417,7 +417,7 @@ Ox.URL = function(options) {
return str.split(',').map(function(str) {
var hasOperator = /^[\+-]/.test(str);
return {
key: hasOperator ? str.substr(1) : str,
key: hasOperator ? str.slice(1) : str,
operator: hasOperator
? str[0]
: Ox.getObjectById(self.options.sortKeys[state.type][

View file

@ -113,7 +113,7 @@ Ox.Button = function(options, self) {
that.attr({
src: Ox.UI.getImageURL(
'symbol' + self.options.title[0].toUpperCase()
+ self.options.title.substr(1)
+ self.options.title.slice(1)
)
});
} else {

View file

@ -513,7 +513,7 @@ Ox.Input = function(options, self) {
} else if (/\./.test(value) && self.options.decimals) {
length = value.split('.')[1].length;
if (length > self.options.decimals) {
value = value.substr(0, value.indexOf('.') + 1 + self.options.decimals);
value = value.slice(0, value.indexOf('.') + 1 + self.options.decimals);
cursor = [oldCursor[0] + 1, oldCursor[1] + 1];
} else if (length < self.options.decimals) {
value += Ox.repeat('0', self.options.decimals - length);
@ -527,7 +527,7 @@ Ox.Input = function(options, self) {
}
}
while (/^0\d/.test(value)) {
value = value.substr(1, value.length);
value = value.slice(1);
}
if (!regexp.test(value) || value < self.options.min || value > self.options.max) {
value = oldValue;
@ -725,7 +725,7 @@ Ox.Input = function(options, self) {
that.triggerEvent('insert', {
end: input.selectionEnd,
id: that.oxid,
selection: input.value.substring(input.selectionStart, input.selectionEnd),
selection: input.value.slice(input.selectionStart, input.selectionEnd),
start: input.selectionStart,
value: input.value
});
@ -734,7 +734,7 @@ Ox.Input = function(options, self) {
function keydown(event) {
var oldCursor = cursor(),
oldValue = self.options.value,
newValue = oldValue.substr(0, oldCursor[0] - 1),
newValue = oldValue.slice(0, oldCursor[0] - 1),
hasDeletedSelectedEnd = (event.keyCode == 8 || event.keyCode == 46) &&
oldCursor[0] < oldCursor[1] && oldCursor[1] == oldValue.length;
if (

View file

@ -190,9 +190,9 @@ Ox.InsertHTMLDialog = function(options, self) {
);
self.options.callback({
position: self.options.start + value.length,
value: self.options.value.substr(0, self.options.start)
value: self.options.value.slice(0, self.options.start)
+ value
+ self.options.value.substr(self.options.end)
+ self.options.value.slice(self.options.end)
});
that.close();
}

View file

@ -119,7 +119,7 @@ Ox.TimeInput = function(options, self) {
function getDate() {
return new Date('1970/01/01 ' + (
self.options.milliseconds
? self.options.value.substr(0, self.options.value.length - 4)
? self.options.value.slice(0, -4)
: self.options.value
));
}
@ -129,7 +129,7 @@ Ox.TimeInput = function(options, self) {
return {
ampm: Ox.formatDate(date, '%p'),
hours: Ox.formatDate(date, self.options.ampm ? '%I' : '%H'),
milliseconds: self.options.milliseconds ? self.options.value.substr(-3) : '000',
milliseconds: self.options.milliseconds ? self.options.value.slice(-3) : '000',
minutes: Ox.formatDate(date, '%M'),
seconds: Ox.formatDate(date, '%S')
};

View file

@ -774,7 +774,7 @@ Ox.ListMap = function(options, self) {
country = Ox.getCountryByGeoname(place.geoname);
place.countryCode = country ? country.code : '';
if (!self.isAsync) {
place.id = self.selectedPlace.substr(1); // fixme: safe?
place.id = self.selectedPlace.slice(1); // fixme: safe?
self.selectedPlace = place.id;
self.options.places.push(place);
self.$list.options({

View file

@ -302,8 +302,9 @@ Ox.AnnotationPanel = function(options, self) {
insert({
end: element.selectionEnd,
id: id,
selection: element.value
.substr(element.selectionStart, element.selectionEnd),
selection: element.value.slice(
element.selectionStart, element.selectionEnd
),
start: element.selectionStart,
value: element.value
});

View file

@ -89,7 +89,7 @@ Ox.VideoElement = function(options, self) {
item.$videos = src.map(function(src, i) {
// in all browsers except firefox,
// loadedmetadata fires only once per src
if(Ox.parseURL(src).protocol.substr(0, 4) == 'http') {
if (Ox.startsWith(Ox.parseURL(src).protocol, 'http')) {
src += '?' + Ox.uid();
}
return $('<video>')
@ -434,7 +434,7 @@ Ox.VideoElement = function(options, self) {
self.$video.each(function(video, i) {
if (i != self.currentPart) {
var src = self.options.src[i];
if(Ox.parseURL(src).protocol.substr(0, 4) == 'http') {
if (Ox.startsWith(Ox.parseURL(src).protocol, 'http')) {
src += '?' + Ox.uid();
}
video.src = src;
@ -443,7 +443,7 @@ Ox.VideoElement = function(options, self) {
} else {
self.items[0].$videos.forEach(function($video, i) {
var src = self.options.src[i];
if (Ox.parseURL(src).protocol.substr(0, 4) == 'http') {
if (Ox.startsWith(Ox.parseURL(src).protocol, 'http')) {
src += '?' + Ox.uid();
}
$video[0].src = src;

View file

@ -309,7 +309,7 @@ Ox.Dialog = function(options, self) {
}
function resizestart(event) {
var edge = event.target.className.substr(17).toLowerCase(),
var edge = event.target.className.slice(17).toLowerCase(),
offset = that.offset();
self.drag = {
edge: edge,

View file

@ -462,8 +462,8 @@ Ox.range = function() {
var len;
if (new RegExp('^' + article + ' ').test(sort[val])) {
len = article.length;
sort[val] = sort[val].substr(len + 1) + ', '
+ sort[val].substr(0, len);
sort[val] = sort[val].slice(len + 1) + ', '
+ sort[val].slice(0, len);
Ox.break();
}
});

View file

@ -119,4 +119,3 @@ Ox.toRGB = function(hex) {
return parseInt(hex.substr(i * 2, 2), 16);
});
};

View file

@ -74,7 +74,7 @@ Ox.MONTHS = [
];
//@ Ox.SHORT_MONTHS <[str]> Short names of months
Ox.SHORT_MONTHS = Ox.MONTHS.map(function(val) {
return val.substr(0, 3);
return val.slice(0, 3);
});
//@ Ox.PATH <str> Path of Ox.js
Ox.PATH = Ox.toArray(
@ -127,5 +127,5 @@ Ox.WEEKDAYS = [
];
//@ Ox.SHORT_WEEKDAYS <[str]> Short names of weekdays
Ox.SHORT_WEEKDAYS = Ox.WEEKDAYS.map(function(val) {
return val.substr(0, 3);
});
return val.slice(0, 3);
});

View file

@ -141,7 +141,7 @@ Ox.localStorage = function(namespace) {
ret = {};
Ox.forEach(localStorage, function(val, key) {
if (Ox.startsWith(key, namespace + '.')) {
ret[key.substr(namespace.length + 1)] = JSON.parse(val);
ret[key.slice(namespace.length + 1)] = JSON.parse(val);
}
});
} else if (arguments.length == 1 && typeof key == 'string') {
@ -214,7 +214,7 @@ Ox.Log = (function() {
if (!log.filterEnabled || log.filter.indexOf(args[0]) > -1) {
date = new Date();
args.unshift(
Ox.formatDate(date, '%H:%M:%S.') + (+date).toString().substr(-3)
Ox.formatDate(date, '%H:%M:%S.') + (+date).toString().slice(-3)
);
window.console && window.console.log.apply(window.console, args);
ret = args.join(' ');
@ -326,7 +326,7 @@ Ox.print = function() {
var args = Ox.toArray(arguments),
date = new Date();
args.unshift(
Ox.formatDate(date, '%H:%M:%S.') + (+date).toString().substr(-3)/*,
Ox.formatDate(date, '%H:%M:%S.') + (+date).toString().slice(-3)/*,
(arguments.callee.caller && arguments.callee.caller.name)
|| '(anonymous)'*/
);

View file

@ -26,7 +26,7 @@ Ox.getDateInWeek = function(date, weekday, utc) {
var sourceWeekday = Ox.getISODay(date, utc),
targetWeekday = Ox.isNumber(weekday) ? weekday
: Ox.indexOf(Ox.WEEKDAYS, function(v) {
return v.substr(0, 3) == weekday.substr(0, 3);
return v.slice(0, 3) == weekday.slice(0, 3);
}) + 1;
return Ox.setDate(date, Ox.getDate(date, utc) - sourceWeekday + targetWeekday, utc);
}
@ -72,7 +72,7 @@ Ox.getDaysInMonth = function(year, month) {
year = Ox.makeYear(year);
month = Ox.isNumber(month) ? month
: Ox.indexOf(Ox.MONTHS, function(v) {
return v.substr(0, 3) == month.substr(0, 3);
return v.slice(0, 3) == month.slice(0, 3);
}) + 1;
return new Date(year, month, 0).getDate();
}

View file

@ -204,14 +204,14 @@
// bytes IHDR name), keep bytes 16 to 19 (width), discard bytes 20 to 29
// (4 bytes height, 5 bytes flags), keep bytes 29 to 32 (IHDR checksum),
// keep the rest (IDAT chunks), discard the last 12 bytes (IEND chunk).
data = str.substr(16, 4) + str.substr(29, 4);
idat = str.substr(33, str.length - 45);
data = str.slice(16, 20) + str.slice(29, 33);
idat = str.slice(33, -45);
while (idat) {
// Each IDAT chunk is 4 bytes length, 4 bytes name, length bytes
// data and 4 bytes checksum. We can discard the name parts.
len = idat.substr(0, 4);
data += len + idat.substr(8, 4 + (len = Ox.decodeBase256(len)));
idat = idat.substr(12 + len);
len = idat.slice(0, 4);
data += len + idat.slice(8, 12 + (len = Ox.decodeBase256(len)));
idat = idat.slice(12 + len);
}
callback && callback(data);
return data;
@ -234,18 +234,18 @@
var image = new Image(),
// PNG file signature and IHDR chunk
data = '\u0089PNG\r\n\u001A\n\u0000\u0000\u0000\u000DIHDR'
+ str.substr(0, 4) + '\u0000\u0000\u0000\u0001'
+ '\u0008\u0006\u0000\u0000\u0000' + str.substr(4, 4),
+ str.slice(0, 4) + '\u0000\u0000\u0000\u0001'
+ '\u0008\u0006\u0000\u0000\u0000' + str.slice(4, 8),
// IDAT chunks
idat = str.substr(8), len;
idat = str.slice(8), len;
function error() {
throw new RangeError('Deflate codec can\'t decode data.');
}
while (idat) {
// Reinsert the IDAT chunk names
len = idat.substr(0, 4);
data += len + 'IDAT' + idat.substr(4, 4 + (len = Ox.decodeBase256(len)));
idat = idat.substr(8 + len);
len = idat.slice(0, 4);
data += len + 'IDAT' + idat.slice(4, 8 + (len = Ox.decodeBase256(len)));
idat = idat.slice(8 + len);
}
// IEND chunk
data += '\u0000\u0000\u0000\u0000IEND\u00AE\u0042\u0060\u0082';
@ -259,7 +259,7 @@
try {
// Parse the first byte as number of bytes to chop at the end,
// and the rest, without these bytes, as an UTF8-encoded string.
str = Ox.decodeUTF8(str.substr(1, str.length - 1 - str.charCodeAt(0)))
str = Ox.decodeUTF8(str.slice(1, -str.CharCodeAt(0)));
} catch (e) {
error();
}

View file

@ -153,7 +153,7 @@ Ox.formatDate = function(date, str, utc) {
['d', function(d) {return Ox.pad(Ox.getDate(d, utc), 2);}],
['e', function(d) {return Ox.pad(Ox.getDate(d, utc), 2, ' ');}],
['G', function(d) {return Ox.getISOYear(d, utc);}],
['g', function(d) {return Ox.getISOYear(d, utc).toString().substr(-2);}],
['g', function(d) {return Ox.getISOYear(d, utc).toString().slice(-2);}],
['H', function(d) {return Ox.pad(Ox.getHours(d, utc), 2);}],
['I', function(d) {return Ox.pad((Ox.getHours(d, utc) + 11) % 12 + 1, 2);}],
['j', function(d) {return Ox.pad(Ox.getDayOfTheYear(d, utc), 3);}],
@ -185,7 +185,7 @@ Ox.formatDate = function(date, str, utc) {
return Math.abs(y) + (y < 1000 ? ' ' + Ox.BCAD[y < 0 ? 0 : 1] : '');
}],
['Y', function(d) {return Ox.getFullYear(d, utc);}],
['y', function(d) {return Ox.getFullYear(d, utc).toString().substr(-2);}],
['y', function(d) {return Ox.getFullYear(d, utc).toString().slice(-2);}],
['Z', function(d) {return d.toString().split('(')[1].replace(')', '');}],
['z', function(d) {return Ox.getTimezoneOffsetString(d);}],
['n', function() {return '\n';}],
@ -496,8 +496,8 @@ Ox.formatNumber = function(num, dec) {
str = Ox.isUndefined(dec) ? abs.toString() : abs.toFixed(dec),
spl = str.split('.');
while (spl[0]) {
arr.unshift(spl[0].substr(-3));
spl[0] = spl[0].substr(0, spl[0].length - 3);
arr.unshift(spl[0].slice(-3));
spl[0] = spl[0].slice(0, -3);
}
spl[0] = arr.join(',');
return (num < 0 ? '-' : '') + spl.join('.');

View file

@ -76,7 +76,7 @@ Ox.doc = (function() {
// or if the second last char is a single or double quote
return matches && (
matches[2].indexOf('/') == -1 ||
'\'"'.indexOf(matches[2].substr(-2, 1)) > -1
'\'"'.indexOf(matches[2].slice(-2, -1)) > -1
) ? Ox.extend({
name: parseName(matches[1].trim()),
summary: matches[3].trim()
@ -163,7 +163,7 @@ Ox.doc = (function() {
// main item
// include leading whitespace
item.source = parseTokens(tokens[i]);
item.line = source.substr(0, item.source[0].offset)
item.line = source.slice(0, item.source[0].offset)
.split('\n').length;
items.push(item);
} else {
@ -186,7 +186,7 @@ Ox.doc = (function() {
// fixme: we cannot properly handle tests where a string contains '\n '
var lines = decodeLinebreaks(str).split('\n ');
return {
statement: lines[0].substr(2),
statement: lines[0].slice(2),
result: lines[1].trim()
};
}
@ -258,7 +258,7 @@ Ox.doc = (function() {
split,
type;
// only split by ':' if there is no default string value
if ('\'"'.indexOf(str.substr(-2, 1)) == -1) {
if ('\'"'.indexOf(str.slice(-2, -1)) == -1) {
split = str.split(':');
str = split[0];
if (split.length == 2) {
@ -637,7 +637,7 @@ Ox.tokenize = (function() {
identifier: function() {
var str;
while ((identifier + number).indexOf(source[++cursor]) > -1) {}
str = source.substring(start, cursor);
str = source.slice(start, cursor);
Ox.forEach(word, function(value, key) {
if (value.indexOf(str) > -1) {
type = key;

View file

@ -48,7 +48,7 @@ Ox.ends = Ox.endsWith = function(str, sub) {
// fixme: rename to ends
str = str.toString();
sub = sub.toString();
return str.substr(str.length - sub.length) == sub;
return str.slice(str.length - sub.length) == sub;
};
/*@
@ -107,7 +107,7 @@ Ox.highlightHTML = function(html, str, classname, tags) {
isEntity = true;
} else if (!isTag && chr == '<') {
Ox.forEach(tags, function(tag) {
if (html.substr(i + 1).match(new RegExp('^/?' + tag + '\\W'))) {
if (html.slice(i + 1).match(new RegExp('^/?' + tag + '\\W'))) {
isTag = true;
Ox.break();
}
@ -138,10 +138,10 @@ Ox.highlightHTML = function(html, str, classname, tags) {
});
positions.forEach(function(position) {
var match = '<span class="' + classname + '">'
+ html.substr(position[0], position[1] - position[0])
+ html.slice(position[0], position[1])
.replace(/(<.*?>)/g, '</span>$1<span class="' + classname + '">')
+ '</span>';
html = html.substr(0, position[0]) + match + html.substr(position[1]);
html = html.slice(0, position[0]) + match + html.slice(position[1]);
});
return html;
}
@ -180,13 +180,11 @@ Ox.pad = function(str, len, pad) {
// fixme: slighly obscure signature
// fixme: weird for negative numbers
var pos = len / (len = Math.abs(len));
str = str.toString().substr(0, len);
str = str.toString().slice(0, len);
pad = Ox.repeat(pad || '0', len - str.length);
str = pos == 1 ? pad + str : str + pad;
str = pos == 1 ?
str.substr(str.length - len, str.length) :
str.substr(0, len);
return str;
return pos == 1
? (pad + str).slice(-len)
: (str + pad).slice(0, len);
};
/*@
@ -209,7 +207,7 @@ Ox.parsePath = function(str) {
return {
pathname: matches[1] || '',
filename: matches[2] || '',
extension: matches[3] ? matches[3].substr(1) : ''
extension: matches[3] ? matches[3].slice(1) : ''
};
}
@ -422,7 +420,7 @@ Ox.starts = Ox.startsWith = function(str, sub) {
// fixme: rename to starts
str = str.toString();
sub = sub.toString();
return str.substr(0, sub.length) == sub;
return str.slice(0, sub.length) == sub;
};
/*@
@ -482,10 +480,10 @@ Ox.toTitleCase <f> Returns a string with capitalized words
@*/
Ox.toTitleCase = function(str) {
return str.split(' ').map(function(val) {
var sub = val.substr(1),
var sub = val.slice(1),
low = sub.toLowerCase();
if (sub == low) {
val = val.substr(0, 1).toUpperCase() + low;
val = val.slice(0, 1).toUpperCase() + low;
}
return val;
}).join(' ');
@ -533,13 +531,13 @@ Ox.truncate = function(str, len, pad, pos) {
left, right;
if (strlen > len) {
if (pos == 'left') {
str = pad + str.substr(padlen + strlen - len);
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.substr(0, left) + pad + str.substr(-right);
str = str.slice(0, left) + pad + str.slice(-right);
} else if (pos == 'right') {
str = str.substr(0, len - padlen) + pad;
str = str.slice(0, len - padlen) + pad;
}
}
return str;
@ -628,7 +626,7 @@ Ox.wordwrap = function(str, len, sep, bal, spa) {
} else {
// word is longer than line
chr = len - lines[lines.length - 1].length;
lines[lines.length - 1] += word.substr(0, chr);
lines[lines.length - 1] += word.slice(0, chr);
Ox.loop(chr, word.length, len, function(pos) {
lines.push(word.substr(pos, len));
});