Skip to content

Commit 7cf362c

Browse files
authored
Merge pull request #77 from wmde/snakHash
Add hash to Snak
2 parents cd9bfe9 + d17091d commit 7cf362c

File tree

6 files changed

+44
-10
lines changed

6 files changed

+44
-10
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ at the heart of the [Wikibase software](http://wikiba.se/).
88

99
## Release notes
1010

11+
### 3.1.0 (dev)
12+
13+
* Added a `hash` parameter to the constructors of
14+
`Snak`, `PropertyValueSnak`, `PropertySomeValueSnak` and `PropertyNoValueSnak`,
15+
as well as a `getHash()` function to the `Snak` class.
16+
1117
### 3.0.1 (2016-09-09)
1218

1319
* Fix an issue with MediaWiki loading (init.php)

src/PropertyNoValueSnak.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var PARENT = wb.datamodel.Snak;
1414
* @constructor
1515
*
1616
* @param {string} propertyId
17+
* @param {string|null} [hash=null]
1718
*/
1819
var SELF
1920
= wb.datamodel.PropertyNoValueSnak

src/PropertySomeValueSnak.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var PARENT = wb.datamodel.Snak;
1414
* @constructor
1515
*
1616
* @param {string} propertyId
17+
* @param {string|null} [hash=null]
1718
*/
1819
var SELF
1920
= wb.datamodel.PropertySomeValueSnak

src/PropertyValueSnak.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@ var PARENT = wb.datamodel.Snak;
1515
*
1616
* @param {string} propertyId
1717
* @param {dataValues.DataValue} value
18+
* @param {string|null} [hash=null]
1819
*
1920
* @throws {Error} value is not a dataValues.DataValue instance.
2021
*/
2122
var SELF = wb.datamodel.PropertyValueSnak = util.inherit(
2223
'WbDataModelPropertyValueSnak',
2324
PARENT,
24-
function( propertyId, value ) {
25+
function( propertyId, value, hash ) {
2526
if( !( value instanceof dv.DataValue ) ) {
2627
throw new Error( 'The value has to be an instance of dataValues.DataValue' );
2728
}
28-
PARENT.call( this, propertyId );
29+
PARENT.call( this, propertyId, hash );
2930
this._value = value;
3031
},
3132
{

src/Snak.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@
1212
* @constructor
1313
*
1414
* @param {string} propertyId
15+
* @param {string|null} [hash=null]
1516
*
1617
* @throws {Error} when trying to instantiate an abstract Snak object.
1718
* @throws {Error} when the property id is omitted.
1819
*/
19-
var SELF = wb.datamodel.Snak = function WbDataModelSnak( propertyId ) {
20+
var SELF = wb.datamodel.Snak = function WbDataModelSnak( propertyId, hash ) {
2021
if( !this.constructor.TYPE ) {
2122
throw new Error( 'Can not create abstract Snak of no specific type' );
2223
} else if( !propertyId ) {
2324
throw new Error( 'Property ID is required for constructing new Snak' );
2425
}
2526
this._propertyId = propertyId;
27+
this._hash = hash || null;
2628
};
2729

2830
/**
@@ -42,6 +44,12 @@ $.extend( SELF.prototype, {
4244
*/
4345
_propertyId: null,
4446

47+
/**
48+
* @property {string|null}
49+
* @private
50+
*/
51+
_hash: null,
52+
4553
/**
4654
* Returns the Snak type.
4755
*
@@ -60,6 +68,17 @@ $.extend( SELF.prototype, {
6068
return this._propertyId;
6169
},
6270

71+
/**
72+
* Returns the hash of this Snak.
73+
* Can be null if this Snak was constructed client-side,
74+
* since hashes can only be computed server-side.
75+
*
76+
* @return {string|null}
77+
*/
78+
getHash: function() {
79+
return this._hash;
80+
},
81+
6382
/**
6483
* @param {*} snak
6584
* @return {boolean}

tests/Snak.tests.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
QUnit.module( 'wikibase.datamodel.Snak' );
99

1010
var testSets = [
11-
[wb.datamodel.PropertyNoValueSnak, ['P1']],
12-
[wb.datamodel.PropertyNoValueSnak, ['P2']],
13-
[wb.datamodel.PropertySomeValueSnak, ['P1']],
14-
[wb.datamodel.PropertyValueSnak, ['P1', new dv.StringValue( 'test' )]],
15-
[wb.datamodel.PropertyValueSnak, ['P2', new dv.StringValue( 'test' )]]
11+
[wb.datamodel.PropertyNoValueSnak, ['P1', undefined]],
12+
[wb.datamodel.PropertyNoValueSnak, ['P2', 'hash']],
13+
[wb.datamodel.PropertySomeValueSnak, ['P1', 'hash']],
14+
[wb.datamodel.PropertyValueSnak, ['P1', new dv.StringValue( 'test' ), undefined]],
15+
[wb.datamodel.PropertyValueSnak, ['P2', new dv.StringValue( 'test' ), 'hash']]
1616
];
1717

1818
/**
@@ -21,11 +21,11 @@ var testSets = [
2121
* @return {wikibase.datamodel.Snak}
2222
*/
2323
function constructSnak( SnakConstructor, params ) {
24-
return new SnakConstructor( params[0], params[1] );
24+
return new SnakConstructor( params[0], params[1], params[2] );
2525
}
2626

2727
QUnit.test( 'Constructor', function( assert ) {
28-
assert.expect( 15 );
28+
assert.expect( 20 );
2929

3030
for( var i = 0; i < testSets.length; i++ ) {
3131
var SnakConstructor = testSets[i][0],
@@ -48,6 +48,12 @@ QUnit.test( 'Constructor', function( assert ) {
4848
SnakConstructor.TYPE,
4949
'Test set #' + i + ': Snak type "' + snak.getType() + '" was set correctly.'
5050
);
51+
52+
assert.strictEqual(
53+
snak.getHash(),
54+
snakParams[snakParams.length - 1] || null,
55+
'Test set #' + i + ': Snak hash was set correctly.'
56+
);
5157
}
5258
} );
5359

0 commit comments

Comments
 (0)