Skip to content

Commit 22f935f

Browse files
Benestarthiemowmde
authored andcommitted
Remove weirdness from Fingerprint methods
Methods to add, set or remove terms and multiterms from a Fingerprint do not require a language code to be passed together with the term because the language can be derived from the term object. In the current state there can be inconsistencies in the datamodel allowing a wrong language code to be set for a term in another language.
1 parent cd9bfe9 commit 22f935f

File tree

2 files changed

+65
-142
lines changed

2 files changed

+65
-142
lines changed

src/Fingerprint.js

Lines changed: 29 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* @since 1.0
88
* @license GPL-2.0+
99
* @author H. Snater < mediawiki@snater.com >
10+
* @author Bene* < benestar.wikimedia@gmail.com >
1011
*
1112
* @constructor
1213
*
@@ -74,12 +75,11 @@ $.extend( SELF.prototype, {
7475
},
7576

7677
/**
77-
* @param {string} languageCode
7878
* @param {wikibase.datamodel.Term} label
7979
* @return {boolean}
8080
*/
81-
hasLabel: function( languageCode, label ) {
82-
return this._labels.hasItem( languageCode, label );
81+
hasLabel: function( label ) {
82+
return this._labels.hasItem( label.getLanguageCode(), label );
8383
},
8484

8585
/**
@@ -91,23 +91,21 @@ $.extend( SELF.prototype, {
9191
},
9292

9393
/**
94-
* @param {string} languageCode
95-
* @param {wikibase.datamodel.Term|null} term
94+
* @param {wikibase.datamodel.Term|null} label
9695
*/
97-
setLabel: function( languageCode, term ) {
98-
if ( term === null || term.getText() === '' ) {
99-
this._labels.removeItemByKey( languageCode );
96+
setLabel: function( label ) {
97+
if ( label === null || label.getText() === '' ) {
98+
this._labels.removeItemByKey( label.getLanguageCode() );
10099
} else {
101-
this._labels.setItem( languageCode, term );
100+
this._labels.setItem( label.getLanguageCode(), label );
102101
}
103102
},
104103

105104
/**
106-
* @param {string} languageCode
107105
* @param {wikibase.datamodel.Term} label
108106
*/
109-
removeLabel: function( languageCode, label ) {
110-
this._labels.removeItem( languageCode, label );
107+
removeLabel: function( label ) {
108+
this._labels.removeItem( label.getLanguageCode(), label );
111109
},
112110

113111
/**
@@ -133,12 +131,11 @@ $.extend( SELF.prototype, {
133131
},
134132

135133
/**
136-
* @param {string} languageCode
137134
* @param {wikibase.datamodel.Term} description
138135
* @return {boolean}
139136
*/
140-
hasDescription: function( languageCode, description ) {
141-
return this._descriptions.hasItem( languageCode, description );
137+
hasDescription: function( description ) {
138+
return this._descriptions.hasItem( description.getLanguageCode(), description );
142139
},
143140

144141
/**
@@ -150,23 +147,21 @@ $.extend( SELF.prototype, {
150147
},
151148

152149
/**
153-
* @param {string} languageCode
154-
* @param {wikibase.datamodel.Term|null} term
150+
* @param {wikibase.datamodel.Term|null} description
155151
*/
156-
setDescription: function( languageCode, term ) {
157-
if ( term === null || term.getText() === '' ) {
158-
this._descriptions.removeItemByKey( languageCode );
152+
setDescription: function( description ) {
153+
if ( description === null || description.getText() === '' ) {
154+
this._descriptions.removeItemByKey( description.getLanguageCode() );
159155
} else {
160-
this._descriptions.setItem( languageCode, term );
156+
this._descriptions.setItem( description.getLanguageCode(), description );
161157
}
162158
},
163159

164160
/**
165-
* @param {string} languageCode
166161
* @param {wikibase.datamodel.Term} description
167162
*/
168-
removeDescription: function( languageCode, description ) {
169-
this._descriptions.removeItem( languageCode, description );
163+
removeDescription: function( description ) {
164+
this._descriptions.removeItem( description.getLanguageCode(), description );
170165
},
171166

172167
/**
@@ -192,12 +187,11 @@ $.extend( SELF.prototype, {
192187
},
193188

194189
/**
195-
* @param {string} languageCode
196190
* @param {wikibase.datamodel.MultiTerm} aliases
197191
* @return {boolean}
198192
*/
199-
hasAliases: function( languageCode, aliases ) {
200-
return this._aliases.hasItem( languageCode, aliases );
193+
hasAliases: function( aliases ) {
194+
return this._aliases.hasItem( aliases.getLanguageCode(), aliases );
201195
},
202196

203197
/**
@@ -209,37 +203,16 @@ $.extend( SELF.prototype, {
209203
},
210204

211205
/**
212-
* @param {string|wikibase.datamodel.MultiTermMap} languageCodeOrAliases
213-
* @param {wikibase.datamodel.MultiTerm|null} [aliases]
214-
*
215-
* @throws {Error} when passing a MultiTerm without a language code.
216-
* @throws {Error} when passing a MultiTermMap with a language code.
217-
* @throws {Error} when neither passing a MultiTerm nor a MultiTermMap object.
206+
* @param {wikibase.datamodel.MultiTerm|wikibase.datamodel.MultiTermMap} aliases
218207
*/
219-
setAliases: function( languageCodeOrAliases, aliases ) {
220-
var languageCode;
221-
222-
if( typeof languageCodeOrAliases === 'string' ) {
223-
languageCode = languageCodeOrAliases;
224-
} else {
225-
aliases = languageCodeOrAliases;
226-
}
227-
228-
if( aliases === null || aliases instanceof wb.datamodel.MultiTerm ) {
229-
if( !languageCode ) {
230-
throw new Error( 'Language code the wb.datamodel.MultiTerm object should be set '
231-
+ 'for needs to be specified' );
232-
}
233-
if ( aliases === null || aliases.isEmpty() ) {
234-
this._aliases.removeItemByKey( languageCode );
208+
setAliases: function( aliases ) {
209+
if ( aliases instanceof wb.datamodel.MultiTerm ) {
210+
if ( aliases.isEmpty() ) {
211+
this._aliases.removeItemByKey( aliases.getLanguageCode() );
235212
} else {
236-
this._aliases.setItem( languageCode, aliases );
213+
this._aliases.setItem( aliases.getLanguageCode(), aliases );
237214
}
238215
} else if( aliases instanceof wb.datamodel.MultiTermMap ) {
239-
if( languageCode ) {
240-
throw new Error( 'Unable to handle language code when setting a '
241-
+ 'wb.datamodel.MultiTermMap' );
242-
}
243216
this._aliases = aliases;
244217
} else {
245218
throw new Error( 'Aliases need to be specified as wb.datamodel.MultiTerm or '
@@ -248,11 +221,10 @@ $.extend( SELF.prototype, {
248221
},
249222

250223
/**
251-
* @param {string} languageCode
252224
* @param {wikibase.datamodel.MultiTerm} aliases
253225
*/
254-
removeAliases: function( languageCode, aliases ) {
255-
this._aliases.removeItem( languageCode, aliases );
226+
removeAliases: function( aliases ) {
227+
this._aliases.removeItem( aliases.getLanguageCode(), aliases );
256228
},
257229

258230
/**

0 commit comments

Comments
 (0)