API Docs for: 1.0.0
Show:

File: default.js

/**
 *  @module Intro2JavaScript
 *  @author Richard S. Mitchell
 */
/**
 *  Global functions and properties.
 *  @class Window
 *  @static
 */
/**
 *  Converts decimal number to hexadecimal string.
 *  @method d2h
 *  @static
 *  @param  {int} d
 *  @return {mixed}   if d is a number then return hexadecimal string else return d
 */
function d2h(d)
{
  return (isNaN(d)) ? d : d.toString(16);
}

/**
 *  Wrapper for document.getElementById().
 *  @method getEl
 *  @static
 *  @param  {String} id   the element's id
 *  @return {HTMLElement} If the element exists then reference to it else null.
 */
function getEl(id)
{
  if (isBlank(id)) {
    return null;
  }

  if (document.getElementById) {
    return document.getElementById(id);
  }
  else if (document.all) {
    return document.all[id];
  }

  return null;
}

/**
 *  Converts hexadecimal string to decimal number.
 *  @method h2d
 *  @static
 *  @param {String} h
 *  @return {mixed}   if h can be parsed to int then return h as decimal int else return h
 */
function h2d(h)
{
  var n = parseInt(h, 16);

  return (isNaN(n)) ? h : n;
}

/**
 *  Tests string variable for content.
 *  @method isBlank
 *  @static
 *  @param  {String} value
 *  @return {boolean}
 */
function isBlank(value)
{
  return (value.trim() == "");
}

/**
 *  Tests whether string value is completely numeric (decimal: "14000" == true; "14,000" == false).
 *  @method isNumeric
 *  @static
 *  @param  {String} value
 *  @return {boolean}
 */
function isNumeric(value)
{
  for (var i = 0; i < value.length; i++) {
    if (isNaN(parseInt(value.charAt(i)))) {
      return false;
    }
  }

  return true;
}

/**
 *  A push-pop stack.
 *  @class Stack
 *  @constructor
 *  @return {void}
 */
function Stack()
{
  /**
   *  The stack itself.
   *  @property stack
   *  @type {Array}
   */
  this.stack = [];
}

/**
 *  Clears the push-pop stack.
 *  @method clearStack
 *  @return {void}
 */
Stack.prototype.clearStack = function (){
  this.stack.length = 0;
};

/**
 *  Fetches the most recently added item from the top of the stack.
 *  @method pop
 *  @return {mixed}
 */
Stack.prototype.pop = function () {
  return this.stack.pop();
};

/**
 *  Stores the item on the top of the stack.
 *  @method push
 *  @return {void}
 */
Stack.prototype.push = function (item) {
  this.stack.push(item);
};

// STRING PROTOTYPE DEFINITIONS ================================================

/**
 *  Extensions to the String class.
 *  @class String
 *  @constructor
 */
/**
 *  Decodes string from UTF-8 to ASCII format.
 *  @method decodeUtf8
 *  @return {String}
 */
String.prototype.decodeUtf8 = function () {
  var s = "";
  var i = 0;
  var c = c1 = c2 = 0;

  while ( i < this.length ) {
    c = this.charCodeAt(i);

    if (c < 128) {
      s += String.fromCharCode(c);
      i++;
    }
    else if ((c > 191) && (c < 224)) {
      c2 = this.charCodeAt(i + 1);
      s += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
      i += 2;
    }
    else {
      c2 = this.charCodeAt(i + 1);
      c3 = this.charCodeAt(i + 2);
      s += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
      i += 3;
    }
  }

  return s;
};

/**
 *  Encodes string from ASCII to UTF-8 format.
 *  @method encodeUtf8
 *  @return {String}
 */
String.prototype.encodeUtf8 = function () {
  var s, c;
  var utfText = "";

  for (var i = 0; i < s.length; i++) {
    c = this.charCodeAt(i);

    if (c < 128) {
      utfText += String.fromCharCode(c);
    }
    else if((c > 127) && (c < 2048)) {
      utfText += String.fromCharCode((c >> 6) | 192);
      utfText += String.fromCharCode((c & 63) | 128);
    }
    else {
      utfText += String.fromCharCode((c >> 12) | 224);
      utfText += String.fromCharCode(((c >> 6) & 63) | 128);
      utfText += String.fromCharCode((c & 63) | 128);
    }
  }

  return utfText;
};

/**
 *  Returns the string repeated n times.
 *
 *      var s = "~-";
 *      console.log(s.repeat(10));
 *      // ~-~-~-~-~-~-~-~-~-~-
 *
 *  @method repeat
 *  @param  {int} n
 *  @return {String} If n is NaN then return the string else return repeated string.
 */
String.prototype.repeat = function (n) {
  n = parseInt(n);

  if (isNaN(n)) {
    return this;
  }

  return new Array(n + 1).join(this);
};

/**
 *  Replaces all target characters with the replacement character. Works by
 *  splitting it into an array and then rejoining it.
 *
 *      var s = "Intro to Javascript";
 *      console.log(s.replaceChars(" ", "_"));
 *      // Intro_to_Javascript
 *
 *  @method replaceChars
 *  @param  {String} tar    target character to replace
 *  @param  {String} rep    replacement character
 *  @return {String} with target characters replaced
 */
String.prototype.replaceChars = function (tar, rep) {
  return this.split(tar).join(rep);
};

/**
 *  Reverses the characters in a string.
 *
 *      var s = "Intro to Javascript";
 *      console.log(s.reverse());
 *      // tpircSavaJ ot ortnI
 *
 *  @method reverse
 *  @return {String}
 */
String.prototype.reverse = function () {
  return this.split("").reverse().join("");
};

/**
 *  Strips spaces from a string.
 *  @method stripSpaces
 *  @return {String}
 */
String.prototype.stripSpaces = function () {
  return this.replaceChar(" ", "");
};

/**
 *  Trims leading whitespace from a string.
 *  @method trimLeft
 *  @return {String}
 */
String.prototype.trimLeft = function () {
  return this.replace(/^\s+/, "");
};

/**
 *  Trims trailing whitespace from a string.
 *  @method trimRight
 *  @return {String}
 */
String.prototype.trimRight = function () {
  return this.replace(/\s+$/, "");
};

/**
 *  Trims leading and trailing whitespace from a string.
 *  @method trim
 *  @return {String}
 */
String.prototype.trim = function () {
  return this.trimLeft().trimRight();
};

/**
 *  Removes the last char from a string.
 *  @method ungetc
 *  @return {String}
 */
String.prototype.ungetc = function () {
  return this.substr(0, this.length - 1);
};