From b36b512a0ff97223cd3ee1df30bf745295026c26 Mon Sep 17 00:00:00 2001 From: meikidd Date: Wed, 28 Dec 2016 15:30:51 +0800 Subject: [PATCH] fix #7 getCode() with not supported language name --- build/index.js | 13 +++++-------- src/index.js | 10 +++++++--- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/build/index.js b/build/index.js index dedad22..13f6a3e 100644 --- a/build/index.js +++ b/build/index.js @@ -40,7 +40,8 @@ var ISO6391 = function ISO6391() { (0, _classCallCheck3.default)(this, ISO6391); }; -ISO6391.getLanguages = function (codes) { +ISO6391.getLanguages = function () { + var codes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; return (0, _entries2.default)(_data2.default).filter(function (_ref) { var _ref2 = (0, _slicedToArray3.default)(_ref, 2), code = _ref2[0], @@ -77,15 +78,11 @@ ISO6391.getAllNativeNames = function () { }; ISO6391.getCode = function (name) { - var _Object$entries$find = (0, _entries2.default)(_data2.default).find(function (_ref5) { - var _ref6 = (0, _slicedToArray3.default)(_ref5, 2), - code = _ref6[0], - language = _ref6[1]; + var code = (0, _keys2.default)(_data2.default).find(function (code) { + var language = _data2.default[code]; return language.name.toLowerCase() === name.toLowerCase() || language.nativeName.toLowerCase() === name.toLowerCase(); - }), - _Object$entries$find2 = (0, _slicedToArray3.default)(_Object$entries$find, 1), - code = _Object$entries$find2[0]; + }); return code === undefined ? '' : code; }; diff --git a/src/index.js b/src/index.js index aa7b08e..64b6df6 100644 --- a/src/index.js +++ b/src/index.js @@ -2,7 +2,7 @@ import 'babel-polyfill'; import LANGUAGES_LIST from './data'; export default class ISO6391 { - static getLanguages = (codes) => ( + static getLanguages = (codes = []) => ( Object.entries(LANGUAGES_LIST) .filter(([code, language]) => codes.includes(code)) .map(([code, language]) => ({ ...language, code })) @@ -17,8 +17,12 @@ export default class ISO6391 { static getAllNativeNames = () => Object.values(LANGUAGES_LIST).map(l => l.nativeName) static getCode = (name) => { - const [code] = Object.entries(LANGUAGES_LIST) - .find(([code, language]) => language.name.toLowerCase() === name.toLowerCase() || language.nativeName.toLowerCase() === name.toLowerCase()) + + const code = Object.keys(LANGUAGES_LIST) + .find((code) => { + const language = LANGUAGES_LIST[code] + return language.name.toLowerCase() === name.toLowerCase() || language.nativeName.toLowerCase() === name.toLowerCase() + }) return code === undefined ? '' : code }