done i think

This commit is contained in:
2022-03-29 23:25:06 -05:00
parent 97c05f168d
commit 5a62482891
24 changed files with 643 additions and 18 deletions

17
lib/index.d.ts vendored Normal file
View File

@@ -0,0 +1,17 @@
import { basicObjectStringable, LangObj } from './types';
export declare function setLang(lang: LangObj): void;
export declare function getLang(): LangObj;
/**
* reads language json (just strings)
* @param id ex: discord.error.noActiveQueue
* @param argsraw list of key/value pairs to represent template values
* @returns language value, defaults to `id` parameter
*/
declare function get(id: string, argsraw?: basicObjectStringable): string;
declare const _default: {
setLang: typeof setLang;
get: typeof get;
};
export default _default;
export * from './types';
export * from './util';

57
lib/index.js Normal file
View File

@@ -0,0 +1,57 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLang = exports.setLang = void 0;
const util_1 = require("./util");
/* MAIN */
var LANG = {};
function setLang(lang) {
LANG = lang;
}
exports.setLang = setLang;
function getLang() {
return LANG;
}
exports.getLang = getLang;
/**
* reads language json (just strings)
* @param id ex: discord.error.noActiveQueue
* @param argsraw list of key/value pairs to represent template values
* @returns language value, defaults to `id` parameter
*/
function get(id, argsraw = {}) {
const args = (0, util_1.convertBasicObject)(argsraw), keySpl = id.split('.').map(k => k.trim()).filter(k => k);
let finding = LANG;
for (const key of keySpl) {
if (key in finding) {
const found = finding[key];
if (typeof found === 'string')
return (0, util_1.template)(found, args);
if (found.embed === true)
return (0, util_1.embedObjStr)(found, args, id);
finding = found;
}
else
break;
}
return id;
}
exports.default = {
setLang,
get
};
__exportStar(require("./types"), exports);
__exportStar(require("./util"), exports);

87
lib/types.d.ts vendored Normal file
View File

@@ -0,0 +1,87 @@
/**
* any indexable object
*/
export declare type basicObject = {
[keys: string]: any;
};
/**
* any indexable object with string values
*/
export declare type basicObjectStringable = {
[keys: string]: string | number | boolean | null;
};
export declare type basicObjectString = {
[keys: string]: string;
};
/**
* an abstract version of strings
*/
export declare type bigStringType = string | string[];
/**
* a representation of an author in the LANG object
*
* `LANG > Language > Embed > Field`
*/
export interface embedField {
name: string;
value: bigStringType;
inline?: boolean;
}
/**
* a representation of an author in the LANG object
*
* `LANG > Language > Embed > Author`
*/
export interface authorData {
name: string;
url?: string;
iconURL?: string;
}
/**
* a representation of a footer in the LANG object
*
* `LANG > Language > Embed > Footer`
*/
export interface footerData {
text: string;
iconURL?: string;
}
/**
* a representation of an embed in the LANG object
*
* `LANG > Language > Embed`
*/
export interface embedObject {
embed: true;
content?: string;
title?: string;
description?: bigStringType;
/**
* URL
*/
url?: string;
/**
* #FFFFFF
*/
color?: string;
footer?: string | footerData;
thumbnail?: string;
/**
* URL
*/
image?: string;
/**
* URL
*/
author?: string | authorData;
fields?: embedField[];
timestamp?: boolean | string | number;
}
/**
* a specific language in the LANG object
*
* `LANG > Language`
*/
export declare type LangObj = {
[keys: string]: LangObj | embedObject | string;
};

2
lib/types.js Normal file
View File

@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

22
lib/util.d.ts vendored Normal file
View File

@@ -0,0 +1,22 @@
import { basicObject, basicObjectString, basicObjectStringable, bigStringType, embedObject } from './types';
/**
*
* @param str
* @param args
* @returns
*/
export declare function template(str: string, args: basicObject): string;
/**
* converts bigString to string
*/
export declare function bigString(bigStr: bigStringType): string;
/**
* converts Hex Color string to an RGB array
*/
export declare function resolveColor(color: string): [number, number, number];
/**
* converts embedObj to a string if applicable
* @param fallback the string to use if no valid strings can be found
*/
export declare function embedObjStr(embedObj: embedObject, args?: basicObjectString, fallback?: string): string;
export declare function convertBasicObject(obj: basicObjectStringable): basicObjectString;

74
lib/util.js Normal file
View File

@@ -0,0 +1,74 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertBasicObject = exports.embedObjStr = exports.resolveColor = exports.bigString = exports.template = void 0;
/**
*
* @param str
* @param args
* @returns
*/
function template(str, args) {
return str.replace(/{\w+}/g, str => {
const key = str.substring(1, str.length - 1);
if (key in args)
return args[key];
return key;
});
}
exports.template = template;
/**
* converts bigString to string
*/
function bigString(bigStr) {
if (Array.isArray(bigStr))
return bigStr.join('\n');
return bigStr;
}
exports.bigString = bigString;
/**
* converts Hex Color string to an RGB array
*/
function resolveColor(color) {
color = color.replace(/[^0-9a-f]/gi, '');
const colorNum = [0, 0, 0];
if (color.length === 3 || color.length === 6) {
const colorSplRaw = /([0-9a-f]{1,2})([0-9a-f]{1,2})([0-9a-f]{1,2})/.exec(color);
if (!colorSplRaw)
return colorNum;
const colorSpl = colorSplRaw.slice(1, 4);
if (color.length === 3)
colorSpl.map(c => c + c);
for (let i = 0; i < colorSpl.length && i < colorNum.length; i++)
colorNum[i] = parseInt(colorSpl[i], 16);
}
return colorNum;
}
exports.resolveColor = resolveColor;
/**
* converts embedObj to a string if applicable
* @param fallback the string to use if no valid strings can be found
*/
function embedObjStr(embedObj, args = {}, fallback = '') {
if (embedObj.content !== undefined)
return template(bigString(embedObj.content), args);
if (embedObj.description !== undefined)
return template(bigString(embedObj.description), args);
return fallback;
}
exports.embedObjStr = embedObjStr;
function convertBasicObject(obj) {
let ret = {};
for (const key in obj) {
const val = obj[key];
if (typeof val === 'string')
ret[key] = val;
else if (typeof val === 'boolean')
ret[key] = val.toString();
else if (typeof val === 'number')
ret[key] = val.toString();
else
ret[key] = '';
}
return ret;
}
exports.convertBasicObject = convertBasicObject;