mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-18 01:36:08 +08:00
core: add String.prototype.formatFromArray api
This commit is contained in:
1 parent
89857de04f
commit
90c8bfefe2
2 files changed
+13
-8
No files matched your search
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "hydrooj",
|
||||
"version": "2.13.31",
|
||||
"version": "2.13.32",
|
||||
"bin": "bin/hydrooj.js",
|
||||
"main": "dist/loader.js",
|
||||
"typings": "dist/loader.d.ts",
|
||||
|
||||
@@ -5,6 +5,7 @@ const locales = {};
|
||||
declare global {
|
||||
interface String {
|
||||
format: (...args: Array<any>) => string;
|
||||
formatFromArray: (args: any[]) => string;
|
||||
rawformat: (object: any) => string;
|
||||
translate: (...languages: string[]) => string;
|
||||
}
|
||||
@@ -20,13 +21,17 @@ String.prototype.format = function formatStr(...args) {
|
||||
result = result.replace(reg, args[key]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
if (args[i] !== undefined) {
|
||||
const reg = new RegExp(`(\\{)${i}(\\})`, 'g');
|
||||
result = result.replace(reg, args[i]);
|
||||
}
|
||||
}
|
||||
} else return this.formatFromArray(args);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
String.prototype.formatFromArray = function formatStr(args) {
|
||||
let result = this;
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
if (args[i] !== undefined) {
|
||||
const reg = new RegExp(`(\\{)${i}(\\})`, 'g');
|
||||
result = result.replace(reg, args[i]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
||||
Reference in new issue
Block a user