fix: replace indexWhere with findIndex (#1125)

This commit is contained in:
Nolan Lawson 2019-03-31 09:21:57 -07:00 committed by GitHub
parent 6744de59f8
commit f758e12b24
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 33 additions and 44 deletions

View file

@ -3,7 +3,7 @@ import { store } from '../_store/store'
import uniqBy from 'lodash-es/uniqBy' import uniqBy from 'lodash-es/uniqBy'
import isEqual from 'lodash-es/isEqual' import isEqual from 'lodash-es/isEqual'
import { database } from '../_database/database' import { database } from '../_database/database'
import { concat, indexWhere } from '../_utils/arrays' import { concat } from '../_utils/arrays'
import { scheduleIdleTask } from '../_utils/scheduleIdleTask' import { scheduleIdleTask } from '../_utils/scheduleIdleTask'
import { timelineItemToSummary } from '../_utils/timelineItemToSummary' import { timelineItemToSummary } from '../_utils/timelineItemToSummary'
@ -47,9 +47,9 @@ function isValidStatusForThread (thread, timelineName, itemSummariesToAdd) {
let itemSummariesToAddIdSet = new Set(itemSummariesToAdd.map(_ => _.id)) let itemSummariesToAddIdSet = new Set(itemSummariesToAdd.map(_ => _.id))
let threadIdSet = new Set(thread.map(_ => _.id)) let threadIdSet = new Set(thread.map(_ => _.id))
let focusedStatusId = timelineName.split('/')[1] // e.g. "status/123456" let focusedStatusId = timelineName.split('/')[1] // e.g. "status/123456"
let focusedStatusIdx = indexWhere(thread, _ => _.id === focusedStatusId) let focusedStatusIdx = thread.findIndex(_ => _.id === focusedStatusId)
return status => { return status => {
let repliedToStatusIdx = indexWhere(thread, _ => _.id === status.in_reply_to_id) let repliedToStatusIdx = thread.findIndex(_ => _.id === status.in_reply_to_id)
return ( return (
// A reply to an ancestor status is not valid for this thread, but for the focused status // A reply to an ancestor status is not valid for this thread, but for the focused status
// itself or any of its descendents, it is valid. // itself or any of its descendents, it is valid.

View file

@ -44,12 +44,3 @@ export function concat () {
} }
return res return res
} }
export function indexWhere (arr, cb) {
for (let i = 0; i < arr.length; i++) {
if (cb(arr[i], i)) {
return i
}
}
return -1
}

View file

@ -2,7 +2,6 @@ import { closeDialogButton, getNthStatus, getNthStatusSelector, modalDialogConte
import { loginAsFoobar } from '../roles' import { loginAsFoobar } from '../roles'
import { Selector as $ } from 'testcafe' import { Selector as $ } from 'testcafe'
import { homeTimeline } from '../fixtures' import { homeTimeline } from '../fixtures'
import { indexWhere } from '../../src/routes/_utils/arrays'
fixture`008-status-media.js` fixture`008-status-media.js`
.page`http://localhost:4002` .page`http://localhost:4002`
@ -10,8 +9,8 @@ fixture`008-status-media.js`
test('shows sensitive images and videos', async t => { test('shows sensitive images and videos', async t => {
await loginAsFoobar(t) await loginAsFoobar(t)
let kittenIdx = indexWhere(homeTimeline, _ => _.spoiler === 'kitten CW') let kittenIdx = homeTimeline.findIndex(_ => _.spoiler === 'kitten CW')
let videoIdx = indexWhere(homeTimeline, _ => _.content === 'secret video') let videoIdx = homeTimeline.findIndex(_ => _.content === 'secret video')
await scrollToStatus(t, 1 + kittenIdx) await scrollToStatus(t, 1 + kittenIdx)
await t.expect($(`${getNthStatusSelector(1 + kittenIdx)} .status-media img`).exists).notOk() await t.expect($(`${getNthStatusSelector(1 + kittenIdx)} .status-media img`).exists).notOk()
@ -27,8 +26,8 @@ test('shows sensitive images and videos', async t => {
test('click and close image and video modals', async t => { test('click and close image and video modals', async t => {
await loginAsFoobar(t) await loginAsFoobar(t)
let videoIdx = indexWhere(homeTimeline, _ => _.content === "here's a video") let videoIdx = homeTimeline.findIndex(_ => _.content === "here's a video")
let kittenIdx = indexWhere(homeTimeline, _ => _.content === "here's an animated kitten gif") let kittenIdx = homeTimeline.findIndex(_ => _.content === "here's an animated kitten gif")
await scrollToStatus(t, 1 + videoIdx) await scrollToStatus(t, 1 + videoIdx)
await t.expect(modalDialogContents.exists).notOk() await t.expect(modalDialogContents.exists).notOk()

View file

@ -5,7 +5,7 @@ import {
} from '../utils' } from '../utils'
import { loginAsFoobar } from '../roles' import { loginAsFoobar } from '../roles'
import { Selector as $ } from 'testcafe' import { Selector as $ } from 'testcafe'
import { indexWhere } from '../../src/routes/_utils/arrays'
import { homeTimeline } from '../fixtures' import { homeTimeline } from '../fixtures'
fixture`010-focus.js` fixture`010-focus.js`
@ -14,7 +14,7 @@ fixture`010-focus.js`
test('modal preserves focus', async t => { test('modal preserves focus', async t => {
await loginAsFoobar(t) await loginAsFoobar(t)
let idx = indexWhere(homeTimeline, _ => _.content === "here's a video") let idx = homeTimeline.findIndex(_ => _.content === "here's a video")
await scrollToStatus(t, 1 + idx) await scrollToStatus(t, 1 + idx)
// explicitly hover-focus-click // explicitly hover-focus-click

View file

@ -7,7 +7,6 @@ import {
} from '../utils' } from '../utils'
import { loginAsFoobar } from '../roles' import { loginAsFoobar } from '../roles'
import { homeTimeline } from '../fixtures' import { homeTimeline } from '../fixtures'
import { indexWhere } from '../../src/routes/_utils/arrays'
fixture`017-compose-reply.js` fixture`017-compose-reply.js`
.page`http://localhost:4002` .page`http://localhost:4002`
@ -63,7 +62,7 @@ test('replies have same privacy as replied-to status by default', async t => {
test('replies have same CW as replied-to status', async t => { test('replies have same CW as replied-to status', async t => {
await loginAsFoobar(t) await loginAsFoobar(t)
let kittenIdx = indexWhere(homeTimeline, _ => _.spoiler === 'kitten CW') let kittenIdx = homeTimeline.findIndex(_ => _.spoiler === 'kitten CW')
await scrollToStatus(t, 1 + kittenIdx) await scrollToStatus(t, 1 + kittenIdx)
await t.click(getNthReplyButton(1 + kittenIdx)) await t.click(getNthReplyButton(1 + kittenIdx))
.expect(getNthReplyContentWarningInput(1 + kittenIdx).value).eql('kitten CW') .expect(getNthReplyContentWarningInput(1 + kittenIdx).value).eql('kitten CW')
@ -75,7 +74,7 @@ test('replies have same CW as replied-to status', async t => {
test('replies save deletions of CW', async t => { test('replies save deletions of CW', async t => {
await loginAsFoobar(t) await loginAsFoobar(t)
let kittenIdx = indexWhere(homeTimeline, _ => _.spoiler === 'kitten CW') let kittenIdx = homeTimeline.findIndex(_ => _.spoiler === 'kitten CW')
await scrollToStatus(t, 1 + kittenIdx) await scrollToStatus(t, 1 + kittenIdx)
await t.click(getNthReplyButton(1 + kittenIdx)) await t.click(getNthReplyButton(1 + kittenIdx))
.expect(getNthReplyContentWarningInput(1 + kittenIdx).value).eql('kitten CW') .expect(getNthReplyContentWarningInput(1 + kittenIdx).value).eql('kitten CW')
@ -89,7 +88,7 @@ test('replies save deletions of CW', async t => {
test('replies save changes to CW', async t => { test('replies save changes to CW', async t => {
await loginAsFoobar(t) await loginAsFoobar(t)
let kittenIdx = indexWhere(homeTimeline, _ => _.spoiler === 'kitten CW') let kittenIdx = homeTimeline.findIndex(_ => _.spoiler === 'kitten CW')
await scrollToStatus(t, 1 + kittenIdx) await scrollToStatus(t, 1 + kittenIdx)
await t.click(getNthReplyButton(1 + kittenIdx)) await t.click(getNthReplyButton(1 + kittenIdx))
.expect(getNthReplyContentWarningInput(1 + kittenIdx).value).eql('kitten CW') .expect(getNthReplyContentWarningInput(1 + kittenIdx).value).eql('kitten CW')

View file

@ -8,7 +8,7 @@ import {
settingsNavButton settingsNavButton
} from '../utils' } from '../utils'
import { Selector as $ } from 'testcafe' import { Selector as $ } from 'testcafe'
import { indexWhere } from '../../src/routes/_utils/arrays'
import { homeTimeline } from '../fixtures' import { homeTimeline } from '../fixtures'
fixture`022-status-aria-label.js` fixture`022-status-aria-label.js`
@ -29,7 +29,7 @@ test('basic aria-labels for statuses', async t => {
test('aria-labels for CWed statuses', async t => { test('aria-labels for CWed statuses', async t => {
await loginAsFoobar(t) await loginAsFoobar(t)
let kittenIdx = indexWhere(homeTimeline, _ => _.spoiler === 'kitten CW') let kittenIdx = homeTimeline.findIndex(_ => _.spoiler === 'kitten CW')
await scrollToStatus(t, 1 + kittenIdx) await scrollToStatus(t, 1 + kittenIdx)
await t await t
.hover(getNthStatus(1 + kittenIdx)) .hover(getNthStatus(1 + kittenIdx))

View file

@ -4,7 +4,7 @@ import {
getNthStatus, getNthStatusMedia, getNthStatusSensitiveMediaButton, homeNavButton, markMediaSensitiveInput, getNthStatus, getNthStatusMedia, getNthStatusSensitiveMediaButton, homeNavButton, markMediaSensitiveInput,
scrollToStatus, settingsNavButton, neverMarkMediaSensitiveInput scrollToStatus, settingsNavButton, neverMarkMediaSensitiveInput
} from '../utils' } from '../utils'
import { indexWhere } from '../../src/routes/_utils/arrays'
import { homeTimeline } from '../fixtures' import { homeTimeline } from '../fixtures'
fixture`023-mark-media-as-sensitive.js` fixture`023-mark-media-as-sensitive.js`
@ -23,11 +23,11 @@ async function checkSensitivityForStatus (t, idx, sensitive) {
} }
async function checkSensitivity (t, shouldBeSensitive) { async function checkSensitivity (t, shouldBeSensitive) {
let sensitiveKittenIdx = indexWhere(homeTimeline, _ => _.spoiler === 'kitten CW') let sensitiveKittenIdx = homeTimeline.findIndex(_ => _.spoiler === 'kitten CW')
let sensitiveVideoIdx = indexWhere(homeTimeline, _ => _.content === 'secret video') let sensitiveVideoIdx = homeTimeline.findIndex(_ => _.content === 'secret video')
let videoIdx = indexWhere(homeTimeline, _ => _.content === "here's a video") let videoIdx = homeTimeline.findIndex(_ => _.content === "here's a video")
let sensitiveAnimatedKittenIdx = indexWhere(homeTimeline, _ => _.content === "here's a secret animated kitten gif") let sensitiveAnimatedKittenIdx = homeTimeline.findIndex(_ => _.content === "here's a secret animated kitten gif")
let animatedKittenIdx = indexWhere(homeTimeline, _ => _.content === "here's an animated kitten gif") let animatedKittenIdx = homeTimeline.findIndex(_ => _.content === "here's an animated kitten gif")
await t.hover(getNthStatus(1)) await t.hover(getNthStatus(1))

View file

@ -13,7 +13,7 @@ import {
} from '../utils' } from '../utils'
import { homeTimeline } from '../fixtures' import { homeTimeline } from '../fixtures'
import { loginAsFoobar } from '../roles' import { loginAsFoobar } from '../roles'
import { indexWhere } from '../../src/routes/_utils/arrays'
import { Selector as $ } from 'testcafe' import { Selector as $ } from 'testcafe'
fixture`025-shortcuts-status.js` fixture`025-shortcuts-status.js`
@ -80,7 +80,7 @@ test('Shortcut o opens active status, backspace goes back', async t => {
}) })
test('Shortcut x shows/hides spoilers', async t => { test('Shortcut x shows/hides spoilers', async t => {
let idx = indexWhere(homeTimeline, _ => _.spoiler === 'kitten CW') let idx = homeTimeline.findIndex(_ => _.spoiler === 'kitten CW')
await loginAsFoobar(t) await loginAsFoobar(t)
await t await t
.expect(getUrl()).eql('http://localhost:4002/') .expect(getUrl()).eql('http://localhost:4002/')
@ -96,7 +96,7 @@ test('Shortcut x shows/hides spoilers', async t => {
}) })
test('Shortcut y shows/hides sensitive image', async t => { test('Shortcut y shows/hides sensitive image', async t => {
let idx = indexWhere(homeTimeline, _ => _.content === "here's a secret kitten") let idx = homeTimeline.findIndex(_ => _.content === "here's a secret kitten")
await loginAsFoobar(t) await loginAsFoobar(t)
await t await t
.expect(getUrl()).eql('http://localhost:4002/') .expect(getUrl()).eql('http://localhost:4002/')
@ -112,7 +112,7 @@ test('Shortcut y shows/hides sensitive image', async t => {
}) })
test('Shortcut f toggles favorite status', async t => { test('Shortcut f toggles favorite status', async t => {
let idx = indexWhere(homeTimeline, _ => _.content === 'this is unlisted') let idx = homeTimeline.findIndex(_ => _.content === 'this is unlisted')
await loginAsFoobar(t) await loginAsFoobar(t)
await t await t
.expect(getUrl()).eql('http://localhost:4002/') .expect(getUrl()).eql('http://localhost:4002/')
@ -127,7 +127,7 @@ test('Shortcut f toggles favorite status', async t => {
}) })
test('Shortcut p toggles profile', async t => { test('Shortcut p toggles profile', async t => {
let idx = indexWhere(homeTimeline, _ => _.content === 'pinned toot 1') let idx = homeTimeline.findIndex(_ => _.content === 'pinned toot 1')
await loginAsFoobar(t) await loginAsFoobar(t)
await t await t
.expect(getUrl()).eql('http://localhost:4002/') .expect(getUrl()).eql('http://localhost:4002/')
@ -139,7 +139,7 @@ test('Shortcut p toggles profile', async t => {
}) })
test('Shortcut m toggles mention', async t => { test('Shortcut m toggles mention', async t => {
let idx = indexWhere(homeTimeline, _ => _.content === 'pinned toot 1') let idx = homeTimeline.findIndex(_ => _.content === 'pinned toot 1')
await loginAsFoobar(t) await loginAsFoobar(t)
await t await t
.expect(getUrl()).eql('http://localhost:4002/') .expect(getUrl()).eql('http://localhost:4002/')

View file

@ -20,7 +20,7 @@ import {
visibleModalDialog visibleModalDialog
} from '../utils' } from '../utils'
import { loginAsFoobar } from '../roles' import { loginAsFoobar } from '../roles'
import { indexWhere } from '../../src/routes/_utils/arrays'
import { homeTimeline } from '../fixtures' import { homeTimeline } from '../fixtures'
fixture`029-back-button-modal.js` fixture`029-back-button-modal.js`
@ -28,7 +28,7 @@ fixture`029-back-button-modal.js`
test('Back button dismisses the modal', async t => { test('Back button dismisses the modal', async t => {
await loginAsFoobar(t) await loginAsFoobar(t)
let idx = indexWhere(homeTimeline, _ => (_.content || '').includes('2 kitten photos')) let idx = homeTimeline.findIndex(_ => (_.content || '').includes('2 kitten photos'))
await scrollToStatus(t, idx + 1) await scrollToStatus(t, idx + 1)
await t await t
.expect(getUrl()).eql('http://localhost:4002/') .expect(getUrl()).eql('http://localhost:4002/')

View file

@ -4,7 +4,7 @@ import {
modalDialog, scrollToStatus, sleep modalDialog, scrollToStatus, sleep
} from '../utils' } from '../utils'
import { loginAsFoobar } from '../roles' import { loginAsFoobar } from '../roles'
import { indexWhere } from '../../src/routes/_utils/arrays'
import { homeTimeline } from '../fixtures' import { homeTimeline } from '../fixtures'
fixture`030-shortcuts-modal.js` fixture`030-shortcuts-modal.js`
@ -23,7 +23,7 @@ test('Backspace dismisses modal', async t => {
test('Backspace dismisses media modal', async t => { test('Backspace dismisses media modal', async t => {
await loginAsFoobar(t) await loginAsFoobar(t)
let idx = indexWhere(homeTimeline, _ => (_.content || '').includes('2 kitten photos')) let idx = homeTimeline.findIndex(_ => (_.content || '').includes('2 kitten photos'))
await scrollToStatus(t, idx + 1) await scrollToStatus(t, idx + 1)
await t await t
.click(getNthStatusMediaButton(idx + 1)) .click(getNthStatusMediaButton(idx + 1))
@ -36,7 +36,7 @@ test('Backspace dismisses media modal', async t => {
test('Left/right changes active media in modal', async t => { test('Left/right changes active media in modal', async t => {
await loginAsFoobar(t) await loginAsFoobar(t)
let idx = indexWhere(homeTimeline, _ => (_.content || '').includes('2 kitten photos')) let idx = homeTimeline.findIndex(_ => (_.content || '').includes('2 kitten photos'))
await scrollToStatus(t, idx + 1) await scrollToStatus(t, idx + 1)
await t await t
.click(getNthStatusMediaButton(idx + 1)) .click(getNthStatusMediaButton(idx + 1))

View file

@ -4,7 +4,7 @@ import {
scrollToBottom, scrollToTop, sleep scrollToBottom, scrollToTop, sleep
} from '../utils' } from '../utils'
import { loginAsFoobar } from '../roles' import { loginAsFoobar } from '../roles'
import { indexWhere } from '../../src/routes/_utils/arrays'
import { homeTimeline } from '../fixtures' import { homeTimeline } from '../fixtures'
fixture`100-favorite-unfavorite.js` fixture`100-favorite-unfavorite.js`
@ -63,7 +63,7 @@ test('unfavorites a status', async t => {
test('Keeps the correct favorites count', async t => { test('Keeps the correct favorites count', async t => {
await loginAsFoobar(t) await loginAsFoobar(t)
let idx = indexWhere(homeTimeline, _ => _.content === 'this is unlisted') let idx = homeTimeline.findIndex(_ => _.content === 'this is unlisted')
await t await t
.hover(getNthStatus(1 + idx)) .hover(getNthStatus(1 + idx))
.click(getNthFavoriteButton(1 + idx)) .click(getNthFavoriteButton(1 + idx))