-
Notifications
You must be signed in to change notification settings - Fork 8
/
lyrics.min.js
1 lines (1 loc) · 2.3 KB
/
lyrics.min.js
1
!function(t,i){"use strict";"function"==typeof define&&define.amd?define([],i):"object"==typeof exports?module.exports=i():t.Lyrics=i()}(this,function(){"use strict";var t=function(t){this.timestamp_offset=0,this.lyrics_all=void 0,this.meta_info=void 0,this.ID_TAGS=[{name:"artist",id:"ar"},{name:"album",id:"al"},{name:"title",id:"ti"},{name:"author",id:"au"},{name:"length",id:"length"},{name:"by",id:"by"},{name:"offset",id:"offset",handler:this.setTimestampOffset},{name:"createdBy",id:"re"},{name:"createdByVersion",id:"ve"}];for(var i=0;i<this.ID_TAGS.length;i++)this.ID_TAGS[i].re=new RegExp("\\["+this.ID_TAGS[i].id+":(.*)\\]$","g");t&&this.load(t)};return t.prototype={constructor:t,load:function(t){this.lyrics_all=new Array,this.meta_info=new Object,this.timestamp_offset=0;for(var i=String(t).split("\n"),e=0;e<i.length;e++){var r=i[e].replace(/(^\s*)|(\s*$)/g,"");if(r){for(var s=!1,n=0;n<this.ID_TAGS.length;n++){var a=this.ID_TAGS[n].re.exec(r);if(a&&!(a.length<2)){s=!0;var l=a[1].replace(/(^\s*)|(\s*$)/g,"");"function"==typeof this.ID_TAGS[n].handler?this.meta_info[String(this.ID_TAGS[n].name)]=this.ID_TAGS[n].handler.call(this,l):this.meta_info[String(this.ID_TAGS[n].name)]=String(l)}}if(!s){for(var h=Array();;){var a=/^(\[\d+:\d+(.\d+)?\])(.*)/g.exec(r);if(!a)break;h.push(a[1]),r=a[a.length-1].replace(/(^\s*)|(\s*$)/g,"")}for(var n=0;n<h.length;n++){var f=/^\[(\d{1,2}):(\d|[0-5]\d)(\.(\d+))?\]$/g.exec(h[n]);f&&this.lyrics_all.push({timestamp:60*Number(f[1])+Number(f[2])+(f[4]?Number("0."+f[4]):0),text:r})}}}}return this.lyrics_all.sort(function(t,i){return t.timestamp>i.timestamp?1:-1}),this.lyrics_all.length||(this.lyrics_all=void 0),this.isEmpty(this.meta_info)&&(this.meta_info=void 0),void 0!==this.lyrics_all||void 0!==this.meta_info},getLyrics:function(){return this.lyrics_all},getLyric:function(t){try{return this.lyrics_all[t]}catch(t){return}},getIDTags:function(){return this.meta_info},select:function(t){if(isNaN(t))return-1;var i=Number(t)+this.timestamp_offset,e=0;if(i<this.lyrics_all[0].timestamp)return-1;for(e=0;e<this.lyrics_all.length-1&&!(this.lyrics_all[e].timestamp<=i&&this.lyrics_all[e+1].timestamp>i);e++);return e},setTimestampOffset:function(t){return this.timestamp_offset=isNaN(t)?0:Number(t)/1e3,Number(t)},isEmpty:function(t){for(var i in t)if(t.hasOwnProperty(i))return!1;return!0}},t});