Eria Eria - 15 days ago 5
Javascript Question

Error "Object has no method 'endsWith'" on custom grunt task

I ran into an error in a custom grunt task. Below I posted a simple test case relating the problem :

Gruntfile.js

module.exports = function( grunt ){

grunt.task.registerTask( 'endsWith', 'Test of string.prototype.endsWith', function(){
var value = grunt.option('value');
grunt.log.writeln( typeof value );
grunt.log.writeln( value.endsWith( 'bar' ) );
})

};


Test

> grunt endsWith --value=foobar
Running "endsWith" task
string
Warning: Object foobar has no method 'endsWith' Use --force to continue.

Aborted due to warnings.

Execution Time (2016-02-12 16:15:19 UTC)
Total 12ms


It's like grunt doesn't recognize the String.proptotype.endsWith function. Is that normal ?

Edit: I'm using node v0.10.4

Answer

.endsWith is an ES6 feature and wasn't implemented in Node.js v0.10.4.

To use .endsWith either upgrade Node.js or add in a polyfill:

String.prototype.endsWith = function(suffix) {
    return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
Comments