Suffii
Suffii

Reputation: 5784

Passing non-breaking spaces before word in string

I have a JavaScript variable below which I need to pass some non-breaking spaces to, before the word.

var str = "    Test";

How do I achieve this?

Upvotes: 0

Views: 1038

Answers (2)

SaintFrag
SaintFrag

Reputation: 333

For future searchers:

var str = "\u00A0\u00A0\u00A0\u00A0Test";

Upvotes: 1

Pranav C Balan
Pranav C Balan

Reputation: 115212

You can do something like this,

  1. Creating a temporary div using $("<div/>")
  2. Adding html content as the string using html(str)
  3. Now to get the decoded string we can use text()

var str = "&nbsp;&nbsp;&nbsp;&nbsp;Test"
console.log($("<div/>").html(str).text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

In pure javascript you can use a textarea,

  1. You can create a textarea using document.createElement('textarea')
  2. Now you can use innerHTML to set the html content
  3. For retrieving the decoded data you can use ele.value

var str = "&nbsp;&nbsp;&nbsp;&nbsp;Test",
  ele = document.createElement('textarea');
ele.innerHTML = str;
console.log(ele.value);

I have added slight variation in your plugin code

/**
 * jquery.typist — animated text typing
 * @author Alexander Burtsev, http://burtsev.me, 2014—2015
 * @license MIT
 */
(function(factory) {
  if (typeof define === 'function' && define.amd) {
    define(['jquery'], factory);
  } else {
    factory(jQuery);
  }
}(function($) {
  'use strict';

  $.fn.typistInit = function() {
    return this.each(function() {
      if (!$(this).data('typist')) {
        new Typist(this);
      }
    });
  };

  $.fn.typist = function(opts) {
    return this.each(function() {
      new Typist(this, opts);
    });
  };

  $.fn.typistAdd = function(text, callback) {
    return this
      .typistInit()
      .each(function() {
        var self = $(this).data('typist');
        self.queue.push({
          text: text,
          callback: callback
        });
        self.type();
      });
  };

  $.fn.typistRemove = function(length, callback) {
    length = parseInt(length) || 0;

    return this
      .typistInit()
      .each(function() {
        var self = $(this).data('typist');
        self.queue.push({
          remove: length,
          callback: callback
        });
        self.type();
      });
  };

  $.fn.typistPause = function(delay, callback) {
    delay = parseInt(delay) || 0;

    return this
      .typistInit()
      .each(function() {
        var self = $(this).data('typist');
        self.queue.push({
          delay: delay,
          callback: callback
        });
        self.type();
      });
  };

  $.fn.typistStop = function() {
    return this
      .typistInit()
      .each(function() {
        var self = $(this).data('typist');
        self.queue.push({
          stop: true
        });
        self.type();
      });
  };

  /**
   * @class
   * @param {HTMLElement} element
   * @param {Object} [opts]
   * @param {String} [opts.text=''] Text for typing
   * @param {Number} [opts.speed=10] Typing speed (characters per second)
   * @param {Boolean} [opts.cursor=true] Shows blinking cursor
   * @param {Number} [opts.blinkSpeed=2] Blinking per second
   * @param {String} [opts.typeFrom='end'] Typing from start/end of element
   * @param {Object} [opts.cursorStyles] CSS properties for cursor element
   */
  function Typist(element, opts) {
    $.extend(this, {
      speed: 10, // characters per second
      text: '',
      cursor: true,
      blinkSpeed: 2, // blink per second
      typeFrom: 'end', // 'start', 'end'

      cursorStyles: {
        display: 'inline-block',
        fontStyle: 'normal',
        margin: '-2px 2px 0 2px'
      }
    }, opts || {});

    this._cursor = null;
    this._element = $(element);
    this._element.data('typist', this);
    this._container = null;

    this.queue = [];
    this.timer = null;
    this.delay = 1000 / this.speed;

    this.blinkTimer = null;
    this.blinkDelay = 1000 / this.blinkSpeed;

    if (this.text) {
      this.queue.push({
        text: this.text
      });
      this.type();
    }
  }

  Typist.prototype =
    /** @lends Typist */
    {
      /**
       * Adds blinking cursor into element
       */
      addCursor: function() {
        if (this._cursor) {
          clearInterval(this.blinkTimer);
          this._cursor
            .stop()
            .remove();
        }

        this._cursor = $('<span>|</span>')
          .css(this.cursorStyles)
          .insertAfter(this._container);

        this.cursorVisible = true;
        this.blinkTimer = setInterval($.proxy(function() {
          this.cursorVisible = !this.cursorVisible;
          this._cursor.animate({
            opacity: this.cursorVisible ? 1 : 0
          }, 100);
        }, this), this.blinkDelay);
      },

      /**
       * Triggers event
       * @param {String} event
       * @return {Typist}
       */
      fire: function(event) {
        this._element.trigger(event, this);
        return this;
      },

      /**
       * New line to <br> tag
       * @param {String} text
       * @return {String}
       */
      nl2br: function(text) {
        return text.replace(/\n/g, '<br>');
      },

      /**
       * <br> tag to new line
       * @param {String} html
       * @return {String}
       */
      br2nl: function(html) {
        return html.replace(/<br.*?>/g, '\n');
      },

      /**
       * Removes given number of characters
       * @param {Number} length
       * @param {Function} [callback]
       */
      remove: function(length, callback) {
        if (length <= 0) {
          callback();
          this.timer = null;
          return this
            .fire('end_remove.typist')
            .type();
        }

        var text = this._container.html();

        length--;
        text = this.br2nl(text);
        text = text.substr(0, text.length - 1);
        text = this.nl2br(text);

        this.timer = setTimeout($.proxy(function() {
          this._container.html(text);
          this.remove(length, callback);
        }, this), this.delay);
      },

      /**
       * Adds given text character by character
       * @param {String|Array} text
       */
      step: function(text, callback) {
        if (typeof text === 'string') {
          text = text.split('').map(function(v){return v.replace(' ','&nbsp;');});
        }

        if (!text.length) {
          callback();
          this.timer = null;
          return this
            .fire('end_type.typist')
            .type();
        }

        var character = text.shift();
        //character = $('<div>').text(character).html();
        character = this.nl2br(character);

        this.timer = setTimeout($.proxy(function() {
          this._container.html(this._container.html() + character);
          this.step(text, callback);
        }, this), this.delay);
      },

      /**
       * Stops all animations and removes cursor
       * @return {[type]} [description]
       */
      stop: function() {
        clearInterval(this.blinkTimer);
        this.blinkTimer = null;

        if (this._cursor) {
          this._cursor.remove();
          this._cursor = null;
        }

        clearTimeout(this.timer);
        this.timer = null;
      },

      /**
       * Gets and invokes tasks from queue
       */
      type: function() {
        if (this.timer) {
          return;
        }

        if (!this._container) {
          this._container = $('<span>');
          if (this.typeFrom === 'start') {
            this._element.prepend(this._container);
          } else {
            this._element.append(this._container);
          }
        }

        if (this.cursor) {
          this.addCursor();
        }

        var item = this.queue.shift() || {},
          callback = $.proxy(item.callback || $.noop, this);

        if (item.delay) {
          this
            .fire('start_pause.typist')
            .timer = setTimeout($.proxy(function() {
              callback();
              this.timer = null;
              this
                .fire('end_pause.typist')
                .type();
            }, this), item.delay);
          return;

        } else if (item.remove) {
          this
            .fire('start_remove.typist')
            .remove(item.remove, callback);
          return;

        } else if (item.stop) {
          this.stop();
          return;
        }

        if (!item.text) {
          return;
        }

        this
          .fire('start_type.typist')
          .step(item.text, callback);
      }
    };
}));

$('.typist')
  .typist({
    speed: 12,
    text: 'Hello!\n'
  })
  .typistAdd('It       working!');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="typist"></p>

Upvotes: 2

Related Questions