You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-26 17:03:12 +03:00
Use native Object and Array methods (#1693)
This commit is contained in:
@@ -30,7 +30,6 @@ import '../olm-loader';
|
|||||||
|
|
||||||
import {logger} from '../../src/logger';
|
import {logger} from '../../src/logger';
|
||||||
import * as testUtils from "../test-utils";
|
import * as testUtils from "../test-utils";
|
||||||
import * as utils from "../../src/utils";
|
|
||||||
import {TestClient} from "../TestClient";
|
import {TestClient} from "../TestClient";
|
||||||
import {CRYPTO_ENABLED} from "../../src/client";
|
import {CRYPTO_ENABLED} from "../../src/client";
|
||||||
|
|
||||||
@@ -244,7 +243,7 @@ function bobSendsReplyMessage() {
|
|||||||
function expectAliSendMessageRequest() {
|
function expectAliSendMessageRequest() {
|
||||||
return expectSendMessageRequest(aliTestClient.httpBackend).then(function(content) {
|
return expectSendMessageRequest(aliTestClient.httpBackend).then(function(content) {
|
||||||
aliMessages.push(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()];
|
const ciphertext = content.ciphertext[bobTestClient.getDeviceKey()];
|
||||||
expect(ciphertext).toBeTruthy();
|
expect(ciphertext).toBeTruthy();
|
||||||
return ciphertext;
|
return ciphertext;
|
||||||
@@ -261,7 +260,7 @@ function expectBobSendMessageRequest() {
|
|||||||
bobMessages.push(content);
|
bobMessages.push(content);
|
||||||
const aliKeyId = "curve25519:" + aliDeviceId;
|
const aliKeyId = "curve25519:" + aliDeviceId;
|
||||||
const aliDeviceCurve25519Key = aliTestClient.deviceKeys.keys[aliKeyId];
|
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];
|
const ciphertext = content.ciphertext[aliDeviceCurve25519Key];
|
||||||
expect(ciphertext).toBeTruthy();
|
expect(ciphertext).toBeTruthy();
|
||||||
return ciphertext;
|
return ciphertext;
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ limitations under the License.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import anotherjson from "another-json";
|
import anotherjson from "another-json";
|
||||||
import * as utils from "../../src/utils";
|
|
||||||
import * as testUtils from "../test-utils";
|
import * as testUtils from "../test-utils";
|
||||||
import {TestClient} from "../TestClient";
|
import {TestClient} from "../TestClient";
|
||||||
import {logger} from "../../src/logger";
|
import {logger} from "../../src/logger";
|
||||||
@@ -32,7 +31,7 @@ const ROOM_ID = "!room:id";
|
|||||||
*/
|
*/
|
||||||
function createOlmSession(olmAccount, recipientTestClient) {
|
function createOlmSession(olmAccount, recipientTestClient) {
|
||||||
return recipientTestClient.awaitOneTimeKeyUpload().then((keys) => {
|
return recipientTestClient.awaitOneTimeKeyUpload().then((keys) => {
|
||||||
const otkId = utils.keys(keys)[0];
|
const otkId = Object.keys(keys)[0];
|
||||||
const otk = keys[otkId];
|
const otk = keys[otkId];
|
||||||
|
|
||||||
const session = new global.Olm.Session();
|
const session = new global.Olm.Session();
|
||||||
@@ -257,7 +256,7 @@ describe("megolm", function() {
|
|||||||
const testOneTimeKeys = JSON.parse(testOlmAccount.one_time_keys());
|
const testOneTimeKeys = JSON.parse(testOlmAccount.one_time_keys());
|
||||||
testOlmAccount.mark_keys_as_published();
|
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 oneTimeKey = testOneTimeKeys.curve25519[keyId];
|
||||||
const keyResult = {
|
const keyResult = {
|
||||||
'key': oneTimeKey,
|
'key': oneTimeKey,
|
||||||
|
|||||||
@@ -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() {
|
describe("removeElement", function() {
|
||||||
it("should remove only 1 element if there is a match", function() {
|
it("should remove only 1 element if there is a match", function() {
|
||||||
const matchFn = 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() {
|
describe("checkObjectHasKeys", function() {
|
||||||
it("should throw for missing keys", function() {
|
it("should throw for missing keys", function() {
|
||||||
expect(function() {
|
expect(function() {
|
||||||
|
|||||||
@@ -3976,9 +3976,9 @@ MatrixClient.prototype.scrollback = function(room, limit, callback) {
|
|||||||
limit,
|
limit,
|
||||||
'b');
|
'b');
|
||||||
}).then(function(res) {
|
}).then(function(res) {
|
||||||
const matrixEvents = utils.map(res.chunk, _PojoToMatrixEventMapper(self));
|
const matrixEvents = res.chunk.map(_PojoToMatrixEventMapper(self));
|
||||||
if (res.state) {
|
if (res.state) {
|
||||||
const stateEvents = utils.map(res.state, _PojoToMatrixEventMapper(self));
|
const stateEvents = res.state.map(_PojoToMatrixEventMapper(self));
|
||||||
room.currentState.setUnknownStateEvents(stateEvents);
|
room.currentState.setUnknownStateEvents(stateEvents);
|
||||||
}
|
}
|
||||||
room.addEventsToTimeline(matrixEvents, true, room.getLiveTimeline());
|
room.addEventsToTimeline(matrixEvents, true, room.getLiveTimeline());
|
||||||
@@ -4067,16 +4067,16 @@ MatrixClient.prototype.getEventTimeline = function(timelineSet, eventId) {
|
|||||||
const events = res.events_after
|
const events = res.events_after
|
||||||
.concat([res.event])
|
.concat([res.event])
|
||||||
.concat(res.events_before);
|
.concat(res.events_before);
|
||||||
const matrixEvents = utils.map(events, self.getEventMapper());
|
const matrixEvents = events.map(self.getEventMapper());
|
||||||
|
|
||||||
let timeline = timelineSet.getTimelineForEvent(matrixEvents[0].getId());
|
let timeline = timelineSet.getTimelineForEvent(matrixEvents[0].getId());
|
||||||
if (!timeline) {
|
if (!timeline) {
|
||||||
timeline = timelineSet.addTimeline();
|
timeline = timelineSet.addTimeline();
|
||||||
timeline.initialiseState(utils.map(res.state,
|
timeline.initialiseState(res.state.map(
|
||||||
self.getEventMapper()));
|
self.getEventMapper()));
|
||||||
timeline.getState(EventTimeline.FORWARDS).paginationToken = res.end;
|
timeline.getState(EventTimeline.FORWARDS).paginationToken = res.end;
|
||||||
} else {
|
} else {
|
||||||
const stateEvents = utils.map(res.state, self.getEventMapper());
|
const stateEvents = res.state.map(self.getEventMapper());
|
||||||
timeline.getState(EventTimeline.BACKWARDS).setUnknownStateEvents(stateEvents);
|
timeline.getState(EventTimeline.BACKWARDS).setUnknownStateEvents(stateEvents);
|
||||||
}
|
}
|
||||||
timelineSet.addEventsToTimeline(matrixEvents, true, timeline, res.start);
|
timelineSet.addEventsToTimeline(matrixEvents, true, timeline, res.start);
|
||||||
@@ -4239,11 +4239,11 @@ MatrixClient.prototype.paginateEventTimeline = function(eventTimeline, opts) {
|
|||||||
promise.then(function(res) {
|
promise.then(function(res) {
|
||||||
if (res.state) {
|
if (res.state) {
|
||||||
const roomState = eventTimeline.getState(dir);
|
const roomState = eventTimeline.getState(dir);
|
||||||
const stateEvents = utils.map(res.state, self.getEventMapper());
|
const stateEvents = res.state.map(self.getEventMapper());
|
||||||
roomState.setUnknownStateEvents(stateEvents);
|
roomState.setUnknownStateEvents(stateEvents);
|
||||||
}
|
}
|
||||||
const token = res.end;
|
const token = res.end;
|
||||||
const matrixEvents = utils.map(res.chunk, self.getEventMapper());
|
const matrixEvents = res.chunk.map(self.getEventMapper());
|
||||||
eventTimeline.getTimelineSet()
|
eventTimeline.getTimelineSet()
|
||||||
.addEventsToTimeline(matrixEvents, backwards, eventTimeline, token);
|
.addEventsToTimeline(matrixEvents, backwards, eventTimeline, token);
|
||||||
|
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ export function getHttpUriForMxc(baseUrl, mxc, width, height,
|
|||||||
if (resizeMethod) {
|
if (resizeMethod) {
|
||||||
params.method = 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
|
// these are thumbnailing params so they probably want the
|
||||||
// thumbnailing API...
|
// thumbnailing API...
|
||||||
prefix = "/_matrix/media/r0/thumbnail/";
|
prefix = "/_matrix/media/r0/thumbnail/";
|
||||||
@@ -72,6 +72,6 @@ export function getHttpUriForMxc(baseUrl, mxc, width, height,
|
|||||||
serverAndMediaId = serverAndMediaId.substr(0, fragmentOffset);
|
serverAndMediaId = serverAndMediaId.substr(0, fragmentOffset);
|
||||||
}
|
}
|
||||||
return baseUrl + prefix + serverAndMediaId +
|
return baseUrl + prefix + serverAndMediaId +
|
||||||
(utils.keys(params).length === 0 ? "" :
|
(Object.keys(params).length === 0 ? "" :
|
||||||
("?" + utils.encodeParams(params))) + fragment;
|
("?" + utils.encodeParams(params))) + fragment;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ limitations under the License.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import {logger} from '../logger';
|
import {logger} from '../logger';
|
||||||
import * as utils from '../utils';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal module. Management of outgoing room key requests.
|
* Internal module. Management of outgoing room key requests.
|
||||||
@@ -496,7 +495,7 @@ function stringifyRequestBody(requestBody) {
|
|||||||
|
|
||||||
function stringifyRecipientList(recipients) {
|
function stringifyRecipientList(recipients) {
|
||||||
return '['
|
return '['
|
||||||
+ utils.map(recipients, (r) => `${r.userId}:${r.deviceId}`).join(",")
|
+ recipients.map((r) => `${r.userId}:${r.deviceId}`).join(",")
|
||||||
+ ']';
|
+ ']';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1076,7 +1076,7 @@ MegolmEncryption.prototype._removeUnknownDevices = function(devicesInRoom) {
|
|||||||
*/
|
*/
|
||||||
MegolmEncryption.prototype._getDevicesInRoom = async function(room) {
|
MegolmEncryption.prototype._getDevicesInRoom = async function(room) {
|
||||||
const members = await room.getEncryptionTargetMembers();
|
const members = await room.getEncryptionTargetMembers();
|
||||||
const roomMembers = utils.map(members, function(u) {
|
const roomMembers = members.map(function(u) {
|
||||||
return u.userId;
|
return u.userId;
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -1371,7 +1371,7 @@ MegolmDecryption.prototype.onRoomKeyEvent = function(event) {
|
|||||||
if (event.getType() == "m.forwarded_room_key") {
|
if (event.getType() == "m.forwarded_room_key") {
|
||||||
exportFormat = true;
|
exportFormat = true;
|
||||||
forwardingKeyChain = content.forwarding_curve25519_key_chain;
|
forwardingKeyChain = content.forwarding_curve25519_key_chain;
|
||||||
if (!utils.isArray(forwardingKeyChain)) {
|
if (!Array.isArray(forwardingKeyChain)) {
|
||||||
forwardingKeyChain = [];
|
forwardingKeyChain = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ OlmEncryption.prototype.encryptMessage = async function(room, eventType, content
|
|||||||
|
|
||||||
const members = await room.getEncryptionTargetMembers();
|
const members = await room.getEncryptionTargetMembers();
|
||||||
|
|
||||||
const users = utils.map(members, function(u) {
|
const users = members.map(function(u) {
|
||||||
return u.userId;
|
return u.userId;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -186,7 +186,7 @@ export function Crypto(baseApis, sessionStore, userId, deviceId,
|
|||||||
// map from algorithm to DecryptionAlgorithm instance, for each room
|
// map from algorithm to DecryptionAlgorithm instance, for each room
|
||||||
this._roomDecryptors = {};
|
this._roomDecryptors = {};
|
||||||
|
|
||||||
this._supportedAlgorithms = utils.keys(
|
this._supportedAlgorithms = Object.keys(
|
||||||
algorithms.DECRYPTION_CLASSES,
|
algorithms.DECRYPTION_CLASSES,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ import * as utils from "./utils";
|
|||||||
import request from "request";
|
import request from "request";
|
||||||
|
|
||||||
matrixcs.request(request);
|
matrixcs.request(request);
|
||||||
utils.runPolyfills();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||||
|
|||||||
@@ -249,7 +249,7 @@ EventTimelineSet.prototype.findEventById = function(eventId) {
|
|||||||
if (!tl) {
|
if (!tl) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
return utils.findElement(tl.getEvents(), function(ev) {
|
return tl.getEvents().find(function(ev) {
|
||||||
return ev.getId() == eventId;
|
return ev.getId() == eventId;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ RoomMember.prototype.setPowerLevelEvent = function(powerLevelEvent) {
|
|||||||
const evContent = powerLevelEvent.getDirectionalContent();
|
const evContent = powerLevelEvent.getDirectionalContent();
|
||||||
|
|
||||||
let maxLevel = evContent.users_default || 0;
|
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);
|
maxLevel = Math.max(maxLevel, lvl);
|
||||||
});
|
});
|
||||||
const oldPowerLevel = this.powerLevel;
|
const oldPowerLevel = this.powerLevel;
|
||||||
@@ -172,7 +172,7 @@ RoomMember.prototype.setTypingEvent = function(event) {
|
|||||||
const oldTyping = this.typing;
|
const oldTyping = this.typing;
|
||||||
this.typing = false;
|
this.typing = false;
|
||||||
const typingList = event.getContent().user_ids;
|
const typingList = event.getContent().user_ids;
|
||||||
if (!utils.isArray(typingList)) {
|
if (!Array.isArray(typingList)) {
|
||||||
// malformed event :/ bail early. TODO: whine?
|
// malformed event :/ bail early. TODO: whine?
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -154,7 +154,7 @@ RoomState.prototype.setInvitedMemberCount = function(count) {
|
|||||||
* @return {Array<RoomMember>} A list of RoomMembers.
|
* @return {Array<RoomMember>} A list of RoomMembers.
|
||||||
*/
|
*/
|
||||||
RoomState.prototype.getMembers = function() {
|
RoomState.prototype.getMembers = function() {
|
||||||
return utils.values(this.members);
|
return Object.values(this.members);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -163,7 +163,7 @@ RoomState.prototype.getMembers = function() {
|
|||||||
* @return {Array<RoomMember>} A list of RoomMembers.
|
* @return {Array<RoomMember>} A list of RoomMembers.
|
||||||
*/
|
*/
|
||||||
RoomState.prototype.getMembersExcept = function(excludedIds) {
|
RoomState.prototype.getMembersExcept = function(excludedIds) {
|
||||||
return utils.values(this.members)
|
return Object.values(this.members)
|
||||||
.filter((m) => !excludedIds.includes(m.userId));
|
.filter((m) => !excludedIds.includes(m.userId));
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -296,7 +296,7 @@ RoomState.prototype.setStateEvents = function(stateEvents) {
|
|||||||
this._updateModifiedTime();
|
this._updateModifiedTime();
|
||||||
|
|
||||||
// update the core event dict
|
// update the core event dict
|
||||||
utils.forEach(stateEvents, function(event) {
|
stateEvents.forEach(function(event) {
|
||||||
if (event.getRoomId() !== self.roomId) {
|
if (event.getRoomId() !== self.roomId) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -319,7 +319,7 @@ RoomState.prototype.setStateEvents = function(stateEvents) {
|
|||||||
// core event dict as these structures may depend on other state events in
|
// 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
|
// 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).
|
// 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) {
|
if (event.getRoomId() !== self.roomId) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -349,8 +349,8 @@ RoomState.prototype.setStateEvents = function(stateEvents) {
|
|||||||
self._updateMember(member);
|
self._updateMember(member);
|
||||||
self.emit("RoomState.members", event, self, member);
|
self.emit("RoomState.members", event, self, member);
|
||||||
} else if (event.getType() === "m.room.power_levels") {
|
} else if (event.getType() === "m.room.power_levels") {
|
||||||
const members = utils.values(self.members);
|
const members = Object.values(self.members);
|
||||||
utils.forEach(members, function(member) {
|
members.forEach(function(member) {
|
||||||
// We only propagate `RoomState.members` event if the
|
// We only propagate `RoomState.members` event if the
|
||||||
// power levels has been changed
|
// power levels has been changed
|
||||||
// large room suffer from large re-rendering especially when not needed
|
// 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
|
* @param {MatrixEvent} event The typing event
|
||||||
*/
|
*/
|
||||||
RoomState.prototype.setTypingEvent = function(event) {
|
RoomState.prototype.setTypingEvent = function(event) {
|
||||||
utils.forEach(utils.values(this.members), function(member) {
|
Object.values(this.members).forEach(function(member) {
|
||||||
member.setTypingEvent(event);
|
member.setTypingEvent(event);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -949,7 +949,7 @@ Room.prototype.getAliases = function() {
|
|||||||
if (aliasEvents) {
|
if (aliasEvents) {
|
||||||
for (let i = 0; i < aliasEvents.length; ++i) {
|
for (let i = 0; i < aliasEvents.length; ++i) {
|
||||||
const aliasEvent = aliasEvents[i];
|
const aliasEvent = aliasEvents[i];
|
||||||
if (utils.isArray(aliasEvent.getContent().aliases)) {
|
if (Array.isArray(aliasEvent.getContent().aliases)) {
|
||||||
const filteredAliases = aliasEvent.getContent().aliases.filter(a => {
|
const filteredAliases = aliasEvent.getContent().aliases.filter(a => {
|
||||||
if (typeof(a) !== "string") return false;
|
if (typeof(a) !== "string") return false;
|
||||||
if (a[0] !== '#') 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.
|
* @return {RoomMember[]} A list of members with the given membership state.
|
||||||
*/
|
*/
|
||||||
Room.prototype.getMembersWithMembership = function(membership) {
|
Room.prototype.getMembersWithMembership = function(membership) {
|
||||||
return utils.filter(this.currentState.getMembers(), function(m) {
|
return this.currentState.getMembers().filter(function(m) {
|
||||||
return m.membership === membership;
|
return m.membership === membership;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -1700,7 +1700,7 @@ Room.prototype.recalculate = function() {
|
|||||||
);
|
);
|
||||||
if (membershipEvent && membershipEvent.getContent().membership === "invite") {
|
if (membershipEvent && membershipEvent.getContent().membership === "invite") {
|
||||||
const strippedStateEvents = membershipEvent.event.invite_room_state || [];
|
const strippedStateEvents = membershipEvent.event.invite_room_state || [];
|
||||||
utils.forEach(strippedStateEvents, function(strippedEvent) {
|
strippedStateEvents.forEach(function(strippedEvent) {
|
||||||
const existingEvent = self.currentState.getStateEvents(
|
const existingEvent = self.currentState.getStateEvents(
|
||||||
strippedEvent.type, strippedEvent.state_key,
|
strippedEvent.type, strippedEvent.state_key,
|
||||||
);
|
);
|
||||||
@@ -1850,9 +1850,9 @@ Room.prototype.addReceipt = function(event, fake) {
|
|||||||
*/
|
*/
|
||||||
Room.prototype._addReceiptsToStructure = function(event, receipts) {
|
Room.prototype._addReceiptsToStructure = function(event, receipts) {
|
||||||
const self = this;
|
const self = this;
|
||||||
utils.keys(event.getContent()).forEach(function(eventId) {
|
Object.keys(event.getContent()).forEach(function(eventId) {
|
||||||
utils.keys(event.getContent()[eventId]).forEach(function(receiptType) {
|
Object.keys(event.getContent()[eventId]).forEach(function(receiptType) {
|
||||||
utils.keys(event.getContent()[eventId][receiptType]).forEach(
|
Object.keys(event.getContent()[eventId][receiptType]).forEach(
|
||||||
function(userId) {
|
function(userId) {
|
||||||
const receipt = event.getContent()[eventId][receiptType][userId];
|
const receipt = event.getContent()[eventId][receiptType][userId];
|
||||||
|
|
||||||
@@ -1892,8 +1892,8 @@ Room.prototype._addReceiptsToStructure = function(event, receipts) {
|
|||||||
*/
|
*/
|
||||||
Room.prototype._buildReceiptCache = function(receipts) {
|
Room.prototype._buildReceiptCache = function(receipts) {
|
||||||
const receiptCacheByEventId = {};
|
const receiptCacheByEventId = {};
|
||||||
utils.keys(receipts).forEach(function(receiptType) {
|
Object.keys(receipts).forEach(function(receiptType) {
|
||||||
utils.keys(receipts[receiptType]).forEach(function(userId) {
|
Object.keys(receipts[receiptType]).forEach(function(userId) {
|
||||||
const receipt = receipts[receiptType][userId];
|
const receipt = receipts[receiptType][userId];
|
||||||
if (!receiptCacheByEventId[receipt.eventId]) {
|
if (!receiptCacheByEventId[receipt.eventId]) {
|
||||||
receiptCacheByEventId[receipt.eventId] = [];
|
receiptCacheByEventId[receipt.eventId] = [];
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ limitations under the License.
|
|||||||
* @module models/search-result
|
* @module models/search-result
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as utils from "../utils";
|
|
||||||
import {EventContext} from "./event-context";
|
import {EventContext} from "./event-context";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -52,8 +51,8 @@ SearchResult.fromJson = function(jsonObj, eventMapper) {
|
|||||||
const context = new EventContext(eventMapper(jsonObj.result));
|
const context = new EventContext(eventMapper(jsonObj.result));
|
||||||
|
|
||||||
context.setPaginateToken(jsonContext.start, true);
|
context.setPaginateToken(jsonContext.start, true);
|
||||||
context.addEvents(utils.map(events_before, eventMapper), true);
|
context.addEvents(events_before.map(eventMapper), true);
|
||||||
context.addEvents(utils.map(events_after, eventMapper), false);
|
context.addEvents(events_after.map(eventMapper), false);
|
||||||
context.setPaginateToken(jsonContext.end, false);
|
context.setPaginateToken(jsonContext.end, false);
|
||||||
|
|
||||||
return new SearchResult(jsonObj.rank, context);
|
return new SearchResult(jsonObj.rank, context);
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ MatrixScheduler.prototype.getQueueForEvent = function(event) {
|
|||||||
if (!name || !this._queues[name]) {
|
if (!name || !this._queues[name]) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return utils.map(this._queues[name], function(obj) {
|
return this._queues[name].map(function(obj) {
|
||||||
return obj.event;
|
return obj.event;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -196,16 +196,18 @@ function _startProcessingQueues(scheduler) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// for each inactive queue with events in them
|
// for each inactive queue with events in them
|
||||||
utils.forEach(utils.filter(utils.keys(scheduler._queues), function(queueName) {
|
Object.keys(scheduler._queues)
|
||||||
return scheduler._activeQueues.indexOf(queueName) === -1 &&
|
.filter(function(queueName) {
|
||||||
scheduler._queues[queueName].length > 0;
|
return scheduler._activeQueues.indexOf(queueName) === -1 &&
|
||||||
}), function(queueName) {
|
scheduler._queues[queueName].length > 0;
|
||||||
// mark the queue as active
|
})
|
||||||
scheduler._activeQueues.push(queueName);
|
.forEach(function(queueName) {
|
||||||
// begin processing the head of the queue
|
// mark the queue as active
|
||||||
debuglog("Spinning up queue: '%s'", queueName);
|
scheduler._activeQueues.push(queueName);
|
||||||
_processQueue(scheduler, queueName);
|
// begin processing the head of the queue
|
||||||
});
|
debuglog("Spinning up queue: '%s'", queueName);
|
||||||
|
_processQueue(scheduler, queueName);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function _processQueue(scheduler, queueName) {
|
function _processQueue(scheduler, queueName) {
|
||||||
@@ -267,7 +269,7 @@ function _processQueue(scheduler, queueName) {
|
|||||||
|
|
||||||
function _peekNextEvent(scheduler, queueName) {
|
function _peekNextEvent(scheduler, queueName) {
|
||||||
const queue = scheduler._queues[queueName];
|
const queue = scheduler._queues[queueName];
|
||||||
if (!utils.isArray(queue)) {
|
if (!Array.isArray(queue)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return queue[0];
|
return queue[0];
|
||||||
@@ -275,7 +277,7 @@ function _peekNextEvent(scheduler, queueName) {
|
|||||||
|
|
||||||
function _removeNextEvent(scheduler, queueName) {
|
function _removeNextEvent(scheduler, queueName) {
|
||||||
const queue = scheduler._queues[queueName];
|
const queue = scheduler._queues[queueName];
|
||||||
if (!utils.isArray(queue)) {
|
if (!Array.isArray(queue)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return queue.shift();
|
return queue.shift();
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ limitations under the License.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import {User} from "../models/user";
|
import {User} from "../models/user";
|
||||||
import * as utils from "../utils";
|
|
||||||
|
|
||||||
function isValidFilterId(filterId) {
|
function isValidFilterId(filterId) {
|
||||||
const isValidStr = typeof filterId === "string" &&
|
const isValidStr = typeof filterId === "string" &&
|
||||||
@@ -113,7 +112,7 @@ MemoryStore.prototype = {
|
|||||||
* @return {Group[]} A list of groups, which may be empty.
|
* @return {Group[]} A list of groups, which may be empty.
|
||||||
*/
|
*/
|
||||||
getGroups: function() {
|
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.
|
* @return {Room[]} A list of rooms, which may be empty.
|
||||||
*/
|
*/
|
||||||
getRooms: function() {
|
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.
|
* @return {RoomSummary[]} A summary of each room.
|
||||||
*/
|
*/
|
||||||
getRoomSummaries: function() {
|
getRoomSummaries: function() {
|
||||||
return utils.map(utils.values(this.rooms), function(room) {
|
return Object.values(this.rooms).map(function(room) {
|
||||||
return room.summary;
|
return room.summary;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@@ -221,7 +220,7 @@ MemoryStore.prototype = {
|
|||||||
* @return {User[]} A list of users, which may be empty.
|
* @return {User[]} A list of users, which may be empty.
|
||||||
*/
|
*/
|
||||||
getUsers: function() {
|
getUsers: function() {
|
||||||
return utils.values(this.users);
|
return Object.values(this.users);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
25
src/sync.js
25
src/sync.js
@@ -278,20 +278,15 @@ SyncApi.prototype.peek = function(roomId) {
|
|||||||
|
|
||||||
// FIXME: Mostly duplicated from _processRoomEvents but not entirely
|
// FIXME: Mostly duplicated from _processRoomEvents but not entirely
|
||||||
// because "state" in this API is at the BEGINNING of the chunk
|
// because "state" in this API is at the BEGINNING of the chunk
|
||||||
const oldStateEvents = utils.map(
|
const oldStateEvents = utils.deepCopy(response.state)
|
||||||
utils.deepCopy(response.state), client.getEventMapper(),
|
.map(client.getEventMapper());
|
||||||
);
|
const stateEvents = response.state.map(client.getEventMapper());
|
||||||
const stateEvents = utils.map(
|
const messages = response.messages.chunk.map(client.getEventMapper());
|
||||||
response.state, client.getEventMapper(),
|
|
||||||
);
|
|
||||||
const messages = utils.map(
|
|
||||||
response.messages.chunk, client.getEventMapper(),
|
|
||||||
);
|
|
||||||
|
|
||||||
// XXX: copypasted from /sync until we kill off this
|
// XXX: copypasted from /sync until we kill off this
|
||||||
// minging v1 API stuff)
|
// minging v1 API stuff)
|
||||||
// handle presence events (User objects)
|
// 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(
|
response.presence.map(client.getEventMapper()).forEach(
|
||||||
function(presenceEvent) {
|
function(presenceEvent) {
|
||||||
let user = client.store.getUser(presenceEvent.getContent().user_id);
|
let user = client.store.getUser(presenceEvent.getContent().user_id);
|
||||||
@@ -1006,7 +1001,7 @@ SyncApi.prototype._processSyncResponse = async function(
|
|||||||
// - The isBrandNewRoom boilerplate is boilerplatey.
|
// - The isBrandNewRoom boilerplate is boilerplatey.
|
||||||
|
|
||||||
// handle presence events (User objects)
|
// 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(
|
data.presence.events.map(client.getEventMapper()).forEach(
|
||||||
function(presenceEvent) {
|
function(presenceEvent) {
|
||||||
let user = client.store.getUser(presenceEvent.getSender());
|
let user = client.store.getUser(presenceEvent.getSender());
|
||||||
@@ -1022,7 +1017,7 @@ SyncApi.prototype._processSyncResponse = async function(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// handle non-room account_data
|
// 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 events = data.account_data.events.map(client.getEventMapper());
|
||||||
const prevEventsMap = events.reduce((m, c) => {
|
const prevEventsMap = events.reduce((m, c) => {
|
||||||
m[c.getId()] = client.store.getAccountData(c.getType());
|
m[c.getId()] = client.store.getAccountData(c.getType());
|
||||||
@@ -1047,7 +1042,7 @@ SyncApi.prototype._processSyncResponse = async function(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// handle to-device events
|
// 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
|
data.to_device.events.length > 0
|
||||||
) {
|
) {
|
||||||
const cancelledKeyVerificationTxns = [];
|
const cancelledKeyVerificationTxns = [];
|
||||||
@@ -1508,7 +1503,7 @@ SyncApi.prototype._mapSyncResponseToRoomArray = function(obj) {
|
|||||||
// [{stuff+Room+isBrandNewRoom}, {stuff+Room+isBrandNewRoom}]
|
// [{stuff+Room+isBrandNewRoom}, {stuff+Room+isBrandNewRoom}]
|
||||||
const client = this.client;
|
const client = this.client;
|
||||||
const self = this;
|
const self = this;
|
||||||
return utils.keys(obj).map(function(roomId) {
|
return Object.keys(obj).map(function(roomId) {
|
||||||
const arrObj = obj[roomId];
|
const arrObj = obj[roomId];
|
||||||
let room = client.store.getRoom(roomId);
|
let room = client.store.getRoom(roomId);
|
||||||
let isBrandNewRoom = false;
|
let isBrandNewRoom = false;
|
||||||
@@ -1529,7 +1524,7 @@ SyncApi.prototype._mapSyncResponseToRoomArray = function(obj) {
|
|||||||
* @return {MatrixEvent[]}
|
* @return {MatrixEvent[]}
|
||||||
*/
|
*/
|
||||||
SyncApi.prototype._mapSyncEventsFormat = function(obj, room, decrypt = true) {
|
SyncApi.prototype._mapSyncEventsFormat = function(obj, room, decrypt = true) {
|
||||||
if (!obj || !utils.isArray(obj.events)) {
|
if (!obj || !Array.isArray(obj.events)) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
const mapper = this.client.getEventMapper({ decrypt });
|
const mapper = this.client.getEventMapper({ decrypt });
|
||||||
|
|||||||
321
src/utils.ts
321
src/utils.ts
@@ -61,116 +61,6 @@ export function encodeUri(pathTemplate: string,
|
|||||||
return pathTemplate;
|
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 <code>fn(element){...}</code>
|
|
||||||
* @return {Array} A new array with the results of the function.
|
|
||||||
*/
|
|
||||||
export function map<T, S>(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 <code>fn(element, index, array){...}</code>.
|
|
||||||
* @return {Array} A new array with the results of the function.
|
|
||||||
*/
|
|
||||||
export function filter<T>(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 <code>Object.keys()</code>.
|
|
||||||
* @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<T>(obj: Record<string, T>): 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 <code>fn(element, index)</code>.
|
|
||||||
*/
|
|
||||||
export function forEach<T>(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 <code>fn(element, index, array)</code>
|
|
||||||
* @param {boolean} reverse True to search in reverse order.
|
|
||||||
* @return {*} The first value in the array which returns <code>true</code> for
|
|
||||||
* the given function.
|
|
||||||
*/
|
|
||||||
export function findElement<T>(
|
|
||||||
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
|
* The removeElement() method removes the first element in the array that
|
||||||
* satisfies (returns true) the provided testing function.
|
* satisfies (returns true) the provided testing function.
|
||||||
@@ -217,16 +107,6 @@ export function isFunction(value: any) {
|
|||||||
return Object.prototype.toString.call(value) === "[object Function]";
|
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.
|
* Checks that the given object has the specified keys.
|
||||||
* @param {Object} obj The object to check.
|
* @param {Object} obj The object to check.
|
||||||
@@ -380,207 +260,6 @@ export function extend(...restParams) {
|
|||||||
return target;
|
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
|
* Inherit the prototype methods from one constructor into another. This is a
|
||||||
* port of the Node.js implementation with an Object.create polyfill.
|
* port of the Node.js implementation with an Object.create polyfill.
|
||||||
|
|||||||
Reference in New Issue
Block a user