Javascript ReplaceAll With Array

Unit Test

by hydrogen2oxygen

HTML

<script src="http://searls.github.io/jasmine-all/jasmine-all-min.js"></script>
<script src="https://searls.github.io/jasmine-all/jasmine-all-min.js"></script>
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>

JavaScript

var lux = {};

lux.replaceAllArray = function(str, findReplaceArray) {

  $.each(findReplaceArray, function(key, value) {
    str = lux.replaceAll(str, key, value);
  });
  
  return str;
};

lux.replaceAll = function(str, find, replace) {
  return str.replace(new RegExp(find, 'g'), replace);
};

var testString = 'This is a #WHAT# written in #LANGUAGE# and unit tested with the framework #FRAMEWORK#!';

var keyValues = {
  '#WHAT#': 'replace all method',
  '#LANGUAGE#': 'JavaScript',
  '#FRAMEWORK#': 'Jasmine'
};

var translated = lux.replaceAllArray(testString, keyValues);

describe("The keys inside the string ", function() {
  it("are replace properly by the lux.replaceAllArray(...) method.", function() {
    expect(translated).toBe('This is a replace all method written in JavaScript and unit tested with the framework Jasmine!');
  });
});