Skip to content

Commit

Permalink
Added typing
Browse files Browse the repository at this point in the history
  • Loading branch information
eoussama committed Dec 8, 2020
1 parent 4e936c1 commit 7a283eb
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 4 deletions.
16 changes: 14 additions & 2 deletions dist/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ function _createClass(Constructor, protoProps, staticProps) {

/**
* @param {{ [x: string]: { [x: string]: any; }; }} dict The new dictionary value
* @throws Invalid dictionary object
*/
set: function set(dict) {
if (!dict || _typeof(dict) !== 'object' || Array.isArray(dict)) throw new Error('[TranslatorJS] Invalid dictionary object.');
Expand All @@ -107,7 +108,8 @@ function _createClass(Constructor, protoProps, staticProps) {
return this._dictionary;
}
/**
* @param {{ [x: string]: { [x: string]: any; }; }} lang The new language value
* @param {string} lang The new language value
* @throws Invalid language key
*/

}, {
Expand Down Expand Up @@ -142,6 +144,9 @@ function _createClass(Constructor, protoProps, staticProps) {
*
* @param {object} dict The translation dictionary
* @param {string} lang The default language
*
* @throws Invalid dictionary object
* @throws Invalid language key
*/

}]);
Expand All @@ -167,6 +172,8 @@ function _createClass(Constructor, protoProps, staticProps) {
*
* @param {string} input The input value to translate
* @param {object} options The translation options
*
* @returns {string} The respective translation
*/


Expand Down Expand Up @@ -348,6 +355,7 @@ function _createClass(Constructor, protoProps, staticProps) {
* Checks if an input language is defined in the dictionary
*
* @param {string} lang The language to check
* @returns {boolean} The availability of the corresponding language
*/

}, {
Expand All @@ -361,10 +369,12 @@ function _createClass(Constructor, protoProps, staticProps) {
return EOTranslator;
}();
/**
* Affects a raw string a collection of parameters
* Affects a raw string from the collection of parameters
*
* @param {string} raw The raw string to add the parameters to
* @param {object} params The parameters object
*
* @returns {string} The parame
*/


Expand All @@ -387,6 +397,8 @@ function _createClass(Constructor, protoProps, staticProps) {
* @param {object} dictionary The dictionary object
* @param {string} language The language to translate to
* @param {array<string>} frags The list of nested keys
*
* @returns {string | undefined} The extracted value
*/

function extractValue(dictionary, language, frags) {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
"demos": "cp ./dist/translator.min.js ./demos/assets/js/",
"prod": "npm run clean && npm run build && npm run demos"
},
"main": "./dist/translator.min.js",
"directories": {
"doc": "docs"
},
"types": "types/translator.d.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/EOussama/translatorjs.git"
Expand Down
16 changes: 14 additions & 2 deletions src/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

/**
* @param {{ [x: string]: { [x: string]: any; }; }} dict The new dictionary value
* @throws Invalid dictionary object
*/
set dictionary(dict) {
if (!dict || typeof dict !== 'object' || Array.isArray(dict))
Expand All @@ -39,7 +40,8 @@
}

/**
* @param {{ [x: string]: { [x: string]: any; }; }} lang The new language value
* @param {string} lang The new language value
* @throws Invalid language key
*/
set language(lang) {

Expand Down Expand Up @@ -77,6 +79,9 @@
*
* @param {object} dict The translation dictionary
* @param {string} lang The default language
*
* @throws Invalid dictionary object
* @throws Invalid language key
*/
constructor(dict, lang = '') {

Expand Down Expand Up @@ -105,6 +110,8 @@
*
* @param {string} input The input value to translate
* @param {object} options The translation options
*
* @returns {string} The respective translation
*/
translate(input = '', options = {}) {
const language = options.lang || this.language;
Expand Down Expand Up @@ -273,6 +280,7 @@
* Checks if an input language is defined in the dictionary
*
* @param {string} lang The language to check
* @returns {boolean} The availability of the corresponding language
*/
isValidLanguage(lang) {
return this.dictionary.hasOwnProperty(lang);
Expand All @@ -282,10 +290,12 @@
}

/**
* Affects a raw string a collection of parameters
* Affects a raw string from the collection of parameters
*
* @param {string} raw The raw string to add the parameters to
* @param {object} params The parameters object
*
* @returns {string} The parame
*/
function assignParams(raw, params) {

Expand All @@ -309,6 +319,8 @@
* @param {object} dictionary The dictionary object
* @param {string} language The language to translate to
* @param {array<string>} frags The list of nested keys
*
* @returns {string | undefined} The extracted value
*/
function extractValue(dictionary, language, frags) {
let temp = dictionary[language];
Expand Down
106 changes: 106 additions & 0 deletions types/translator.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
declare module 'translatorjs' {

/**
* @author EOussama
* @description The translator class
* @default
* @class
*/
class EOTranslator {

//#region Properties

/**
* @description The dictionary that the translator uses
* @throws Invalid dictionary object
*/
dictionary: { [x: string]: { [x: string]: any } };

/**
* @description The default language
* @throws Invalid language key
*/
language: string;

/**
* @description The list of available languages
* @readonly
*/
readonly languages: string[];

//#endregion

//#region Lifecycle

/**
* @description Instantiates a translator object
*
* @param {object} dict The translation dictionary
* @param {string} lang The default language
*
* @throws Invalid dictionary object
* @throws Invalid language key
*/
constructor(dict: object, lang: string = '')

//#endregion

//#region Methods

/**
* @description Translates an input value
*
* @param {string} input The input value to translate
* @param {object} options The translation options
*
* @returns {string} The respective translation
*/
translate(input: string = '', options: object = {}): string;

/**
* @description Translates the contents of a DOM elemnt
*
* @param {HTMLElement} DOMElement The DOM element to translate the content of
* @param {string} lang The language to translate to
*/
translateElement(DOMElement: HTMLElement, lang: string): void;

/**
* @description Translates a DOM document/element
*
* @param {HTMLElement} DOMContainer The HTML container
* @param {string} lang The language to translate to
*/
translateDOM(DOMContainer: HTMLElement, lang: string): void;

/**
* @description Adds a new translation to a given language or updates an existing
* one accordingly.
*
* @param {string} lang The language to add the translation to
* @param {string} keys The key of the translation
* @param {string} translation The translation to add
*/
add(lang: string, key: string, translation: string): void;

/**
* @description Removes a translation from a given language
*
* @param {string} lang The language to remove the translation from
* @param {string} keys The key of the translation to remove
*/
remove(lang: string, key: string): void;

/**
* @description Checks if an input language is defined in the dictionary
*
* @param {string} lang The language to check
* @returns {boolean} The availability of the corresponding language
*/
isValidLanguage(lang: string): boolean;

//#endregion
}

export = EOTranslator
}

0 comments on commit 7a283eb

Please sign in to comment.