2

I have nodes in a navigation tree. I want to select all the nodes that are part of the url so I can highlight them. I had it working but forgot an edge case where the last part of the url starts with the same string.

In this case if somebody is at the url /products/foobar-super I want it to select /products and /products/foobar-super but not /products/foobar.

describe('part of url', function () {

  it('matches /products and /products/foobar-super', function () {

    var current = '/producten/foobar-super';

    var nodes = [
      '/products',
      '/products/foobar',
      '/products/foobar-super',
    ];

    var result = [];

    nodes.forEach(function (node) {
      if (new RegExp(node + '.*').test(current)) {
        result.push(node);
      }
    });

    result.should.eql([
      '/products',
      '/products/foobar-super',
    ]);

  });

});

Jsfiddle with the test: http://jsfiddle.net/RKDga/2/

Not sure if it is possible with a regex the other solution I guess is to split the node and the current url on / compare those.

2
  • Yes! The second one, split the url on / and check. It's very simple, you'll be able to go back to it in a month and understand exactly what it does. I suggest you write this code yourself and answer this question yourself, I suspect you'll be able to do so just fine. Commented Mar 17, 2013 at 19:44
  • 1
    Just wrote it guess I was stuck thinking to solve this with a regex. Commented Mar 17, 2013 at 19:47

3 Answers 3

1

The test will pass if

new RegExp(node + '.*')

is changed to

new RegExp(node + '(/|$)')

It prevents a match unless node is followed by / or the end of the string.

Sign up to request clarification or add additional context in comments.

1 Comment

Since I was asking for a regex in my question I'll accept your answer. It's so simple I am actually a little embarrassed.
1

Here's an other solution without a regex:

describe('part of url', function () {

  it('matches /products and /products/foobar-super', function () {

    var current = '/products/foobar-super';

    var nodes = [
      '/products',
      '/products/foobar',
      '/products/foobar-super',
    ];

    var result = [];

    nodes.forEach(function (node) {

      var node_parts = node.split('/');
      var current_parts = current.split('/');
      var match = true;

      node_parts.forEach(function (node_part, i) {

        if (node_part !== current_parts[i]) {
          match = false;
        }

      });

      if (match) {
        result.push(node);
      }

    });

    result.should.eql([
      '/products',
      '/products/foobar-super',
    ]);

  });

});

2 Comments

If this is what you ended up using it would be great if you could explain your code and accept your answer.
@BenjaminGruenbaum you can't accept your own answer within two days of the question.
1

Following code may help you JSFIDDLE

    /*globals mocha: true, describe: true, it: true, beforeEach:true */
(function() {
    function assert(expr, msg) {
        if (!expr) throw new Error(msg || 'failed');
    }

    mocha.setup({
        ui: "bdd",
        ignoreLeaks: true
    });

    describe('part of url', function () {

      it('matches /products and /products/foobar-super', function () {

        var current = '/products/foobar-super';

        var nodes = [
          '/products',
          '/products/foobar',
          '/products/foobar-super',
        ];

        var result = [];
         ;
    var urllist =  current.split("/");
   var temp; 
    nodes.forEach(function (node) {
       temp = "";
      for(var i=1;i<urllist.length; i++){
            temp += "/"+ urllist[i];
          if(node == temp){
             result.push(node );
          }
      }
    });

 console.log(result);          

        assert(result[0] === '/products', 'first item should be /products');
        assert(result[1] === '/products/foobar-super', 'second item should be /products/foobar-super');


      });

    });


mocha.run();
}());

3 Comments

Would you mind explaining what you changed in the code and why?
@Juhana I added jsfiddle link.
That's cool, but would you mind explaining what you changed in the code and why?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.