@ecomplus/utils
Version:
JS utility functions to E-Com Plus (not only) related apps
53 lines (52 loc) • 1.77 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
require("core-js/modules/es.function.name.js");
var _config2 = _interopRequireDefault(require("./../lib/config"));
var _i18n = _interopRequireDefault(require("./i18n"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* @method
* @memberof ecomUtils
* @name name
* @description Returns object name by lang.
* @param {Object.<string, *>} body - Object (product, category, brand, grid...) body
* @param {string} [lang=$ecomConfig.get('lang')] - Snake case language code, eg.: 'en_us', 'pt_br'
* @returns {string}
*
* @example
* // Sample object with name and translations
* const product = { name: 'Test', i18n: { en_us: 'Test', pt_br: 'Teste' } }
* ecomUtils.name(product)
* // => 'Test'
* ecomUtils.name(product, 'pt_br')
* // => 'Teste'
*
* @example
* // Without translations
* ecomUtils.name({ name: 'Hello' })
* // => 'Hello'
* ecomUtils.name({ name: 'Hello' }, 'pt_br')
* // => 'Hello'
* ecomUtils.name({ title: 'Mundo' }, 'en_us')
* // => 'Mundo'
*
* @example
* // You can also set the configured lang first
* $ecomConfig.set('lang', 'pt_br')
* // Then call `name` without expliciting lang
* ecomUtils.name({ name: 'Test', i18n: { en_us: 'Test', pt_br: 'Teste' } })
* // => 'Teste'
*/
var name = function name(body) {
var lang = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _config2.default.get('lang');
// prefer translated item name
if (lang && body.i18n && body.i18n[lang]) {
return body.i18n[lang];
} else {
return body.name || body.title || (0, _i18n.default)(body.i18n, lang) || '';
}
};
var _default = exports.default = name;