@lrc-maker/lrc-parser
Version:
lrc-parser for lrc-maker
178 lines (169 loc) • 7.2 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.lrcParser = {}));
})(this, (function (exports) { 'use strict';
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function __values(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
}
function __read(o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
}
var parser = function (lrcString, option) {
var e_1, _a;
if (option === void 0) { option = {}; }
var _b = option.trimStart, trimStart = _b === void 0 ? false : _b, _c = option.trimEnd, trimEnd = _c === void 0 ? false : _c;
var lines = lrcString.split(/\r\n|\n|\r/u);
var timeTag = /\[\s*(\d{1,3}):(\d{1,2}(?:[:.]\d{1,3})?)\s*]/g;
var infoTag = /\[\s*(\w{1,6})\s*:(.*?)]/;
var info = new Map();
var lyric = [];
try {
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
var line = lines_1_1.value;
if (line[0] !== "[") {
lyric.push({
text: line,
});
continue;
}
timeTag.lastIndex = 0;
var rTimeTag = timeTag.exec(line);
if (rTimeTag !== null) {
var mm = Number.parseInt(rTimeTag[1], 10);
var ss = Number.parseFloat(rTimeTag[2].replace(":", "."));
var text = line.slice(timeTag.lastIndex);
lyric.push({
time: mm * 60 + ss,
text: text,
});
continue;
}
var rInfoTag = infoTag.exec(line);
if (rInfoTag !== null) {
var value = rInfoTag[2].trim();
if (value === "") {
continue;
}
info.set(rInfoTag[1], value);
continue;
}
lyric.push({
text: line,
});
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
}
finally { if (e_1) throw e_1.error; }
}
if (trimStart && trimEnd) {
lyric.forEach(function (line) {
line.text = line.text.trim();
});
}
else if (trimStart) {
lyric.forEach(function (line) {
line.text = line.text.trimStart();
});
}
else if (trimEnd) {
lyric.forEach(function (line) {
line.text = line.text.trimEnd();
});
}
return { info: info, lyric: lyric };
};
var storedFormatter = new Map();
var getFormatter = function (fixed) {
if (storedFormatter.has(fixed)) {
return storedFormatter.get(fixed);
}
var newFormatter = new Intl.NumberFormat("en", {
minimumIntegerDigits: 2,
minimumFractionDigits: fixed,
maximumFractionDigits: fixed,
useGrouping: false,
});
storedFormatter.set(fixed, newFormatter);
return newFormatter;
};
var convertTimeToTag = function (time, fixed, withBrackets) {
if (withBrackets === void 0) { withBrackets = true; }
if (time === undefined) {
return "";
}
var formatter = getFormatter(fixed);
var mm = Math.floor(time / 60)
.toString()
.padStart(2, "0");
var ss = formatter.format(time % 60);
return withBrackets ? "[".concat(mm, ":").concat(ss, "]") : "".concat(mm, ":").concat(ss);
};
var formatText = function (text, spaceStart, spaceEnd) {
var newText = text;
if (spaceStart >= 0) {
newText = " ".repeat(spaceStart) + newText.trimStart();
}
if (spaceEnd >= 0) {
newText = newText.trimEnd() + " ".repeat(spaceEnd);
}
return newText;
};
var stringify = function (state, option) {
var spaceStart = option.spaceStart, spaceEnd = option.spaceEnd, fixed = option.fixed, _a = option.endOfLine, endOfLine = _a === void 0 ? "\r\n" : _a;
var infos = Array.from(state.info.entries()).map(function (_a) {
var _b = __read(_a, 2), name = _b[0], value = _b[1];
return "[".concat(name, ": ").concat(value, "]");
});
var lines = state.lyric.map(function (line) {
if (line.time === undefined) {
return line.text;
}
var text = formatText(line.text, spaceStart, spaceEnd);
return "".concat(convertTimeToTag(line.time, fixed)).concat(text);
});
return infos.concat(lines).join(endOfLine);
};
exports.convertTimeToTag = convertTimeToTag;
exports.formatText = formatText;
exports.parser = parser;
exports.stringify = stringify;
}));
//# sourceMappingURL=lrc-parser.js.map