Skip to content

URI.withinString - TypeError: Cannot read property 'length' of undefined #303

Closed
@dandreit

Description

@dandreit

Hi,

I'm using URI.js in node.js (v4.2.6) [ npm install --save urijs ]

And I wanted to extract several URLs from a text, so I wrote this little file:
lib/parseUrlsFromText.js:

const URI = require('urijs');

module.exports = function parseUrlsFromText(text) {
  console.log('text: ' + JSON.stringify(text, null, 2));
  var urls = [];
  URI.withinString(text, function(url) {
    console.log('detected url: ' + url);
    urls.push(url);
  });
  console.log('urls: ' + JSON.stringify(urls, null, 2));
  return urls;
};

* I then tried to test it, and then added the console.logs because of fails:
test/parseUrlsFromText.js:

const expect = require('chai').expect;
const parseUrlsFromText = require('../lib/parseUrlsFromText');

describe('lib/parseUrlsFromText.js', () => {

  // text: null
  // urls: []
  // -> OK
  it('should return empty array on \'null\' text', done => {
    expect(parseUrlsFromText(null)).to.deep.equal([]);
    done();
  });

  // text: "www.example.com"
  // detected_url: www.example.com
  // urls: []
  // result: 
  // -> TypeError: Cannot read property 'length' of undefined
  //    at Function.URI.withinString (node_modules/urijs/src/URI.js:972:40)
  it('should return a URL contained in \'www.example.com\'', done => {
    // expect(parseUrlsFromText('I love www.example.com!')).to.deep.equal(['example.com']);
    console.log('result: ' + parseUrlsFromText('www.example.com'));
    done();
  });

  // text: "I love example.com!"
  // urls: []
  // result:
  // -> Result empty, but NO BUG
  it('should return a URL contained in \'I love example.com!\'', done => {
    // expect(parseUrlsFromText('I love example.com!')).to.deep.equal(['example.com']);
    console.log('result: ' + parseUrlsFromText('I love example.com!'));
    done();
  });
...

I have not dug further, but this solved my problem:
In (node_modules/urijs/src/)URI.js:969 & 972
-> conditional add of slice.length and result.length

URI.withinString = function(string, callback, options) {
...
    while (true) {
      ...
      end = start + (slice && slice.length) ? slice.length : 0;
      var result = callback(slice, start, end, string);
      string = string.slice(0, start) + result + string.slice(end);
      _start.lastIndex = start + (result && result.length) ? result.length : 0;
    }

It may not be enough to make a Pull Request, but it's probably worth considering.
Hope that helps!
Cheers

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions