+
Skip to content

388 - Update the displayList implementation and move into the project #439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/app/shared/FeedSummary.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import {quantify, pluralize, displayList} from "hurdak"
import {quantify, pluralize} from "hurdak"
import {
isScopeFeed,
isRelayFeed,
Expand All @@ -18,7 +18,7 @@
} from "@welshman/feeds"
import {displayRelayUrl} from "@welshman/util"
import {displayProfileByPubkey} from "@welshman/app"
import {formatTimestampAsDate} from "src/util/misc"
import {displayList, formatTimestampAsDate} from "src/util/misc"
import Chip from "src/partials/Chip.svelte"

export let feed
Expand Down
3 changes: 2 additions & 1 deletion src/app/views/ChannelCreate.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
import {derived} from "svelte/store"
import {uniq} from "@welshman/lib"
import {pubkey, displayProfileByPubkey, inboxRelaySelectionsByPubkey} from "@welshman/app"
import {displayList, pluralize} from "hurdak"
import {pluralize} from "hurdak"
import Field from "src/partials/Field.svelte"
import FlexColumn from "src/partials/FlexColumn.svelte"
import Anchor from "src/partials/Anchor.svelte"
import PersonSelect from "src/app/shared/PersonSelect.svelte"
import {router} from "src/app/util/router"
import {hasNip44} from "src/engine"
import {displayList} from "src/util/misc"

let value = []

Expand Down
2 changes: 1 addition & 1 deletion src/app/views/ChannelsListItem.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<script lang="ts">
import {without} from "ramda"
import {displayList} from "hurdak"
import {derived} from "svelte/store"
import {pubkey, profiles, displayProfileByPubkey} from "@welshman/app"
import PersonCircles from "src/app/shared/PersonCircles.svelte"
import Card from "src/partials/Card.svelte"
import {router} from "src/app/util/router"
import {channelHasNewMessages} from "src/engine"
import {displayList} from "src/util/misc"

export let channel

Expand Down
3 changes: 1 addition & 2 deletions src/app/views/RelayList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import {onMount} from "svelte"
import {derived} from "svelte/store"
import {groupBy, sortBy, uniq} from "ramda"
import {displayList} from "hurdak"
import {ctx, pushToMapKey} from "@welshman/lib"
import {
pubkey,
Expand All @@ -18,7 +17,7 @@
relaySelectionsByPubkey,
} from "@welshman/app"
import {Tags, isShareableRelayUrl, normalizeRelayUrl, profileHasName} from "@welshman/util"
import {createScroller} from "src/util/misc"
import {createScroller, displayList} from "src/util/misc"
import {showWarning} from "src/partials/Toast.svelte"
import Tabs from "src/partials/Tabs.svelte"
import Modal from "src/partials/Modal.svelte"
Expand Down
4 changes: 2 additions & 2 deletions src/partials/Channel.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import cx from "classnames"
import {onMount} from "svelte"
import {displayList, pluralize} from "hurdak"
import {pluralize} from "hurdak"
import {derived} from "svelte/store"
import {sleep, remove} from "@welshman/lib"
import type {TrustedEvent} from "@welshman/util"
Expand All @@ -14,7 +14,7 @@
} from "@welshman/app"
import {prop, max, reverse, pluck, sortBy, last} from "ramda"
import {fly, slide} from "src/util/transition"
import {createScroller, formatTimestamp} from "src/util/misc"
import {createScroller, displayList, formatTimestamp} from "src/util/misc"
import Spinner from "src/partials/Spinner.svelte"
import Anchor from "src/partials/Anchor.svelte"
import Popover from "src/partials/Popover.svelte"
Expand Down
2 changes: 1 addition & 1 deletion src/partials/ImageInput.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<script lang="ts">
import {createEventDispatcher} from "svelte"
import {displayList} from "hurdak"
import Input from "src/partials/Input.svelte"
import Modal from "src/partials/Modal.svelte"
import Spinner from "src/partials/Spinner.svelte"
import Anchor from "src/partials/Anchor.svelte"
import {listenForFile} from "src/util/html"
import {uploadFiles, getSetting} from "src/engine"
import {displayList} from "src/util/misc"

export let icon = null
export let value = null
Expand Down
14 changes: 14 additions & 0 deletions src/util/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,20 @@ export const toSpliced = <T>(xs: T[], start: number, deleteCount: number = 0, ..
...xs.slice(start + deleteCount),
]

export const displayList = <T>(xs: T[], conj = "and", n = 6, locale = "en-US") => {
// Convert all elements to strings for Intl.ListFormat
const stringItems = xs.map(String)

if (xs.length > n + 2) {
const formattedList = new Intl.ListFormat(locale, {style: "long", type: "unit"}).format(
stringItems.slice(0, n),
)
return `${formattedList}, ${conj} ${xs.length - n} others`
}

return new Intl.ListFormat(locale, {style: "long", type: "conjunction"}).format(stringItems)
}

// Local storage

export const synced = <T>(key: string, defaultValue: T, delay = 300) => {
Expand Down
59 changes: 59 additions & 0 deletions tests/unit/util/misc.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {describe, expect, it} from "vitest"
import {displayList} from "../../../src/util/misc"

describe("misc utils", () => {
describe("displayList", () => {
it("should return an empty string when the list is empty", () => {
const list = []
const output = displayList(list)

expect(output).toEqual("")
})

it("should return a single entry when list length is one", () => {
const list = ["Apple"]
const output = displayList(list)

expect(output).toEqual("Apple")
})

it("should return a string of both items when the list length is two", () => {
const list = ["Apple", "Banana"]
const output = displayList(list)

expect(output).toEqual("Apple and Banana")
})

it("should return a string of all items when the list length is three", () => {
const list = ["Apple", "Banana", "Cherry"]
const output = displayList(list)

expect(output).toEqual("Apple, Banana, and Cherry")
})

it("should return a truncated string of all items when the list is long", () => {
const list = [
"Apple",
"Banana",
"Orange",
"Pear",
"Grapes",
"Mango",
"Pineapple",
"Kiwi",
"Strawberry",
"Blueberry",
]
const output = displayList(list)

expect(output).toEqual("Apple, Banana, Orange, Pear, Grapes, Mango, and 4 others")
})

it("should return a string of both items with list of two numbers", () => {
const list = [7, 17]
const output = displayList(list)

expect(output).toEqual("7 and 17")
})
})
})
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"compilerOptions": {
"baseUrl": ".",
"module": "esnext",
"target": "es2021",
"paths": {
"src/*": ["src/*"]
},
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载