Nodejs String Ends With endswith(needle)

Here you can find the source of endswith(needle)

Method Source Code

/*/*from   w w w.j  a v  a2s.com*/

Legal boring crap follows. In simple english, you can use this
code in your own project, be your project commercial or free.
Just be sure to include the license and stuff. The "copyright"
here is just for technical reasons.

Copyright 2011, Philip Peterson.

This file is part of Pumpkinpy.

Pumpkinpy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Pumpkinpy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Pumpkinpy.  If not, see <http://www.gnu.org/licenses/>.

*/

String.prototype.endswith = function (needle) {
   var nedlen = needle.length;
   var haylen = this.length;

   if (nedlen > haylen) {
      return false; // Prevents something like -1 from being produced and deemed a success.
   }
   
   return this.lastIndexOf(needle) === haylen-nedlen;
};

Related

  1. endsWith(value)
    String.prototype.endsWith = function(value) {
      var substringLength = value.length;
      var thisLength = this.length;
      if (substringLength > thisLength) {
        return false;
      };
      var howMuchLettersNeedToBeRemooved = thisLength - substringLength;
      var endsString = this.substring(howMuchLettersNeedToBeRemooved);
      if (endsString === value) {
    ...
    
  2. endsWithAny(endings)
    String.prototype.endsWithAny = function (endings) {
      var string = this;
      return endings.some(function (ending) {
        return string.endsWith(ending);
      });
    };
    
  3. endsWithString.prototype.endsWith || (end)
    String.prototype.endsWith = String.prototype.endsWith || function(end){
      return this.substr(this.length - end.length, end.length) === end;
    if (!window.require) {
      window.isBrowser = true;
      window.require = name => {
        if (name === 'path') {
          return {
            sep: '/'
    ...
    
  4. endsWithString.prototype.endsWith || (searchString, length)
    String.prototype.endsWith = String.prototype.endsWith || function(searchString, length) {
        length = typeof length === 'number' && length <  this.length && length >= 0 ? length : this.length;
        return this.substr(length-searchString.length, searchString.length) == searchString;
    console.log("abcd".endsWith("cd", 3));
    
  5. endsWithendsWith(suffix)
    String.prototype.endsWith = function endsWith(suffix) {
      var t = String(suffix);
      var index = this.lastIndexOf(t);
      return (index >= 0) && (index === this.length - t.length);
    };
    
  6. endswith(s)
    String.prototype.endswith = function(s) {
      return (this.match(s+"$")==s)
    
  7. endswith(str)
    String.prototype.endswith = function(str)
      return this.slice(this.length - str.length) === str;
    };
    
  8. endswith(str)
    String.prototype.endswith = function(str) {
      return (this.indexOf(str, this.length - str.length) !== -1) ? true: false;
    
  9. endswith(suffix)
    String.prototype.endswith = function(suffix) {
        return this.indexOf(suffix, this.length - suffix.length) !== -1;
    };