From b1b2f339b846631b8137a180785916d9afc7b66e Mon Sep 17 00:00:00 2001 From: Eric Cornelissen Date: Wed, 24 Jul 2019 20:17:46 +0100 Subject: [PATCH] Don't enumerate the get method (#1555) * Test that all elements when iterating simpleIcons are objects * Refactor index template to hide .get method from enumeration --- scripts/templates/index.js | 28 ++++++++++++++++------------ tests/index.test.js | 8 ++++++++ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/scripts/templates/index.js b/scripts/templates/index.js index 8dda4057..58cf0f50 100644 --- a/scripts/templates/index.js +++ b/scripts/templates/index.js @@ -1,17 +1,21 @@ var icons = {%s}; -module.exports = icons; -module.exports.get = function(targetName) { - if (icons[targetName]) { - return icons[targetName]; - } else { - var normalizedName = targetName.toLowerCase(); - for (var iconName in icons) { - var icon = icons[iconName]; - if ((icon.title && icon.title.toLowerCase() === normalizedName) - || (icon.slug && icon.slug === normalizedName)) { - return icon; +Object.defineProperty(icons, "get", { + enumerate: false, + value: function(targetName) { + if (icons[targetName]) { + return icons[targetName]; + } else { + var normalizedName = targetName.toLowerCase(); + for (var iconName in icons) { + var icon = icons[iconName]; + if ((icon.title && icon.title.toLowerCase() === normalizedName) + || (icon.slug && icon.slug === normalizedName)) { + return icon; + } } } } -} +}); + +module.exports = icons; diff --git a/tests/index.test.js b/tests/index.test.js index f040045b..5a01f200 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -44,3 +44,11 @@ icons.forEach(icon => { expect(found.title).toEqual(icon.title); }); }); + +test(`Iterating over simpleIcons only exposes icons`, () => { + const iconArray = Object.values(simpleIcons); + for (let icon of iconArray) { + expect(icon).toBeDefined(); + expect(typeof icon).toBe('object'); + } +});