1

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

Eria
  • 2,653
  • 6
  • 29
  • 56

2 Answers2

6

.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;
};
Community
  • 1
  • 1
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
0

If you are on older version of node you can use String.match method.

replace

grunt.log.writeln(value.endsWith('bar'));

with

grunt.log.writeln( value.match("bar$") );

complete code

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.match("bar$") );
    })

};
Darshan
  • 1,064
  • 7
  • 15