Underscore.js _.map() to native Array.map() (optional with Object.entries/values)

This commit is contained in:
djmaze 2020-07-22 20:09:31 +02:00
parent 032fa8c736
commit a82575a830
27 changed files with 68 additions and 78 deletions

View file

@ -40,7 +40,7 @@ class AdminApp extends AbstractApp {
DomainStore.domains.loading(false);
if (StorageResultType.Success === result && data && data.Result) {
DomainStore.domains(
_.map(data.Result, ([enabled, alias], name) => ({
Object.entries(data.Result).map(([enabled, alias], name) => ({
name: name,
disabled: ko.observable(!enabled),
alias: alias,
@ -57,7 +57,7 @@ class AdminApp extends AbstractApp {
PluginStore.plugins.loading(false);
if (StorageResultType.Success === result && data && data.Result) {
PluginStore.plugins(
_.map(data.Result, (item) => ({
data.Result.map(item => ({
name: item.Name,
disabled: ko.observable(!item.Enabled),
configured: ko.observable(!!item.Configured)
@ -87,7 +87,7 @@ class AdminApp extends AbstractApp {
if (isArray(data.Result.List)) {
list = _.compact(
_.map(data.Result.List, (item) => {
data.Result.List.map(item => {
if (item) {
item.loading = ko.observable(!isUnd(loading[item.file]));
return 'core' === item.type && !item.canBeInstalled ? null : item;

View file

@ -520,7 +520,7 @@ class AppUser extends AbstractApp {
.getKeyId()
.toHex()
.toLowerCase(),
_.uniq(_.compact(_.map(oItem.getKeyIds(), (item) => (item && item.toHex ? item.toHex() : null)))),
_.uniq(_.compact(oItem.getKeyIds().map(item => (item && item.toHex ? item.toHex() : null)))),
aUsers,
aEmails,
oItem.isPrivate(),
@ -590,9 +590,8 @@ class AppUser extends AbstractApp {
delegateRunOnDestroy(AccountStore.accounts());
AccountStore.accounts(
_.map(
oData.Result.Accounts,
(sValue) => new AccountModel(sValue, sValue !== parentEmail, counts[sValue] || 0)
oData.Result.Accounts.map(
sValue => new AccountModel(sValue, sValue !== parentEmail, counts[sValue] || 0)
)
);
}
@ -606,7 +605,7 @@ class AppUser extends AbstractApp {
delegateRunOnDestroy(IdentityStore.identities());
IdentityStore.identities(
_.map(oData.Result.Identities, (identityData) => {
oData.Result.Identities.map(identityData => {
const id = pString(identityData.Id),
email = pString(identityData.Email),
identity = new IdentityModel(id, email);
@ -636,7 +635,7 @@ class AppUser extends AbstractApp {
TemplateStore.templates(
_.compact(
_.map(data.Result.Templates, (templateData) => {
data.Result.Templates.map(templateData => {
const template = new TemplateModel();
return template.parse(templateData) ? template : null;
})
@ -820,7 +819,7 @@ class AppUser extends AbstractApp {
messages = MessageStore.messageListChecked();
}
rootUids = _.uniq(_.compact(_.map(messages, (oMessage) => (oMessage && oMessage.uid ? oMessage.uid : null))));
rootUids = _.uniq(_.compact(messages.map(oMessage => (oMessage && oMessage.uid ? oMessage.uid : null))));
if ('' !== sFolderFullNameRaw && 0 < rootUids.length) {
switch (iSetAction) {
@ -881,7 +880,7 @@ class AppUser extends AbstractApp {
Remote.suggestions((result, data) => {
if (StorageResultType.Success === result && data && isArray(data.Result)) {
autocompleteCallback(
_.compact(_.map(data.Result, (item) => (item && item[0] ? new EmailModel(item[0], item[1]) : null)))
_.compact(data.Result.map(item => (item && item[0] ? new EmailModel(item[0], item[1]) : null)))
);
} else if (StorageResultType.Abort !== result) {
autocompleteCallback([]);

View file

@ -1,4 +1,3 @@
import _ from '_';
import ko from 'ko';
import { isUnd } from 'Common/Utils';
import { AbstractComponent } from 'Component/Abstract';
@ -21,7 +20,7 @@ class AbstractRadio extends AbstractComponent {
this.readOnly = isUnd(params.readOnly) ? false : !!params.readOnly;
if (params.values) {
this.values(_.map(params.values, (label, value) => ({ label: label, value: value })));
this.values(Object.entries(params.values).map((label, value) => ({ label: label, value: value })));
}
this.click = this.click.bind(this);

15
dev/External/ko.js vendored
View file

@ -824,14 +824,13 @@ ko.bindingHandlers.emailsTags = {
return null;
},
parseHook: (input) =>
_.map(
_.flatten(
_.map(input, (inputValue) => {
const values = EmailModel.parseEmailLine(inputValue);
return values.length ? values : inputValue;
})
),
(item) => (_.isObject(item) ? [item.toLine(false), item] : [item, null])
_.flatten(
input.map(inputValue => {
const values = EmailModel.parseEmailLine(inputValue);
return values.length ? values : inputValue;
})
).map(
item => (_.isObject(item) ? [item.toLine(false), item] : [item, null])
),
change: (event) => {
$el.data('EmailsTagsValue', event.target.value);

View file

@ -187,7 +187,7 @@ export const staticCombinedIconClass = (data) => {
if (isNonEmptyArray(data)) {
result = 'icon-attachment';
types = _.uniq(_.compact(_.map(data, (item) => (item ? staticFileType(getFileExtension(item[0]), item[1]) : ''))));
types = _.uniq(_.compact(data.map(item => (item ? staticFileType(getFileExtension(item[0]), item[1]) : ''))));
if (types && 1 === types.length && types[0]) {
switch (types[0]) {

View file

@ -185,7 +185,7 @@ class EmailModel {
const parsedResult = addressparser(line);
if (isNonEmptyArray(parsedResult)) {
return _.compact(
_.map(parsedResult, (item) =>
parsedResult.map(item =>
item.address ? new EmailModel(item.address.replace(/^[<]+(.*)[>]+$/g, '$1'), item.name || '') : null
)
);

View file

@ -190,7 +190,7 @@ class FilterModel extends AbstractModel {
Enabled: this.enabled() ? '1' : '0',
Name: this.name(),
ConditionsType: this.conditionsType(),
Conditions: _.map(this.conditions(), (item) => item.toJson()),
Conditions: this.conditions().map(item => item.toJson()),
ActionValue: this.actionValue(),
ActionValueSecond: this.actionValueSecond(),
@ -231,7 +231,7 @@ class FilterModel extends AbstractModel {
if (isNonEmptyArray(json.Conditions)) {
this.conditions(
_.compact(
_.map(json.Conditions, (aData) => {
json.Conditions.map(aData => {
const filterCondition = new FilterConditionModel();
return filterCondition && filterCondition.parse(aData) ? filterCondition : null;
})
@ -282,7 +282,7 @@ class FilterModel extends AbstractModel {
filter.actionKeep(this.actionKeep());
filter.actionNoStop(this.actionNoStop());
filter.conditions(_.map(this.conditions(), (item) => item.cloneSelf()));
filter.conditions(this.conditions().map(item => item.cloneSelf()));
return filter;
}

View file

@ -202,8 +202,7 @@ class MessageModel extends AbstractModel {
getEmails(properties) {
return _.compact(
_.uniq(
_.map(
_.reduce(properties, (carry, property) => carry.concat(this[property]), []),
_.reduce(properties, (carry, property) => carry.concat(this[property]), []).map(
(oItem) => (oItem ? oItem.email : '')
)
)
@ -628,7 +627,7 @@ class MessageModel extends AbstractModel {
* @returns {string}
*/
attachmentsToStringLine() {
const attachLines = _.map(this.attachments(), (item) => item.fileName + ' (' + item.friendlySize + ')');
const attachLines = this.attachments().map(item => item.fileName + ' (' + item.friendlySize + ')');
return attachLines && 0 < attachLines.length ? attachLines.join(', ') : '';
}

View file

@ -1,5 +1,3 @@
import _ from '_';
import { pString, pInt, isArray, trim, boolToAjax } from 'Common/Utils';
import {
@ -247,7 +245,7 @@ class RemoteUserAjax extends AbstractAjaxRemote {
this.defaultRequest(fCallback, 'FiltersSave', {
'Raw': raw,
'RawIsActive': boolToAjax(isRawIsActive),
'Filters': _.map(filters, (item) => item.toJson())
'Filters': filters.map(item => item.toJson())
});
}

View file

@ -48,7 +48,7 @@ class ContactsAdminSettings {
this.contactsTypes = ko.observableArray([]);
this.contactsTypesOptions = ko.computed(() =>
_.map(this.contactsTypes(), (value) => {
this.contactsTypes().map(value => {
const disabled = supportedTypes.includes(value);
return {
'id': value,

View file

@ -63,7 +63,7 @@ class GeneralAdminSettings {
: '';
this.themesOptions = ko.computed(() =>
_.map(this.themes(), (theme) => ({ optValue: theme, optText: convertThemeName(theme) }))
this.themes().map(theme => ({ optValue: theme, optText: convertThemeName(theme) }))
);
this.languageFullName = ko.computed(() => convertLangName(this.language()));

View file

@ -114,7 +114,7 @@ class FiltersUserSettings {
this.filters(
_.compact(
_.map(data.Result.Filters, (aItem) => {
data.Result.Filters.map(aItem => {
const filter = new FilterModel();
return filter && filter.parse(aItem) ? filter : null;
})

View file

@ -1,5 +1,4 @@
import window from 'window';
import _ from '_';
import ko from 'ko';
import Jua from 'Jua';
@ -63,7 +62,7 @@ class ThemesUserSettings {
const currentTheme = this.theme();
this.themesObjects(
_.map(this.themes(), (theme) => ({
this.themes().map(theme => ({
name: theme,
nameDisplay: convertThemeName(theme),
selected: ko.observable(theme === currentTheme),

View file

@ -17,7 +17,7 @@ class AccountUserStore {
}
computers() {
this.accountsEmails = ko.computed(() => _.compact(_.map(this.accounts(), (item) => (item ? item.email : null))));
this.accountsEmails = ko.computed(() => _.compact(this.accounts().map(item => (item ? item.email : null))));
this.accountsUnreadCount = ko.computed(() => 0);
// this.accountsUnreadCount = ko.computed(() => {

View file

@ -95,7 +95,7 @@ class FolderUserStore {
});
this.folderListSystem = ko.computed(() =>
_.compact(_.map(this.folderListSystemNames(), (name) => getFolderFromCacheList(name)))
_.compact(this.folderListSystemNames().map(name => getFolderFromCacheList(name)))
);
this.folderMenuForMove = ko.computed(() =>

View file

@ -6,7 +6,7 @@ class IdentityUserStore {
this.identities = ko.observableArray([]);
this.identities.loading = ko.observable(false).extend({ throttle: 100 });
this.identitiesIDS = ko.computed(() => _.compact(_.map(this.identities(), (item) => (item ? item.id : null))));
this.identitiesIDS = ko.computed(() => _.compact(this.identities().map(item => (item ? item.id : null))));
}
}

View file

@ -294,7 +294,7 @@ class MessageUserStore {
* @param {boolean=} copy = false
*/
removeMessagesFromList(fromFolderFullNameRaw, uidForRemove, toFolderFullNameRaw = '', copy = false) {
uidForRemove = _.map(uidForRemove, (mValue) => pInt(mValue));
uidForRemove = uidForRemove.map(mValue => pInt(mValue));
let unseenCount = 0,
messageList = this.messageList(),
@ -709,7 +709,7 @@ class MessageUserStore {
* @returns {string}
*/
calculateMessageListHash(list) {
return _.map(list, (message) => '' + message.hash + '_' + message.threadsLen() + '_' + message.flagHash()).join(
return list.map(message => '' + message.hash + '_' + message.threadsLen() + '_' + message.flagHash()).join(
'|'
);
}

View file

@ -44,7 +44,7 @@ class PgpUserStore {
findPublicKeysByEmail(email) {
return _.compact(
_.flatten(
_.map(this.openpgpkeysPublic(), (item) => {
this.openpgpkeysPublic().map(item => {
const key = item && item.emails.includes(email) ? item : null;
return key ? key.getNativeKeys() : [null];
}),
@ -56,7 +56,7 @@ class PgpUserStore {
findPublicKeysBySigningKeyIds(signingKeyIds) {
return _.compact(
_.flatten(
_.map(signingKeyIds, (id) => {
signingKeyIds.map(id => {
const key = id && id.toHex ? this.findPublicKeyByHex(id.toHex()) : null;
return key ? key.getNativeKeys() : [null];
}),
@ -69,7 +69,7 @@ class PgpUserStore {
let result = isArray(encryptionKeyIds)
? _.compact(
_.flatten(
_.map(encryptionKeyIds, (id) => {
encryptionKeyIds.map(id => {
const key = id && id.toHex ? this.findPrivateKeyByHex(id.toHex()) : null;
return key ? (returnWrapKeys ? [key] : key.getNativeKeys()) : [null];
}),
@ -82,13 +82,13 @@ class PgpUserStore {
result = _.uniq(
_.compact(
_.flatten(
_.map(recipients, (sEmail) => {
recipients.map(sEmail => {
const keys = sEmail ? this.findAllPrivateKeysByEmailNotNative(sEmail) : null;
return keys
? returnWrapKeys
? keys
: _.flatten(
_.map(keys, (key) => key.getNativeKeys()),
keys.map(key => key.getNativeKeys()),
true
)
: [null];
@ -297,7 +297,7 @@ class PgpUserStore {
} else if (validPrivateKey) {
const keyIds = isNonEmptyArray(signingKeyIds) ? signingKeyIds : null,
additional = keyIds
? _.compact(_.map(keyIds, (item) => (item && item.toHex ? item.toHex() : null))).join(', ')
? _.compact(keyIds.map(item => (item && item.toHex ? item.toHex() : null))).join(', ')
: '';
store.controlsHelper(
@ -354,7 +354,7 @@ class PgpUserStore {
} else {
const keyIds = isNonEmptyArray(signingKeyIds) ? signingKeyIds : null,
additional = keyIds
? _.compact(_.map(keyIds, (item) => (item && item.toHex ? item.toHex() : null))).join(', ')
? _.compact(keyIds.map(item => (item && item.toHex ? item.toHex() : null))).join(', ')
: '';
store.controlsHelper(

View file

@ -16,7 +16,7 @@ class TemplateUserStore {
subscribers() {
this.templates.subscribe((list) => {
this.templatesNames(_.compact(_.map(list, (item) => (item ? item.name : null))));
this.templatesNames(_.compact(list.map(item => (item ? item.name : null))));
});
// this.templatesNames.subscribe((aList) => {

View file

@ -257,7 +257,7 @@ class ComposePopupView extends AbstractViewNext {
this.identities = IdentityStore.identities;
this.identitiesOptions = ko.computed(() =>
_.map(IdentityStore.identities(), (item) => ({
IdentityStore.identities().map(item => ({
'item': item,
'optValue': item.id(),
'optText': item.formattedName()
@ -541,7 +541,7 @@ class ComposePopupView extends AbstractViewNext {
emailsSource(oData, fResponse) {
getApp().getAutocomplete(oData.term, (aData) => {
fResponse(_.map(aData, (oEmailItem) => oEmailItem.toLine(false)));
fResponse(aData.map(oEmailItem => oEmailItem.toLine(false)));
});
}
@ -859,7 +859,7 @@ class ComposePopupView extends AbstractViewNext {
addEmailsTo(fKoValue, emails) {
if (isNonEmptyArray(emails)) {
const value = trim(fKoValue()),
values = _.uniq(_.compact(_.map(emails, (item) => (item ? item.toLine(false) : null))));
values = _.uniq(_.compact(emails.map(item => (item ? item.toLine(false) : null))));
fKoValue(value + ('' === value ? '' : ', ') + trim(values.join(', ')));
}
@ -873,7 +873,7 @@ class ComposePopupView extends AbstractViewNext {
*/
emailArrayToStringLineHelper(aList, bFriendly) {
bFriendly = !!bFriendly;
return _.map(aList, (item) => item.toLine(bFriendly)).join(', ');
return aList.map(item => item.toLine(bFriendly)).join(', ');
}
/**
@ -1605,9 +1605,8 @@ class ComposePopupView extends AbstractViewNext {
* @returns {Array}
*/
getAttachmentsDownloadsForUpload() {
return _.map(
this.attachments().filter(item => item && '' === item.tempName()),
(item) => item.id
return this.attachments().filter(item => item && '' === item.tempName()).map(
item => item.id
);
}

View file

@ -44,14 +44,14 @@ class ComposeOpenPgpPopupView extends AbstractViewNext {
this.signKey = ko.observable(null);
this.encryptKeys = ko.observableArray([]);
this.encryptKeysView = ko.computed(() => _.compact(_.map(this.encryptKeys(), (oKey) => (oKey ? oKey.key : null))));
this.encryptKeysView = ko.computed(() => _.compact(this.encryptKeys().map(oKey => (oKey ? oKey.key : null))));
this.privateKeysOptions = ko.computed(() => {
const opts = _.map(PgpStore.openpgpkeysPrivate(), (oKey, iIndex) => {
const opts = PgpStore.openpgpkeysPrivate().map((oKey, iIndex) => {
if (this.signKey() && this.signKey().key.id === oKey.id) {
return null;
}
return _.map(oKey.users, (user) => ({
return oKey.users.map(user => ({
'id': oKey.guid,
'name': '(' + oKey.id.substr(KEY_NAME_SUBSTR).toUpperCase() + ') ' + user,
'key': oKey,
@ -63,11 +63,11 @@ class ComposeOpenPgpPopupView extends AbstractViewNext {
});
this.publicKeysOptions = ko.computed(() => {
const opts = _.map(PgpStore.openpgpkeysPublic(), (oKey, index) => {
const opts = PgpStore.openpgpkeysPublic().map((oKey, index) => {
if (this.encryptKeysView().includes(oKey)) {
return null;
}
return _.map(oKey.users, (user) => ({
return oKey.users.map(user => ({
'id': oKey.guid,
'name': '(' + oKey.id.substr(KEY_NAME_SUBSTR).toUpperCase() + ') ' + user,
'key': oKey,
@ -356,7 +356,7 @@ class ComposeOpenPgpPopupView extends AbstractViewNext {
rec = rec.join(', ').split(',');
rec = _.compact(
_.map(rec, (value) => {
rec.map(value => {
email.clear();
email.parse(trim(value));
return '' === email.email ? false : email.email;
@ -386,10 +386,10 @@ class ComposeOpenPgpPopupView extends AbstractViewNext {
_.uniq(
_.compact(
_.flatten(
_.map(rec, (recEmail) => {
rec.map(recEmail => {
const keys = PgpStore.findAllPublicKeysByEmailNotNative(recEmail);
return keys
? _.map(keys, (publicKey) => ({
? keys.map(publicKey => ({
'empty': !publicKey,
'selected': ko.observable(!!publicKey),
'removable': ko.observable(

View file

@ -175,7 +175,7 @@ class ContactsPopupView extends AbstractViewNext {
});
this.contactsCheckedOrSelectedUids = ko.computed(() =>
_.map(this.contactsCheckedOrSelected(), (contact) => contact.idContact)
this.contactsCheckedOrSelected().map(contact => contact.idContact)
);
this.selector = new Selector(
@ -202,7 +202,7 @@ class ContactsPopupView extends AbstractViewNext {
this.watchDirty = ko.observable(false);
this.watchHash = ko.observable(false);
this.viewHash = ko.computed(() => '' + _.map(this.viewProperties(), (oItem) => oItem.value()).join(''));
this.viewHash = ko.computed(() => '' + this.viewProperties().map(oItem => oItem.value()).join(''));
// this.saveCommandDebounce = _.debounce(this.saveCommand.bind(this), 1000);
@ -240,7 +240,7 @@ class ContactsPopupView extends AbstractViewNext {
const aC = this.contactsCheckedOrSelected();
if (isNonEmptyArray(aC)) {
aE = _.map(aC, (oItem) => {
aE = aC.map(oItem => {
if (oItem) {
const data = oItem.getNameAndEmailHelper(),
email = data ? new EmailModel(data[0], data[1]) : null;
@ -591,7 +591,7 @@ class ContactsPopupView extends AbstractViewNext {
if (StorageResultType.Success === result && data && data.Result && data.Result.List) {
if (isNonEmptyArray(data.Result.List)) {
list = _.map(data.Result.List, (item) => {
list = data.Result.List.map(item => {
const contact = new ContactModel();
return contact.parse(item) ? contact : null;
});

View file

@ -1,4 +1,3 @@
import _ from '_';
import ko from 'ko';
import { StorageResultType, Notification } from 'Common/Enums';
@ -33,7 +32,7 @@ class DomainAliasPopupView extends AbstractViewNext {
this.domains = DomainStore.domainsWithoutAliases;
this.domainsOptions = ko.computed(() =>
_.map(this.domains(), (item) => ({ optValue: item.name, optText: item.name }))
this.domains().map(item => ({ optValue: item.name, optText: item.name }))
);
this.canBeSaved = ko.computed(() => !this.saving() && '' !== this.name() && '' !== this.alias());

View file

@ -1,4 +1,3 @@
import _ from '_';
import ko from 'ko';
import { convertLangName } from 'Common/Utils';
@ -22,7 +21,7 @@ class LanguagesPopupView extends AbstractViewNext {
this.languages = ko.computed(() => {
const userLanguage = this.userLanguage();
return _.map(this.langs(), (language) => ({
return this.langs().map(language => ({
key: language,
user: language === userLanguage,
selected: ko.observable(false),

View file

@ -88,7 +88,7 @@ class PluginPopupView extends AbstractViewNext {
const config = oPlugin.Config;
if (isNonEmptyArray(config)) {
this.configures(
_.map(config, (item) => ({
config.map(item => ({
'value': ko.observable(item[0]),
'placeholder': ko.observable(item[6]),
'Name': item[1],

View file

@ -651,7 +651,7 @@ class MessageListMailBoxUserView extends AbstractViewNext {
flagMessages(currentMessage) {
const checked = this.messageListCheckedOrSelected();
if (currentMessage) {
const checkedUids = _.map(checked, (message) => message.uid);
const checkedUids = checked.map(message => message.uid);
if (0 < checkedUids.length && checkedUids.includes(currentMessage.uid)) {
this.setAction(
currentMessage.folderFullNameRaw,

View file

@ -453,7 +453,7 @@ class MessageViewMailBoxUserView extends AbstractViewNext {
// aTo = [],
// EmailModel = require('Model/Email').default,
// fParseEmailLine = function(sLine) {
// return sLine ? _.compact(_.map([window.decodeURIComponent(sLine)], function(sItem) {
// return sLine ? _.compact([window.decodeURIComponent(sLine)].map(sItem => {
// var oEmailModel = new EmailModel();
// oEmailModel.parse(sItem);
// return '' !== oEmailModel.email ? oEmailModel : null;
@ -478,7 +478,7 @@ class MessageViewMailBoxUserView extends AbstractViewNext {
const div = $('<div>'),
dynamicEls = _.compact(
_.map(this.message().attachments(), (item) => {
this.message().attachments().map(item => {
if (item && !item.isLinked && item.isImage()) {
if (item === attachment) {
index = listIndex;
@ -885,7 +885,7 @@ class MessageViewMailBoxUserView extends AbstractViewNext {
getAttachmentsHashes() {
const atts = this.message() ? this.message().attachments() : [];
return _.compact(_.map(atts, (item) => (item && !item.isLinked && item.checked() ? item.download : '')));
return _.compact(atts.map(item => (item && !item.isLinked && item.checked() ? item.download : '')));
}
downloadAsZip() {