Skip to content

Commit 8137796

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 64bdf05 commit 8137796

File tree

1 file changed

+27
-55
lines changed

1 file changed

+27
-55
lines changed

src/Fingerprint.js

Lines changed: 27 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* @since 1.0
88
* @licence GNU GPL v2+
99
* @author H. Snater < mediawiki@snater.com >
10+
* @author Bene* < benestar.wikimedia@gmail.com >
1011
*
1112
* @constructor
1213
*
@@ -71,12 +72,11 @@ $.extend( SELF.prototype, {
7172
},
7273

7374
/**
74-
* @param {string} languageCode
7575
* @param {wikibase.datamodel.Term} label
7676
* @return {boolean}
7777
*/
78-
hasLabel: function( languageCode, label ) {
79-
return this._labels.hasItem( languageCode, label );
78+
hasLabel: function( label ) {
79+
return this._labels.hasItem( label.getLanguageCode(), label );
8080
},
8181

8282
/**
@@ -88,23 +88,21 @@ $.extend( SELF.prototype, {
8888
},
8989

9090
/**
91-
* @param {string} languageCode
92-
* @param {wikibase.datamodel.Term} term
91+
* @param {wikibase.datamodel.Term} label
9392
*/
94-
setLabel: function( languageCode, term ) {
95-
if ( term.getText() === '' ) {
96-
this._labels.removeItemByKey( languageCode );
93+
setLabel: function( label ) {
94+
if ( label.getText() === '' ) {
95+
this._labels.removeItemByKey( label.getLanguageCode() );
9796
} else {
98-
this._labels.setItem( languageCode, term );
97+
this._labels.setItem( label.getLanguageCode(), label );
9998
}
10099
},
101100

102101
/**
103-
* @param {string} languageCode
104102
* @param {wikibase.datamodel.Term} label
105103
*/
106-
removeLabel: function( languageCode, label ) {
107-
this._labels.removeItem( languageCode, label );
104+
removeLabel: function( label ) {
105+
this._labels.removeItem( label.getLanguageCode(), label );
108106
},
109107

110108
/**
@@ -130,12 +128,11 @@ $.extend( SELF.prototype, {
130128
},
131129

132130
/**
133-
* @param {string} languageCode
134131
* @param {wikibase.datamodel.Term} description
135132
* @return {boolean}
136133
*/
137-
hasDescription: function( languageCode, description ) {
138-
return this._descriptions.hasItem( languageCode, description );
134+
hasDescription: function( description ) {
135+
return this._descriptions.hasItem( description.getLanguageCode(), description );
139136
},
140137

141138
/**
@@ -147,23 +144,21 @@ $.extend( SELF.prototype, {
147144
},
148145

149146
/**
150-
* @param {string} languageCode
151-
* @param {wikibase.datamodel.Term} term
147+
* @param {wikibase.datamodel.Term} description
152148
*/
153-
setDescription: function( languageCode, term ) {
149+
setDescription: function( description ) {
154150
if ( term.getText() === '' ) {
155-
this._descriptions.removeItemByKey( languageCode );
151+
this._descriptions.removeItemByKey( description.getLanguageCode() );
156152
} else {
157-
this._descriptions.setItem( languageCode, term );
153+
this._descriptions.setItem( description.getLanguageCode(), description );
158154
}
159155
},
160156

161157
/**
162-
* @param {string} languageCode
163158
* @param {wikibase.datamodel.Term} description
164159
*/
165-
removeDescription: function( languageCode, description ) {
166-
this._descriptions.removeItem( languageCode, description );
160+
removeDescription: function( description ) {
161+
this._descriptions.removeItem( description.getLanguageCode(), description );
167162
},
168163

169164
/**
@@ -189,12 +184,11 @@ $.extend( SELF.prototype, {
189184
},
190185

191186
/**
192-
* @param {string} languageCode
193187
* @param {wikibase.datamodel.MultiTerm} aliases
194188
* @return {boolean}
195189
*/
196-
hasAliases: function( languageCode, aliases ) {
197-
return this._aliases.hasItem( languageCode, aliases );
190+
hasAliases: function( aliases ) {
191+
return this._aliases.hasItem( aliases.getLanguageCode(), aliases );
198192
},
199193

200194
/**
@@ -206,37 +200,16 @@ $.extend( SELF.prototype, {
206200
},
207201

208202
/**
209-
* @param {string|wikibase.datamodel.MultiTermMap} languageCodeOrAliases
210-
* @param {wikibase.datamodel.MultiTerm} [aliases]
211-
*
212-
* @throws {Error} when passing a MultiTerm without a language code.
213-
* @throws {Error} when passing a MultiTermMap with a language code.
214-
* @throws {Error} when neither passing a MultiTerm nor a MultiTermMap object.
203+
* @param {wikibase.datamodel.MultiTerm|wikibase.datamodel.MultiTermMap} aliases
215204
*/
216-
setAliases: function( languageCodeOrAliases, aliases ) {
217-
var languageCode;
218-
219-
if( typeof languageCodeOrAliases === 'string' ) {
220-
languageCode = languageCodeOrAliases;
221-
} else {
222-
aliases = languageCodeOrAliases;
223-
}
224-
225-
if( aliases instanceof wb.datamodel.MultiTerm ) {
226-
if( !languageCode ) {
227-
throw new Error( 'Language code the wb.datamodel.MultiTerm object should be set '
228-
+ 'for needs to be specified' );
229-
}
205+
setAliases: function( aliases ) {
206+
if ( aliases instanceof wb.datamodel.MultiTerm ) {
230207
if ( aliases.isEmpty() ) {
231-
this._aliases.removeItemByKey( languageCode );
208+
this._aliases.removeItemByKey( aliases.getLanguageCode() );
232209
} else {
233-
this._aliases.setItem( languageCode, aliases );
210+
this._aliases.setItem( aliases.getLanguageCode(), aliases );
234211
}
235212
} else if( aliases instanceof wb.datamodel.MultiTermMap ) {
236-
if( languageCode ) {
237-
throw new Error( 'Unable to handle language code when setting a '
238-
+ 'wb.datamodel.MultiTermMap' );
239-
}
240213
this._aliases = aliases;
241214
} else {
242215
throw new Error( 'Aliases need to be specified as wb.datamodel.MultiTerm or '
@@ -245,11 +218,10 @@ $.extend( SELF.prototype, {
245218
},
246219

247220
/**
248-
* @param {string} languageCode
249221
* @param {wikibase.datamodel.MultiTerm} aliases
250222
*/
251-
removeAliases: function( languageCode, aliases ) {
252-
this._aliases.removeItem( languageCode, aliases );
223+
removeAliases: function( aliases ) {
224+
this._aliases.removeItem( aliases.getLanguageCode(), aliases );
253225
},
254226

255227
/**

0 commit comments

Comments
 (0)