diff --git a/spec/integ/matrix-client-crypto.spec.js b/spec/integ/matrix-client-crypto.spec.js index 3ca72c320..c86e0e60e 100644 --- a/spec/integ/matrix-client-crypto.spec.js +++ b/spec/integ/matrix-client-crypto.spec.js @@ -30,7 +30,6 @@ import '../olm-loader'; import {logger} from '../../src/logger'; import * as testUtils from "../test-utils"; -import * as utils from "../../src/utils"; import {TestClient} from "../TestClient"; import {CRYPTO_ENABLED} from "../../src/client"; @@ -244,7 +243,7 @@ function bobSendsReplyMessage() { function expectAliSendMessageRequest() { return expectSendMessageRequest(aliTestClient.httpBackend).then(function(content) { aliMessages.push(content); - expect(utils.keys(content.ciphertext)).toEqual([bobTestClient.getDeviceKey()]); + expect(Object.keys(content.ciphertext)).toEqual([bobTestClient.getDeviceKey()]); const ciphertext = content.ciphertext[bobTestClient.getDeviceKey()]; expect(ciphertext).toBeTruthy(); return ciphertext; @@ -261,7 +260,7 @@ function expectBobSendMessageRequest() { bobMessages.push(content); const aliKeyId = "curve25519:" + aliDeviceId; const aliDeviceCurve25519Key = aliTestClient.deviceKeys.keys[aliKeyId]; - expect(utils.keys(content.ciphertext)).toEqual([aliDeviceCurve25519Key]); + expect(Object.keys(content.ciphertext)).toEqual([aliDeviceCurve25519Key]); const ciphertext = content.ciphertext[aliDeviceCurve25519Key]; expect(ciphertext).toBeTruthy(); return ciphertext; diff --git a/spec/integ/megolm-integ.spec.js b/spec/integ/megolm-integ.spec.js index c8b117953..698e030f9 100644 --- a/spec/integ/megolm-integ.spec.js +++ b/spec/integ/megolm-integ.spec.js @@ -16,7 +16,6 @@ limitations under the License. */ import anotherjson from "another-json"; -import * as utils from "../../src/utils"; import * as testUtils from "../test-utils"; import {TestClient} from "../TestClient"; import {logger} from "../../src/logger"; @@ -32,7 +31,7 @@ const ROOM_ID = "!room:id"; */ function createOlmSession(olmAccount, recipientTestClient) { return recipientTestClient.awaitOneTimeKeyUpload().then((keys) => { - const otkId = utils.keys(keys)[0]; + const otkId = Object.keys(keys)[0]; const otk = keys[otkId]; const session = new global.Olm.Session(); @@ -257,7 +256,7 @@ describe("megolm", function() { const testOneTimeKeys = JSON.parse(testOlmAccount.one_time_keys()); testOlmAccount.mark_keys_as_published(); - const keyId = utils.keys(testOneTimeKeys.curve25519)[0]; + const keyId = Object.keys(testOneTimeKeys.curve25519)[0]; const oneTimeKey = testOneTimeKeys.curve25519[keyId]; const keyResult = { 'key': oneTimeKey, diff --git a/spec/unit/utils.spec.js b/spec/unit/utils.spec.js index 686912913..d44376112 100644 --- a/spec/unit/utils.spec.js +++ b/spec/unit/utils.spec.js @@ -26,40 +26,6 @@ describe("utils", function() { }); }); - describe("forEach", function() { - it("should be invoked for each element", function() { - const arr = []; - utils.forEach([55, 66, 77], function(element) { - arr.push(element); - }); - expect(arr).toEqual([55, 66, 77]); - }); - }); - - describe("findElement", function() { - it("should find only 1 element if there is a match", function() { - const matchFn = function() { - return true; - }; - const arr = [55, 66, 77]; - expect(utils.findElement(arr, matchFn)).toEqual(55); - }); - it("should be able to find in reverse order", function() { - const matchFn = function() { - return true; - }; - const arr = [55, 66, 77]; - expect(utils.findElement(arr, matchFn, true)).toEqual(77); - }); - it("should find nothing if the function never returns true", function() { - const matchFn = function() { - return false; - }; - const arr = [55, 66, 77]; - expect(utils.findElement(arr, matchFn)).toBeFalsy(); - }); - }); - describe("removeElement", function() { it("should remove only 1 element if there is a match", function() { const matchFn = function() { @@ -103,20 +69,6 @@ describe("utils", function() { }); }); - describe("isArray", function() { - it("should return true for arrays", function() { - expect(utils.isArray([])).toBe(true); - expect(utils.isArray([5, 3, 7])).toBe(true); - - expect(utils.isArray()).toBe(false); - expect(utils.isArray(null)).toBe(false); - expect(utils.isArray({})).toBe(false); - expect(utils.isArray("foo")).toBe(false); - expect(utils.isArray(555)).toBe(false); - expect(utils.isArray(function() {})).toBe(false); - }); - }); - describe("checkObjectHasKeys", function() { it("should throw for missing keys", function() { expect(function() { diff --git a/src/client.js b/src/client.js index 5d9d1d2cf..12bfc6a33 100644 --- a/src/client.js +++ b/src/client.js @@ -3976,9 +3976,9 @@ MatrixClient.prototype.scrollback = function(room, limit, callback) { limit, 'b'); }).then(function(res) { - const matrixEvents = utils.map(res.chunk, _PojoToMatrixEventMapper(self)); + const matrixEvents = res.chunk.map(_PojoToMatrixEventMapper(self)); if (res.state) { - const stateEvents = utils.map(res.state, _PojoToMatrixEventMapper(self)); + const stateEvents = res.state.map(_PojoToMatrixEventMapper(self)); room.currentState.setUnknownStateEvents(stateEvents); } room.addEventsToTimeline(matrixEvents, true, room.getLiveTimeline()); @@ -4067,16 +4067,16 @@ MatrixClient.prototype.getEventTimeline = function(timelineSet, eventId) { const events = res.events_after .concat([res.event]) .concat(res.events_before); - const matrixEvents = utils.map(events, self.getEventMapper()); + const matrixEvents = events.map(self.getEventMapper()); let timeline = timelineSet.getTimelineForEvent(matrixEvents[0].getId()); if (!timeline) { timeline = timelineSet.addTimeline(); - timeline.initialiseState(utils.map(res.state, + timeline.initialiseState(res.state.map( self.getEventMapper())); timeline.getState(EventTimeline.FORWARDS).paginationToken = res.end; } else { - const stateEvents = utils.map(res.state, self.getEventMapper()); + const stateEvents = res.state.map(self.getEventMapper()); timeline.getState(EventTimeline.BACKWARDS).setUnknownStateEvents(stateEvents); } timelineSet.addEventsToTimeline(matrixEvents, true, timeline, res.start); @@ -4239,11 +4239,11 @@ MatrixClient.prototype.paginateEventTimeline = function(eventTimeline, opts) { promise.then(function(res) { if (res.state) { const roomState = eventTimeline.getState(dir); - const stateEvents = utils.map(res.state, self.getEventMapper()); + const stateEvents = res.state.map(self.getEventMapper()); roomState.setUnknownStateEvents(stateEvents); } const token = res.end; - const matrixEvents = utils.map(res.chunk, self.getEventMapper()); + const matrixEvents = res.chunk.map(self.getEventMapper()); eventTimeline.getTimelineSet() .addEventsToTimeline(matrixEvents, backwards, eventTimeline, token); diff --git a/src/content-repo.js b/src/content-repo.js index 0d1ae1ea0..1b92d59ae 100644 --- a/src/content-repo.js +++ b/src/content-repo.js @@ -59,7 +59,7 @@ export function getHttpUriForMxc(baseUrl, mxc, width, height, if (resizeMethod) { params.method = resizeMethod; } - if (utils.keys(params).length > 0) { + if (Object.keys(params).length > 0) { // these are thumbnailing params so they probably want the // thumbnailing API... prefix = "/_matrix/media/r0/thumbnail/"; @@ -72,6 +72,6 @@ export function getHttpUriForMxc(baseUrl, mxc, width, height, serverAndMediaId = serverAndMediaId.substr(0, fragmentOffset); } return baseUrl + prefix + serverAndMediaId + - (utils.keys(params).length === 0 ? "" : + (Object.keys(params).length === 0 ? "" : ("?" + utils.encodeParams(params))) + fragment; } diff --git a/src/crypto/OutgoingRoomKeyRequestManager.js b/src/crypto/OutgoingRoomKeyRequestManager.js index c3f314f7a..899c3ca96 100644 --- a/src/crypto/OutgoingRoomKeyRequestManager.js +++ b/src/crypto/OutgoingRoomKeyRequestManager.js @@ -15,7 +15,6 @@ limitations under the License. */ import {logger} from '../logger'; -import * as utils from '../utils'; /** * Internal module. Management of outgoing room key requests. @@ -496,7 +495,7 @@ function stringifyRequestBody(requestBody) { function stringifyRecipientList(recipients) { return '[' - + utils.map(recipients, (r) => `${r.userId}:${r.deviceId}`).join(",") + + recipients.map((r) => `${r.userId}:${r.deviceId}`).join(",") + ']'; } diff --git a/src/crypto/algorithms/megolm.js b/src/crypto/algorithms/megolm.js index f2552ed1c..c06b6da16 100644 --- a/src/crypto/algorithms/megolm.js +++ b/src/crypto/algorithms/megolm.js @@ -1076,7 +1076,7 @@ MegolmEncryption.prototype._removeUnknownDevices = function(devicesInRoom) { */ MegolmEncryption.prototype._getDevicesInRoom = async function(room) { const members = await room.getEncryptionTargetMembers(); - const roomMembers = utils.map(members, function(u) { + const roomMembers = members.map(function(u) { return u.userId; }); @@ -1371,7 +1371,7 @@ MegolmDecryption.prototype.onRoomKeyEvent = function(event) { if (event.getType() == "m.forwarded_room_key") { exportFormat = true; forwardingKeyChain = content.forwarding_curve25519_key_chain; - if (!utils.isArray(forwardingKeyChain)) { + if (!Array.isArray(forwardingKeyChain)) { forwardingKeyChain = []; } diff --git a/src/crypto/algorithms/olm.js b/src/crypto/algorithms/olm.js index 6add378e9..f23841533 100644 --- a/src/crypto/algorithms/olm.js +++ b/src/crypto/algorithms/olm.js @@ -95,7 +95,7 @@ OlmEncryption.prototype.encryptMessage = async function(room, eventType, content const members = await room.getEncryptionTargetMembers(); - const users = utils.map(members, function(u) { + const users = members.map(function(u) { return u.userId; }); diff --git a/src/crypto/index.js b/src/crypto/index.js index 802d4660a..cdcce3cd5 100644 --- a/src/crypto/index.js +++ b/src/crypto/index.js @@ -186,7 +186,7 @@ export function Crypto(baseApis, sessionStore, userId, deviceId, // map from algorithm to DecryptionAlgorithm instance, for each room this._roomDecryptors = {}; - this._supportedAlgorithms = utils.keys( + this._supportedAlgorithms = Object.keys( algorithms.DECRYPTION_CLASSES, ); diff --git a/src/index.ts b/src/index.ts index 29edc8bb0..e599e0065 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,7 +19,6 @@ import * as utils from "./utils"; import request from "request"; matrixcs.request(request); -utils.runPolyfills(); try { // eslint-disable-next-line @typescript-eslint/no-var-requires diff --git a/src/models/event-timeline-set.js b/src/models/event-timeline-set.js index fd04eabe5..84853d07d 100644 --- a/src/models/event-timeline-set.js +++ b/src/models/event-timeline-set.js @@ -249,7 +249,7 @@ EventTimelineSet.prototype.findEventById = function(eventId) { if (!tl) { return undefined; } - return utils.findElement(tl.getEvents(), function(ev) { + return tl.getEvents().find(function(ev) { return ev.getId() == eventId; }); }; diff --git a/src/models/room-member.js b/src/models/room-member.js index 444c45609..421788c5d 100644 --- a/src/models/room-member.js +++ b/src/models/room-member.js @@ -133,7 +133,7 @@ RoomMember.prototype.setPowerLevelEvent = function(powerLevelEvent) { const evContent = powerLevelEvent.getDirectionalContent(); let maxLevel = evContent.users_default || 0; - utils.forEach(utils.values(evContent.users), function(lvl) { + Object.values(evContent.users).forEach(function(lvl) { maxLevel = Math.max(maxLevel, lvl); }); const oldPowerLevel = this.powerLevel; @@ -172,7 +172,7 @@ RoomMember.prototype.setTypingEvent = function(event) { const oldTyping = this.typing; this.typing = false; const typingList = event.getContent().user_ids; - if (!utils.isArray(typingList)) { + if (!Array.isArray(typingList)) { // malformed event :/ bail early. TODO: whine? return; } diff --git a/src/models/room-state.js b/src/models/room-state.js index 9be252f2c..1b4943f5f 100644 --- a/src/models/room-state.js +++ b/src/models/room-state.js @@ -154,7 +154,7 @@ RoomState.prototype.setInvitedMemberCount = function(count) { * @return {Array} A list of RoomMembers. */ RoomState.prototype.getMembers = function() { - return utils.values(this.members); + return Object.values(this.members); }; /** @@ -163,7 +163,7 @@ RoomState.prototype.getMembers = function() { * @return {Array} A list of RoomMembers. */ RoomState.prototype.getMembersExcept = function(excludedIds) { - return utils.values(this.members) + return Object.values(this.members) .filter((m) => !excludedIds.includes(m.userId)); }; @@ -296,7 +296,7 @@ RoomState.prototype.setStateEvents = function(stateEvents) { this._updateModifiedTime(); // update the core event dict - utils.forEach(stateEvents, function(event) { + stateEvents.forEach(function(event) { if (event.getRoomId() !== self.roomId) { return; } @@ -319,7 +319,7 @@ RoomState.prototype.setStateEvents = function(stateEvents) { // core event dict as these structures may depend on other state events in // the given array (e.g. disambiguating display names in one go to do both // clashing names rather than progressively which only catches 1 of them). - utils.forEach(stateEvents, function(event) { + stateEvents.forEach(function(event) { if (event.getRoomId() !== self.roomId) { return; } @@ -349,8 +349,8 @@ RoomState.prototype.setStateEvents = function(stateEvents) { self._updateMember(member); self.emit("RoomState.members", event, self, member); } else if (event.getType() === "m.room.power_levels") { - const members = utils.values(self.members); - utils.forEach(members, function(member) { + const members = Object.values(self.members); + members.forEach(function(member) { // We only propagate `RoomState.members` event if the // power levels has been changed // large room suffer from large re-rendering especially when not needed @@ -511,7 +511,7 @@ RoomState.prototype._setOutOfBandMember = function(stateEvent) { * @param {MatrixEvent} event The typing event */ RoomState.prototype.setTypingEvent = function(event) { - utils.forEach(utils.values(this.members), function(member) { + Object.values(this.members).forEach(function(member) { member.setTypingEvent(event); }); }; diff --git a/src/models/room.js b/src/models/room.js index c99df7718..97c8498b1 100644 --- a/src/models/room.js +++ b/src/models/room.js @@ -949,7 +949,7 @@ Room.prototype.getAliases = function() { if (aliasEvents) { for (let i = 0; i < aliasEvents.length; ++i) { const aliasEvent = aliasEvents[i]; - if (utils.isArray(aliasEvent.getContent().aliases)) { + if (Array.isArray(aliasEvent.getContent().aliases)) { const filteredAliases = aliasEvent.getContent().aliases.filter(a => { if (typeof(a) !== "string") return false; if (a[0] !== '#') return false; @@ -1077,7 +1077,7 @@ Room.prototype.getInvitedAndJoinedMemberCount = function() { * @return {RoomMember[]} A list of members with the given membership state. */ Room.prototype.getMembersWithMembership = function(membership) { - return utils.filter(this.currentState.getMembers(), function(m) { + return this.currentState.getMembers().filter(function(m) { return m.membership === membership; }); }; @@ -1700,7 +1700,7 @@ Room.prototype.recalculate = function() { ); if (membershipEvent && membershipEvent.getContent().membership === "invite") { const strippedStateEvents = membershipEvent.event.invite_room_state || []; - utils.forEach(strippedStateEvents, function(strippedEvent) { + strippedStateEvents.forEach(function(strippedEvent) { const existingEvent = self.currentState.getStateEvents( strippedEvent.type, strippedEvent.state_key, ); @@ -1850,9 +1850,9 @@ Room.prototype.addReceipt = function(event, fake) { */ Room.prototype._addReceiptsToStructure = function(event, receipts) { const self = this; - utils.keys(event.getContent()).forEach(function(eventId) { - utils.keys(event.getContent()[eventId]).forEach(function(receiptType) { - utils.keys(event.getContent()[eventId][receiptType]).forEach( + Object.keys(event.getContent()).forEach(function(eventId) { + Object.keys(event.getContent()[eventId]).forEach(function(receiptType) { + Object.keys(event.getContent()[eventId][receiptType]).forEach( function(userId) { const receipt = event.getContent()[eventId][receiptType][userId]; @@ -1892,8 +1892,8 @@ Room.prototype._addReceiptsToStructure = function(event, receipts) { */ Room.prototype._buildReceiptCache = function(receipts) { const receiptCacheByEventId = {}; - utils.keys(receipts).forEach(function(receiptType) { - utils.keys(receipts[receiptType]).forEach(function(userId) { + Object.keys(receipts).forEach(function(receiptType) { + Object.keys(receipts[receiptType]).forEach(function(userId) { const receipt = receipts[receiptType][userId]; if (!receiptCacheByEventId[receipt.eventId]) { receiptCacheByEventId[receipt.eventId] = []; diff --git a/src/models/search-result.js b/src/models/search-result.js index a8b50ea4b..2250e786f 100644 --- a/src/models/search-result.js +++ b/src/models/search-result.js @@ -19,7 +19,6 @@ limitations under the License. * @module models/search-result */ -import * as utils from "../utils"; import {EventContext} from "./event-context"; /** @@ -52,8 +51,8 @@ SearchResult.fromJson = function(jsonObj, eventMapper) { const context = new EventContext(eventMapper(jsonObj.result)); context.setPaginateToken(jsonContext.start, true); - context.addEvents(utils.map(events_before, eventMapper), true); - context.addEvents(utils.map(events_after, eventMapper), false); + context.addEvents(events_before.map(eventMapper), true); + context.addEvents(events_after.map(eventMapper), false); context.setPaginateToken(jsonContext.end, false); return new SearchResult(jsonObj.rank, context); diff --git a/src/scheduler.js b/src/scheduler.js index 9addbc8e5..e0b2bb8ee 100644 --- a/src/scheduler.js +++ b/src/scheduler.js @@ -65,7 +65,7 @@ MatrixScheduler.prototype.getQueueForEvent = function(event) { if (!name || !this._queues[name]) { return null; } - return utils.map(this._queues[name], function(obj) { + return this._queues[name].map(function(obj) { return obj.event; }); }; @@ -196,16 +196,18 @@ function _startProcessingQueues(scheduler) { return; } // for each inactive queue with events in them - utils.forEach(utils.filter(utils.keys(scheduler._queues), function(queueName) { - return scheduler._activeQueues.indexOf(queueName) === -1 && - scheduler._queues[queueName].length > 0; - }), function(queueName) { - // mark the queue as active - scheduler._activeQueues.push(queueName); - // begin processing the head of the queue - debuglog("Spinning up queue: '%s'", queueName); - _processQueue(scheduler, queueName); - }); + Object.keys(scheduler._queues) + .filter(function(queueName) { + return scheduler._activeQueues.indexOf(queueName) === -1 && + scheduler._queues[queueName].length > 0; + }) + .forEach(function(queueName) { + // mark the queue as active + scheduler._activeQueues.push(queueName); + // begin processing the head of the queue + debuglog("Spinning up queue: '%s'", queueName); + _processQueue(scheduler, queueName); + }); } function _processQueue(scheduler, queueName) { @@ -267,7 +269,7 @@ function _processQueue(scheduler, queueName) { function _peekNextEvent(scheduler, queueName) { const queue = scheduler._queues[queueName]; - if (!utils.isArray(queue)) { + if (!Array.isArray(queue)) { return null; } return queue[0]; @@ -275,7 +277,7 @@ function _peekNextEvent(scheduler, queueName) { function _removeNextEvent(scheduler, queueName) { const queue = scheduler._queues[queueName]; - if (!utils.isArray(queue)) { + if (!Array.isArray(queue)) { return null; } return queue.shift(); diff --git a/src/store/memory.js b/src/store/memory.js index bf2e9cfd2..1b582ee43 100644 --- a/src/store/memory.js +++ b/src/store/memory.js @@ -23,7 +23,6 @@ limitations under the License. */ import {User} from "../models/user"; -import * as utils from "../utils"; function isValidFilterId(filterId) { const isValidStr = typeof filterId === "string" && @@ -113,7 +112,7 @@ MemoryStore.prototype = { * @return {Group[]} A list of groups, which may be empty. */ getGroups: function() { - return utils.values(this.groups); + return Object.values(this.groups); }, /** @@ -175,7 +174,7 @@ MemoryStore.prototype = { * @return {Room[]} A list of rooms, which may be empty. */ getRooms: function() { - return utils.values(this.rooms); + return Object.values(this.rooms); }, /** @@ -194,7 +193,7 @@ MemoryStore.prototype = { * @return {RoomSummary[]} A summary of each room. */ getRoomSummaries: function() { - return utils.map(utils.values(this.rooms), function(room) { + return Object.values(this.rooms).map(function(room) { return room.summary; }); }, @@ -221,7 +220,7 @@ MemoryStore.prototype = { * @return {User[]} A list of users, which may be empty. */ getUsers: function() { - return utils.values(this.users); + return Object.values(this.users); }, /** diff --git a/src/sync.js b/src/sync.js index 3cb82fa31..b33597fe1 100644 --- a/src/sync.js +++ b/src/sync.js @@ -278,20 +278,15 @@ SyncApi.prototype.peek = function(roomId) { // FIXME: Mostly duplicated from _processRoomEvents but not entirely // because "state" in this API is at the BEGINNING of the chunk - const oldStateEvents = utils.map( - utils.deepCopy(response.state), client.getEventMapper(), - ); - const stateEvents = utils.map( - response.state, client.getEventMapper(), - ); - const messages = utils.map( - response.messages.chunk, client.getEventMapper(), - ); + const oldStateEvents = utils.deepCopy(response.state) + .map(client.getEventMapper()); + const stateEvents = response.state.map(client.getEventMapper()); + const messages = response.messages.chunk.map(client.getEventMapper()); // XXX: copypasted from /sync until we kill off this // minging v1 API stuff) // handle presence events (User objects) - if (response.presence && utils.isArray(response.presence)) { + if (response.presence && Array.isArray(response.presence)) { response.presence.map(client.getEventMapper()).forEach( function(presenceEvent) { let user = client.store.getUser(presenceEvent.getContent().user_id); @@ -1006,7 +1001,7 @@ SyncApi.prototype._processSyncResponse = async function( // - The isBrandNewRoom boilerplate is boilerplatey. // handle presence events (User objects) - if (data.presence && utils.isArray(data.presence.events)) { + if (data.presence && Array.isArray(data.presence.events)) { data.presence.events.map(client.getEventMapper()).forEach( function(presenceEvent) { let user = client.store.getUser(presenceEvent.getSender()); @@ -1022,7 +1017,7 @@ SyncApi.prototype._processSyncResponse = async function( } // handle non-room account_data - if (data.account_data && utils.isArray(data.account_data.events)) { + if (data.account_data && Array.isArray(data.account_data.events)) { const events = data.account_data.events.map(client.getEventMapper()); const prevEventsMap = events.reduce((m, c) => { m[c.getId()] = client.store.getAccountData(c.getType()); @@ -1047,7 +1042,7 @@ SyncApi.prototype._processSyncResponse = async function( } // handle to-device events - if (data.to_device && utils.isArray(data.to_device.events) && + if (data.to_device && Array.isArray(data.to_device.events) && data.to_device.events.length > 0 ) { const cancelledKeyVerificationTxns = []; @@ -1508,7 +1503,7 @@ SyncApi.prototype._mapSyncResponseToRoomArray = function(obj) { // [{stuff+Room+isBrandNewRoom}, {stuff+Room+isBrandNewRoom}] const client = this.client; const self = this; - return utils.keys(obj).map(function(roomId) { + return Object.keys(obj).map(function(roomId) { const arrObj = obj[roomId]; let room = client.store.getRoom(roomId); let isBrandNewRoom = false; @@ -1529,7 +1524,7 @@ SyncApi.prototype._mapSyncResponseToRoomArray = function(obj) { * @return {MatrixEvent[]} */ SyncApi.prototype._mapSyncEventsFormat = function(obj, room, decrypt = true) { - if (!obj || !utils.isArray(obj.events)) { + if (!obj || !Array.isArray(obj.events)) { return []; } const mapper = this.client.getEventMapper({ decrypt }); diff --git a/src/utils.ts b/src/utils.ts index 17b3924cd..76f829eee 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -61,116 +61,6 @@ export function encodeUri(pathTemplate: string, return pathTemplate; } -/** - * Applies a map function to the given array. - * @param {Array} array The array to apply the function to. - * @param {Function} fn The function that will be invoked for each element in - * the array with the signature fn(element){...} - * @return {Array} A new array with the results of the function. - */ -export function map(array: T[], fn: (t: T) => S): S[] { - const results = new Array(array.length); - for (let i = 0; i < array.length; i++) { - results[i] = fn(array[i]); - } - return results; -} - -/** - * Applies a filter function to the given array. - * @param {Array} array The array to apply the function to. - * @param {Function} fn The function that will be invoked for each element in - * the array. It should return true to keep the element. The function signature - * looks like fn(element, index, array){...}. - * @return {Array} A new array with the results of the function. - */ -export function filter(array: T[], - fn: (t: T, i?: number, a?: T[]) => boolean): T[] { - const results: T[] = []; - for (let i = 0; i < array.length; i++) { - if (fn(array[i], i, array)) { - results.push(array[i]); - } - } - return results; -} - -/** - * Get the keys for an object. Same as Object.keys(). - * @param {Object} obj The object to get the keys for. - * @return {string[]} The keys of the object. - */ -export function keys(obj: object): string[] { - const result = []; - for (const key in obj) { - if (!obj.hasOwnProperty(key)) { - continue; - } - result.push(key); - } - return result; -} - -/** - * Get the values for an object. - * @param {Object} obj The object to get the values for. - * @return {Array<*>} The values of the object. - */ -export function values(obj: Record): T[] { - const result = []; - for (const key in obj) { - if (!obj.hasOwnProperty(key)) { - continue; - } - result.push(obj[key]); - } - return result; -} - -/** - * Invoke a function for each item in the array. - * @param {Array} array The array. - * @param {Function} fn The function to invoke for each element. Has the - * function signature fn(element, index). - */ -export function forEach(array: T[], fn: (t: T, i: number) => void) { - for (let i = 0; i < array.length; i++) { - fn(array[i], i); - } -} - -/** - * The findElement() method returns a value in the array, if an element in the array - * satisfies (returns true) the provided testing function. Otherwise undefined - * is returned. - * @param {Array} array The array. - * @param {Function} fn Function to execute on each value in the array, with the - * function signature fn(element, index, array) - * @param {boolean} reverse True to search in reverse order. - * @return {*} The first value in the array which returns true for - * the given function. - */ -export function findElement( - array: T[], - fn: (t: T, i?: number, a?: T[]) => boolean, - reverse?: boolean, -) { - let i; - if (reverse) { - for (i = array.length - 1; i >= 0; i--) { - if (fn(array[i], i, array)) { - return array[i]; - } - } - } else { - for (i = 0; i < array.length; i++) { - if (fn(array[i], i, array)) { - return array[i]; - } - } - } -} - /** * The removeElement() method removes the first element in the array that * satisfies (returns true) the provided testing function. @@ -217,16 +107,6 @@ export function isFunction(value: any) { return Object.prototype.toString.call(value) === "[object Function]"; } -/** - * Checks if the given thing is an array. - * @param {*} value The thing to check. - * @return {boolean} True if it is an array. - */ -export function isArray(value: any) { - return Array.isArray ? Array.isArray(value) : - Boolean(value && value.constructor === Array); -} - /** * Checks that the given object has the specified keys. * @param {Object} obj The object to check. @@ -380,207 +260,6 @@ export function extend(...restParams) { return target; } -/** - * Run polyfills to add Array.map and Array.filter if they are missing. - */ -export function runPolyfills() { - // Array.prototype.filter - // ======================================================== - // SOURCE: - // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter - if (!Array.prototype.filter) { - // eslint-disable-next-line no-extend-native - Array.prototype.filter = function(fun: Function/*, thisArg*/, ...restProps) { - if (this === void 0 || this === null) { - throw new TypeError(); - } - - const t = Object(this); - const len = t.length >>> 0; - if (typeof fun !== 'function') { - throw new TypeError(); - } - - const res = []; - const thisArg = restProps ? restProps[0] : void 0; - for (let i = 0; i < len; i++) { - if (i in t) { - const val = t[i]; - - // NOTE: Technically this should Object.defineProperty at - // the next index, as push can be affected by - // properties on Object.prototype and Array.prototype. - // But that method's new, and collisions should be - // rare, so use the more-compatible alternative. - if (fun.call(thisArg, val, i, t)) { - res.push(val); - } - } - } - return res; - }; - } - - // Array.prototype.map - // ======================================================== - // SOURCE: - // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map - // Production steps of ECMA-262, Edition 5, 15.4.4.19 - // Reference: http://es5.github.io/#x15.4.4.19 - if (!Array.prototype.map) { - // eslint-disable-next-line no-extend-native - Array.prototype.map = function(callback, thisArg) { - let T; - let k; - - if (this === null || this === undefined) { - throw new TypeError(' this is null or not defined'); - } - - // 1. Let O be the result of calling ToObject passing the |this| - // value as the argument. - const O = Object(this); - - // 2. Let lenValue be the result of calling the Get internal - // method of O with the argument "length". - // 3. Let len be ToUint32(lenValue). - const len = O.length >>> 0; - - // 4. If IsCallable(callback) is false, throw a TypeError exception. - // See: http://es5.github.com/#x9.11 - if (typeof callback !== 'function') { - throw new TypeError(callback + ' is not a function'); - } - - // 5. If thisArg was supplied, let T be thisArg; else let T be undefined. - if (arguments.length > 1) { - T = thisArg; - } - - // 6. Let A be a new array created as if by the expression new Array(len) - // where Array is the standard built-in constructor with that name and - // len is the value of len. - const A = new Array(len); - - // 7. Let k be 0 - k = 0; - - // 8. Repeat, while k < len - while (k < len) { - let kValue; - let mappedValue; - - // a. Let Pk be ToString(k). - // This is implicit for LHS operands of the in operator - // b. Let kPresent be the result of calling the HasProperty internal - // method of O with argument Pk. - // This step can be combined with c - // c. If kPresent is true, then - if (k in O) { - // i. Let kValue be the result of calling the Get internal - // method of O with argument Pk. - kValue = O[k]; - - // ii. Let mappedValue be the result of calling the Call internal - // method of callback with T as the this value and argument - // list containing kValue, k, and O. - mappedValue = callback.call(T, kValue, k, O); - - // iii. Call the DefineOwnProperty internal method of A with arguments - // Pk, Property Descriptor - // { Value: mappedValue, - // Writable: true, - // Enumerable: true, - // Configurable: true }, - // and false. - - // In browsers that support Object.defineProperty, use the following: - // Object.defineProperty(A, k, { - // value: mappedValue, - // writable: true, - // enumerable: true, - // configurable: true - // }); - - // For best browser support, use the following: - A[k] = mappedValue; - } - // d. Increase k by 1. - k++; - } - - // 9. return A - return A; - }; - } - - // Array.prototype.forEach - // ======================================================== - // SOURCE: - // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach - // Production steps of ECMA-262, Edition 5, 15.4.4.18 - // Reference: http://es5.github.io/#x15.4.4.18 - if (!Array.prototype.forEach) { - // eslint-disable-next-line no-extend-native - Array.prototype.forEach = function(callback, thisArg) { - let T; - let k; - - if (this === null || this === undefined) { - throw new TypeError(' this is null or not defined'); - } - - // 1. Let O be the result of calling ToObject passing the |this| value as the - // argument. - const O = Object(this); - - // 2. Let lenValue be the result of calling the Get internal method of O with the - // argument "length". - // 3. Let len be ToUint32(lenValue). - const len = O.length >>> 0; - - // 4. If IsCallable(callback) is false, throw a TypeError exception. - // See: http://es5.github.com/#x9.11 - if (typeof callback !== "function") { - throw new TypeError(callback + ' is not a function'); - } - - // 5. If thisArg was supplied, let T be thisArg; else let T be undefined. - if (arguments.length > 1) { - T = thisArg; - } - - // 6. Let k be 0 - k = 0; - - // 7. Repeat, while k < len - while (k < len) { - let kValue; - - // a. Let Pk be ToString(k). - // This is implicit for LHS operands of the in operator - // b. Let kPresent be the result of calling the HasProperty internal - // method of O with - // argument Pk. - // This step can be combined with c - // c. If kPresent is true, then - if (k in O) { - // i. Let kValue be the result of calling the Get internal method of O with - // argument Pk - kValue = O[k]; - - // ii. Call the Call internal method of callback with T as the this value and - // argument list containing kValue, k, and O. - callback.call(T, kValue, k, O); - } - // d. Increase k by 1. - k++; - } - // 8. return undefined - }; - } -} - /** * Inherit the prototype methods from one constructor into another. This is a * port of the Node.js implementation with an Object.create polyfill.