More complete definitions for the trim functions

This commit is contained in:
Kovid Goyal 2015-06-24 03:44:13 +05:30
parent 9fb0c01724
commit 22987bea12

View File

@ -103,9 +103,33 @@ class Context(object):
self.eval(''' self.eval('''
console = { log: function() { print(Array.prototype.join.call(arguments, ' ')); } }; console = { log: function() { print(Array.prototype.join.call(arguments, ' ')); } };
Duktape.modSearch = function (id, require, exports, module) { return Duktape.load_file(id); } Duktape.modSearch = function (id, require, exports, module) { return Duktape.load_file(id); }
String.prototype.trimLeft = function() { return this.replace(/^\s+/, ''); }; if (!String.prototype.trim) {
String.prototype.trimRight = function() { return this.replace(/\s+$/, ''); }; (function() {
String.prototype.trim = function() { return this.replace(/^\s+/, '').replace(/\s+$/, ''); }; // Make sure we trim BOM and NBSP
var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
String.prototype.trim = function() {
return this.replace(rtrim, '');
};
})();
}
if (!String.prototype.trimLeft) {
(function() {
// Make sure we trim BOM and NBSP
var rtrim = /^[\s\uFEFF\xA0]+/g;
String.prototype.trimLeft = function() {
return this.replace(rtrim, '');
};
})();
}
if (!String.prototype.trimRight) {
(function() {
// Make sure we trim BOM and NBSP
var rtrim = /[\s\uFEFF\xA0]+$/g;
String.prototype.trimRight = function() {
return this.replace(rtrim, '');
};
})();
}
Duktape.readfile = function(path, encoding) { Duktape.readfile = function(path, encoding) {
var x = Duktape.pyreadfile(path, encoding); var x = Duktape.pyreadfile(path, encoding);
var data = x[0]; var errcode = x[1]; var errmsg = x[2]; var data = x[0]; var errcode = x[1]; var errmsg = x[2];