Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove weirdness from Fingerprint methods #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 29 additions & 57 deletions src/Fingerprint.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @since 1.0
* @license GPL-2.0+
* @author H. Snater < [email protected] >
* @author Bene* < [email protected] >
*
* @constructor
*
Expand Down Expand Up @@ -74,12 +75,11 @@ $.extend( SELF.prototype, {
},

/**
* @param {string} languageCode
* @param {wikibase.datamodel.Term} label
* @return {boolean}
*/
hasLabel: function( languageCode, label ) {
return this._labels.hasItem( languageCode, label );
hasLabel: function( label ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @JeroenDeDauw said this should be hasLabel: function( languageCode ).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that we already have hasLabelFor below which does exactly that. We could just remove this method but I think it is useful if you don't only want to check if a label is set but also compare it to the value passed here.

return this._labels.hasItem( label.getLanguageCode(), label );
},

/**
Expand All @@ -91,23 +91,21 @@ $.extend( SELF.prototype, {
},

/**
* @param {string} languageCode
* @param {wikibase.datamodel.Term|null} term
* @param {wikibase.datamodel.Term|null} label
*/
setLabel: function( languageCode, term ) {
if ( term === null || term.getText() === '' ) {
this._labels.removeItemByKey( languageCode );
setLabel: function( label ) {
if ( label === null || label.getText() === '' ) {
this._labels.removeItemByKey( label.getLanguageCode() );
} else {
this._labels.setItem( languageCode, term );
this._labels.setItem( label.getLanguageCode(), label );
}
},

/**
* @param {string} languageCode
* @param {wikibase.datamodel.Term} label
*/
removeLabel: function( languageCode, label ) {
this._labels.removeItem( languageCode, label );
removeLabel: function( label ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All remove… methods can most probably be function( languageCode ) too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also have removeLabelFor and removeDescriptionFor which do exactly that. The same arguments apply as above.

Edit: Even removeAliasesFor

this._labels.removeItem( label.getLanguageCode(), label );
},

/**
Expand All @@ -133,12 +131,11 @@ $.extend( SELF.prototype, {
},

/**
* @param {string} languageCode
* @param {wikibase.datamodel.Term} description
* @return {boolean}
*/
hasDescription: function( languageCode, description ) {
return this._descriptions.hasItem( languageCode, description );
hasDescription: function( description ) {
return this._descriptions.hasItem( description.getLanguageCode(), description );
},

/**
Expand All @@ -150,23 +147,21 @@ $.extend( SELF.prototype, {
},

/**
* @param {string} languageCode
* @param {wikibase.datamodel.Term|null} term
* @param {wikibase.datamodel.Term|null} description
*/
setDescription: function( languageCode, term ) {
if ( term === null || term.getText() === '' ) {
this._descriptions.removeItemByKey( languageCode );
setDescription: function( description ) {
if ( description === null || description.getText() === '' ) {
this._descriptions.removeItemByKey( description.getLanguageCode() );
} else {
this._descriptions.setItem( languageCode, term );
this._descriptions.setItem( description.getLanguageCode(), description );
}
},

/**
* @param {string} languageCode
* @param {wikibase.datamodel.Term} description
*/
removeDescription: function( languageCode, description ) {
this._descriptions.removeItem( languageCode, description );
removeDescription: function( description ) {
this._descriptions.removeItem( description.getLanguageCode(), description );
},

/**
Expand All @@ -192,12 +187,11 @@ $.extend( SELF.prototype, {
},

/**
* @param {string} languageCode
* @param {wikibase.datamodel.MultiTerm} aliases
* @return {boolean}
*/
hasAliases: function( languageCode, aliases ) {
return this._aliases.hasItem( languageCode, aliases );
hasAliases: function( aliases ) {
return this._aliases.hasItem( aliases.getLanguageCode(), aliases );
},

/**
Expand All @@ -209,37 +203,16 @@ $.extend( SELF.prototype, {
},

/**
* @param {string|wikibase.datamodel.MultiTermMap} languageCodeOrAliases
* @param {wikibase.datamodel.MultiTerm|null} [aliases]
*
* @throws {Error} when passing a MultiTerm without a language code.
* @throws {Error} when passing a MultiTermMap with a language code.
* @throws {Error} when neither passing a MultiTerm nor a MultiTermMap object.
* @param {wikibase.datamodel.MultiTerm|wikibase.datamodel.MultiTermMap} aliases
*/
setAliases: function( languageCodeOrAliases, aliases ) {
var languageCode;

if( typeof languageCodeOrAliases === 'string' ) {
languageCode = languageCodeOrAliases;
} else {
aliases = languageCodeOrAliases;
}

if( aliases === null || aliases instanceof wb.datamodel.MultiTerm ) {
if( !languageCode ) {
throw new Error( 'Language code the wb.datamodel.MultiTerm object should be set '
+ 'for needs to be specified' );
}
if ( aliases === null || aliases.isEmpty() ) {
this._aliases.removeItemByKey( languageCode );
setAliases: function( aliases ) {
if ( aliases instanceof wb.datamodel.MultiTerm ) {
if ( aliases.isEmpty() ) {
this._aliases.removeItemByKey( aliases.getLanguageCode() );
} else {
this._aliases.setItem( languageCode, aliases );
this._aliases.setItem( aliases.getLanguageCode(), aliases );
}
} else if( aliases instanceof wb.datamodel.MultiTermMap ) {
if( languageCode ) {
throw new Error( 'Unable to handle language code when setting a '
+ 'wb.datamodel.MultiTermMap' );
}
this._aliases = aliases;
} else {
throw new Error( 'Aliases need to be specified as wb.datamodel.MultiTerm or '
Expand All @@ -248,11 +221,10 @@ $.extend( SELF.prototype, {
},

/**
* @param {string} languageCode
* @param {wikibase.datamodel.MultiTerm} aliases
*/
removeAliases: function( languageCode, aliases ) {
this._aliases.removeItem( languageCode, aliases );
removeAliases: function( aliases ) {
this._aliases.removeItem( aliases.getLanguageCode(), aliases );
},

/**
Expand Down
Loading