One of strings in array to match an expression One of strings in array to match an expression javascript javascript

One of strings in array to match an expression


As said in the comments, although I initially use map, reduce would allow you to do what you need, and in this caste at least makes a lot more sense:

protractor.promise.all([text1, text2, text3]).then(function (values) {    expect(        values.reduce(function(p, v) {            return v.match(/expression/) || p;        }, false)    ).toBe(true);});

Or writing the same thing, but using ES6 arrow functions:

protractor.promise.all([text1, text2, text3]).then(function(values) {    exptect(        values.reduce( (p, v) => v.match(/expression/) || p, false )    ).toBe(true);});

Both do the same thing, the reduce callback will default to false, until the v.match expression evaluates to true.
I'm assuming this is obvious to most people, but I thought I'd provide both syntaxes and some explanation for future reference


Perhaps this solution could be optimized a bit more, to stop matching the pattern once a single match has been found:

protractor.promise.all([text1, text2, text3]).then(function (values) {    expect(        values.reduce(function(p, v) {            return p || !!v.match(/expression/);        }, false)    ).toBe(true);});

All I did was to use the current reduce value as default (once that has been set to true, there's no point in testing any other string value). To ensure v.match evaluates to a boolean instead of an array, I just used !!v.match(). That part is optional though. In ES6, the same thing looks like this:

protractor.promise.all([text1, text2, text3]).then(function(values) {    exptect(        values.reduce( (p, v) => p || !!v.match(/expression/), false )    ).toBe(true);});

This might perform better with big data sets (considering the match calls stop once the first match was found, as opposed to v.match being called every time).


You could simply use map to get a list of boolean and then assert the result with toContain(true):

var all = protractor.promise.all;var map = protractor.promise.map;expect(map(all([text1, text2, text3]), RegExp.prototype.test, /expression/)).toContain(true);

You could also use a custom matcher:

expect(all([text1, text2, text3])).toContainPattern(/expression/);

And the custom matcher declared in beforeEach:

beforeEach(function() {  jasmine.addMatchers({    toContainPattern: function() {      return {        compare: function(actual, regex) {          return {            pass: actual.some(RegExp.prototype.test, regex),             message: "Expected [" + actual + "] to have one or more match with " + regex          };        }      };    }  });});


If this works,

protractor.promise.all([text1, text2, text3]).then(function (values) {    expect(values[0] + values[1] + values[2]).toMatch(/expression/);});

I think you can write this as follows;

protractor.promise.all([text1, text2, text3]).then(function (values) {    expect(values.join('')).toMatch(/expression/);});

And it's scalable. :)