All files unicodejs.textstring.js

100% Statements 23/23
100% Branches 12/12
100% Functions 6/6
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98                                  1x 1830x                       1x 29019x 29019x                         1x 11594x   11594x 180x 180x 177x     11417x                       1x 12217x   12217x 227x 227x 224x     11993x                 1x 4651x                 1x 1x    
/*!
 * UnicodeJS TextString class.
 *
 * @copyright 2013-2018 UnicodeJS team and others; see AUTHORS.txt
 * @license The MIT License (MIT); see LICENSE.txt
 */
 
/**
 * This class provides a simple interface to fetching plain text
 * from a data source. The base class reads data from a string, but
 * an extended class could provide access to a more complex structure,
 * e.g. an array or an HTML document tree.
 *
 * @class unicodeJS.TextString
 * @constructor
 * @param {string} text Text
 */
unicodeJS.TextString = function UnicodeJSTextString( text ) {
	this.text = text;
};
 
/* Methods */
 
/**
 * Read code unit at specified position
 *
 * @method
 * @param {number} position Position to read from
 * @return {string|null} Code unit, or null if out of bounds
 */
unicodeJS.TextString.prototype.read = function ( position ) {
	var dataAt = this.text[ position ];
	return dataAt !== undefined ? dataAt : null;
};
 
/**
 * Read unicode codepoint after the specified offset
 *
 * This is the same as the code unit (=Javascript character) at that offset,
 * unless a valid surrogate pair ends at that code unit. (This is consistent
 * with the behaviour of String.prototype.codePointAt)
 *
 * @param {number} position Position
 * @return {string|null} Unicode codepoint, or null if out of bounds
 */
unicodeJS.TextString.prototype.nextCodepoint = function ( position ) {
	var codeUnit = this.read( position );
 
	if ( unicodeJS.isLeadingSurrogate( codeUnit ) ) {
		var nextCodeUnit = this.read( position + 1 );
		if ( unicodeJS.isTrailingSurrogate( nextCodeUnit ) ) {
			return codeUnit + nextCodeUnit;
		}
	}
	return codeUnit;
};
 
/**
 * Read unicode codepoint before the specified offset
 *
 * This is the same as the code unit (=Javascript character) at the previous
 * offset, unless a valid surrogate pair ends at that offset.
 *
 * @param {number} position Position
 * @return {string|null} Unicode codepoint, or null if out of bounds
 */
unicodeJS.TextString.prototype.prevCodepoint = function ( position ) {
	var codeUnit = this.read( position - 1 );
 
	if ( unicodeJS.isTrailingSurrogate( codeUnit ) ) {
		var prevCodeUnit = this.read( position - 2 );
		if ( unicodeJS.isLeadingSurrogate( prevCodeUnit ) ) {
			return prevCodeUnit + codeUnit;
		}
	}
	return codeUnit;
};
 
/**
 * Check if the current offset is in the middle of a surrogate pair
 *
 * @param {number} position Position
 * @return {boolean}
 */
unicodeJS.TextString.prototype.isMidSurrogate = function ( position ) {
	return unicodeJS.isLeadingSurrogate( this.read( position - 1 ) ) &&
		unicodeJS.isTrailingSurrogate( this.read( position ) );
};
 
/**
 * Get as a plain string
 *
 * @return {string} Plain javascript string
 */
unicodeJS.TextString.prototype.toString = function () {
	return this.text;
};