From c73d53f50aa2703dafee25af4f82a216e0d46177 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 24 Jun 2015 03:48:39 +0530 Subject: [PATCH] Add String.startsWith and String.endsWith --- src/duktape/__init__.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/duktape/__init__.py b/src/duktape/__init__.py index ff8d36cc43..50b917f2ab 100644 --- a/src/duktape/__init__.py +++ b/src/duktape/__init__.py @@ -111,7 +111,7 @@ class Context(object): return this.replace(rtrim, ''); }; })(); - } + }; if (!String.prototype.trimLeft) { (function() { // Make sure we trim BOM and NBSP @@ -120,7 +120,7 @@ class Context(object): return this.replace(rtrim, ''); }; })(); - } + }; if (!String.prototype.trimRight) { (function() { // Make sure we trim BOM and NBSP @@ -129,6 +129,23 @@ class Context(object): return this.replace(rtrim, ''); }; })(); + }; + if (!String.prototype.startsWith) { + String.prototype.startsWith = function(searchString, position) { + position = position || 0; + return this.indexOf(searchString, position) === position; + }; + } + if (!String.prototype.endsWith) { + String.prototype.endsWith = function(searchString, position) { + var subjectString = this.toString(); + if (position === undefined || position > subjectString.length) { + position = subjectString.length; + } + position -= searchString.length; + var lastIndex = subjectString.indexOf(searchString, position); + return lastIndex !== -1 && lastIndex === position; + }; } Duktape.readfile = function(path, encoding) { var x = Duktape.pyreadfile(path, encoding);