Browse Source

Stop using various lodash functions

JC Brand 1 year ago
parent
commit
8db4e432ef

+ 4 - 6
src/headless/plugins/muc/affiliations/utils.js

@@ -3,8 +3,6 @@
  * @license Mozilla Public License (MPLv2)
  */
 import { AFFILIATIONS } from '@converse/headless/plugins/muc/index.js';
-import difference from 'lodash-es/difference';
-import indexOf from 'lodash-es/indexOf';
 import { _converse, api, converse, log } from '@converse/headless';
 import { parseMemberListIQ } from '../parsers.js';
 
@@ -90,7 +88,7 @@ export function setAffiliations (muc_jid, users) {
  * Related ticket: https://issues.prosody.im/345
  *
  * @param { ('outcast'|'member'|'admin'|'owner') } affiliation - The affiliation to be set
- * @param { String|Array<String> } jids - The JID(s) of the MUCs in which the
+ * @param { String|Array<String> } muc_jids - The JID(s) of the MUCs in which the
  *  affiliations need to be set.
  * @param { object } members - A map of jids, affiliations and
  *  optionally reasons. Only those entries with the
@@ -158,19 +156,19 @@ export function computeAffiliationsDelta (exclude_existing, remove_absentees, ne
     const new_jids = new_list.map(o => o.jid);
     const old_jids = old_list.map(o => o.jid);
     // Get the new affiliations
-    let delta = difference(new_jids, old_jids).map(jid => new_list[indexOf(new_jids, jid)]);
+    let delta = new_jids.filter(jid => !old_jids.includes(jid)).map(jid => new_list[new_jids.indexOf(jid)]);
     if (!exclude_existing) {
         // Get the changed affiliations
         delta = delta.concat(
             new_list.filter(item => {
-                const idx = indexOf(old_jids, item.jid);
+                const idx = old_jids.indexOf(item.jid);
                 return idx >= 0 ? item.affiliation !== old_list[idx].affiliation : false;
             })
         );
     }
     if (remove_absentees) {
         // Get the removed affiliations
-        delta = delta.concat(difference(old_jids, new_jids).map(jid => ({ 'jid': jid, 'affiliation': 'none' })));
+        delta = delta.concat(old_jids.filter(jid => !new_jids.includes(jid)).map(jid => ({ 'jid': jid, 'affiliation': 'none' })));
     }
     return delta;
 }

+ 1 - 2
src/headless/plugins/roster/presence.js

@@ -1,4 +1,3 @@
-import isNaN from "lodash-es/isNaN";
 import { Collection } from "@converse/skeletor/src/collection";
 import { Model } from '@converse/skeletor/src/model.js';
 import { converse } from "@converse/headless";
@@ -62,7 +61,7 @@ class Presence extends Model {
         const resource = this.resources.get(name);
         const settings = {
             name,
-            'priority': isNaN(parseInt(priority, 10)) ? 0 : parseInt(priority, 10),
+            'priority': Number.isNaN(parseInt(priority, 10)) ? 0 : parseInt(priority, 10),
             'show': presence.querySelector('show')?.textContent ?? 'online',
             'timestamp': delay ? dayjs(delay.getAttribute('stamp')).toISOString() : (new Date()).toISOString()
         };

+ 1 - 2
src/headless/plugins/status/status.js

@@ -1,4 +1,3 @@
-import isNaN from 'lodash-es/isNaN';
 import isObject from 'lodash-es/isObject';
 import { Model } from '@converse/skeletor/src/model.js';
 import { _converse, api, converse } from '@converse/headless';
@@ -72,7 +71,7 @@ export default class XMPPStatus extends Model {
         if (status_message) presence.c('status').t(status_message).up();
 
         const priority = api.settings.get("priority");
-        presence.c('priority').t(isNaN(Number(priority)) ? 0 : priority).up();
+        presence.c('priority').t(Number.isNaN(Number(priority)) ? 0 : priority).up();
 
         const { idle, idle_seconds } = _converse;
         if (idle) {

+ 2 - 4
src/headless/utils/core.js

@@ -5,9 +5,7 @@
  */
 import DOMPurify from 'dompurify';
 import _converse from '@converse/headless/shared/_converse.js';
-import compact from "lodash-es/compact";
 import isObject from "lodash-es/isObject";
-import last from "lodash-es/last";
 import log from '../log.js';
 import sizzle from "sizzle";
 import { Model } from '@converse/skeletor/src/model.js';
@@ -177,7 +175,7 @@ u.getLongestSubstring = function (string, candidates) {
 
 export function isValidJID (jid) {
     if (typeof jid === 'string') {
-        return compact(jid.split('@')).length === 2 && !jid.startsWith('@') && !jid.endsWith('@');
+        return jid.split('@').filter(s => !!s).length === 2 && !jid.startsWith('@') && !jid.endsWith('@');
     }
     return false;
 }
@@ -464,7 +462,7 @@ u.isMentionBoundary = (s) => s !== '@' && RegExp(`(\\p{Z}|\\p{P})`, 'u').test(s)
 
 u.replaceCurrentWord = function (input, new_value) {
     const caret = input.selectionEnd || undefined;
-    const current_word = last(input.value.slice(0, caret).split(/\s/));
+    const current_word = input.value.slice(0, caret).split(/\s/).pop();
     const value = input.value;
     const mention_boundary = u.isMentionBoundary(current_word[0]) ? current_word[0] : '';
     input.value = value.slice(0, caret - current_word.length) + mention_boundary + `${new_value} ` + value.slice(caret);

+ 1 - 3
src/plugins/bookmark-views/utils.js

@@ -1,5 +1,3 @@
-import invokeMap from 'lodash-es/invokeMap';
-import { Model } from '@converse/skeletor/src/model.js';
 import { __ } from 'i18n';
 import { _converse, api, converse } from '@converse/headless';
 import { checkBookmarksSupport } from '@converse/headless/plugins/bookmarks/utils';
@@ -29,7 +27,7 @@ export async function removeBookmarkViaEvent (ev) {
     const jid = ev.currentTarget.getAttribute('data-room-jid');
     const result = await api.confirm(__('Are you sure you want to remove the bookmark "%1$s"?', name));
     if (result) {
-        invokeMap(_converse.bookmarks.where({ jid }), Model.prototype.destroy);
+        _converse.bookmarks.where({ jid }).forEach(b => b.destroy());
     }
 }
 

+ 2 - 3
src/plugins/muc-views/modals/muc-list.js

@@ -1,5 +1,4 @@
 import BaseModal from "plugins/modal/modal.js";
-import head from "lodash-es/head";
 import tplMUCDescription from "../templates/muc-description.js";
 import tplMUCList from "../templates/muc-list.js";
 import tplSpinner from "templates/spinner.js";
@@ -24,8 +23,8 @@ function insertRoomInfo (el, stanza) {
         'beforeEnd',
         u.getElementFromTemplateResult(tplMUCDescription({
             'jid': stanza.getAttribute('from'),
-            'desc': head(sizzle('field[var="muc#roominfo_description"] value', stanza))?.textContent,
-            'occ': head(sizzle('field[var="muc#roominfo_occupants"] value', stanza))?.textContent,
+            'desc': sizzle('field[var="muc#roominfo_description"] value', stanza).shift()?.textContent,
+            'occ': sizzle('field[var="muc#roominfo_occupants"] value', stanza).shift()?.textContent,
             'hidden': sizzle('feature[var="muc_hidden"]', stanza).length,
             'membersonly': sizzle('feature[var="muc_membersonly"]', stanza).length,
             'moderated': sizzle('feature[var="muc_moderated"]', stanza).length,

+ 5 - 6
src/plugins/omemo/store.js

@@ -1,6 +1,4 @@
 /* global libsignal */
-import difference from 'lodash-es/difference';
-import invokeMap from 'lodash-es/invokeMap';
 import range from 'lodash-es/range';
 import omit from 'lodash-es/omit';
 import { Model } from '@converse/skeletor/src/model.js';
@@ -185,10 +183,11 @@ class OMEMOStore extends Model {
     }
 
     async generateMissingPreKeys () {
-        const missing_keys = difference(
-            invokeMap(range(0, _converse.NUM_PREKEYS), Number.prototype.toString),
-            Object.keys(this.getPreKeys())
-        );
+        const prekeyIds = Object.keys(this.getPreKeys());
+        const missing_keys = range(0, _converse.NUM_PREKEYS)
+            .map(id => id.toString())
+            .filter(id => !prekeyIds.includes(id));
+
         if (missing_keys.length < 1) {
             log.warn('No missing prekeys to generate for our own device');
             return Promise.resolve();

+ 2 - 4
src/plugins/omemo/utils.js

@@ -1,6 +1,4 @@
 /* global libsignal */
-import concat from 'lodash-es/concat';
-import difference from 'lodash-es/difference';
 import tplAudio from 'templates/audio.js';
 import tplFile from 'templates/file.js';
 import tplImage from 'templates/image.js';
@@ -581,7 +579,7 @@ async function updateDevicesFromStanza (stanza) {
     const jid = stanza.getAttribute('from');
     const devicelist = await api.omemo.devicelists.get(jid, true);
     const devices = devicelist.devices;
-    const removed_ids = difference(devices.pluck('id'), device_ids);
+    const removed_ids = devices.pluck('id').filter(id => !device_ids.includes(id));
 
     removed_ids.forEach(id => {
         if (jid === _converse.bare_jid && id === _converse.omemo_store.get('device_id')) {
@@ -781,7 +779,7 @@ async function getBundlesAndBuildSessions (chatbox) {
     let devices;
     if (chatbox.get('type') === _converse.CHATROOMS_TYPE) {
         const collections = await Promise.all(chatbox.occupants.map(o => getDevicesForContact(o.get('jid'))));
-        devices = collections.reduce((a, b) => concat(a, b.models), []);
+        devices = collections.reduce((a, b) => a.concat(b.models), []);
     } else if (chatbox.get('type') === _converse.PRIVATE_CHAT_TYPE) {
         const their_devices = await getDevicesForContact(chatbox.get('jid'));
         if (their_devices.length === 0) {

+ 1 - 2
src/plugins/rosterview/modals/add-contact.js

@@ -1,7 +1,6 @@
 import 'shared/autocomplete/index.js';
 import BaseModal from "plugins/modal/modal.js";
 import api from '@converse/headless/shared/api';
-import compact from 'lodash-es/compact';
 import debounce from 'lodash-es/debounce';
 import tplAddContactModal from "./templates/add-contact.js";
 import { Strophe } from 'strophe.js';
@@ -113,7 +112,7 @@ export default class AddContactModal extends BaseModal {
 
     validateSubmission (jid) {
         const el = this.querySelector('.invalid-feedback');
-        if (!jid || compact(jid.split('@')).length < 2) {
+        if (!jid || jid.split('@').filter(s => !!s).length < 2) {
             addClass('is-invalid', this.querySelector('input[name="jid"]'));
             addClass('d-block', el);
             return false;