All files / src/dm ve.dm.LinearData.js

95.83% Statements 46/48
94.44% Branches 17/18
89.47% Functions 17/19
95.83% Lines 46/48

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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254                                1x 3208x 3208x         1x                       1x 33057x                               1x   243153x                 1x 108061x                 1x 57762x                     1x 350565x                 1x 6473x                           1x   5765x   5765x   5765x                 1x                 1x 42582x               1x 15057x                   1x 7463x                   1x 11x                     1x 3696x                     1x 1x                       1x 738x                       1x 1x                         1x 4390x   4390x 4390x 4252x 4252x   4390x   4390x               1x            
/*!
 * VisualEditor LinearData class.
 *
 * Class containing linear data and an hash-value store.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Generic linear data storage
 *
 * @class
 * @constructor
 * @param {ve.dm.HashValueStore} store Hash-value store
 * @param {Array} [data] Linear data
 */
ve.dm.LinearData = function VeDmLinearData( store, data ) {
	this.store = store;
	this.data = data ? ve.deepFreeze( data, true ) : [];
};
 
/* Inheritance */
 
OO.initClass( ve.dm.LinearData );
 
/* Static Methods */
 
/**
 * Get the type of an element
 *
 * This will return the same string for close and open elements.
 *
 * @param {Object} item Element item
 * @return {string} Type of the element
 */
ve.dm.LinearData.static.getType = function ( item ) {
	return this.isCloseElementData( item ) ? item.type.slice( 1 ) : item.type;
};
 
/**
 * Check if data item is an element.
 *
 * This method assumes that any value that has a type property that's a string is an element object.
 *
 * Element data:
 *
 *      <heading> a </heading> <paragraph> b c <img></img> </paragraph>
 *     ^         . ^          ^           . . ^     ^     ^            .
 *
 * @param {Object|Array|string} item Linear data item
 * @return {boolean} Item is an element
 */
ve.dm.LinearData.static.isElementData = function ( item ) {
	// Data exists and appears to be an element
	return item !== undefined && typeof item.type === 'string';
};
 
/**
 * Checks if data item is an open element.
 *
 * @param {Object} item Element item
 * @return {boolean} Item is an open element
 */
ve.dm.LinearData.static.isOpenElementData = function ( item ) {
	return this.isElementData( item ) && item.type.charAt( 0 ) !== '/';
};
 
/**
 * Checks if data item is a close element.
 *
 * @param {Object} item Element item
 * @return {boolean} Item is a close element
 */
ve.dm.LinearData.static.isCloseElementData = function ( item ) {
	return this.isElementData( item ) && item.type.charAt( 0 ) === '/';
};
 
/* Methods */
 
/**
 * Gets linear data from a specified index, or all data if no index specified
 *
 * @param {number} [offset] Offset to get data from
 * @return {Object|Array} Data from index, or all data (by reference)
 */
ve.dm.LinearData.prototype.getData = function ( offset ) {
	return offset === undefined ? this.data : this.data[ offset ];
};
 
/**
 * Sets linear data at a specified index
 *
 * @param {number} offset Offset to set data at
 * @param {Object|string} value Value to store
 */
ve.dm.LinearData.prototype.setData = function ( offset, value ) {
	this.data[ offset ] = typeof value === 'object' ? ve.deepFreeze( value ) : value;
	// this.data[ offset ] = value;
};
 
/**
 * Modify an existing object in the linear model
 *
 * As objects in the model and immutable, this creates
 * a clone which is passed to a callback function for
 * modification, then the clone is inserted into the model.
 *
 * @param {number} offset Offset to modify data at
 * @param {Function} modify Modification function. First argument is the data element to modify.
 */
ve.dm.LinearData.prototype.modifyData = function ( offset, modify ) {
	// Unfreeze object by creating a new copy
	const newItem = ve.copy( this.getData( offset ) );
	// Modify via callback
	modify( newItem );
	// Insert into linear model, re-freezing it
	this.setData( offset, newItem );
};
 
/**
 * Push data to the end of the array
 *
 * @param {...Object} [values] Values to store
 * @return {number} The new length of the linear data
 */
ve.dm.LinearData.prototype.push = function ( ...values ) {
	return this.data.push( ...values );
};
 
/**
 * Gets length of the linear data
 *
 * @return {number} Length of the linear data
 */
ve.dm.LinearData.prototype.getLength = function () {
	return this.getData().length;
};
 
/**
 * Gets the hash-value store
 *
 * @return {ve.dm.HashValueStore} The hash-value store
 */
ve.dm.LinearData.prototype.getStore = function () {
	return this.store;
};
 
/**
 * Slice linear data
 *
 * @param {number} begin Index to begin at
 * @param {number} [end] Index to end at
 * @return {Array} One-level deep copy of sliced range
 */
ve.dm.LinearData.prototype.slice = function ( begin, end ) {
	return this.data.slice( begin, end );
};
 
/**
 * Slice linear data and return new LinearData object containing result
 *
 * @param {number} begin Index to begin at
 * @param {number} [end] Index to end at
 * @return {ve.dm.LinearData} LinearData object containing one-level deep copy of sliced range
 */
ve.dm.LinearData.prototype.sliceObject = function ( begin, end ) {
	return new this.constructor( this.getStore(), this.slice( begin, end ) );
};
 
/**
 * Splice linear data
 *
 * @param {number} index Splice from
 * @param {number} deleteCount Items to be removed
 * @param {...Object} [elements] Items to be inserted
 * @return {Array} Elements removed
 */
ve.dm.LinearData.prototype.splice = function ( index, deleteCount, ...elements ) {
	return this.data.splice( index, deleteCount, ...elements );
};
 
/**
 * Splice linear data and return new LinearData object containing result
 *
 * @param {number} index Splice from
 * @param {number} deleteCount Items to be removed
 * @param {...Object} [elements] Items to be inserted
 * @return {ve.dm.LinearData} LinearData object containing elements removed
 */
ve.dm.LinearData.prototype.spliceObject = function ( index, deleteCount, ...elements ) {
	return new this.constructor( this.getStore(), this.splice( index, deleteCount, ...elements ) );
};
 
/**
 * Returns ve.batchSplice of linear data
 *
 * @see ve.batchSplice
 * @param {number} offset
 * @param {number} remove
 * @param {Array} data
 * @return {Array}
 */
ve.dm.LinearData.prototype.batchSplice = function ( offset, remove, data ) {
	return ve.batchSplice( this.getData(), offset, remove, data );
};
 
/**
 * Returns ve.batchSplice of linear data, wrapped in a LinearData object
 *
 * @see ve.batchSplice
 * @param {number} offset
 * @param {number} remove
 * @param {Array} data
 * @return {ve.dm.LinearData}
 */
ve.dm.LinearData.prototype.batchSpliceObject = function ( offset, remove, data ) {
	return new this.constructor(
		this.getStore(),
		this.batchSplice( offset, remove, data )
	);
};
 
/**
 * Get a slice or copy of the provided data.
 *
 * @param {ve.Range} [range] Range of data to get, all data will be given by default
 * @param {boolean} [deep=false] Whether to return a deep copy (WARNING! This may be very slow)
 * @return {Array} Slice or copy of data
 */
ve.dm.LinearData.prototype.getDataSlice = function ( range, deep ) {
	const length = this.getLength();
	let end,
		start = 0;
	if ( range !== undefined ) {
		start = Math.max( 0, Math.min( length, range.start ) );
		end = Math.max( 0, Math.min( length, range.end ) );
	}
	const data = this.slice( start, end );
	// Return either the slice or a deep copy of the slice
	return deep ? ve.copy( data ) : data;
};
 
/*
 * Clone the data, with a deep copy of the data.
 *
 * @return {ve.dm.LinearData} Clone of this object
 */
ve.dm.LinearData.prototype.clone = function () {
	return new this.constructor(
		this.getStore(),
		ve.copy( this.data )
	);
};