Skip to content

Commit

Permalink
fix(FEC-7226, FEC-7243): create comparer func to default tracks (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Ziv authored and OrenMe committed Oct 9, 2017
1 parent c79ddf7 commit 204cc61
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,7 @@ export default class Player extends FakeEventTarget {
*/
_setDefaultTrack(type: string, language: string, defaultTrack: Track): void {
if (language) {
const track: ?Track = this._getTracksByType(type).find(track => track.language === language);
const track: ?Track = this._getTracksByType(type).find(track => TextTrack.langComparer(language, track.language));
if (track) {
this.selectTrack(track);
}
Expand Down
15 changes: 15 additions & 0 deletions src/track/text-track.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ import Track from './track'
* @classdesc
*/
export default class TextTrack extends Track {
/**
* Comparing language strings according to their length.
* @param {string} inputLang - The configured language.
* @param {string} trackLang - The default track language.
* @returns {boolean} - Whether the strings are equal or starts with the same substring.
*/
static langComparer(inputLang: string, trackLang: string): boolean {
const inputLangLength = inputLang.length;
const trackLangLength = trackLang.length;
if (inputLangLength === trackLangLength) {
return inputLang === trackLang;
}
return (inputLangLength > trackLangLength ? inputLang.startsWith(trackLang) : trackLang.startsWith(inputLang));
}

/**
* The kind of the text track:
* subtitles/captions/metadata.
Expand Down
17 changes: 17 additions & 0 deletions test/src/track/text-track.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import TextTrack from '../../../src/track/text-track'

describe('TextTrack', () => {
describe('langComparer', () => {
it('should compare 2 languages with same length', () => {
TextTrack.langComparer('ita', 'ita').should.be.true;
TextTrack.langComparer('ita', 'eng').should.be.false;
});

it('should compare 2 languages with different length', () => {
TextTrack.langComparer('ita', 'it').should.be.true;
TextTrack.langComparer('it', 'ita').should.be.true;
TextTrack.langComparer('es', 'ita').should.be.false;
TextTrack.langComparer('ita', 'es').should.be.false;
});
});
});

0 comments on commit 204cc61

Please sign in to comment.