taco
taco

Reputation: 1373

Formatting an array of Javascript data

This code is for a web chat. I'm looking to have this code inspect each word, and then format only the twitter @joeblow username as twitter profile link.

Simply put, I'm parsing chat data to turn the text into twitter links, hashtags,a /command or just leave it as text if it's not any of these three. Below is my debug code.

I'm trying to learn how to use apply or call and don't completely understand them. As far as the code, it should take in the context array, and apply the format function to each item in the array.

Please note this is my first time trying to use apply() and I'm not sure what I'm doing wrong.

When I run format.apply(undefined,context('@joeblow is my username')); it just returns [twitter]@joeblow. It should be printing something like [twitter]@joeblow [default]is [default]my [default]username. In a real world scenario, @joeblow would be HTML that links to @joeblows profile (the pseudo code below is just for brevity).

I want the format() function to apply to every word passed. The context() function just takes the passed in data and makes it into an array. I know I could write a for loop, but I'm trying to learn how to use apply()/call().

function preformat(message) {
  format_message = {
    '#': 'hashtag',
    '/': 'command',
    '@': 'twitter'
  }
  var char = message.charAt(0);
  var type = format_message[char];
  if (type) {
    return type;
  } else {
    return 'default';
  }
}

function context(input) {
  var message = [];
  var words = input.split(' ');
  for (var i=0;i<words.length;i++) {
    message[i] = words[i];
  }
  return message;
}

function format(message) {
  var type = preformat(message);
  format.types = {
    'command': function(type, message) { return '[command]' + message; },
    'hashtag': function(type, message) { return '[hashtag]' + message; },
    'twitter': function(type, message) { return '[twitter]' + message; },
    'default': function(type, message) { return '[default]' + message; }
  }
  var parse = format.types[type];
  return parse(type,message);
}

I'm executing this using:

format.apply(undefined,context('@joeblow is my username'));

Upvotes: 0

Views: 269

Answers (1)

Alnitak
Alnitak

Reputation: 339786

The issue you're having is that whilst you're using apply to pass the split string to format as individual arguments, nowhere within that function do you actually iterate over those arguments.

.apply won't do that for you automatically - you must either use an explicit loop, or a function like .map that implicitly loops over an array.

The primary use for .apply is for invoking functions when you don't know in advance how many parameters you're going to pass to a function that expects separate parameters instead of an array.

Since your format function effectively only takes a single argument and returns the formatted version of that argument just try this, instead:

context("@joeblow is my username").map(format).join(' ');

Upvotes: 1

Related Questions