r92168 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92167‎ | r92168 | r92169 >
Date:17:28, 14 July 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Fixed object comparison function and added some documentation.
Modified paths:
  • /trunk/parsers/wikidom/lib/es/es.Content.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/es/es.Content.js
@@ -1,3 +1,14 @@
 2+/* Classes */
 3+
 4+/**
 5+ * Content objects are wrappers around arrays of plain or annotated characters. Data in this form
 6+ * is ultimately equivalent to but more efficient to work with than WikiDom line objects (plain text
 7+ * paired with offset annotation), especially when performing substring operations. Content can be
 8+ * derived from or converted to one or more WikiDom line objects.
 9+ *
 10+ * @param content {Array} List of plain or annotated characters
 11+ * @returns {Content}
 12+ */
213 function Content( content ) {
314 this.data = content || [];
415 };
@@ -2,31 +13,61 @@
314
4 -Content.compareObjects = function( a, b ) {
5 - for ( var key in a ) {
6 - if ( typeof a[key] !== typeof b[key] ) {
 15+/* Static Methods */
 16+
 17+/**
 18+ * Recursively compares string and number property between two objects.
 19+ *
 20+ * A false result may be caused by property inequality or by properties in one object missing from
 21+ * the other. An asymmetrical test may also be performed, which checks only that properties in the
 22+ * first object are present in the second object, but not the inverse.
 23+ *
 24+ * @param a {Object} First object to compare
 25+ * @param b {Object} Second object to compare
 26+ * @param asymmetrical {Boolean} Whether to check only that b contains values from a
 27+ * @return {Boolean} If the objects contain the same values as each other
 28+ */
 29+Content.compareObjects = function( a, b, asymmetrical ) {
 30+ var aValue, bValue, aType, bType;
 31+ for ( var k in a ) {
 32+ aValue = a[k];
 33+ bValue = b[k];
 34+ aType = typeof aValue;
 35+ bType = typeof bValue;
 36+ if ( aType !== bType
 37+ || ( ( aType === 'string' || aType === 'number' ) && aValue !== bValue )
 38+ || ( $.isPlainObject( aValue ) && !Content.compareObjects( aValue, bValue ) ) ) {
739 return false
8 - } else if ( typeof a[key] === 'string' || typeof a[key] === 'number' ) {
9 - if ( a[key] !== b[key] ) {
10 - return false;
11 - }
12 - } else if ( $.isPlainObject( a[key] ) ) {
13 - if ( !Content.compareObjects( a[key], b[key] ) ) {
14 - return false;
15 - }
1640 }
1741 }
18 - return true;
 42+ // If the check is not asymmetrical, recursing with the arguments swapped will verify our result
 43+ return asymmetrical ? true : Content.compareObjects( b, a, true );
1944 };
2045
21 -Content.copyObject = function( src ) {
22 - var dst = {};
23 - for ( var key in src ) {
24 - if ( typeof src[key] === 'string' || typeof src[key] === 'number' ) {
25 - dst[key] = src[key];
26 - } else if ( $.isPlainObject( src[key] ) ) {
27 - dst[key] = Content.copyObject( src[key] );
 46+/**
 47+ * Gets a recursive copy of an object's string, number and plain-object property.
 48+ *
 49+ * @param source {Object} Object to copy
 50+ * @return {Object} Copy of source object
 51+ */
 52+Content.copyObject = function( source ) {
 53+ var destination = {};
 54+ for ( var key in source ) {
 55+ sourceValue = source[key];
 56+ sourceType = typeof sourceValue;
 57+ if ( sourceType === 'string' || sourceType === 'number' ) {
 58+ destination[key] = sourceValue;
 59+ } else if ( $.isPlainObject( sourceValue ) ) {
 60+ destination[key] = Content.copyObject( sourceValue );
2861 }
2962 }
30 - return dst;
 63+ return destination;
3164 };
3265
 66+/**
 67+ * Gets content data from a WikiDom line object, which uses a series of offset-based annotations to
 68+ * supplement plain text.
 69+ *
 70+ * @param line {Object} WikiDom compatible line object, containing text and optionally annotations
 71+ * properties, the latter of which being an array of annotation objects including range information
 72+ * @return {Array} List of plain or annotated characters
 73+ */
3374 Content.convertLine = function( line ) {
@@ -52,10 +93,27 @@
5394 return data;
5495 };
5596
 97+/**
 98+ * Creates a new Content object from a WikiDom line object.
 99+ *
 100+ * @param line {Object} WikiDom compatible line object - @see Content.convertLine
 101+ * @return {Content} New content object containing data derived from the WikiDom line
 102+ */
56103 Content.newFromLine = function( line ) {
57104 return new Content( Content.convertLine( line ) );
58105 };
59106
 107+/**
 108+ * Creates a new Content object from a list of WikiDom line objects.
 109+ *
 110+ * This plural version of Content.newFromLine inserts non-annotated new line characters between
 111+ * lines, preserving the divisions between the original line objects. When Content objects are
 112+ * converted to WikiDom line objects, these new line characters are used to split the content data
 113+ * into multiple line objects, thus making a clean round trip possible.
 114+ *
 115+ * @param line {Array} List of WikiDom compatible line objects - @see Content.convertLine
 116+ * @return {Content} New content object containing data derived from the WikiDom line
 117+ */
60118 Content.newFromLines = function( lines ) {
61119 var data = [];
62120 for ( var i = 0; i < lines.length; i++ ) {
@@ -67,6 +125,13 @@
68126 return new Content( data );
69127 };
70128
 129+/**
 130+ * Gets plain text version of the content within a specific range.
 131+ *
 132+ * @param start {Integer} Optional beginning of range, if omitted range will begin at 0
 133+ * @param end {Integer} Optional end of range, if omitted range will end a this.data.length
 134+ * @return {String} Plain text within given range
 135+ */
71136 Content.prototype.substring = function( start, end ) {
72137 // Wrap values
73138 start = Math.max( 0, start || 0 );

Status & tagging log