Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object.values and Array.find written with older browser support in mind #11

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import LANGUAGES_LIST from './data';

function objectValues(object) {
const result = [];
for (const key of object) {
if (object.hasOwnProperty(key)) {
result.push(object[key]);
}
}
return result;
}

function arrayFind(array, fn) {
const len = array.length;
for (let i=0; i < len; i++) {
const item = array[i];
if (fn(item)) {
return item;
}
}
return undefined;
}

export default class ISO6391 {
static getLanguages(codes = []) {
return codes.map(code => ({
Expand All @@ -14,19 +35,19 @@ export default class ISO6391 {
}

static getAllNames() {
return Object.values(LANGUAGES_LIST).map(l => l.name);
return objectValues(LANGUAGES_LIST).map(l => l.name);
}

static getNativeName(code) {
return ISO6391.validate(code) ? LANGUAGES_LIST[code].nativeName : '';
}

static getAllNativeNames() {
return Object.values(LANGUAGES_LIST).map(l => l.nativeName);
return objectValues(LANGUAGES_LIST).map(l => l.nativeName);
}

static getCode(name) {
const code = Object.keys(LANGUAGES_LIST).find(code => {
const code = arrayFind(Object.keys(LANGUAGES_LIST), code => {
const language = LANGUAGES_LIST[code];

return (
Expand Down
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,15 @@ describe('getLanguages()', function() {
]);
});
});

describe('getAllNames()', function() {
it('returns a nonempty array', function() {
assert(0 < ISO6391.getAllNames().length);
});
});

describe('getAllNativeNames()', function() {
it('returns a nonempty array', function() {
assert(0 < ISO6391.getAllNativeNames().length);
});
});