Skip to content

Commit

Permalink
[Refactor] remove array.prototype.foreach
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jul 11, 2024
1 parent 8e58f2e commit 78022fe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
34 changes: 18 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

'use strict';

var forEach = require('array.prototype.foreach');
var entries = require('object.entries');
var warning = require('warning');
var has = require('hasown');
Expand Down Expand Up @@ -118,13 +117,14 @@ var defaultPluralRules = {

function langToTypeMap(mapping) {
var ret = {};
forEach(entries(mapping), function (entry) {
var type = entry[0];
var langs = entry[1];
forEach(langs, function (lang) {
ret[lang] = type;
});
});
var mappingEntries = entries(mapping);
for (var i = 0; i < mappingEntries.length; i += 1) {
var type = mappingEntries[i][0];
var langs = mappingEntries[i][1];
for (var j = 0; j < langs.length; j += 1) {
ret[langs[j]] = type;
}
}
return ret;
}

Expand Down Expand Up @@ -325,16 +325,17 @@ Polyglot.prototype.locale = function (newLocale) {
//
// This feature is used internally to support nested phrase objects.
Polyglot.prototype.extend = function (morePhrases, prefix) {
forEach(entries(morePhrases || {}), function (entry) {
var key = entry[0];
var phrase = entry[1];
var phraseEntries = entries(morePhrases || {});
for (var i = 0; i < phraseEntries.length; i += 1) {
var key = phraseEntries[i][0];
var phrase = phraseEntries[i][1];
var prefixedKey = prefix ? prefix + '.' + key : key;
if (typeof phrase === 'object') {
this.extend(phrase, prefixedKey);
} else {
this.phrases[prefixedKey] = phrase;
}
}, this);
}
};

// ### polyglot.unset(phrases)
Expand All @@ -352,16 +353,17 @@ Polyglot.prototype.unset = function (morePhrases, prefix) {
if (typeof morePhrases === 'string') {
delete this.phrases[morePhrases];
} else {
forEach(entries(morePhrases || {}), function (entry) {
var key = entry[0];
var phrase = entry[1];
var phraseEntries = entries(morePhrases || {});
for (var i = 0; i < phraseEntries.length; i += 1) {
var key = phraseEntries[i][0];
var phrase = phraseEntries[i][1];
var prefixedKey = prefix ? prefix + '.' + key : key;
if (typeof phrase === 'object') {
this.unset(phrase, prefixedKey);
} else {
delete this.phrases[prefixedKey];
}
}, this);
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
],
"author": "Spike Brehm <[email protected]>",
"dependencies": {
"array.prototype.foreach": "^1.0.7",
"hasown": "^2.0.2",
"object.entries": "^1.1.8",
"warning": "^4.0.3"
},
"devDependencies": {
"array.prototype.foreach": "^1.0.7",
"aud": "^2.0.4",
"chai": "^3.5.0",
"docco": "^0.7.0",
Expand Down

0 comments on commit 78022fe

Please sign in to comment.